You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FS.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util;
  44. import java.io.File;
  45. import java.security.AccessController;
  46. import java.security.PrivilegedAction;
  47. /** Abstraction to support various file system operations not in Java. */
  48. public abstract class FS {
  49. /** The implementation selected for this operating system and JRE. */
  50. public static final FS INSTANCE;
  51. static {
  52. if (FS_Win32.detect()) {
  53. if (FS_Win32_Cygwin.detect())
  54. INSTANCE = new FS_Win32_Cygwin();
  55. else
  56. INSTANCE = new FS_Win32();
  57. } else if (FS_POSIX_Java6.detect())
  58. INSTANCE = new FS_POSIX_Java6();
  59. else
  60. INSTANCE = new FS_POSIX_Java5();
  61. }
  62. /**
  63. * Does this operating system and JRE support the execute flag on files?
  64. *
  65. * @return true if this implementation can provide reasonably accurate
  66. * executable bit information; false otherwise.
  67. */
  68. 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.
  74. *
  75. * @param f
  76. * abstract path to test.
  77. * @return true if the file is believed to be executable by the user.
  78. */
  79. public abstract boolean canExecute(File f);
  80. /**
  81. * Set a file to be executable by the user.
  82. * <p>
  83. * Not all platforms and JREs support executable flags on files. If the
  84. * feature is unsupported this method will always return false and no
  85. * changes will be made to the file specified.
  86. *
  87. * @param f
  88. * path to modify the executable status of.
  89. * @param canExec
  90. * true to enable execution; false to disable it.
  91. * @return true if the change succeeded; false otherwise.
  92. */
  93. public abstract boolean setExecute(File f, boolean canExec);
  94. /**
  95. * Resolve this file to its actual path name that the JRE can use.
  96. * <p>
  97. * This method can be relatively expensive. Computing a translation may
  98. * require forking an external process per path name translated. Callers
  99. * should try to minimize the number of translations necessary by caching
  100. * the results.
  101. * <p>
  102. * Not all platforms and JREs require path name translation. Currently only
  103. * Cygwin on Win32 require translation for Cygwin based paths.
  104. *
  105. * @param dir
  106. * directory relative to which the path name is.
  107. * @param name
  108. * path name to translate.
  109. * @return the translated path. <code>new File(dir,name)</code> if this
  110. * platform does not require path name translation.
  111. */
  112. public static File resolve(final File dir, final String name) {
  113. return INSTANCE.resolveImpl(dir, name);
  114. }
  115. /**
  116. * Resolve this file to its actual path name that the JRE can use.
  117. * <p>
  118. * This method can be relatively expensive. Computing a translation may
  119. * require forking an external process per path name translated. Callers
  120. * should try to minimize the number of translations necessary by caching
  121. * the results.
  122. * <p>
  123. * Not all platforms and JREs require path name translation. Currently only
  124. * Cygwin on Win32 require translation for Cygwin based paths.
  125. *
  126. * @param dir
  127. * directory relative to which the path name is.
  128. * @param name
  129. * path name to translate.
  130. * @return the translated path. <code>new File(dir,name)</code> if this
  131. * platform does not require path name translation.
  132. */
  133. protected File resolveImpl(final File dir, final String name) {
  134. final File abspn = new File(name);
  135. if (abspn.isAbsolute())
  136. return abspn;
  137. return new File(dir, name);
  138. }
  139. /**
  140. * Determine the user's home directory (location where preferences are).
  141. * <p>
  142. * This method can be expensive on the first invocation if path name
  143. * translation is required. Subsequent invocations return a cached result.
  144. * <p>
  145. * Not all platforms and JREs require path name translation. Currently only
  146. * Cygwin on Win32 requires translation of the Cygwin HOME directory.
  147. *
  148. * @return the user's home directory; null if the user does not have one.
  149. */
  150. public static File userHome() {
  151. return USER_HOME.home;
  152. }
  153. private static class USER_HOME {
  154. static final File home = INSTANCE.userHomeImpl();
  155. }
  156. /**
  157. * Determine the user's home directory (location where preferences are).
  158. *
  159. * @return the user's home directory; null if the user does not have one.
  160. */
  161. protected File userHomeImpl() {
  162. final String home = AccessController
  163. .doPrivileged(new PrivilegedAction<String>() {
  164. public String run() {
  165. return System.getProperty("user.home");
  166. }
  167. });
  168. if (home == null || home.length() == 0)
  169. return null;
  170. return new File(home).getAbsoluteFile();
  171. }
  172. }