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