Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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.BufferedReader;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.io.InputStream;
  48. import java.io.InputStreamReader;
  49. import java.security.AccessController;
  50. import java.security.PrivilegedAction;
  51. import java.util.Arrays;
  52. import java.util.concurrent.atomic.AtomicBoolean;
  53. /** Abstraction to support various file system operations not in Java. */
  54. public abstract class FS {
  55. /** The auto-detected implementation selected for this operating system and JRE. */
  56. public static final FS DETECTED = detect();
  57. /**
  58. * Auto-detect the appropriate file system abstraction.
  59. *
  60. * @return detected file system abstraction
  61. */
  62. public static FS detect() {
  63. return detect(null);
  64. }
  65. /**
  66. * Auto-detect the appropriate file system abstraction, taking into account
  67. * the presence of a Cygwin installation on the system. Using jgit in
  68. * combination with Cygwin requires a more elaborate (and possibly slower)
  69. * resolution of file system paths.
  70. *
  71. * @param cygwinUsed
  72. * <ul>
  73. * <li><code>Boolean.TRUE</code> to assume that Cygwin is used in
  74. * combination with jgit</li>
  75. * <li><code>Boolean.FALSE</code> to assume that Cygwin is
  76. * <b>not</b> used with jgit</li>
  77. * <li><code>null</code> to auto-detect whether a Cygwin
  78. * installation is present on the system and in this case assume
  79. * that Cygwin is used</li>
  80. * </ul>
  81. *
  82. * Note: this parameter is only relevant on Windows.
  83. *
  84. * @return detected file system abstraction
  85. */
  86. public static FS detect(Boolean cygwinUsed) {
  87. if (FS_Win32.isWin32()) {
  88. if (cygwinUsed == null)
  89. cygwinUsed = Boolean.valueOf(FS_Win32_Cygwin.isCygwin());
  90. if (cygwinUsed.booleanValue())
  91. return new FS_Win32_Cygwin();
  92. else
  93. return new FS_Win32();
  94. } else if (FS_POSIX_Java6.hasExecute())
  95. return new FS_POSIX_Java6();
  96. else
  97. return new FS_POSIX_Java5();
  98. }
  99. private volatile Holder<File> userHome;
  100. private volatile Holder<File> gitPrefix;
  101. /**
  102. * Constructs a file system abstraction.
  103. */
  104. protected FS() {
  105. // Do nothing by default.
  106. }
  107. /**
  108. * Initialize this FS using another's current settings.
  109. *
  110. * @param src
  111. * the source FS to copy from.
  112. */
  113. protected FS(FS src) {
  114. userHome = src.userHome;
  115. gitPrefix = src.gitPrefix;
  116. }
  117. /** @return a new instance of the same type of FS. */
  118. public abstract FS newInstance();
  119. /**
  120. * Does this operating system and JRE support the execute flag on files?
  121. *
  122. * @return true if this implementation can provide reasonably accurate
  123. * executable bit information; false otherwise.
  124. */
  125. public abstract boolean supportsExecute();
  126. /**
  127. * Is this file system case sensitive
  128. *
  129. * @return true if this implementation is case sensitive
  130. */
  131. public abstract boolean isCaseSensitive();
  132. /**
  133. * Determine if the file is executable (or not).
  134. * <p>
  135. * Not all platforms and JREs support executable flags on files. If the
  136. * feature is unsupported this method will always return false.
  137. *
  138. * @param f
  139. * abstract path to test.
  140. * @return true if the file is believed to be executable by the user.
  141. */
  142. public abstract boolean canExecute(File f);
  143. /**
  144. * Set a file to be executable by the user.
  145. * <p>
  146. * Not all platforms and JREs support executable flags on files. If the
  147. * feature is unsupported this method will always return false and no
  148. * changes will be made to the file specified.
  149. *
  150. * @param f
  151. * path to modify the executable status of.
  152. * @param canExec
  153. * true to enable execution; false to disable it.
  154. * @return true if the change succeeded; false otherwise.
  155. */
  156. public abstract boolean setExecute(File f, boolean canExec);
  157. /**
  158. * Resolve this file to its actual path name that the JRE can use.
  159. * <p>
  160. * This method can be relatively expensive. Computing a translation may
  161. * require forking an external process per path name translated. Callers
  162. * should try to minimize the number of translations necessary by caching
  163. * the results.
  164. * <p>
  165. * Not all platforms and JREs require path name translation. Currently only
  166. * Cygwin on Win32 require translation for Cygwin based paths.
  167. *
  168. * @param dir
  169. * directory relative to which the path name is.
  170. * @param name
  171. * path name to translate.
  172. * @return the translated path. <code>new File(dir,name)</code> if this
  173. * platform does not require path name translation.
  174. */
  175. public File resolve(final File dir, final String name) {
  176. final File abspn = new File(name);
  177. if (abspn.isAbsolute())
  178. return abspn;
  179. return new File(dir, name);
  180. }
  181. /**
  182. * Determine the user's home directory (location where preferences are).
  183. * <p>
  184. * This method can be expensive on the first invocation if path name
  185. * translation is required. Subsequent invocations return a cached result.
  186. * <p>
  187. * Not all platforms and JREs require path name translation. Currently only
  188. * Cygwin on Win32 requires translation of the Cygwin HOME directory.
  189. *
  190. * @return the user's home directory; null if the user does not have one.
  191. */
  192. public File userHome() {
  193. Holder<File> p = userHome;
  194. if (p == null) {
  195. p = new Holder<File>(userHomeImpl());
  196. userHome = p;
  197. }
  198. return p.value;
  199. }
  200. /**
  201. * Set the user's home directory location.
  202. *
  203. * @param path
  204. * the location of the user's preferences; null if there is no
  205. * home directory for the current user.
  206. * @return {@code this}.
  207. */
  208. public FS setUserHome(File path) {
  209. userHome = new Holder<File>(path);
  210. return this;
  211. }
  212. /**
  213. * Does this file system have problems with atomic renames?
  214. *
  215. * @return true if the caller should retry a failed rename of a lock file.
  216. */
  217. public abstract boolean retryFailedLockFileCommit();
  218. /**
  219. * Determine the user's home directory (location where preferences are).
  220. *
  221. * @return the user's home directory; null if the user does not have one.
  222. */
  223. protected File userHomeImpl() {
  224. final String home = AccessController
  225. .doPrivileged(new PrivilegedAction<String>() {
  226. public String run() {
  227. return System.getProperty("user.home");
  228. }
  229. });
  230. if (home == null || home.length() == 0)
  231. return null;
  232. return new File(home).getAbsoluteFile();
  233. }
  234. /**
  235. * Searches the given path to see if it contains one of the given files.
  236. * Returns the first it finds. Returns null if not found or if path is null.
  237. *
  238. * @param path
  239. * List of paths to search separated by File.pathSeparator
  240. * @param lookFor
  241. * Files to search for in the given path
  242. * @return the first match found, or null
  243. **/
  244. static File searchPath(final String path, final String... lookFor) {
  245. if (path == null)
  246. return null;
  247. for (final String p : path.split(File.pathSeparator)) {
  248. for (String command : lookFor) {
  249. final File e = new File(p, command);
  250. if (e.isFile())
  251. return e.getAbsoluteFile();
  252. }
  253. }
  254. return null;
  255. }
  256. /**
  257. * Execute a command and return a single line of output as a String
  258. *
  259. * @param dir
  260. * Working directory for the command
  261. * @param command
  262. * as component array
  263. * @param encoding
  264. * @return the one-line output of the command
  265. */
  266. protected static String readPipe(File dir, String[] command, String encoding) {
  267. final boolean debug = Boolean.parseBoolean(SystemReader.getInstance()
  268. .getProperty("jgit.fs.debug"));
  269. try {
  270. if (debug)
  271. System.err.println("readpipe " + Arrays.asList(command) + ","
  272. + dir);
  273. final Process p = Runtime.getRuntime().exec(command, null, dir);
  274. final BufferedReader lineRead = new BufferedReader(
  275. new InputStreamReader(p.getInputStream(), encoding));
  276. p.getOutputStream().close();
  277. final AtomicBoolean gooblerFail = new AtomicBoolean(false);
  278. Thread gobbler = new Thread() {
  279. public void run() {
  280. InputStream is = p.getErrorStream();
  281. try {
  282. int ch;
  283. if (debug)
  284. while ((ch = is.read()) != -1)
  285. System.err.print((char) ch);
  286. else
  287. while (is.read() != -1) {
  288. // ignore
  289. }
  290. } catch (IOException e) {
  291. // Just print on stderr for debugging
  292. if (debug)
  293. e.printStackTrace(System.err);
  294. gooblerFail.set(true);
  295. }
  296. try {
  297. is.close();
  298. } catch (IOException e) {
  299. // Just print on stderr for debugging
  300. if (debug)
  301. e.printStackTrace(System.err);
  302. gooblerFail.set(true);
  303. }
  304. }
  305. };
  306. gobbler.start();
  307. String r = null;
  308. try {
  309. r = lineRead.readLine();
  310. if (debug) {
  311. System.err.println("readpipe may return '" + r + "'");
  312. System.err.println("(ignoring remaing output:");
  313. }
  314. String l;
  315. while ((l = lineRead.readLine()) != null) {
  316. if (debug)
  317. System.err.println(l);
  318. }
  319. } finally {
  320. p.getErrorStream().close();
  321. lineRead.close();
  322. }
  323. for (;;) {
  324. try {
  325. int rc = p.waitFor();
  326. gobbler.join();
  327. if (rc == 0 && r != null && r.length() > 0
  328. && !gooblerFail.get())
  329. return r;
  330. if (debug)
  331. System.err.println("readpipe rc=" + rc);
  332. break;
  333. } catch (InterruptedException ie) {
  334. // Stop bothering me, I have a zombie to reap.
  335. }
  336. }
  337. } catch (IOException e) {
  338. if (debug)
  339. System.err.println(e);
  340. // Ignore error (but report)
  341. }
  342. if (debug)
  343. System.err.println("readpipe returns null");
  344. return null;
  345. }
  346. /** @return the $prefix directory C Git would use. */
  347. public File gitPrefix() {
  348. Holder<File> p = gitPrefix;
  349. if (p == null) {
  350. String overrideGitPrefix = SystemReader.getInstance().getProperty(
  351. "jgit.gitprefix");
  352. if (overrideGitPrefix != null)
  353. p = new Holder<File>(new File(overrideGitPrefix));
  354. else
  355. p = new Holder<File>(discoverGitPrefix());
  356. gitPrefix = p;
  357. }
  358. return p.value;
  359. }
  360. /** @return the $prefix directory C Git would use. */
  361. protected abstract File discoverGitPrefix();
  362. /**
  363. * Set the $prefix directory C Git uses.
  364. *
  365. * @param path
  366. * the directory. Null if C Git is not installed.
  367. * @return {@code this}
  368. */
  369. public FS setGitPrefix(File path) {
  370. gitPrefix = new Holder<File>(path);
  371. return this;
  372. }
  373. /**
  374. * Initialize a ProcesssBuilder to run a command using the system shell.
  375. *
  376. * @param cmd
  377. * command to execute. This string should originate from the
  378. * end-user, and thus is platform specific.
  379. * @param args
  380. * arguments to pass to command. These should be protected from
  381. * shell evaluation.
  382. * @return a partially completed process builder. Caller should finish
  383. * populating directory, environment, and then start the process.
  384. */
  385. public abstract ProcessBuilder runInShell(String cmd, String[] args);
  386. private static class Holder<V> {
  387. final V value;
  388. Holder(V value) {
  389. this.value = value;
  390. }
  391. }
  392. }