2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.util
;
41 import java
.security
.AccessController
;
42 import java
.security
.PrivilegedAction
;
44 /** Abstraction to support various file system operations not in Java. */
45 public abstract class FS
{
46 /** The implementation selected for this operating system and JRE. */
47 public static final FS INSTANCE
;
50 if (FS_Win32
.detect()) {
51 if (FS_Win32_Cygwin
.detect())
52 INSTANCE
= new FS_Win32_Cygwin();
54 INSTANCE
= new FS_Win32();
55 } else if (FS_POSIX_Java6
.detect())
56 INSTANCE
= new FS_POSIX_Java6();
58 INSTANCE
= new FS_POSIX_Java5();
62 * Does this operating system and JRE support the execute flag on files?
64 * @return true if this implementation can provide reasonably accurate
65 * executable bit information; false otherwise.
67 public abstract boolean supportsExecute();
70 * Determine if the file is executable (or not).
72 * Not all platforms and JREs support executable flags on files. If the
73 * feature is unsupported this method will always return false.
76 * abstract path to test.
77 * @return true if the file is believed to be executable by the user.
79 public abstract boolean canExecute(File f
);
82 * Set a file to be executable by the user.
84 * Not all platforms and JREs support executable flags on files. If the
85 * feature is unsupported this method will always return false and no
86 * changes will be made to the file specified.
89 * path to modify the executable status of.
91 * true to enable execution; false to disable it.
92 * @return true if the change succeeded; false otherwise.
94 public abstract boolean setExecute(File f
, boolean canExec
);
97 * Resolve this file to its actual path name that the JRE can use.
99 * This method can be relatively expensive. Computing a translation may
100 * require forking an external process per path name translated. Callers
101 * should try to minimize the number of translations necessary by caching
104 * Not all platforms and JREs require path name translation. Currently only
105 * Cygwin on Win32 require translation for Cygwin based paths.
108 * directory relative to which the path name is.
110 * path name to translate.
111 * @return the translated path. <code>new File(dir,name)</code> if this
112 * platform does not require path name translation.
114 public static File
resolve(final File dir
, final String name
) {
115 return INSTANCE
.resolveImpl(dir
, name
);
119 * Resolve this file to its actual path name that the JRE can use.
121 * This method can be relatively expensive. Computing a translation may
122 * require forking an external process per path name translated. Callers
123 * should try to minimize the number of translations necessary by caching
126 * Not all platforms and JREs require path name translation. Currently only
127 * Cygwin on Win32 require translation for Cygwin based paths.
130 * directory relative to which the path name is.
132 * path name to translate.
133 * @return the translated path. <code>new File(dir,name)</code> if this
134 * platform does not require path name translation.
136 protected File
resolveImpl(final File dir
, final String name
) {
137 final File abspn
= new File(name
);
138 if (abspn
.isAbsolute())
140 return new File(dir
, name
);
144 * Determine the user's home directory (location where preferences are).
146 * This method can be expensive on the first invocation if path name
147 * translation is required. Subsequent invocations return a cached result.
149 * Not all platforms and JREs require path name translation. Currently only
150 * Cygwin on Win32 requires translation of the Cygwin HOME directory.
152 * @return the user's home directory; null if the user does not have one.
154 public static File
userHome() {
155 return USER_HOME
.home
;
158 private static class USER_HOME
{
159 static final File home
= INSTANCE
.userHomeImpl();
163 * Determine the user's home directory (location where preferences are).
165 * @return the user's home directory; null if the user does not have one.
167 protected File
userHomeImpl() {
168 final String home
= AccessController
169 .doPrivileged(new PrivilegedAction
<String
>() {
170 public String
run() {
171 return System
.getProperty("user.home");
174 if (home
== null || home
.length() == 0)
176 return new File(home
).getAbsoluteFile();