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 66KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  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 static java.nio.charset.StandardCharsets.UTF_8;
  45. import static java.time.Instant.EPOCH;
  46. import java.io.BufferedReader;
  47. import java.io.ByteArrayInputStream;
  48. import java.io.Closeable;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.io.InputStreamReader;
  53. import java.io.OutputStream;
  54. import java.io.OutputStreamWriter;
  55. import java.io.PrintStream;
  56. import java.io.Writer;
  57. import java.nio.charset.Charset;
  58. import java.nio.file.AccessDeniedException;
  59. import java.nio.file.FileStore;
  60. import java.nio.file.Files;
  61. import java.nio.file.Path;
  62. import java.nio.file.attribute.BasicFileAttributes;
  63. import java.nio.file.attribute.FileTime;
  64. import java.security.AccessController;
  65. import java.security.PrivilegedAction;
  66. import java.text.MessageFormat;
  67. import java.time.Duration;
  68. import java.time.Instant;
  69. import java.util.ArrayList;
  70. import java.util.Arrays;
  71. import java.util.HashMap;
  72. import java.util.Map;
  73. import java.util.Objects;
  74. import java.util.Optional;
  75. import java.util.UUID;
  76. import java.util.concurrent.CancellationException;
  77. import java.util.concurrent.CompletableFuture;
  78. import java.util.concurrent.ConcurrentHashMap;
  79. import java.util.concurrent.ExecutionException;
  80. import java.util.concurrent.ExecutorService;
  81. import java.util.concurrent.Executors;
  82. import java.util.concurrent.TimeUnit;
  83. import java.util.concurrent.TimeoutException;
  84. import java.util.concurrent.atomic.AtomicBoolean;
  85. import java.util.concurrent.atomic.AtomicReference;
  86. import java.util.concurrent.locks.Lock;
  87. import java.util.concurrent.locks.ReentrantLock;
  88. import org.eclipse.jgit.annotations.NonNull;
  89. import org.eclipse.jgit.annotations.Nullable;
  90. import org.eclipse.jgit.api.errors.JGitInternalException;
  91. import org.eclipse.jgit.errors.CommandFailedException;
  92. import org.eclipse.jgit.errors.ConfigInvalidException;
  93. import org.eclipse.jgit.errors.LockFailedException;
  94. import org.eclipse.jgit.internal.JGitText;
  95. import org.eclipse.jgit.internal.storage.file.FileSnapshot;
  96. import org.eclipse.jgit.lib.ConfigConstants;
  97. import org.eclipse.jgit.lib.Constants;
  98. import org.eclipse.jgit.lib.Repository;
  99. import org.eclipse.jgit.storage.file.FileBasedConfig;
  100. import org.eclipse.jgit.treewalk.FileTreeIterator.FileEntry;
  101. import org.eclipse.jgit.treewalk.FileTreeIterator.FileModeStrategy;
  102. import org.eclipse.jgit.treewalk.WorkingTreeIterator.Entry;
  103. import org.eclipse.jgit.util.ProcessResult.Status;
  104. import org.slf4j.Logger;
  105. import org.slf4j.LoggerFactory;
  106. /**
  107. * Abstraction to support various file system operations not in Java.
  108. */
  109. public abstract class FS {
  110. private static final Logger LOG = LoggerFactory.getLogger(FS.class);
  111. /**
  112. * An empty array of entries, suitable as a return value for
  113. * {@link #list(File, FileModeStrategy)}.
  114. *
  115. * @since 5.0
  116. */
  117. protected static final Entry[] NO_ENTRIES = {};
  118. /**
  119. * This class creates FS instances. It will be overridden by a Java7 variant
  120. * if such can be detected in {@link #detect(Boolean)}.
  121. *
  122. * @since 3.0
  123. */
  124. public static class FSFactory {
  125. /**
  126. * Constructor
  127. */
  128. protected FSFactory() {
  129. // empty
  130. }
  131. /**
  132. * Detect the file system
  133. *
  134. * @param cygwinUsed
  135. * @return FS instance
  136. */
  137. public FS detect(Boolean cygwinUsed) {
  138. if (SystemReader.getInstance().isWindows()) {
  139. if (cygwinUsed == null)
  140. cygwinUsed = Boolean.valueOf(FS_Win32_Cygwin.isCygwin());
  141. if (cygwinUsed.booleanValue())
  142. return new FS_Win32_Cygwin();
  143. else
  144. return new FS_Win32();
  145. } else {
  146. return new FS_POSIX();
  147. }
  148. }
  149. }
  150. /**
  151. * Result of an executed process. The caller is responsible to close the
  152. * contained {@link TemporaryBuffer}s
  153. *
  154. * @since 4.2
  155. */
  156. public static class ExecutionResult {
  157. private TemporaryBuffer stdout;
  158. private TemporaryBuffer stderr;
  159. private int rc;
  160. /**
  161. * @param stdout
  162. * @param stderr
  163. * @param rc
  164. */
  165. public ExecutionResult(TemporaryBuffer stdout, TemporaryBuffer stderr,
  166. int rc) {
  167. this.stdout = stdout;
  168. this.stderr = stderr;
  169. this.rc = rc;
  170. }
  171. /**
  172. * @return buffered standard output stream
  173. */
  174. public TemporaryBuffer getStdout() {
  175. return stdout;
  176. }
  177. /**
  178. * @return buffered standard error stream
  179. */
  180. public TemporaryBuffer getStderr() {
  181. return stderr;
  182. }
  183. /**
  184. * @return the return code of the process
  185. */
  186. public int getRc() {
  187. return rc;
  188. }
  189. }
  190. /**
  191. * Attributes of FileStores on this system
  192. *
  193. * @since 5.1.9
  194. */
  195. public final static class FileStoreAttributes {
  196. private static final Duration UNDEFINED_DURATION = Duration
  197. .ofNanos(Long.MAX_VALUE);
  198. /**
  199. * Fallback filesystem timestamp resolution. The worst case timestamp
  200. * resolution on FAT filesystems is 2 seconds.
  201. */
  202. public static final Duration FALLBACK_TIMESTAMP_RESOLUTION = Duration
  203. .ofMillis(2000);
  204. /**
  205. * Fallback FileStore attributes used when we can't measure the
  206. * filesystem timestamp resolution. The last modified time granularity
  207. * of FAT filesystems is 2 seconds.
  208. */
  209. public static final FileStoreAttributes FALLBACK_FILESTORE_ATTRIBUTES = new FileStoreAttributes(
  210. FALLBACK_TIMESTAMP_RESOLUTION);
  211. private static final Map<FileStore, FileStoreAttributes> attributeCache = new ConcurrentHashMap<>();
  212. private static final SimpleLruCache<Path, FileStoreAttributes> attrCacheByPath = new SimpleLruCache<>(
  213. 100, 0.2f);
  214. private static AtomicBoolean background = new AtomicBoolean();
  215. private static Map<FileStore, Lock> locks = new ConcurrentHashMap<>();
  216. private static void setBackground(boolean async) {
  217. background.set(async);
  218. }
  219. private static final String javaVersionPrefix = System
  220. .getProperty("java.vendor") + '|' //$NON-NLS-1$
  221. + System.getProperty("java.version") + '|'; //$NON-NLS-1$
  222. private static final Duration FALLBACK_MIN_RACY_INTERVAL = Duration
  223. .ofMillis(10);
  224. /**
  225. * Configures size and purge factor of the path-based cache for file
  226. * system attributes. Caching of file system attributes avoids recurring
  227. * lookup of @{code FileStore} of files which may be expensive on some
  228. * platforms.
  229. *
  230. * @param maxSize
  231. * maximum size of the cache, default is 100
  232. * @param purgeFactor
  233. * when the size of the map reaches maxSize the oldest
  234. * entries will be purged to free up some space for new
  235. * entries, {@code purgeFactor} is the fraction of
  236. * {@code maxSize} to purge when this happens
  237. * @since 5.1.9
  238. */
  239. public static void configureAttributesPathCache(int maxSize,
  240. float purgeFactor) {
  241. FileStoreAttributes.attrCacheByPath.configure(maxSize, purgeFactor);
  242. }
  243. /**
  244. * Get the FileStoreAttributes for the given FileStore
  245. *
  246. * @param path
  247. * file residing in the FileStore to get attributes for
  248. * @return FileStoreAttributes for the given path.
  249. */
  250. public static FileStoreAttributes get(Path path) {
  251. path = path.toAbsolutePath();
  252. Path dir = Files.isDirectory(path) ? path : path.getParent();
  253. FileStoreAttributes cached = attrCacheByPath.get(dir);
  254. if (cached != null) {
  255. return cached;
  256. }
  257. FileStoreAttributes attrs = getFileStoreAttributes(dir);
  258. attrCacheByPath.put(dir, attrs);
  259. return attrs;
  260. }
  261. private static FileStoreAttributes getFileStoreAttributes(Path dir) {
  262. FileStore s;
  263. try {
  264. if (Files.exists(dir)) {
  265. s = Files.getFileStore(dir);
  266. FileStoreAttributes c = attributeCache.get(s);
  267. if (c != null) {
  268. return c;
  269. }
  270. if (!Files.isWritable(dir)) {
  271. // cannot measure resolution in a read-only directory
  272. LOG.debug(
  273. "{}: cannot measure timestamp resolution in read-only directory {}", //$NON-NLS-1$
  274. Thread.currentThread(), dir);
  275. return FALLBACK_FILESTORE_ATTRIBUTES;
  276. }
  277. } else {
  278. // cannot determine FileStore of an unborn directory
  279. LOG.debug(
  280. "{}: cannot measure timestamp resolution of unborn directory {}", //$NON-NLS-1$
  281. Thread.currentThread(), dir);
  282. return FALLBACK_FILESTORE_ATTRIBUTES;
  283. }
  284. CompletableFuture<Optional<FileStoreAttributes>> f = CompletableFuture
  285. .supplyAsync(() -> {
  286. Lock lock = locks.computeIfAbsent(s,
  287. l -> new ReentrantLock());
  288. if (!lock.tryLock()) {
  289. LOG.debug(
  290. "{}: couldn't get lock to measure timestamp resolution in {}", //$NON-NLS-1$
  291. Thread.currentThread(), dir);
  292. return Optional.empty();
  293. }
  294. Optional<FileStoreAttributes> attributes = Optional
  295. .empty();
  296. try {
  297. // Some earlier future might have set the value
  298. // and removed itself since we checked for the
  299. // value above. Hence check cache again.
  300. FileStoreAttributes c = attributeCache
  301. .get(s);
  302. if (c != null) {
  303. return Optional.of(c);
  304. }
  305. attributes = readFromConfig(s);
  306. if (attributes.isPresent()) {
  307. attributeCache.put(s, attributes.get());
  308. return attributes;
  309. }
  310. Optional<Duration> resolution = measureFsTimestampResolution(
  311. s, dir);
  312. if (resolution.isPresent()) {
  313. c = new FileStoreAttributes(
  314. resolution.get());
  315. attributeCache.put(s, c);
  316. // for high timestamp resolution measure
  317. // minimal racy interval
  318. if (c.fsTimestampResolution
  319. .toNanos() < 100_000_000L) {
  320. c.minimalRacyInterval = measureMinimalRacyInterval(
  321. dir);
  322. }
  323. if (LOG.isDebugEnabled()) {
  324. LOG.debug(c.toString());
  325. }
  326. saveToConfig(s, c);
  327. }
  328. attributes = Optional.of(c);
  329. } finally {
  330. lock.unlock();
  331. locks.remove(s);
  332. }
  333. return attributes;
  334. });
  335. f.exceptionally(e -> {
  336. LOG.error(e.getLocalizedMessage(), e);
  337. return Optional.empty();
  338. });
  339. // even if measuring in background wait a little - if the result
  340. // arrives, it's better than returning the large fallback
  341. Optional<FileStoreAttributes> d = background.get() ? f.get(
  342. 100, TimeUnit.MILLISECONDS) : f.get();
  343. if (d.isPresent()) {
  344. return d.get();
  345. }
  346. // return fallback until measurement is finished
  347. } catch (IOException | InterruptedException
  348. | ExecutionException | CancellationException e) {
  349. LOG.error(e.getMessage(), e);
  350. } catch (TimeoutException | SecurityException e) {
  351. // use fallback
  352. }
  353. LOG.debug("{}: use fallback timestamp resolution for directory {}", //$NON-NLS-1$
  354. Thread.currentThread(), dir);
  355. return FALLBACK_FILESTORE_ATTRIBUTES;
  356. }
  357. @SuppressWarnings("boxing")
  358. private static Duration measureMinimalRacyInterval(Path dir) {
  359. LOG.debug("{}: start measure minimal racy interval in {}", //$NON-NLS-1$
  360. Thread.currentThread(), dir);
  361. int n = 0;
  362. int failures = 0;
  363. long racyNanos = 0;
  364. ArrayList<Long> deltas = new ArrayList<>();
  365. Path probe = dir.resolve(".probe-" + UUID.randomUUID()); //$NON-NLS-1$
  366. Instant end = Instant.now().plusSeconds(3);
  367. try {
  368. Files.createFile(probe);
  369. do {
  370. n++;
  371. write(probe, "a"); //$NON-NLS-1$
  372. FileSnapshot snapshot = FileSnapshot.save(probe.toFile());
  373. read(probe);
  374. write(probe, "b"); //$NON-NLS-1$
  375. if (!snapshot.isModified(probe.toFile())) {
  376. deltas.add(Long.valueOf(snapshot.lastDelta()));
  377. racyNanos = snapshot.lastRacyThreshold();
  378. failures++;
  379. }
  380. } while (Instant.now().compareTo(end) < 0);
  381. } catch (IOException e) {
  382. LOG.error(e.getMessage(), e);
  383. return FALLBACK_MIN_RACY_INTERVAL;
  384. } finally {
  385. deleteProbe(probe);
  386. }
  387. if (failures > 0) {
  388. Stats stats = new Stats();
  389. for (Long d : deltas) {
  390. stats.add(d);
  391. }
  392. LOG.debug(
  393. "delta [ns] since modification FileSnapshot failed to detect\n" //$NON-NLS-1$
  394. + "count, failures, racy limit [ns], delta min [ns]," //$NON-NLS-1$
  395. + " delta max [ns], delta avg [ns]," //$NON-NLS-1$
  396. + " delta stddev [ns]\n" //$NON-NLS-1$
  397. + "{}, {}, {}, {}, {}, {}, {}", //$NON-NLS-1$
  398. n, failures, racyNanos, stats.min(), stats.max(),
  399. stats.avg(), stats.stddev());
  400. return Duration
  401. .ofNanos(Double.valueOf(stats.max()).longValue());
  402. }
  403. // since no failures occurred using the measured filesystem
  404. // timestamp resolution there is no need for minimal racy interval
  405. LOG.debug("{}: no failures when measuring minimal racy interval", //$NON-NLS-1$
  406. Thread.currentThread());
  407. return Duration.ZERO;
  408. }
  409. private static void write(Path p, String body) throws IOException {
  410. FileUtils.mkdirs(p.getParent().toFile(), true);
  411. try (Writer w = new OutputStreamWriter(Files.newOutputStream(p),
  412. UTF_8)) {
  413. w.write(body);
  414. }
  415. }
  416. private static String read(Path p) throws IOException {
  417. final byte[] body = IO.readFully(p.toFile());
  418. return new String(body, 0, body.length, UTF_8);
  419. }
  420. private static Optional<Duration> measureFsTimestampResolution(
  421. FileStore s, Path dir) {
  422. LOG.debug("{}: start measure timestamp resolution {} in {}", //$NON-NLS-1$
  423. Thread.currentThread(), s, dir);
  424. Path probe = dir.resolve(".probe-" + UUID.randomUUID()); //$NON-NLS-1$
  425. try {
  426. Files.createFile(probe);
  427. FileTime t1 = Files.getLastModifiedTime(probe);
  428. FileTime t2 = t1;
  429. Instant t1i = t1.toInstant();
  430. for (long i = 1; t2.compareTo(t1) <= 0; i += 1 + i / 20) {
  431. Files.setLastModifiedTime(probe,
  432. FileTime.from(t1i.plusNanos(i * 1000)));
  433. t2 = Files.getLastModifiedTime(probe);
  434. }
  435. Duration fsResolution = Duration.between(t1.toInstant(), t2.toInstant());
  436. Duration clockResolution = measureClockResolution();
  437. fsResolution = fsResolution.plus(clockResolution);
  438. LOG.debug("{}: end measure timestamp resolution {} in {}", //$NON-NLS-1$
  439. Thread.currentThread(), s, dir);
  440. return Optional.of(fsResolution);
  441. } catch (AccessDeniedException e) {
  442. LOG.warn(e.getLocalizedMessage(), e); // see bug 548648
  443. } catch (IOException e) {
  444. LOG.error(e.getLocalizedMessage(), e);
  445. } finally {
  446. deleteProbe(probe);
  447. }
  448. return Optional.empty();
  449. }
  450. private static Duration measureClockResolution() {
  451. Duration clockResolution = Duration.ZERO;
  452. for (int i = 0; i < 10; i++) {
  453. Instant t1 = Instant.now();
  454. Instant t2 = t1;
  455. while (t2.compareTo(t1) <= 0) {
  456. t2 = Instant.now();
  457. }
  458. Duration r = Duration.between(t1, t2);
  459. if (r.compareTo(clockResolution) > 0) {
  460. clockResolution = r;
  461. }
  462. }
  463. return clockResolution;
  464. }
  465. private static void deleteProbe(Path probe) {
  466. try {
  467. FileUtils.delete(probe.toFile(),
  468. FileUtils.SKIP_MISSING | FileUtils.RETRY);
  469. } catch (IOException e) {
  470. LOG.error(e.getMessage(), e);
  471. }
  472. }
  473. private static Optional<FileStoreAttributes> readFromConfig(
  474. FileStore s) {
  475. FileBasedConfig userConfig = SystemReader.getInstance()
  476. .openUserConfig(null, FS.DETECTED);
  477. try {
  478. userConfig.load(false);
  479. } catch (IOException e) {
  480. LOG.error(MessageFormat.format(JGitText.get().readConfigFailed,
  481. userConfig.getFile().getAbsolutePath()), e);
  482. } catch (ConfigInvalidException e) {
  483. LOG.error(MessageFormat.format(
  484. JGitText.get().repositoryConfigFileInvalid,
  485. userConfig.getFile().getAbsolutePath(),
  486. e.getMessage()));
  487. }
  488. String key = getConfigKey(s);
  489. Duration resolution = Duration.ofNanos(userConfig.getTimeUnit(
  490. ConfigConstants.CONFIG_FILESYSTEM_SECTION, key,
  491. ConfigConstants.CONFIG_KEY_TIMESTAMP_RESOLUTION,
  492. UNDEFINED_DURATION.toNanos(), TimeUnit.NANOSECONDS));
  493. if (UNDEFINED_DURATION.equals(resolution)) {
  494. return Optional.empty();
  495. }
  496. Duration minRacyThreshold = Duration.ofNanos(userConfig.getTimeUnit(
  497. ConfigConstants.CONFIG_FILESYSTEM_SECTION, key,
  498. ConfigConstants.CONFIG_KEY_MIN_RACY_THRESHOLD,
  499. UNDEFINED_DURATION.toNanos(), TimeUnit.NANOSECONDS));
  500. FileStoreAttributes c = new FileStoreAttributes(resolution);
  501. if (!UNDEFINED_DURATION.equals(minRacyThreshold)) {
  502. c.minimalRacyInterval = minRacyThreshold;
  503. }
  504. return Optional.of(c);
  505. }
  506. private static void saveToConfig(FileStore s,
  507. FileStoreAttributes c) {
  508. FileBasedConfig userConfig = SystemReader.getInstance()
  509. .openUserConfig(null, FS.DETECTED);
  510. long resolution = c.getFsTimestampResolution().toNanos();
  511. TimeUnit resolutionUnit = getUnit(resolution);
  512. long resolutionValue = resolutionUnit.convert(resolution,
  513. TimeUnit.NANOSECONDS);
  514. long minRacyThreshold = c.getMinimalRacyInterval().toNanos();
  515. TimeUnit minRacyThresholdUnit = getUnit(minRacyThreshold);
  516. long minRacyThresholdValue = minRacyThresholdUnit
  517. .convert(minRacyThreshold, TimeUnit.NANOSECONDS);
  518. final int max_retries = 5;
  519. int retries = 0;
  520. boolean succeeded = false;
  521. String key = getConfigKey(s);
  522. while (!succeeded && retries < max_retries) {
  523. try {
  524. userConfig.load(false);
  525. userConfig.setString(
  526. ConfigConstants.CONFIG_FILESYSTEM_SECTION, key,
  527. ConfigConstants.CONFIG_KEY_TIMESTAMP_RESOLUTION,
  528. String.format("%d %s", //$NON-NLS-1$
  529. Long.valueOf(resolutionValue),
  530. resolutionUnit.name().toLowerCase()));
  531. userConfig.setString(
  532. ConfigConstants.CONFIG_FILESYSTEM_SECTION, key,
  533. ConfigConstants.CONFIG_KEY_MIN_RACY_THRESHOLD,
  534. String.format("%d %s", //$NON-NLS-1$
  535. Long.valueOf(minRacyThresholdValue),
  536. minRacyThresholdUnit.name().toLowerCase()));
  537. userConfig.save();
  538. succeeded = true;
  539. } catch (LockFailedException e) {
  540. // race with another thread, wait a bit and try again
  541. try {
  542. LOG.warn(MessageFormat.format(JGitText.get().cannotLock,
  543. userConfig.getFile().getAbsolutePath()));
  544. retries++;
  545. Thread.sleep(20);
  546. } catch (InterruptedException e1) {
  547. Thread.interrupted();
  548. }
  549. } catch (IOException e) {
  550. LOG.error(MessageFormat.format(
  551. JGitText.get().cannotSaveConfig,
  552. userConfig.getFile().getAbsolutePath()), e);
  553. } catch (ConfigInvalidException e) {
  554. LOG.error(MessageFormat.format(
  555. JGitText.get().repositoryConfigFileInvalid,
  556. userConfig.getFile().getAbsolutePath(),
  557. e.getMessage()));
  558. }
  559. }
  560. }
  561. private static String getConfigKey(FileStore s) {
  562. final String storeKey;
  563. if (SystemReader.getInstance().isWindows()) {
  564. Object attribute = null;
  565. try {
  566. attribute = s.getAttribute("volume:vsn"); //$NON-NLS-1$
  567. } catch (IOException ignored) {
  568. // ignore
  569. }
  570. if (attribute instanceof Integer) {
  571. storeKey = attribute.toString();
  572. } else {
  573. storeKey = s.name();
  574. }
  575. } else {
  576. storeKey = s.name();
  577. }
  578. return javaVersionPrefix + storeKey;
  579. }
  580. private static TimeUnit getUnit(long nanos) {
  581. TimeUnit unit;
  582. if (nanos < 200_000L) {
  583. unit = TimeUnit.NANOSECONDS;
  584. } else if (nanos < 200_000_000L) {
  585. unit = TimeUnit.MICROSECONDS;
  586. } else {
  587. unit = TimeUnit.MILLISECONDS;
  588. }
  589. return unit;
  590. }
  591. private final @NonNull Duration fsTimestampResolution;
  592. private Duration minimalRacyInterval;
  593. /**
  594. * @return the measured minimal interval after a file has been modified
  595. * in which we cannot rely on lastModified to detect
  596. * modifications
  597. */
  598. public Duration getMinimalRacyInterval() {
  599. return minimalRacyInterval;
  600. }
  601. /**
  602. * @return the measured filesystem timestamp resolution
  603. */
  604. @NonNull
  605. public Duration getFsTimestampResolution() {
  606. return fsTimestampResolution;
  607. }
  608. /**
  609. * Construct a FileStoreAttributeCache entry for the given filesystem
  610. * timestamp resolution
  611. *
  612. * @param fsTimestampResolution
  613. */
  614. public FileStoreAttributes(
  615. @NonNull Duration fsTimestampResolution) {
  616. this.fsTimestampResolution = fsTimestampResolution;
  617. this.minimalRacyInterval = Duration.ZERO;
  618. }
  619. @SuppressWarnings({ "nls", "boxing" })
  620. @Override
  621. public String toString() {
  622. return String.format(
  623. "FileStoreAttributes[fsTimestampResolution=%,d µs, "
  624. + "minimalRacyInterval=%,d µs]",
  625. fsTimestampResolution.toNanos() / 1000,
  626. minimalRacyInterval.toNanos() / 1000);
  627. }
  628. }
  629. /** The auto-detected implementation selected for this operating system and JRE. */
  630. public static final FS DETECTED = detect();
  631. private volatile static FSFactory factory;
  632. /**
  633. * Auto-detect the appropriate file system abstraction.
  634. *
  635. * @return detected file system abstraction
  636. */
  637. public static FS detect() {
  638. return detect(null);
  639. }
  640. /**
  641. * Whether FileStore attributes should be determined asynchronously
  642. *
  643. * @param asynch
  644. * whether FileStore attributes should be determined
  645. * asynchronously. If false access to cached attributes may block
  646. * for some seconds for the first call per FileStore
  647. * @since 5.1.9
  648. */
  649. public static void setAsyncFileStoreAttributes(boolean asynch) {
  650. FileStoreAttributes.setBackground(asynch);
  651. }
  652. /**
  653. * Auto-detect the appropriate file system abstraction, taking into account
  654. * the presence of a Cygwin installation on the system. Using jgit in
  655. * combination with Cygwin requires a more elaborate (and possibly slower)
  656. * resolution of file system paths.
  657. *
  658. * @param cygwinUsed
  659. * <ul>
  660. * <li><code>Boolean.TRUE</code> to assume that Cygwin is used in
  661. * combination with jgit</li>
  662. * <li><code>Boolean.FALSE</code> to assume that Cygwin is
  663. * <b>not</b> used with jgit</li>
  664. * <li><code>null</code> to auto-detect whether a Cygwin
  665. * installation is present on the system and in this case assume
  666. * that Cygwin is used</li>
  667. * </ul>
  668. *
  669. * Note: this parameter is only relevant on Windows.
  670. * @return detected file system abstraction
  671. */
  672. public static FS detect(Boolean cygwinUsed) {
  673. if (factory == null) {
  674. factory = new FS.FSFactory();
  675. }
  676. return factory.detect(cygwinUsed);
  677. }
  678. /**
  679. * Get cached FileStore attributes, if not yet available measure them using
  680. * a probe file under the given directory.
  681. *
  682. * @param dir
  683. * the directory under which the probe file will be created to
  684. * measure the timer resolution.
  685. * @return measured filesystem timestamp resolution
  686. * @since 5.1.9
  687. */
  688. public static FileStoreAttributes getFileStoreAttributes(
  689. @NonNull Path dir) {
  690. return FileStoreAttributes.get(dir);
  691. }
  692. private volatile Holder<File> userHome;
  693. private volatile Holder<File> gitSystemConfig;
  694. /**
  695. * Constructs a file system abstraction.
  696. */
  697. protected FS() {
  698. // Do nothing by default.
  699. }
  700. /**
  701. * Initialize this FS using another's current settings.
  702. *
  703. * @param src
  704. * the source FS to copy from.
  705. */
  706. protected FS(FS src) {
  707. userHome = src.userHome;
  708. gitSystemConfig = src.gitSystemConfig;
  709. }
  710. /**
  711. * Create a new instance of the same type of FS.
  712. *
  713. * @return a new instance of the same type of FS.
  714. */
  715. public abstract FS newInstance();
  716. /**
  717. * Does this operating system and JRE support the execute flag on files?
  718. *
  719. * @return true if this implementation can provide reasonably accurate
  720. * executable bit information; false otherwise.
  721. */
  722. public abstract boolean supportsExecute();
  723. /**
  724. * Does this file system support atomic file creation via
  725. * java.io.File#createNewFile()? In certain environments (e.g. on NFS) it is
  726. * not guaranteed that when two file system clients run createNewFile() in
  727. * parallel only one will succeed. In such cases both clients may think they
  728. * created a new file.
  729. *
  730. * @return true if this implementation support atomic creation of new Files
  731. * by {@link java.io.File#createNewFile()}
  732. * @since 4.5
  733. */
  734. public boolean supportsAtomicCreateNewFile() {
  735. return true;
  736. }
  737. /**
  738. * Does this operating system and JRE supports symbolic links. The
  739. * capability to handle symbolic links is detected at runtime.
  740. *
  741. * @return true if symbolic links may be used
  742. * @since 3.0
  743. */
  744. public boolean supportsSymlinks() {
  745. return false;
  746. }
  747. /**
  748. * Is this file system case sensitive
  749. *
  750. * @return true if this implementation is case sensitive
  751. */
  752. public abstract boolean isCaseSensitive();
  753. /**
  754. * Determine if the file is executable (or not).
  755. * <p>
  756. * Not all platforms and JREs support executable flags on files. If the
  757. * feature is unsupported this method will always return false.
  758. * <p>
  759. * <em>If the platform supports symbolic links and <code>f</code> is a symbolic link
  760. * this method returns false, rather than the state of the executable flags
  761. * on the target file.</em>
  762. *
  763. * @param f
  764. * abstract path to test.
  765. * @return true if the file is believed to be executable by the user.
  766. */
  767. public abstract boolean canExecute(File f);
  768. /**
  769. * Set a file to be executable by the user.
  770. * <p>
  771. * Not all platforms and JREs support executable flags on files. If the
  772. * feature is unsupported this method will always return false and no
  773. * changes will be made to the file specified.
  774. *
  775. * @param f
  776. * path to modify the executable status of.
  777. * @param canExec
  778. * true to enable execution; false to disable it.
  779. * @return true if the change succeeded; false otherwise.
  780. */
  781. public abstract boolean setExecute(File f, boolean canExec);
  782. /**
  783. * Get the last modified time of a file system object. If the OS/JRE support
  784. * symbolic links, the modification time of the link is returned, rather
  785. * than that of the link target.
  786. *
  787. * @param f
  788. * a {@link java.io.File} object.
  789. * @return last modified time of f
  790. * @throws java.io.IOException
  791. * @since 3.0
  792. * @deprecated use {@link #lastModifiedInstant(Path)} instead
  793. */
  794. @Deprecated
  795. public long lastModified(File f) throws IOException {
  796. return FileUtils.lastModified(f);
  797. }
  798. /**
  799. * Get the last modified time of a file system object. If the OS/JRE support
  800. * symbolic links, the modification time of the link is returned, rather
  801. * than that of the link target.
  802. *
  803. * @param p
  804. * a {@link Path} object.
  805. * @return last modified time of p
  806. * @since 5.1.9
  807. */
  808. public Instant lastModifiedInstant(Path p) {
  809. return FileUtils.lastModifiedInstant(p);
  810. }
  811. /**
  812. * Get the last modified time of a file system object. If the OS/JRE support
  813. * symbolic links, the modification time of the link is returned, rather
  814. * than that of the link target.
  815. *
  816. * @param f
  817. * a {@link File} object.
  818. * @return last modified time of p
  819. * @since 5.1.9
  820. */
  821. public Instant lastModifiedInstant(File f) {
  822. return FileUtils.lastModifiedInstant(f.toPath());
  823. }
  824. /**
  825. * Set the last modified time of a file system object. If the OS/JRE support
  826. * symbolic links, the link is modified, not the target,
  827. *
  828. * @param f
  829. * a {@link java.io.File} object.
  830. * @param time
  831. * last modified time
  832. * @throws java.io.IOException
  833. * @since 3.0
  834. * @deprecated use {@link #setLastModified(Path, Instant)} instead
  835. */
  836. @Deprecated
  837. public void setLastModified(File f, long time) throws IOException {
  838. FileUtils.setLastModified(f, time);
  839. }
  840. /**
  841. * Set the last modified time of a file system object. If the OS/JRE support
  842. * symbolic links, the link is modified, not the target,
  843. *
  844. * @param p
  845. * a {@link Path} object.
  846. * @param time
  847. * last modified time
  848. * @throws java.io.IOException
  849. * @since 5.1.9
  850. */
  851. public void setLastModified(Path p, Instant time) throws IOException {
  852. FileUtils.setLastModified(p, time);
  853. }
  854. /**
  855. * Get the length of a file or link, If the OS/JRE supports symbolic links
  856. * it's the length of the link, else the length of the target.
  857. *
  858. * @param path
  859. * a {@link java.io.File} object.
  860. * @return length of a file
  861. * @throws java.io.IOException
  862. * @since 3.0
  863. */
  864. public long length(File path) throws IOException {
  865. return FileUtils.getLength(path);
  866. }
  867. /**
  868. * Delete a file. Throws an exception if delete fails.
  869. *
  870. * @param f
  871. * a {@link java.io.File} object.
  872. * @throws java.io.IOException
  873. * this may be a Java7 subclass with detailed information
  874. * @since 3.3
  875. */
  876. public void delete(File f) throws IOException {
  877. FileUtils.delete(f);
  878. }
  879. /**
  880. * Resolve this file to its actual path name that the JRE can use.
  881. * <p>
  882. * This method can be relatively expensive. Computing a translation may
  883. * require forking an external process per path name translated. Callers
  884. * should try to minimize the number of translations necessary by caching
  885. * the results.
  886. * <p>
  887. * Not all platforms and JREs require path name translation. Currently only
  888. * Cygwin on Win32 require translation for Cygwin based paths.
  889. *
  890. * @param dir
  891. * directory relative to which the path name is.
  892. * @param name
  893. * path name to translate.
  894. * @return the translated path. <code>new File(dir,name)</code> if this
  895. * platform does not require path name translation.
  896. */
  897. public File resolve(File dir, String name) {
  898. final File abspn = new File(name);
  899. if (abspn.isAbsolute())
  900. return abspn;
  901. return new File(dir, name);
  902. }
  903. /**
  904. * Determine the user's home directory (location where preferences are).
  905. * <p>
  906. * This method can be expensive on the first invocation if path name
  907. * translation is required. Subsequent invocations return a cached result.
  908. * <p>
  909. * Not all platforms and JREs require path name translation. Currently only
  910. * Cygwin on Win32 requires translation of the Cygwin HOME directory.
  911. *
  912. * @return the user's home directory; null if the user does not have one.
  913. */
  914. public File userHome() {
  915. Holder<File> p = userHome;
  916. if (p == null) {
  917. p = new Holder<>(userHomeImpl());
  918. userHome = p;
  919. }
  920. return p.value;
  921. }
  922. /**
  923. * Set the user's home directory location.
  924. *
  925. * @param path
  926. * the location of the user's preferences; null if there is no
  927. * home directory for the current user.
  928. * @return {@code this}.
  929. */
  930. public FS setUserHome(File path) {
  931. userHome = new Holder<>(path);
  932. return this;
  933. }
  934. /**
  935. * Does this file system have problems with atomic renames?
  936. *
  937. * @return true if the caller should retry a failed rename of a lock file.
  938. */
  939. public abstract boolean retryFailedLockFileCommit();
  940. /**
  941. * Return all the attributes of a file, without following symbolic links.
  942. *
  943. * @param file
  944. * @return {@link BasicFileAttributes} of the file
  945. * @throws IOException in case of any I/O errors accessing the file
  946. *
  947. * @since 4.5.6
  948. */
  949. public BasicFileAttributes fileAttributes(File file) throws IOException {
  950. return FileUtils.fileAttributes(file);
  951. }
  952. /**
  953. * Determine the user's home directory (location where preferences are).
  954. *
  955. * @return the user's home directory; null if the user does not have one.
  956. */
  957. protected File userHomeImpl() {
  958. final String home = AccessController
  959. .doPrivileged(new PrivilegedAction<String>() {
  960. @Override
  961. public String run() {
  962. return System.getProperty("user.home"); //$NON-NLS-1$
  963. }
  964. });
  965. if (home == null || home.length() == 0)
  966. return null;
  967. return new File(home).getAbsoluteFile();
  968. }
  969. /**
  970. * Searches the given path to see if it contains one of the given files.
  971. * Returns the first it finds. Returns null if not found or if path is null.
  972. *
  973. * @param path
  974. * List of paths to search separated by File.pathSeparator
  975. * @param lookFor
  976. * Files to search for in the given path
  977. * @return the first match found, or null
  978. * @since 3.0
  979. */
  980. protected static File searchPath(String path, String... lookFor) {
  981. if (path == null)
  982. return null;
  983. for (String p : path.split(File.pathSeparator)) {
  984. for (String command : lookFor) {
  985. final File e = new File(p, command);
  986. if (e.isFile())
  987. return e.getAbsoluteFile();
  988. }
  989. }
  990. return null;
  991. }
  992. /**
  993. * Execute a command and return a single line of output as a String
  994. *
  995. * @param dir
  996. * Working directory for the command
  997. * @param command
  998. * as component array
  999. * @param encoding
  1000. * to be used to parse the command's output
  1001. * @return the one-line output of the command or {@code null} if there is
  1002. * none
  1003. * @throws org.eclipse.jgit.errors.CommandFailedException
  1004. * thrown when the command failed (return code was non-zero)
  1005. */
  1006. @Nullable
  1007. protected static String readPipe(File dir, String[] command,
  1008. String encoding) throws CommandFailedException {
  1009. return readPipe(dir, command, encoding, null);
  1010. }
  1011. /**
  1012. * Execute a command and return a single line of output as a String
  1013. *
  1014. * @param dir
  1015. * Working directory for the command
  1016. * @param command
  1017. * as component array
  1018. * @param encoding
  1019. * to be used to parse the command's output
  1020. * @param env
  1021. * Map of environment variables to be merged with those of the
  1022. * current process
  1023. * @return the one-line output of the command or {@code null} if there is
  1024. * none
  1025. * @throws org.eclipse.jgit.errors.CommandFailedException
  1026. * thrown when the command failed (return code was non-zero)
  1027. * @since 4.0
  1028. */
  1029. @Nullable
  1030. protected static String readPipe(File dir, String[] command,
  1031. String encoding, Map<String, String> env)
  1032. throws CommandFailedException {
  1033. final boolean debug = LOG.isDebugEnabled();
  1034. try {
  1035. if (debug) {
  1036. LOG.debug("readpipe " + Arrays.asList(command) + "," //$NON-NLS-1$ //$NON-NLS-2$
  1037. + dir);
  1038. }
  1039. ProcessBuilder pb = new ProcessBuilder(command);
  1040. pb.directory(dir);
  1041. if (env != null) {
  1042. pb.environment().putAll(env);
  1043. }
  1044. Process p;
  1045. try {
  1046. p = pb.start();
  1047. } catch (IOException e) {
  1048. // Process failed to start
  1049. throw new CommandFailedException(-1, e.getMessage(), e);
  1050. }
  1051. p.getOutputStream().close();
  1052. GobblerThread gobbler = new GobblerThread(p, command, dir);
  1053. gobbler.start();
  1054. String r = null;
  1055. try (BufferedReader lineRead = new BufferedReader(
  1056. new InputStreamReader(p.getInputStream(), encoding))) {
  1057. r = lineRead.readLine();
  1058. if (debug) {
  1059. LOG.debug("readpipe may return '" + r + "'"); //$NON-NLS-1$ //$NON-NLS-2$
  1060. LOG.debug("remaining output:\n"); //$NON-NLS-1$
  1061. String l;
  1062. while ((l = lineRead.readLine()) != null) {
  1063. LOG.debug(l);
  1064. }
  1065. }
  1066. }
  1067. for (;;) {
  1068. try {
  1069. int rc = p.waitFor();
  1070. gobbler.join();
  1071. if (rc == 0 && !gobbler.fail.get()) {
  1072. return r;
  1073. } else {
  1074. if (debug) {
  1075. LOG.debug("readpipe rc=" + rc); //$NON-NLS-1$
  1076. }
  1077. throw new CommandFailedException(rc,
  1078. gobbler.errorMessage.get(),
  1079. gobbler.exception.get());
  1080. }
  1081. } catch (InterruptedException ie) {
  1082. // Stop bothering me, I have a zombie to reap.
  1083. }
  1084. }
  1085. } catch (IOException e) {
  1086. LOG.error("Caught exception in FS.readPipe()", e); //$NON-NLS-1$
  1087. }
  1088. if (debug) {
  1089. LOG.debug("readpipe returns null"); //$NON-NLS-1$
  1090. }
  1091. return null;
  1092. }
  1093. private static class GobblerThread extends Thread {
  1094. /* The process has 5 seconds to exit after closing stderr */
  1095. private static final int PROCESS_EXIT_TIMEOUT = 5;
  1096. private final Process p;
  1097. private final String desc;
  1098. private final String dir;
  1099. final AtomicBoolean fail = new AtomicBoolean();
  1100. final AtomicReference<String> errorMessage = new AtomicReference<>();
  1101. final AtomicReference<Throwable> exception = new AtomicReference<>();
  1102. GobblerThread(Process p, String[] command, File dir) {
  1103. this.p = p;
  1104. this.desc = Arrays.toString(command);
  1105. this.dir = Objects.toString(dir);
  1106. }
  1107. @Override
  1108. public void run() {
  1109. StringBuilder err = new StringBuilder();
  1110. try (InputStream is = p.getErrorStream()) {
  1111. int ch;
  1112. while ((ch = is.read()) != -1) {
  1113. err.append((char) ch);
  1114. }
  1115. } catch (IOException e) {
  1116. if (waitForProcessCompletion(e) && p.exitValue() != 0) {
  1117. setError(e, e.getMessage(), p.exitValue());
  1118. fail.set(true);
  1119. } else {
  1120. // ignore. command terminated faster and stream was just closed
  1121. // or the process didn't terminate within timeout
  1122. }
  1123. } finally {
  1124. if (waitForProcessCompletion(null) && err.length() > 0) {
  1125. setError(null, err.toString(), p.exitValue());
  1126. if (p.exitValue() != 0) {
  1127. fail.set(true);
  1128. }
  1129. }
  1130. }
  1131. }
  1132. @SuppressWarnings("boxing")
  1133. private boolean waitForProcessCompletion(IOException originalError) {
  1134. try {
  1135. if (!p.waitFor(PROCESS_EXIT_TIMEOUT, TimeUnit.SECONDS)) {
  1136. setError(originalError, MessageFormat.format(
  1137. JGitText.get().commandClosedStderrButDidntExit,
  1138. desc, PROCESS_EXIT_TIMEOUT), -1);
  1139. fail.set(true);
  1140. return false;
  1141. }
  1142. } catch (InterruptedException e) {
  1143. setError(originalError, MessageFormat.format(
  1144. JGitText.get().threadInterruptedWhileRunning, desc), -1);
  1145. fail.set(true);
  1146. return false;
  1147. }
  1148. return true;
  1149. }
  1150. private void setError(IOException e, String message, int exitCode) {
  1151. exception.set(e);
  1152. errorMessage.set(MessageFormat.format(
  1153. JGitText.get().exceptionCaughtDuringExecutionOfCommand,
  1154. desc, dir, Integer.valueOf(exitCode), message));
  1155. }
  1156. }
  1157. /**
  1158. * Discover the path to the Git executable.
  1159. *
  1160. * @return the path to the Git executable or {@code null} if it cannot be
  1161. * determined.
  1162. * @since 4.0
  1163. */
  1164. protected abstract File discoverGitExe();
  1165. /**
  1166. * Discover the path to the system-wide Git configuration file
  1167. *
  1168. * @return the path to the system-wide Git configuration file or
  1169. * {@code null} if it cannot be determined.
  1170. * @since 4.0
  1171. */
  1172. protected File discoverGitSystemConfig() {
  1173. File gitExe = discoverGitExe();
  1174. if (gitExe == null) {
  1175. return null;
  1176. }
  1177. // Bug 480782: Check if the discovered git executable is JGit CLI
  1178. String v;
  1179. try {
  1180. v = readPipe(gitExe.getParentFile(),
  1181. new String[] { "git", "--version" }, //$NON-NLS-1$ //$NON-NLS-2$
  1182. Charset.defaultCharset().name());
  1183. } catch (CommandFailedException e) {
  1184. LOG.warn(e.getMessage());
  1185. return null;
  1186. }
  1187. if (StringUtils.isEmptyOrNull(v)
  1188. || (v != null && v.startsWith("jgit"))) { //$NON-NLS-1$
  1189. return null;
  1190. }
  1191. // Trick Git into printing the path to the config file by using "echo"
  1192. // as the editor.
  1193. Map<String, String> env = new HashMap<>();
  1194. env.put("GIT_EDITOR", "echo"); //$NON-NLS-1$ //$NON-NLS-2$
  1195. String w;
  1196. try {
  1197. w = readPipe(gitExe.getParentFile(),
  1198. new String[] { "git", "config", "--system", "--edit" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  1199. Charset.defaultCharset().name(), env);
  1200. } catch (CommandFailedException e) {
  1201. LOG.warn(e.getMessage());
  1202. return null;
  1203. }
  1204. if (StringUtils.isEmptyOrNull(w)) {
  1205. return null;
  1206. }
  1207. return new File(w);
  1208. }
  1209. /**
  1210. * Get the currently used path to the system-wide Git configuration file.
  1211. *
  1212. * @return the currently used path to the system-wide Git configuration file
  1213. * or {@code null} if none has been set.
  1214. * @since 4.0
  1215. */
  1216. public File getGitSystemConfig() {
  1217. if (gitSystemConfig == null) {
  1218. gitSystemConfig = new Holder<>(discoverGitSystemConfig());
  1219. }
  1220. return gitSystemConfig.value;
  1221. }
  1222. /**
  1223. * Set the path to the system-wide Git configuration file to use.
  1224. *
  1225. * @param configFile
  1226. * the path to the config file.
  1227. * @return {@code this}
  1228. * @since 4.0
  1229. */
  1230. public FS setGitSystemConfig(File configFile) {
  1231. gitSystemConfig = new Holder<>(configFile);
  1232. return this;
  1233. }
  1234. /**
  1235. * Get the parent directory of this file's parent directory
  1236. *
  1237. * @param grandchild
  1238. * a {@link java.io.File} object.
  1239. * @return the parent directory of this file's parent directory or
  1240. * {@code null} in case there's no grandparent directory
  1241. * @since 4.0
  1242. */
  1243. protected static File resolveGrandparentFile(File grandchild) {
  1244. if (grandchild != null) {
  1245. File parent = grandchild.getParentFile();
  1246. if (parent != null)
  1247. return parent.getParentFile();
  1248. }
  1249. return null;
  1250. }
  1251. /**
  1252. * Check if a file is a symbolic link and read it
  1253. *
  1254. * @param path
  1255. * a {@link java.io.File} object.
  1256. * @return target of link or null
  1257. * @throws java.io.IOException
  1258. * @since 3.0
  1259. */
  1260. public String readSymLink(File path) throws IOException {
  1261. return FileUtils.readSymLink(path);
  1262. }
  1263. /**
  1264. * Whether the path is a symbolic link (and we support these).
  1265. *
  1266. * @param path
  1267. * a {@link java.io.File} object.
  1268. * @return true if the path is a symbolic link (and we support these)
  1269. * @throws java.io.IOException
  1270. * @since 3.0
  1271. */
  1272. public boolean isSymLink(File path) throws IOException {
  1273. return FileUtils.isSymlink(path);
  1274. }
  1275. /**
  1276. * Tests if the path exists, in case of a symbolic link, true even if the
  1277. * target does not exist
  1278. *
  1279. * @param path
  1280. * a {@link java.io.File} object.
  1281. * @return true if path exists
  1282. * @since 3.0
  1283. */
  1284. public boolean exists(File path) {
  1285. return FileUtils.exists(path);
  1286. }
  1287. /**
  1288. * Check if path is a directory. If the OS/JRE supports symbolic links and
  1289. * path is a symbolic link to a directory, this method returns false.
  1290. *
  1291. * @param path
  1292. * a {@link java.io.File} object.
  1293. * @return true if file is a directory,
  1294. * @since 3.0
  1295. */
  1296. public boolean isDirectory(File path) {
  1297. return FileUtils.isDirectory(path);
  1298. }
  1299. /**
  1300. * Examine if path represents a regular file. If the OS/JRE supports
  1301. * symbolic links the test returns false if path represents a symbolic link.
  1302. *
  1303. * @param path
  1304. * a {@link java.io.File} object.
  1305. * @return true if path represents a regular file
  1306. * @since 3.0
  1307. */
  1308. public boolean isFile(File path) {
  1309. return FileUtils.isFile(path);
  1310. }
  1311. /**
  1312. * Whether path is hidden, either starts with . on unix or has the hidden
  1313. * attribute in windows
  1314. *
  1315. * @param path
  1316. * a {@link java.io.File} object.
  1317. * @return true if path is hidden, either starts with . on unix or has the
  1318. * hidden attribute in windows
  1319. * @throws java.io.IOException
  1320. * @since 3.0
  1321. */
  1322. public boolean isHidden(File path) throws IOException {
  1323. return FileUtils.isHidden(path);
  1324. }
  1325. /**
  1326. * Set the hidden attribute for file whose name starts with a period.
  1327. *
  1328. * @param path
  1329. * a {@link java.io.File} object.
  1330. * @param hidden
  1331. * whether to set the file hidden
  1332. * @throws java.io.IOException
  1333. * @since 3.0
  1334. */
  1335. public void setHidden(File path, boolean hidden) throws IOException {
  1336. FileUtils.setHidden(path, hidden);
  1337. }
  1338. /**
  1339. * Create a symbolic link
  1340. *
  1341. * @param path
  1342. * a {@link java.io.File} object.
  1343. * @param target
  1344. * target path of the symlink
  1345. * @throws java.io.IOException
  1346. * @since 3.0
  1347. */
  1348. public void createSymLink(File path, String target) throws IOException {
  1349. FileUtils.createSymLink(path, target);
  1350. }
  1351. /**
  1352. * Create a new file. See {@link java.io.File#createNewFile()}. Subclasses
  1353. * of this class may take care to provide a safe implementation for this
  1354. * even if {@link #supportsAtomicCreateNewFile()} is <code>false</code>
  1355. *
  1356. * @param path
  1357. * the file to be created
  1358. * @return <code>true</code> if the file was created, <code>false</code> if
  1359. * the file already existed
  1360. * @throws java.io.IOException
  1361. * @deprecated use {@link #createNewFileAtomic(File)} instead
  1362. * @since 4.5
  1363. */
  1364. @Deprecated
  1365. public boolean createNewFile(File path) throws IOException {
  1366. return path.createNewFile();
  1367. }
  1368. /**
  1369. * A token representing a file created by
  1370. * {@link #createNewFileAtomic(File)}. The token must be retained until the
  1371. * file has been deleted in order to guarantee that the unique file was
  1372. * created atomically. As soon as the file is no longer needed the lock
  1373. * token must be closed.
  1374. *
  1375. * @since 4.7
  1376. */
  1377. public static class LockToken implements Closeable {
  1378. private boolean isCreated;
  1379. private Optional<Path> link;
  1380. LockToken(boolean isCreated, Optional<Path> link) {
  1381. this.isCreated = isCreated;
  1382. this.link = link;
  1383. }
  1384. /**
  1385. * @return {@code true} if the file was created successfully
  1386. */
  1387. public boolean isCreated() {
  1388. return isCreated;
  1389. }
  1390. @Override
  1391. public void close() {
  1392. if (!link.isPresent()) {
  1393. return;
  1394. }
  1395. Path p = link.get();
  1396. if (!Files.exists(p)) {
  1397. return;
  1398. }
  1399. try {
  1400. Files.delete(p);
  1401. } catch (IOException e) {
  1402. LOG.error(MessageFormat
  1403. .format(JGitText.get().closeLockTokenFailed, this), e);
  1404. }
  1405. }
  1406. @Override
  1407. public String toString() {
  1408. return "LockToken [lockCreated=" + isCreated + //$NON-NLS-1$
  1409. ", link=" //$NON-NLS-1$
  1410. + (link.isPresent() ? link.get().getFileName() + "]" //$NON-NLS-1$
  1411. : "<null>]"); //$NON-NLS-1$
  1412. }
  1413. }
  1414. /**
  1415. * Create a new file. See {@link java.io.File#createNewFile()}. Subclasses
  1416. * of this class may take care to provide a safe implementation for this
  1417. * even if {@link #supportsAtomicCreateNewFile()} is <code>false</code>
  1418. *
  1419. * @param path
  1420. * the file to be created
  1421. * @return LockToken this token must be closed after the created file was
  1422. * deleted
  1423. * @throws IOException
  1424. * @since 4.7
  1425. */
  1426. public LockToken createNewFileAtomic(File path) throws IOException {
  1427. return new LockToken(path.createNewFile(), Optional.empty());
  1428. }
  1429. /**
  1430. * See
  1431. * {@link org.eclipse.jgit.util.FileUtils#relativizePath(String, String, String, boolean)}.
  1432. *
  1433. * @param base
  1434. * The path against which <code>other</code> should be
  1435. * relativized.
  1436. * @param other
  1437. * The path that will be made relative to <code>base</code>.
  1438. * @return A relative path that, when resolved against <code>base</code>,
  1439. * will yield the original <code>other</code>.
  1440. * @see FileUtils#relativizePath(String, String, String, boolean)
  1441. * @since 3.7
  1442. */
  1443. public String relativize(String base, String other) {
  1444. return FileUtils.relativizePath(base, other, File.separator, this.isCaseSensitive());
  1445. }
  1446. /**
  1447. * Enumerates children of a directory.
  1448. *
  1449. * @param directory
  1450. * to get the children of
  1451. * @param fileModeStrategy
  1452. * to use to calculate the git mode of a child
  1453. * @return an array of entries for the children
  1454. *
  1455. * @since 5.0
  1456. */
  1457. public Entry[] list(File directory, FileModeStrategy fileModeStrategy) {
  1458. final File[] all = directory.listFiles();
  1459. if (all == null) {
  1460. return NO_ENTRIES;
  1461. }
  1462. final Entry[] result = new Entry[all.length];
  1463. for (int i = 0; i < result.length; i++) {
  1464. result[i] = new FileEntry(all[i], this, fileModeStrategy);
  1465. }
  1466. return result;
  1467. }
  1468. /**
  1469. * Checks whether the given hook is defined for the given repository, then
  1470. * runs it with the given arguments.
  1471. * <p>
  1472. * The hook's standard output and error streams will be redirected to
  1473. * <code>System.out</code> and <code>System.err</code> respectively. The
  1474. * hook will have no stdin.
  1475. * </p>
  1476. *
  1477. * @param repository
  1478. * The repository for which a hook should be run.
  1479. * @param hookName
  1480. * The name of the hook to be executed.
  1481. * @param args
  1482. * Arguments to pass to this hook. Cannot be <code>null</code>,
  1483. * but can be an empty array.
  1484. * @return The ProcessResult describing this hook's execution.
  1485. * @throws org.eclipse.jgit.api.errors.JGitInternalException
  1486. * if we fail to run the hook somehow. Causes may include an
  1487. * interrupted process or I/O errors.
  1488. * @since 4.0
  1489. */
  1490. public ProcessResult runHookIfPresent(Repository repository,
  1491. final String hookName,
  1492. String[] args) throws JGitInternalException {
  1493. return runHookIfPresent(repository, hookName, args, System.out, System.err,
  1494. null);
  1495. }
  1496. /**
  1497. * Checks whether the given hook is defined for the given repository, then
  1498. * runs it with the given arguments.
  1499. *
  1500. * @param repository
  1501. * The repository for which a hook should be run.
  1502. * @param hookName
  1503. * The name of the hook to be executed.
  1504. * @param args
  1505. * Arguments to pass to this hook. Cannot be <code>null</code>,
  1506. * but can be an empty array.
  1507. * @param outRedirect
  1508. * A print stream on which to redirect the hook's stdout. Can be
  1509. * <code>null</code>, in which case the hook's standard output
  1510. * will be lost.
  1511. * @param errRedirect
  1512. * A print stream on which to redirect the hook's stderr. Can be
  1513. * <code>null</code>, in which case the hook's standard error
  1514. * will be lost.
  1515. * @param stdinArgs
  1516. * A string to pass on to the standard input of the hook. May be
  1517. * <code>null</code>.
  1518. * @return The ProcessResult describing this hook's execution.
  1519. * @throws org.eclipse.jgit.api.errors.JGitInternalException
  1520. * if we fail to run the hook somehow. Causes may include an
  1521. * interrupted process or I/O errors.
  1522. * @since 4.0
  1523. */
  1524. public ProcessResult runHookIfPresent(Repository repository,
  1525. final String hookName,
  1526. String[] args, PrintStream outRedirect, PrintStream errRedirect,
  1527. String stdinArgs) throws JGitInternalException {
  1528. return new ProcessResult(Status.NOT_SUPPORTED);
  1529. }
  1530. /**
  1531. * See
  1532. * {@link #runHookIfPresent(Repository, String, String[], PrintStream, PrintStream, String)}
  1533. * . Should only be called by FS supporting shell scripts execution.
  1534. *
  1535. * @param repository
  1536. * The repository for which a hook should be run.
  1537. * @param hookName
  1538. * The name of the hook to be executed.
  1539. * @param args
  1540. * Arguments to pass to this hook. Cannot be <code>null</code>,
  1541. * but can be an empty array.
  1542. * @param outRedirect
  1543. * A print stream on which to redirect the hook's stdout. Can be
  1544. * <code>null</code>, in which case the hook's standard output
  1545. * will be lost.
  1546. * @param errRedirect
  1547. * A print stream on which to redirect the hook's stderr. Can be
  1548. * <code>null</code>, in which case the hook's standard error
  1549. * will be lost.
  1550. * @param stdinArgs
  1551. * A string to pass on to the standard input of the hook. May be
  1552. * <code>null</code>.
  1553. * @return The ProcessResult describing this hook's execution.
  1554. * @throws org.eclipse.jgit.api.errors.JGitInternalException
  1555. * if we fail to run the hook somehow. Causes may include an
  1556. * interrupted process or I/O errors.
  1557. * @since 4.0
  1558. */
  1559. protected ProcessResult internalRunHookIfPresent(Repository repository,
  1560. final String hookName, String[] args, PrintStream outRedirect,
  1561. PrintStream errRedirect, String stdinArgs)
  1562. throws JGitInternalException {
  1563. final File hookFile = findHook(repository, hookName);
  1564. if (hookFile == null)
  1565. return new ProcessResult(Status.NOT_PRESENT);
  1566. final String hookPath = hookFile.getAbsolutePath();
  1567. final File runDirectory;
  1568. if (repository.isBare())
  1569. runDirectory = repository.getDirectory();
  1570. else
  1571. runDirectory = repository.getWorkTree();
  1572. final String cmd = relativize(runDirectory.getAbsolutePath(),
  1573. hookPath);
  1574. ProcessBuilder hookProcess = runInShell(cmd, args);
  1575. hookProcess.directory(runDirectory);
  1576. Map<String, String> environment = hookProcess.environment();
  1577. environment.put(Constants.GIT_DIR_KEY,
  1578. repository.getDirectory().getAbsolutePath());
  1579. if (!repository.isBare()) {
  1580. environment.put(Constants.GIT_WORK_TREE_KEY,
  1581. repository.getWorkTree().getAbsolutePath());
  1582. }
  1583. try {
  1584. return new ProcessResult(runProcess(hookProcess, outRedirect,
  1585. errRedirect, stdinArgs), Status.OK);
  1586. } catch (IOException e) {
  1587. throw new JGitInternalException(MessageFormat.format(
  1588. JGitText.get().exceptionCaughtDuringExecutionOfHook,
  1589. hookName), e);
  1590. } catch (InterruptedException e) {
  1591. throw new JGitInternalException(MessageFormat.format(
  1592. JGitText.get().exceptionHookExecutionInterrupted,
  1593. hookName), e);
  1594. }
  1595. }
  1596. /**
  1597. * Tries to find a hook matching the given one in the given repository.
  1598. *
  1599. * @param repository
  1600. * The repository within which to find a hook.
  1601. * @param hookName
  1602. * The name of the hook we're trying to find.
  1603. * @return The {@link java.io.File} containing this particular hook if it
  1604. * exists in the given repository, <code>null</code> otherwise.
  1605. * @since 4.0
  1606. */
  1607. public File findHook(Repository repository, String hookName) {
  1608. File gitDir = repository.getDirectory();
  1609. if (gitDir == null)
  1610. return null;
  1611. final File hookFile = new File(new File(gitDir,
  1612. Constants.HOOKS), hookName);
  1613. return hookFile.isFile() ? hookFile : null;
  1614. }
  1615. /**
  1616. * Runs the given process until termination, clearing its stdout and stderr
  1617. * streams on-the-fly.
  1618. *
  1619. * @param processBuilder
  1620. * The process builder configured for this process.
  1621. * @param outRedirect
  1622. * A OutputStream on which to redirect the processes stdout. Can
  1623. * be <code>null</code>, in which case the processes standard
  1624. * output will be lost.
  1625. * @param errRedirect
  1626. * A OutputStream on which to redirect the processes stderr. Can
  1627. * be <code>null</code>, in which case the processes standard
  1628. * error will be lost.
  1629. * @param stdinArgs
  1630. * A string to pass on to the standard input of the hook. Can be
  1631. * <code>null</code>.
  1632. * @return the exit value of this process.
  1633. * @throws java.io.IOException
  1634. * if an I/O error occurs while executing this process.
  1635. * @throws java.lang.InterruptedException
  1636. * if the current thread is interrupted while waiting for the
  1637. * process to end.
  1638. * @since 4.2
  1639. */
  1640. public int runProcess(ProcessBuilder processBuilder,
  1641. OutputStream outRedirect, OutputStream errRedirect, String stdinArgs)
  1642. throws IOException, InterruptedException {
  1643. InputStream in = (stdinArgs == null) ? null : new ByteArrayInputStream(
  1644. stdinArgs.getBytes(UTF_8));
  1645. return runProcess(processBuilder, outRedirect, errRedirect, in);
  1646. }
  1647. /**
  1648. * Runs the given process until termination, clearing its stdout and stderr
  1649. * streams on-the-fly.
  1650. *
  1651. * @param processBuilder
  1652. * The process builder configured for this process.
  1653. * @param outRedirect
  1654. * An OutputStream on which to redirect the processes stdout. Can
  1655. * be <code>null</code>, in which case the processes standard
  1656. * output will be lost.
  1657. * @param errRedirect
  1658. * An OutputStream on which to redirect the processes stderr. Can
  1659. * be <code>null</code>, in which case the processes standard
  1660. * error will be lost.
  1661. * @param inRedirect
  1662. * An InputStream from which to redirect the processes stdin. Can
  1663. * be <code>null</code>, in which case the process doesn't get
  1664. * any data over stdin. It is assumed that the whole InputStream
  1665. * will be consumed by the process. The method will close the
  1666. * inputstream after all bytes are read.
  1667. * @return the return code of this process.
  1668. * @throws java.io.IOException
  1669. * if an I/O error occurs while executing this process.
  1670. * @throws java.lang.InterruptedException
  1671. * if the current thread is interrupted while waiting for the
  1672. * process to end.
  1673. * @since 4.2
  1674. */
  1675. public int runProcess(ProcessBuilder processBuilder,
  1676. OutputStream outRedirect, OutputStream errRedirect,
  1677. InputStream inRedirect) throws IOException,
  1678. InterruptedException {
  1679. final ExecutorService executor = Executors.newFixedThreadPool(2);
  1680. Process process = null;
  1681. // We'll record the first I/O exception that occurs, but keep on trying
  1682. // to dispose of our open streams and file handles
  1683. IOException ioException = null;
  1684. try {
  1685. process = processBuilder.start();
  1686. executor.execute(
  1687. new StreamGobbler(process.getErrorStream(), errRedirect));
  1688. executor.execute(
  1689. new StreamGobbler(process.getInputStream(), outRedirect));
  1690. @SuppressWarnings("resource") // Closed in the finally block
  1691. OutputStream outputStream = process.getOutputStream();
  1692. try {
  1693. if (inRedirect != null) {
  1694. new StreamGobbler(inRedirect, outputStream).copy();
  1695. }
  1696. } finally {
  1697. try {
  1698. outputStream.close();
  1699. } catch (IOException e) {
  1700. // When the process exits before consuming the input, the OutputStream
  1701. // is replaced with the null output stream. This null output stream
  1702. // throws IOException for all write calls. When StreamGobbler fails to
  1703. // flush the buffer because of this, this close call tries to flush it
  1704. // again. This causes another IOException. Since we ignore the
  1705. // IOException in StreamGobbler, we also ignore the exception here.
  1706. }
  1707. }
  1708. return process.waitFor();
  1709. } catch (IOException e) {
  1710. ioException = e;
  1711. } finally {
  1712. shutdownAndAwaitTermination(executor);
  1713. if (process != null) {
  1714. try {
  1715. process.waitFor();
  1716. } catch (InterruptedException e) {
  1717. // Thrown by the outer try.
  1718. // Swallow this one to carry on our cleanup, and clear the
  1719. // interrupted flag (processes throw the exception without
  1720. // clearing the flag).
  1721. Thread.interrupted();
  1722. }
  1723. // A process doesn't clean its own resources even when destroyed
  1724. // Explicitly try and close all three streams, preserving the
  1725. // outer I/O exception if any.
  1726. if (inRedirect != null) {
  1727. inRedirect.close();
  1728. }
  1729. try {
  1730. process.getErrorStream().close();
  1731. } catch (IOException e) {
  1732. ioException = ioException != null ? ioException : e;
  1733. }
  1734. try {
  1735. process.getInputStream().close();
  1736. } catch (IOException e) {
  1737. ioException = ioException != null ? ioException : e;
  1738. }
  1739. try {
  1740. process.getOutputStream().close();
  1741. } catch (IOException e) {
  1742. ioException = ioException != null ? ioException : e;
  1743. }
  1744. process.destroy();
  1745. }
  1746. }
  1747. // We can only be here if the outer try threw an IOException.
  1748. throw ioException;
  1749. }
  1750. /**
  1751. * Shuts down an {@link ExecutorService} in two phases, first by calling
  1752. * {@link ExecutorService#shutdown() shutdown} to reject incoming tasks, and
  1753. * then calling {@link ExecutorService#shutdownNow() shutdownNow}, if
  1754. * necessary, to cancel any lingering tasks. Returns true if the pool has
  1755. * been properly shutdown, false otherwise.
  1756. * <p>
  1757. *
  1758. * @param pool
  1759. * the pool to shutdown
  1760. * @return <code>true</code> if the pool has been properly shutdown,
  1761. * <code>false</code> otherwise.
  1762. */
  1763. private static boolean shutdownAndAwaitTermination(ExecutorService pool) {
  1764. boolean hasShutdown = true;
  1765. pool.shutdown(); // Disable new tasks from being submitted
  1766. try {
  1767. // Wait a while for existing tasks to terminate
  1768. if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
  1769. pool.shutdownNow(); // Cancel currently executing tasks
  1770. // Wait a while for tasks to respond to being canceled
  1771. if (!pool.awaitTermination(60, TimeUnit.SECONDS))
  1772. hasShutdown = false;
  1773. }
  1774. } catch (InterruptedException ie) {
  1775. // (Re-)Cancel if current thread also interrupted
  1776. pool.shutdownNow();
  1777. // Preserve interrupt status
  1778. Thread.currentThread().interrupt();
  1779. hasShutdown = false;
  1780. }
  1781. return hasShutdown;
  1782. }
  1783. /**
  1784. * Initialize a ProcessBuilder to run a command using the system shell.
  1785. *
  1786. * @param cmd
  1787. * command to execute. This string should originate from the
  1788. * end-user, and thus is platform specific.
  1789. * @param args
  1790. * arguments to pass to command. These should be protected from
  1791. * shell evaluation.
  1792. * @return a partially completed process builder. Caller should finish
  1793. * populating directory, environment, and then start the process.
  1794. */
  1795. public abstract ProcessBuilder runInShell(String cmd, String[] args);
  1796. /**
  1797. * Execute a command defined by a {@link java.lang.ProcessBuilder}.
  1798. *
  1799. * @param pb
  1800. * The command to be executed
  1801. * @param in
  1802. * The standard input stream passed to the process
  1803. * @return The result of the executed command
  1804. * @throws java.lang.InterruptedException
  1805. * @throws java.io.IOException
  1806. * @since 4.2
  1807. */
  1808. public ExecutionResult execute(ProcessBuilder pb, InputStream in)
  1809. throws IOException, InterruptedException {
  1810. try (TemporaryBuffer stdout = new TemporaryBuffer.LocalFile(null);
  1811. TemporaryBuffer stderr = new TemporaryBuffer.Heap(1024,
  1812. 1024 * 1024)) {
  1813. int rc = runProcess(pb, stdout, stderr, in);
  1814. return new ExecutionResult(stdout, stderr, rc);
  1815. }
  1816. }
  1817. private static class Holder<V> {
  1818. final V value;
  1819. Holder(V value) {
  1820. this.value = value;
  1821. }
  1822. }
  1823. /**
  1824. * File attributes we typically care for.
  1825. *
  1826. * @since 3.3
  1827. */
  1828. public static class Attributes {
  1829. /**
  1830. * @return true if this are the attributes of a directory
  1831. */
  1832. public boolean isDirectory() {
  1833. return isDirectory;
  1834. }
  1835. /**
  1836. * @return true if this are the attributes of an executable file
  1837. */
  1838. public boolean isExecutable() {
  1839. return isExecutable;
  1840. }
  1841. /**
  1842. * @return true if this are the attributes of a symbolic link
  1843. */
  1844. public boolean isSymbolicLink() {
  1845. return isSymbolicLink;
  1846. }
  1847. /**
  1848. * @return true if this are the attributes of a regular file
  1849. */
  1850. public boolean isRegularFile() {
  1851. return isRegularFile;
  1852. }
  1853. /**
  1854. * @return the time when the file was created
  1855. */
  1856. public long getCreationTime() {
  1857. return creationTime;
  1858. }
  1859. /**
  1860. * @return the time (milliseconds since 1970-01-01) when this object was
  1861. * last modified
  1862. * @deprecated use getLastModifiedInstant instead
  1863. */
  1864. @Deprecated
  1865. public long getLastModifiedTime() {
  1866. return lastModifiedInstant.toEpochMilli();
  1867. }
  1868. /**
  1869. * @return the time when this object was last modified
  1870. * @since 5.1.9
  1871. */
  1872. public Instant getLastModifiedInstant() {
  1873. return lastModifiedInstant;
  1874. }
  1875. private final boolean isDirectory;
  1876. private final boolean isSymbolicLink;
  1877. private final boolean isRegularFile;
  1878. private final long creationTime;
  1879. private final Instant lastModifiedInstant;
  1880. private final boolean isExecutable;
  1881. private final File file;
  1882. private final boolean exists;
  1883. /**
  1884. * file length
  1885. */
  1886. protected long length = -1;
  1887. final FS fs;
  1888. Attributes(FS fs, File file, boolean exists, boolean isDirectory,
  1889. boolean isExecutable, boolean isSymbolicLink,
  1890. boolean isRegularFile, long creationTime,
  1891. Instant lastModifiedInstant, long length) {
  1892. this.fs = fs;
  1893. this.file = file;
  1894. this.exists = exists;
  1895. this.isDirectory = isDirectory;
  1896. this.isExecutable = isExecutable;
  1897. this.isSymbolicLink = isSymbolicLink;
  1898. this.isRegularFile = isRegularFile;
  1899. this.creationTime = creationTime;
  1900. this.lastModifiedInstant = lastModifiedInstant;
  1901. this.length = length;
  1902. }
  1903. /**
  1904. * Constructor when there are issues with reading. All attributes except
  1905. * given will be set to the default values.
  1906. *
  1907. * @param fs
  1908. * @param path
  1909. */
  1910. public Attributes(File path, FS fs) {
  1911. this(fs, path, false, false, false, false, false, 0L, EPOCH, 0L);
  1912. }
  1913. /**
  1914. * @return length of this file object
  1915. */
  1916. public long getLength() {
  1917. if (length == -1)
  1918. return length = file.length();
  1919. return length;
  1920. }
  1921. /**
  1922. * @return the filename
  1923. */
  1924. public String getName() {
  1925. return file.getName();
  1926. }
  1927. /**
  1928. * @return the file the attributes apply to
  1929. */
  1930. public File getFile() {
  1931. return file;
  1932. }
  1933. boolean exists() {
  1934. return exists;
  1935. }
  1936. }
  1937. /**
  1938. * Get the file attributes we care for.
  1939. *
  1940. * @param path
  1941. * a {@link java.io.File} object.
  1942. * @return the file attributes we care for.
  1943. * @since 3.3
  1944. */
  1945. public Attributes getAttributes(File path) {
  1946. boolean isDirectory = isDirectory(path);
  1947. boolean isFile = !isDirectory && path.isFile();
  1948. assert path.exists() == isDirectory || isFile;
  1949. boolean exists = isDirectory || isFile;
  1950. boolean canExecute = exists && !isDirectory && canExecute(path);
  1951. boolean isSymlink = false;
  1952. Instant lastModified = exists ? lastModifiedInstant(path) : EPOCH;
  1953. long createTime = 0L;
  1954. return new Attributes(this, path, exists, isDirectory, canExecute,
  1955. isSymlink, isFile, createTime, lastModified, -1);
  1956. }
  1957. /**
  1958. * Normalize the unicode path to composed form.
  1959. *
  1960. * @param file
  1961. * a {@link java.io.File} object.
  1962. * @return NFC-format File
  1963. * @since 3.3
  1964. */
  1965. public File normalize(File file) {
  1966. return file;
  1967. }
  1968. /**
  1969. * Normalize the unicode path to composed form.
  1970. *
  1971. * @param name
  1972. * path name
  1973. * @return NFC-format string
  1974. * @since 3.3
  1975. */
  1976. public String normalize(String name) {
  1977. return name;
  1978. }
  1979. /**
  1980. * This runnable will consume an input stream's content into an output
  1981. * stream as soon as it gets available.
  1982. * <p>
  1983. * Typically used to empty processes' standard output and error, preventing
  1984. * them to choke.
  1985. * </p>
  1986. * <p>
  1987. * <b>Note</b> that a {@link StreamGobbler} will never close either of its
  1988. * streams.
  1989. * </p>
  1990. */
  1991. private static class StreamGobbler implements Runnable {
  1992. private InputStream in;
  1993. private OutputStream out;
  1994. public StreamGobbler(InputStream stream, OutputStream output) {
  1995. this.in = stream;
  1996. this.out = output;
  1997. }
  1998. @Override
  1999. public void run() {
  2000. try {
  2001. copy();
  2002. } catch (IOException e) {
  2003. // Do nothing on read failure; leave streams open.
  2004. }
  2005. }
  2006. void copy() throws IOException {
  2007. boolean writeFailure = false;
  2008. byte buffer[] = new byte[4096];
  2009. int readBytes;
  2010. while ((readBytes = in.read(buffer)) != -1) {
  2011. // Do not try to write again after a failure, but keep
  2012. // reading as long as possible to prevent the input stream
  2013. // from choking.
  2014. if (!writeFailure && out != null) {
  2015. try {
  2016. out.write(buffer, 0, readBytes);
  2017. out.flush();
  2018. } catch (IOException e) {
  2019. writeFailure = true;
  2020. }
  2021. }
  2022. }
  2023. }
  2024. }
  2025. }