Refactor AbstractTreeIterator semantics to start on first entry
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / util / FS.java
blob1e885716cd92510940dc6a0cf797d1141c17da67
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
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
21 * written permission.
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;
40 import java.io.File;
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;
49 static {
50 if (FS_Win32.detect()) {
51 if (FS_Win32_Cygwin.detect())
52 INSTANCE = new FS_Win32_Cygwin();
53 else
54 INSTANCE = new FS_Win32();
55 } else if (FS_POSIX_Java6.detect())
56 INSTANCE = new FS_POSIX_Java6();
57 else
58 INSTANCE = new FS_POSIX_Java5();
61 /**
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();
69 /**
70 * Determine if the file is executable (or not).
71 * <p>
72 * Not all platforms and JREs support executable flags on files. If the
73 * feature is unsupported this method will always return false.
75 * @param f
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);
81 /**
82 * Set a file to be executable by the user.
83 * <p>
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.
88 * @param f
89 * path to modify the executable status of.
90 * @param canExec
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);
96 /**
97 * Resolve this file to its actual path name that the JRE can use.
98 * <p>
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
102 * the results.
103 * <p>
104 * Not all platforms and JREs require path name translation. Currently only
105 * Cygwin on Win32 require translation for Cygwin based paths.
107 * @param dir
108 * directory relative to which the path name is.
109 * @param name
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.
120 * <p>
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
124 * the results.
125 * <p>
126 * Not all platforms and JREs require path name translation. Currently only
127 * Cygwin on Win32 require translation for Cygwin based paths.
129 * @param dir
130 * directory relative to which the path name is.
131 * @param name
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())
139 return abspn;
140 return new File(dir, name);
144 * Determine the user's home directory (location where preferences are).
145 * <p>
146 * This method can be expensive on the first invocation if path name
147 * translation is required. Subsequent invocations return a cached result.
148 * <p>
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)
175 return null;
176 return new File(home).getAbsoluteFile();