1
2
3
4
5
6
7
8
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.PrintStream;
26 import java.io.Writer;
27 import java.nio.charset.Charset;
28 import java.nio.file.AccessDeniedException;
29 import java.nio.file.FileStore;
30 import java.nio.file.Files;
31 import java.nio.file.InvalidPathException;
32 import java.nio.file.Path;
33 import java.nio.file.attribute.BasicFileAttributes;
34 import java.nio.file.attribute.FileTime;
35 import java.security.AccessControlException;
36 import java.security.AccessController;
37 import java.security.PrivilegedAction;
38 import java.text.MessageFormat;
39 import java.time.Duration;
40 import java.time.Instant;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.HashMap;
44 import java.util.Map;
45 import java.util.Objects;
46 import java.util.Optional;
47 import java.util.UUID;
48 import java.util.concurrent.CancellationException;
49 import java.util.concurrent.CompletableFuture;
50 import java.util.concurrent.ConcurrentHashMap;
51 import java.util.concurrent.ExecutionException;
52 import java.util.concurrent.Executor;
53 import java.util.concurrent.ExecutorService;
54 import java.util.concurrent.Executors;
55 import java.util.concurrent.SynchronousQueue;
56 import java.util.concurrent.ThreadPoolExecutor;
57 import java.util.concurrent.TimeUnit;
58 import java.util.concurrent.TimeoutException;
59 import java.util.concurrent.atomic.AtomicBoolean;
60 import java.util.concurrent.atomic.AtomicInteger;
61 import java.util.concurrent.atomic.AtomicReference;
62 import java.util.concurrent.locks.Lock;
63 import java.util.concurrent.locks.ReentrantLock;
64
65 import org.eclipse.jgit.annotations.NonNull;
66 import org.eclipse.jgit.annotations.Nullable;
67 import org.eclipse.jgit.api.errors.JGitInternalException;
68 import org.eclipse.jgit.errors.CommandFailedException;
69 import org.eclipse.jgit.errors.ConfigInvalidException;
70 import org.eclipse.jgit.errors.LockFailedException;
71 import org.eclipse.jgit.internal.JGitText;
72 import org.eclipse.jgit.internal.storage.file.FileSnapshot;
73 import org.eclipse.jgit.lib.Config;
74 import org.eclipse.jgit.lib.ConfigConstants;
75 import org.eclipse.jgit.lib.Constants;
76 import org.eclipse.jgit.lib.Repository;
77 import org.eclipse.jgit.lib.StoredConfig;
78 import org.eclipse.jgit.treewalk.FileTreeIterator.FileEntry;
79 import org.eclipse.jgit.treewalk.FileTreeIterator.FileModeStrategy;
80 import org.eclipse.jgit.treewalk.WorkingTreeIterator.Entry;
81 import org.eclipse.jgit.util.ProcessResult.Status;
82 import org.slf4j.Logger;
83 import org.slf4j.LoggerFactory;
84
85
86
87
88 public abstract class FS {
89 private static final Logger LOG = LoggerFactory.getLogger(FS.class);
90
91
92
93
94
95
96
97 protected static final Entry[] NO_ENTRIES = {};
98
99 private volatile Boolean supportSymlinks;
100
101
102
103
104
105
106
107 public static class FSFactory {
108
109
110
111 protected FSFactory() {
112
113 }
114
115
116
117
118
119
120
121 public FS detect(Boolean cygwinUsed) {
122 if (SystemReader.getInstance().isWindows()) {
123 if (cygwinUsed == null) {
124 cygwinUsed = Boolean.valueOf(FS_Win32_Cygwin.isCygwin());
125 }
126 if (cygwinUsed.booleanValue()) {
127 return new FS_Win32_Cygwin();
128 }
129 return new FS_Win32();
130 }
131 return new FS_POSIX();
132 }
133 }
134
135
136
137
138
139
140
141 public static class ExecutionResult {
142 private TemporaryBuffer stdout;
143
144 private TemporaryBuffer stderr;
145
146 private int rc;
147
148
149
150
151
152
153 public ExecutionResult(TemporaryBuffer./../org/eclipse/jgit/util/TemporaryBuffer.html#TemporaryBuffer">TemporaryBuffer stdout, TemporaryBuffer stderr,
154 int rc) {
155 this.stdout = stdout;
156 this.stderr = stderr;
157 this.rc = rc;
158 }
159
160
161
162
163 public TemporaryBuffer getStdout() {
164 return stdout;
165 }
166
167
168
169
170 public TemporaryBuffer getStderr() {
171 return stderr;
172 }
173
174
175
176
177 public int getRc() {
178 return rc;
179 }
180 }
181
182
183
184
185
186
187 public static final class FileStoreAttributes {
188
189 private static final Duration UNDEFINED_DURATION = Duration
190 .ofNanos(Long.MAX_VALUE);
191
192
193
194
195
196 public static final Duration FALLBACK_TIMESTAMP_RESOLUTION = Duration
197 .ofMillis(2000);
198
199
200
201
202
203
204 public static final FileStoreAttributes FALLBACK_FILESTORE_ATTRIBUTES = new FileStoreAttributes(
205 FALLBACK_TIMESTAMP_RESOLUTION);
206
207 private static final String JAVA_VERSION_PREFIX = System
208 .getProperty("java.vendor") + '|'
209 + System.getProperty("java.version") + '|';
210
211 private static final Duration FALLBACK_MIN_RACY_INTERVAL = Duration
212 .ofMillis(10);
213
214 private static final Map<FileStore, FileStoreAttributes> attributeCache = new ConcurrentHashMap<>();
215
216 private static final SimpleLruCache<Path, FileStoreAttributes> attrCacheByPath = new SimpleLruCache<>(
217 100, 0.2f);
218
219 private static final AtomicBoolean background = new AtomicBoolean();
220
221 private static final Map<FileStore, Lock> locks = new ConcurrentHashMap<>();
222
223 private static final AtomicInteger threadNumber = new AtomicInteger(1);
224
225
226
227
228
229
230
231
232
233
234
235
236
237 private static final Executor FUTURE_RUNNER = new ThreadPoolExecutor(0,
238 5, 30L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(),
239 runnable -> {
240 Thread t = new Thread(runnable, "FileStoreAttributeReader-"
241 + threadNumber.getAndIncrement());
242
243
244 t.setDaemon(true);
245 return t;
246 });
247
248
249
250
251
252
253
254
255
256
257 public static void setBackground(boolean async) {
258 background.set(async);
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 public static void configureAttributesPathCache(int maxSize,
277 float purgeFactor) {
278 FileStoreAttributes.attrCacheByPath.configure(maxSize, purgeFactor);
279 }
280
281
282
283
284
285
286
287
288 public static FileStoreAttributes get(Path path) {
289 try {
290 path = path.toAbsolutePath();
291 Path dir = Files.isDirectory(path) ? path : path.getParent();
292 FileStoreAttributes cached = attrCacheByPath.get(dir);
293 if (cached != null) {
294 return cached;
295 }
296 FileStoreAttributes attrs = getFileStoreAttributes(dir);
297 attrCacheByPath.put(dir, attrs);
298 return attrs;
299 } catch (SecurityException e) {
300 return FALLBACK_FILESTORE_ATTRIBUTES;
301 }
302 }
303
304 private static FileStoreAttributes getFileStoreAttributes(Path dir) {
305 FileStore s;
306 try {
307 if (Files.exists(dir)) {
308 s = Files.getFileStore(dir);
309 FileStoreAttributes c = attributeCache.get(s);
310 if (c != null) {
311 return c;
312 }
313 if (!Files.isWritable(dir)) {
314
315 LOG.debug(
316 "{}: cannot measure timestamp resolution in read-only directory {}",
317 Thread.currentThread(), dir);
318 return FALLBACK_FILESTORE_ATTRIBUTES;
319 }
320 } else {
321
322 LOG.debug(
323 "{}: cannot measure timestamp resolution of unborn directory {}",
324 Thread.currentThread(), dir);
325 return FALLBACK_FILESTORE_ATTRIBUTES;
326 }
327
328 CompletableFuture<Optional<FileStoreAttributes>> f = CompletableFuture
329 .supplyAsync(() -> {
330 Lock lock = locks.computeIfAbsent(s,
331 l -> new ReentrantLock());
332 if (!lock.tryLock()) {
333 LOG.debug(
334 "{}: couldn't get lock to measure timestamp resolution in {}",
335 Thread.currentThread(), dir);
336 return Optional.empty();
337 }
338 Optional<FileStoreAttributes> attributes = Optional
339 .empty();
340 try {
341
342
343
344 FileStoreAttributes c = attributeCache.get(s);
345 if (c != null) {
346 return Optional.of(c);
347 }
348 attributes = readFromConfig(s);
349 if (attributes.isPresent()) {
350 attributeCache.put(s, attributes.get());
351 return attributes;
352 }
353
354 Optional<Duration> resolution = measureFsTimestampResolution(
355 s, dir);
356 if (resolution.isPresent()) {
357 c = new FileStoreAttributes(
358 resolution.get());
359 attributeCache.put(s, c);
360
361
362 if (c.fsTimestampResolution
363 .toNanos() < 100_000_000L) {
364 c.minimalRacyInterval = measureMinimalRacyInterval(
365 dir);
366 }
367 if (LOG.isDebugEnabled()) {
368 LOG.debug(c.toString());
369 }
370 saveToConfig(s, c);
371 }
372 attributes = Optional.of(c);
373 } finally {
374 lock.unlock();
375 locks.remove(s);
376 }
377 return attributes;
378 }, FUTURE_RUNNER);
379 f = f.exceptionally(e -> {
380 LOG.error(e.getLocalizedMessage(), e);
381 return Optional.empty();
382 });
383
384
385 Optional<FileStoreAttributes> d = background.get() ? f.get(
386 100, TimeUnit.MILLISECONDS) : f.get();
387 if (d.isPresent()) {
388 return d.get();
389 }
390
391 } catch (IOException | InterruptedException
392 | ExecutionException | CancellationException e) {
393 LOG.error(e.getMessage(), e);
394 } catch (TimeoutException | SecurityException e) {
395
396 }
397 LOG.debug("{}: use fallback timestamp resolution for directory {}",
398 Thread.currentThread(), dir);
399 return FALLBACK_FILESTORE_ATTRIBUTES;
400 }
401
402 @SuppressWarnings("boxing")
403 private static Duration measureMinimalRacyInterval(Path dir) {
404 LOG.debug("{}: start measure minimal racy interval in {}",
405 Thread.currentThread(), dir);
406 int n = 0;
407 int failures = 0;
408 long racyNanos = 0;
409 ArrayList<Long> deltas = new ArrayList<>();
410 Path probe = dir.resolve(".probe-" + UUID.randomUUID());
411 Instant end = Instant.now().plusSeconds(3);
412 try {
413 Files.createFile(probe);
414 do {
415 n++;
416 write(probe, "a");
417 FileSnapshot snapshot = FileSnapshot.save(probe.toFile());
418 read(probe);
419 write(probe, "b");
420 if (!snapshot.isModified(probe.toFile())) {
421 deltas.add(Long.valueOf(snapshot.lastDelta()));
422 racyNanos = snapshot.lastRacyThreshold();
423 failures++;
424 }
425 } while (Instant.now().compareTo(end) < 0);
426 } catch (IOException e) {
427 LOG.error(e.getMessage(), e);
428 return FALLBACK_MIN_RACY_INTERVAL;
429 } finally {
430 deleteProbe(probe);
431 }
432 if (failures > 0) {
433 Stats stats = new Stats();
434 for (Long d : deltas) {
435 stats.add(d);
436 }
437 LOG.debug(
438 "delta [ns] since modification FileSnapshot failed to detect\n"
439 + "count, failures, racy limit [ns], delta min [ns],"
440 + " delta max [ns], delta avg [ns],"
441 + " delta stddev [ns]\n"
442 + "{}, {}, {}, {}, {}, {}, {}",
443 n, failures, racyNanos, stats.min(), stats.max(),
444 stats.avg(), stats.stddev());
445 return Duration
446 .ofNanos(Double.valueOf(stats.max()).longValue());
447 }
448
449
450 LOG.debug("{}: no failures when measuring minimal racy interval",
451 Thread.currentThread());
452 return Duration.ZERO;
453 }
454
455 private static void write(Path p, String body) throws IOException {
456 FileUtils.mkdirs(p.getParent().toFile(), true);
457 try (Writer w = new OutputStreamWriter(Files.newOutputStream(p),
458 UTF_8)) {
459 w.write(body);
460 }
461 }
462
463 private static String read(Path p) throws IOException {
464 final byte[] body = IO.readFully(p.toFile());
465 return new String(body, 0, body.length, UTF_8);
466 }
467
468 private static Optional<Duration> measureFsTimestampResolution(
469 FileStore s, Path dir) {
470 LOG.debug("{}: start measure timestamp resolution {} in {}",
471 Thread.currentThread(), s, dir);
472 Path probe = dir.resolve(".probe-" + UUID.randomUUID());
473 try {
474 Files.createFile(probe);
475 FileTime t1 = Files.getLastModifiedTime(probe);
476 FileTime t2 = t1;
477 Instant t1i = t1.toInstant();
478 for (long i = 1; t2.compareTo(t1) <= 0; i += 1 + i / 20) {
479 Files.setLastModifiedTime(probe,
480 FileTime.from(t1i.plusNanos(i * 1000)));
481 t2 = Files.getLastModifiedTime(probe);
482 }
483 Duration fsResolution = Duration.between(t1.toInstant(), t2.toInstant());
484 Duration clockResolution = measureClockResolution();
485 fsResolution = fsResolution.plus(clockResolution);
486 LOG.debug("{}: end measure timestamp resolution {} in {}",
487 Thread.currentThread(), s, dir);
488 return Optional.of(fsResolution);
489 } catch (SecurityException e) {
490
491
492
493 LOG.warn(e.getLocalizedMessage(), e);
494 } catch (AccessDeniedException e) {
495 LOG.warn(e.getLocalizedMessage(), e);
496 } catch (IOException e) {
497 LOG.error(e.getLocalizedMessage(), e);
498 } finally {
499 deleteProbe(probe);
500 }
501 return Optional.empty();
502 }
503
504 private static Duration measureClockResolution() {
505 Duration clockResolution = Duration.ZERO;
506 for (int i = 0; i < 10; i++) {
507 Instant t1 = Instant.now();
508 Instant t2 = t1;
509 while (t2.compareTo(t1) <= 0) {
510 t2 = Instant.now();
511 }
512 Duration r = Duration.between(t1, t2);
513 if (r.compareTo(clockResolution) > 0) {
514 clockResolution = r;
515 }
516 }
517 return clockResolution;
518 }
519
520 private static void deleteProbe(Path probe) {
521 try {
522 FileUtils.delete(probe.toFile(),
523 FileUtils.SKIP_MISSING | FileUtils.RETRY);
524 } catch (IOException e) {
525 LOG.error(e.getMessage(), e);
526 }
527 }
528
529 private static Optional<FileStoreAttributes> readFromConfig(
530 FileStore s) {
531 StoredConfig userConfig;
532 try {
533 userConfig = SystemReader.getInstance().getUserConfig();
534 } catch (IOException | ConfigInvalidException e) {
535 LOG.error(JGitText.get().readFileStoreAttributesFailed, e);
536 return Optional.empty();
537 }
538 String key = getConfigKey(s);
539 Duration resolution = Duration.ofNanos(userConfig.getTimeUnit(
540 ConfigConstants.CONFIG_FILESYSTEM_SECTION, key,
541 ConfigConstants.CONFIG_KEY_TIMESTAMP_RESOLUTION,
542 UNDEFINED_DURATION.toNanos(), TimeUnit.NANOSECONDS));
543 if (UNDEFINED_DURATION.equals(resolution)) {
544 return Optional.empty();
545 }
546 Duration minRacyThreshold = Duration.ofNanos(userConfig.getTimeUnit(
547 ConfigConstants.CONFIG_FILESYSTEM_SECTION, key,
548 ConfigConstants.CONFIG_KEY_MIN_RACY_THRESHOLD,
549 UNDEFINED_DURATION.toNanos(), TimeUnit.NANOSECONDS));
550 FileStoreAttributes c = new FileStoreAttributes(resolution);
551 if (!UNDEFINED_DURATION.equals(minRacyThreshold)) {
552 c.minimalRacyInterval = minRacyThreshold;
553 }
554 return Optional.of(c);
555 }
556
557 private static void saveToConfig(FileStore s,
558 FileStoreAttributes c) {
559 StoredConfig jgitConfig;
560 try {
561 jgitConfig = SystemReader.getInstance().getJGitConfig();
562 } catch (IOException | ConfigInvalidException e) {
563 LOG.error(JGitText.get().saveFileStoreAttributesFailed, e);
564 return;
565 }
566 long resolution = c.getFsTimestampResolution().toNanos();
567 TimeUnit resolutionUnit = getUnit(resolution);
568 long resolutionValue = resolutionUnit.convert(resolution,
569 TimeUnit.NANOSECONDS);
570
571 long minRacyThreshold = c.getMinimalRacyInterval().toNanos();
572 TimeUnit minRacyThresholdUnit = getUnit(minRacyThreshold);
573 long minRacyThresholdValue = minRacyThresholdUnit
574 .convert(minRacyThreshold, TimeUnit.NANOSECONDS);
575
576 final int max_retries = 5;
577 int retries = 0;
578 boolean succeeded = false;
579 String key = getConfigKey(s);
580 while (!succeeded && retries < max_retries) {
581 try {
582 jgitConfig.setString(
583 ConfigConstants.CONFIG_FILESYSTEM_SECTION, key,
584 ConfigConstants.CONFIG_KEY_TIMESTAMP_RESOLUTION,
585 String.format("%d %s",
586 Long.valueOf(resolutionValue),
587 resolutionUnit.name().toLowerCase()));
588 jgitConfig.setString(
589 ConfigConstants.CONFIG_FILESYSTEM_SECTION, key,
590 ConfigConstants.CONFIG_KEY_MIN_RACY_THRESHOLD,
591 String.format("%d %s",
592 Long.valueOf(minRacyThresholdValue),
593 minRacyThresholdUnit.name().toLowerCase()));
594 jgitConfig.save();
595 succeeded = true;
596 } catch (LockFailedException e) {
597
598 try {
599 retries++;
600 if (retries < max_retries) {
601 Thread.sleep(100);
602 LOG.debug("locking {} failed, retries {}/{}",
603 jgitConfig, Integer.valueOf(retries),
604 Integer.valueOf(max_retries));
605 } else {
606 LOG.warn(MessageFormat.format(
607 JGitText.get().lockFailedRetry, jgitConfig,
608 Integer.valueOf(retries)));
609 }
610 } catch (InterruptedException e1) {
611 Thread.currentThread().interrupt();
612 break;
613 }
614 } catch (IOException e) {
615 LOG.error(MessageFormat.format(
616 JGitText.get().cannotSaveConfig, jgitConfig), e);
617 break;
618 }
619 }
620 }
621
622 private static String getConfigKey(FileStore s) {
623 final String storeKey;
624 if (SystemReader.getInstance().isWindows()) {
625 Object attribute = null;
626 try {
627 attribute = s.getAttribute("volume:vsn");
628 } catch (IOException ignored) {
629
630 }
631 if (attribute instanceof Integer) {
632 storeKey = attribute.toString();
633 } else {
634 storeKey = s.name();
635 }
636 } else {
637 storeKey = s.name();
638 }
639 return JAVA_VERSION_PREFIX + storeKey;
640 }
641
642 private static TimeUnit getUnit(long nanos) {
643 TimeUnit unit;
644 if (nanos < 200_000L) {
645 unit = TimeUnit.NANOSECONDS;
646 } else if (nanos < 200_000_000L) {
647 unit = TimeUnit.MICROSECONDS;
648 } else {
649 unit = TimeUnit.MILLISECONDS;
650 }
651 return unit;
652 }
653
654 private final @NonNull Duration fsTimestampResolution;
655
656 private Duration minimalRacyInterval;
657
658
659
660
661
662
663 public Duration getMinimalRacyInterval() {
664 return minimalRacyInterval;
665 }
666
667
668
669
670 @NonNull
671 public Duration getFsTimestampResolution() {
672 return fsTimestampResolution;
673 }
674
675
676
677
678
679
680
681 public FileStoreAttributes(
682 @NonNull Duration fsTimestampResolution) {
683 this.fsTimestampResolution = fsTimestampResolution;
684 this.minimalRacyInterval = Duration.ZERO;
685 }
686
687 @SuppressWarnings({ "nls", "boxing" })
688 @Override
689 public String toString() {
690 return String.format(
691 "FileStoreAttributes[fsTimestampResolution=%,d µs, "
692 + "minimalRacyInterval=%,d µs]",
693 fsTimestampResolution.toNanos() / 1000,
694 minimalRacyInterval.toNanos() / 1000);
695 }
696
697 }
698
699
700 public static final FS DETECTED = detect();
701
702 private static volatile FSFactory factory;
703
704
705
706
707
708
709 public static FS detect() {
710 return detect(null);
711 }
712
713
714
715
716
717
718
719
720
721
722
723 @Deprecated
724 public static void setAsyncFileStoreAttributes(boolean asynch) {
725 FileStoreAttributes.setBackground(asynch);
726 }
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748 public static FS detect(Boolean cygwinUsed) {
749 if (factory == null) {
750 factory = new FS.FSFactory();
751 }
752 return factory.detect(cygwinUsed);
753 }
754
755
756
757
758
759
760
761
762
763
764
765 public static FileStoreAttributes getFileStoreAttributes(
766 @NonNull Path dir) {
767 return FileStoreAttributes.get(dir);
768 }
769
770 private volatile Holder<File> userHome;
771
772 private volatile Holder<File> gitSystemConfig;
773
774
775
776
777 protected FS() {
778
779 }
780
781
782
783
784
785
786
787 protected FSme="FS" href="../../../../org/eclipse/jgit/util/FS.html#FS">FS(FS src) {
788 userHome = src.userHome;
789 gitSystemConfig = src.gitSystemConfig;
790 }
791
792
793
794
795
796
797 public abstract FS newInstance();
798
799
800
801
802
803
804
805 public abstract boolean supportsExecute();
806
807
808
809
810
811
812
813
814
815
816
817
818 public boolean supportsAtomicCreateNewFile() {
819 return true;
820 }
821
822
823
824
825
826
827
828
829 public boolean supportsSymlinks() {
830 if (supportSymlinks == null) {
831 detectSymlinkSupport();
832 }
833 return Boolean.TRUE.equals(supportSymlinks);
834 }
835
836 private void detectSymlinkSupport() {
837 File tempFile = null;
838 try {
839 tempFile = File.createTempFile("tempsymlinktarget", "");
840 File linkName = new File(tempFile.getParentFile(), "tempsymlink");
841 createSymLink(linkName, tempFile.getPath());
842 supportSymlinks = Boolean.TRUE;
843 linkName.delete();
844 } catch (IOException | UnsupportedOperationException | SecurityException
845 | InternalError e) {
846 supportSymlinks = Boolean.FALSE;
847 } finally {
848 if (tempFile != null) {
849 try {
850 FileUtils.delete(tempFile);
851 } catch (IOException e) {
852 LOG.error(JGitText.get().cannotDeleteFile, tempFile);
853 }
854 }
855 }
856 }
857
858
859
860
861
862
863 public abstract boolean isCaseSensitive();
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879 public abstract boolean canExecute(File f);
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894 public abstract boolean setExecute(File f, boolean canExec);
895
896
897
898
899
900
901
902
903
904
905
906
907
908 @Deprecated
909 public long lastModified(File f) throws IOException {
910 return FileUtils.lastModified(f);
911 }
912
913
914
915
916
917
918
919
920
921
922
923 public Instant lastModifiedInstant(Path p) {
924 return FileUtils.lastModifiedInstant(p);
925 }
926
927
928
929
930
931
932
933
934
935
936
937 public Instant lastModifiedInstant(File f) {
938 return FileUtils.lastModifiedInstant(f.toPath());
939 }
940
941
942
943
944
945
946
947
948
949
950
951
952
953 @Deprecated
954 public void setLastModified(File f, long time) throws IOException {
955 FileUtils.setLastModified(f, time);
956 }
957
958
959
960
961
962
963
964
965
966
967
968
969 public void setLastModified(Path p, Instant time) throws IOException {
970 FileUtils.setLastModified(p, time);
971 }
972
973
974
975
976
977
978
979
980
981
982
983 public long length(File path) throws IOException {
984 return FileUtils.getLength(path);
985 }
986
987
988
989
990
991
992
993
994
995
996 public void delete(File f) throws IOException {
997 FileUtils.delete(f);
998 }
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018 public File resolve(File dir, String name) {
1019 final File abspn = new File(name);
1020 if (abspn.isAbsolute())
1021 return abspn;
1022 return new File(dir, name);
1023 }
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036 public File userHome() {
1037 Holder<File> p = userHome;
1038 if (p == null) {
1039 p = new Holder<>(userHomeImpl());
1040 userHome = p;
1041 }
1042 return p.value;
1043 }
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053 public FS setUserHome(File path) {
1054 userHome = new Holder<>(path);
1055 return this;
1056 }
1057
1058
1059
1060
1061
1062
1063 public abstract boolean retryFailedLockFileCommit();
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074 public BasicFileAttributes fileAttributes(File file) throws IOException {
1075 return FileUtils.fileAttributes(file);
1076 }
1077
1078
1079
1080
1081
1082
1083 protected File userHomeImpl() {
1084 final String home = AccessController.doPrivileged(
1085 (PrivilegedAction<String>) () -> System.getProperty("user.home")
1086 );
1087 if (home == null || home.length() == 0)
1088 return null;
1089 return new File(home).getAbsoluteFile();
1090 }
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103 protected static File searchPath(String path, String... lookFor) {
1104 if (path == null)
1105 return null;
1106
1107 for (String p : path.split(File.pathSeparator)) {
1108 for (String command : lookFor) {
1109 final File file = new File(p, command);
1110 try {
1111 if (file.isFile()) {
1112 return file.getAbsoluteFile();
1113 }
1114 } catch (SecurityException e) {
1115 LOG.warn(MessageFormat.format(
1116 JGitText.get().skipNotAccessiblePath,
1117 file.getPath()));
1118 }
1119 }
1120 }
1121 return null;
1122 }
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138 @Nullable
1139 protected static String readPipe(File dir, String[] command,
1140 String encoding) throws CommandFailedException {
1141 return readPipe(dir, command, encoding, null);
1142 }
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162 @Nullable
1163 protected static String readPipe(File dir, String[] command,
1164 String encoding, Map<String, String> env)
1165 throws CommandFailedException {
1166 final boolean debug = LOG.isDebugEnabled();
1167 try {
1168 if (debug) {
1169 LOG.debug("readpipe " + Arrays.asList(command) + ","
1170 + dir);
1171 }
1172 ProcessBuilder pb = new ProcessBuilder(command);
1173 pb.directory(dir);
1174 if (env != null) {
1175 pb.environment().putAll(env);
1176 }
1177 Process p;
1178 try {
1179 p = pb.start();
1180 } catch (IOException e) {
1181
1182 throw new CommandFailedException(-1, e.getMessage(), e);
1183 }
1184 p.getOutputStream().close();
1185 GobblerThread gobbler = new GobblerThread(p, command, dir);
1186 gobbler.start();
1187 String r = null;
1188 try (BufferedReader lineRead = new BufferedReader(
1189 new InputStreamReader(p.getInputStream(), encoding))) {
1190 r = lineRead.readLine();
1191 if (debug) {
1192 LOG.debug("readpipe may return '" + r + "'");
1193 LOG.debug("remaining output:\n");
1194 String l;
1195 while ((l = lineRead.readLine()) != null) {
1196 LOG.debug(l);
1197 }
1198 }
1199 }
1200
1201 for (;;) {
1202 try {
1203 int rc = p.waitFor();
1204 gobbler.join();
1205 if (rc == 0 && !gobbler.fail.get()) {
1206 return r;
1207 }
1208 if (debug) {
1209 LOG.debug("readpipe rc=" + rc);
1210 }
1211 throw new CommandFailedException(rc,
1212 gobbler.errorMessage.get(),
1213 gobbler.exception.get());
1214 } catch (InterruptedException ie) {
1215
1216 }
1217 }
1218 } catch (IOException e) {
1219 LOG.error("Caught exception in FS.readPipe()", e);
1220 } catch (AccessControlException e) {
1221 LOG.warn(MessageFormat.format(
1222 JGitText.get().readPipeIsNotAllowedRequiredPermission,
1223 command, dir, e.getPermission()));
1224 } catch (SecurityException e) {
1225 LOG.warn(MessageFormat.format(JGitText.get().readPipeIsNotAllowed,
1226 command, dir));
1227 }
1228 if (debug) {
1229 LOG.debug("readpipe returns null");
1230 }
1231 return null;
1232 }
1233
1234 private static class GobblerThread extends Thread {
1235
1236
1237 private static final int PROCESS_EXIT_TIMEOUT = 5;
1238
1239 private final Process p;
1240 private final String desc;
1241 private final String dir;
1242 final AtomicBoolean fail = new AtomicBoolean();
1243 final AtomicReference<String> errorMessage = new AtomicReference<>();
1244 final AtomicReference<Throwable> exception = new AtomicReference<>();
1245
1246 GobblerThread(Process p, String[] command, File dir) {
1247 this.p = p;
1248 this.desc = Arrays.toString(command);
1249 this.dir = Objects.toString(dir);
1250 }
1251
1252 @Override
1253 public void run() {
1254 StringBuilder err = new StringBuilder();
1255 try (InputStream is = p.getErrorStream()) {
1256 int ch;
1257 while ((ch = is.read()) != -1) {
1258 err.append((char) ch);
1259 }
1260 } catch (IOException e) {
1261 if (waitForProcessCompletion(e) && p.exitValue() != 0) {
1262 setError(e, e.getMessage(), p.exitValue());
1263 fail.set(true);
1264 } else {
1265
1266
1267 }
1268 } finally {
1269 if (waitForProcessCompletion(null) && err.length() > 0) {
1270 setError(null, err.toString(), p.exitValue());
1271 if (p.exitValue() != 0) {
1272 fail.set(true);
1273 }
1274 }
1275 }
1276 }
1277
1278 @SuppressWarnings("boxing")
1279 private boolean waitForProcessCompletion(IOException originalError) {
1280 try {
1281 if (!p.waitFor(PROCESS_EXIT_TIMEOUT, TimeUnit.SECONDS)) {
1282 setError(originalError, MessageFormat.format(
1283 JGitText.get().commandClosedStderrButDidntExit,
1284 desc, PROCESS_EXIT_TIMEOUT), -1);
1285 fail.set(true);
1286 return false;
1287 }
1288 } catch (InterruptedException e) {
1289 setError(originalError, MessageFormat.format(
1290 JGitText.get().threadInterruptedWhileRunning, desc), -1);
1291 fail.set(true);
1292 return false;
1293 }
1294 return true;
1295 }
1296
1297 private void setError(IOException e, String message, int exitCode) {
1298 exception.set(e);
1299 errorMessage.set(MessageFormat.format(
1300 JGitText.get().exceptionCaughtDuringExecutionOfCommand,
1301 desc, dir, Integer.valueOf(exitCode), message));
1302 }
1303 }
1304
1305
1306
1307
1308
1309
1310
1311
1312 protected abstract File discoverGitExe();
1313
1314
1315
1316
1317
1318
1319
1320
1321 protected File discoverGitSystemConfig() {
1322 File gitExe = discoverGitExe();
1323 if (gitExe == null) {
1324 return null;
1325 }
1326
1327
1328 String v;
1329 try {
1330 v = readPipe(gitExe.getParentFile(),
1331 new String[] { "git", "--version" },
1332 Charset.defaultCharset().name());
1333 } catch (CommandFailedException e) {
1334 LOG.warn(e.getMessage());
1335 return null;
1336 }
1337 if (StringUtils.isEmptyOrNull(v)
1338 || (v != null && v.startsWith("jgit"))) {
1339 return null;
1340 }
1341
1342
1343
1344 Map<String, String> env = new HashMap<>();
1345 env.put("GIT_EDITOR", "echo");
1346
1347 String w;
1348 try {
1349 w = readPipe(gitExe.getParentFile(),
1350 new String[] { "git", "config", "--system", "--edit" },
1351 Charset.defaultCharset().name(), env);
1352 } catch (CommandFailedException e) {
1353 LOG.warn(e.getMessage());
1354 return null;
1355 }
1356 if (StringUtils.isEmptyOrNull(w)) {
1357 return null;
1358 }
1359
1360 return new File(w);
1361 }
1362
1363
1364
1365
1366
1367
1368
1369
1370 public File getGitSystemConfig() {
1371 if (gitSystemConfig == null) {
1372 gitSystemConfig = new Holder<>(discoverGitSystemConfig());
1373 }
1374 return gitSystemConfig.value;
1375 }
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385 public FS setGitSystemConfig(File configFile) {
1386 gitSystemConfig = new Holder<>(configFile);
1387 return this;
1388 }
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399 protected static File resolveGrandparentFile(File grandchild) {
1400 if (grandchild != null) {
1401 File parent = grandchild.getParentFile();
1402 if (parent != null)
1403 return parent.getParentFile();
1404 }
1405 return null;
1406 }
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417 public String readSymLink(File path) throws IOException {
1418 return FileUtils.readSymLink(path);
1419 }
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430 public boolean isSymLink(File path) throws IOException {
1431 return FileUtils.isSymlink(path);
1432 }
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443 public boolean exists(File path) {
1444 return FileUtils.exists(path);
1445 }
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456 public boolean isDirectory(File path) {
1457 return FileUtils.isDirectory(path);
1458 }
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469 public boolean isFile(File path) {
1470 return FileUtils.isFile(path);
1471 }
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484 public boolean isHidden(File path) throws IOException {
1485 return FileUtils.isHidden(path);
1486 }
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498 public void setHidden(File path, boolean hidden) throws IOException {
1499 FileUtils.setHidden(path, hidden);
1500 }
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512 public void createSymLink(File path, String target) throws IOException {
1513 FileUtils.createSymLink(path, target);
1514 }
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529 @Deprecated
1530 public boolean createNewFile(File path) throws IOException {
1531 return path.createNewFile();
1532 }
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543 public static class LockToken implements Closeable {
1544 private boolean isCreated;
1545
1546 private Optional<Path> link;
1547
1548 LockToken(boolean isCreated, Optional<Path> link) {
1549 this.isCreated = isCreated;
1550 this.link = link;
1551 }
1552
1553
1554
1555
1556 public boolean isCreated() {
1557 return isCreated;
1558 }
1559
1560 @Override
1561 public void close() {
1562 if (!link.isPresent()) {
1563 return;
1564 }
1565 Path p = link.get();
1566 if (!Files.exists(p)) {
1567 return;
1568 }
1569 try {
1570 Files.delete(p);
1571 } catch (IOException e) {
1572 LOG.error(MessageFormat
1573 .format(JGitText.get().closeLockTokenFailed, this), e);
1574 }
1575 }
1576
1577 @Override
1578 public String toString() {
1579 return "LockToken [lockCreated=" + isCreated +
1580 ", link="
1581 + (link.isPresent() ? link.get().getFileName() + "]"
1582 : "<null>]");
1583 }
1584 }
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598 public LockToken createNewFileAtomic(File path) throws IOException {
1599 return new LockToken(path.createNewFile(), Optional.empty());
1600 }
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616 public String relativize(String base, String other) {
1617 return FileUtils.relativizePath(base, other, File.separator, this.isCaseSensitive());
1618 }
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631 public Entry[] list(File directory, FileModeStrategy fileModeStrategy) {
1632 final File[] all = directory.listFiles();
1633 if (all == null) {
1634 return NO_ENTRIES;
1635 }
1636 final Entry[] result = new Entry[all.length];
1637 for (int i = 0; i < result.length; i++) {
1638 result[i] = new FileEntry(all[i], this, fileModeStrategy);
1639 }
1640 return result;
1641 }
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665 public ProcessResult runHookIfPresent(Repository repository,
1666 final String hookName,
1667 String[] args) throws JGitInternalException {
1668 return runHookIfPresent(repository, hookName, args, System.out, System.err,
1669 null);
1670 }
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700 public ProcessResult runHookIfPresent(Repository repository,
1701 final String hookName,
1702 String[] args, PrintStream outRedirect, PrintStream errRedirect,
1703 String stdinArgs) throws JGitInternalException {
1704 return new ProcessResult(Status.NOT_SUPPORTED);
1705 }
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736 protected ProcessResult internalRunHookIfPresent(Repository repository,
1737 final String hookName, String[] args, PrintStream outRedirect,
1738 PrintStream errRedirect, String stdinArgs)
1739 throws JGitInternalException {
1740 File hookFile = findHook(repository, hookName);
1741 if (hookFile == null || hookName == null) {
1742 return new ProcessResult(Status.NOT_PRESENT);
1743 }
1744
1745 File runDirectory = getRunDirectory(repository, hookName);
1746 if (runDirectory == null) {
1747 return new ProcessResult(Status.NOT_PRESENT);
1748 }
1749 String cmd = hookFile.getAbsolutePath();
1750 ProcessBuilder hookProcess = runInShell(cmd, args);
1751 hookProcess.directory(runDirectory.getAbsoluteFile());
1752 Map<String, String> environment = hookProcess.environment();
1753 environment.put(Constants.GIT_DIR_KEY,
1754 repository.getDirectory().getAbsolutePath());
1755 if (!repository.isBare()) {
1756 environment.put(Constants.GIT_WORK_TREE_KEY,
1757 repository.getWorkTree().getAbsolutePath());
1758 }
1759 try {
1760 return new ProcessResult(runProcess(hookProcess, outRedirect,
1761 errRedirect, stdinArgs), Status.OK);
1762 } catch (IOException e) {
1763 throw new JGitInternalException(MessageFormat.format(
1764 JGitText.get().exceptionCaughtDuringExecutionOfHook,
1765 hookName), e);
1766 } catch (InterruptedException e) {
1767 throw new JGitInternalException(MessageFormat.format(
1768 JGitText.get().exceptionHookExecutionInterrupted,
1769 hookName), e);
1770 }
1771 }
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785 public File findHook(Repository repository, String hookName) {
1786 if (hookName == null) {
1787 return null;
1788 }
1789 File hookDir = getHooksDirectory(repository);
1790 if (hookDir == null) {
1791 return null;
1792 }
1793 File hookFile = new File(hookDir, hookName);
1794 if (hookFile.isAbsolute()) {
1795 if (!hookFile.exists() || (FS.DETECTED.supportsExecute()
1796 && !FS.DETECTED.canExecute(hookFile))) {
1797 return null;
1798 }
1799 } else {
1800 try {
1801 File runDirectory = getRunDirectory(repository, hookName);
1802 if (runDirectory == null) {
1803 return null;
1804 }
1805 Path hookPath = runDirectory.getAbsoluteFile().toPath()
1806 .resolve(hookFile.toPath());
1807 FS fs = repository.getFS();
1808 if (fs == null) {
1809 fs = FS.DETECTED;
1810 }
1811 if (!Files.exists(hookPath) || (fs.supportsExecute()
1812 && !fs.canExecute(hookPath.toFile()))) {
1813 return null;
1814 }
1815 hookFile = hookPath.toFile();
1816 } catch (InvalidPathException e) {
1817 LOG.warn(MessageFormat.format(JGitText.get().invalidHooksPath,
1818 hookFile));
1819 return null;
1820 }
1821 }
1822 return hookFile;
1823 }
1824
1825 private File getRunDirectory(Repository repository,
1826 @NonNull String hookName) {
1827 if (repository.isBare()) {
1828 return repository.getDirectory();
1829 }
1830 switch (hookName) {
1831 case "pre-receive":
1832 case "update":
1833 case "post-receive":
1834 case "post-update":
1835 case "push-to-checkout":
1836 return repository.getDirectory();
1837 default:
1838 return repository.getWorkTree();
1839 }
1840 }
1841
1842 private File getHooksDirectory(Repository repository) {
1843 Config config = repository.getConfig();
1844 String hooksDir = config.getString(ConfigConstants.CONFIG_CORE_SECTION,
1845 null, ConfigConstants.CONFIG_KEY_HOOKS_PATH);
1846 if (hooksDir != null) {
1847 return new File(hooksDir);
1848 }
1849 File dir = repository.getDirectory();
1850 return dir == null ? null : new File(dir, Constants.HOOKS);
1851 }
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878 public int runProcess(ProcessBuilder processBuilder,
1879 OutputStream outRedirect, OutputStream errRedirect, String stdinArgs)
1880 throws IOException, InterruptedException {
1881 InputStream in = (stdinArgs == null) ? null : new ByteArrayInputStream(
1882 stdinArgs.getBytes(UTF_8));
1883 return runProcess(processBuilder, outRedirect, errRedirect, in);
1884 }
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914 public int runProcess(ProcessBuilder processBuilder,
1915 OutputStream outRedirect, OutputStream errRedirect,
1916 InputStream inRedirect) throws IOException,
1917 InterruptedException {
1918 final ExecutorService executor = Executors.newFixedThreadPool(2);
1919 Process process = null;
1920
1921
1922 IOException ioException = null;
1923 try {
1924 process = processBuilder.start();
1925 executor.execute(
1926 new StreamGobbler(process.getErrorStream(), errRedirect));
1927 executor.execute(
1928 new StreamGobbler(process.getInputStream(), outRedirect));
1929 @SuppressWarnings("resource")
1930 OutputStream outputStream = process.getOutputStream();
1931 try {
1932 if (inRedirect != null) {
1933 new StreamGobbler(inRedirect, outputStream).copy();
1934 }
1935 } finally {
1936 try {
1937 outputStream.close();
1938 } catch (IOException e) {
1939
1940
1941
1942
1943
1944
1945 }
1946 }
1947 return process.waitFor();
1948 } catch (IOException e) {
1949 ioException = e;
1950 } finally {
1951 shutdownAndAwaitTermination(executor);
1952 if (process != null) {
1953 try {
1954 process.waitFor();
1955 } catch (InterruptedException e) {
1956
1957
1958
1959
1960 Thread.interrupted();
1961 }
1962
1963
1964
1965 if (inRedirect != null) {
1966 inRedirect.close();
1967 }
1968 try {
1969 process.getErrorStream().close();
1970 } catch (IOException e) {
1971 ioException = ioException != null ? ioException : e;
1972 }
1973 try {
1974 process.getInputStream().close();
1975 } catch (IOException e) {
1976 ioException = ioException != null ? ioException : e;
1977 }
1978 try {
1979 process.getOutputStream().close();
1980 } catch (IOException e) {
1981 ioException = ioException != null ? ioException : e;
1982 }
1983 process.destroy();
1984 }
1985 }
1986
1987 throw ioException;
1988 }
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003 private static boolean shutdownAndAwaitTermination(ExecutorService pool) {
2004 boolean hasShutdown = true;
2005 pool.shutdown();
2006 try {
2007
2008 if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
2009 pool.shutdownNow();
2010
2011 if (!pool.awaitTermination(60, TimeUnit.SECONDS))
2012 hasShutdown = false;
2013 }
2014 } catch (InterruptedException ie) {
2015
2016 pool.shutdownNow();
2017
2018 Thread.currentThread().interrupt();
2019 hasShutdown = false;
2020 }
2021 return hasShutdown;
2022 }
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036 public abstract ProcessBuilder runInShell(String cmd, String[] args);
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050 public ExecutionResult execute(ProcessBuilder pb, InputStream in)
2051 throws IOException, InterruptedException {
2052 try (TemporaryBuffer stdout = new TemporaryBuffer.LocalFile(null);
2053 TemporaryBuffer stderr = new TemporaryBuffer.Heap(1024,
2054 1024 * 1024)) {
2055 int rc = runProcess(pb, stdout, stderr, in);
2056 return new ExecutionResult(stdout, stderr, rc);
2057 }
2058 }
2059
2060 private static class Holder<V> {
2061 final V value;
2062
2063 Holder(V value) {
2064 this.value = value;
2065 }
2066 }
2067
2068
2069
2070
2071
2072
2073 public static class Attributes {
2074
2075
2076
2077
2078 public boolean isDirectory() {
2079 return isDirectory;
2080 }
2081
2082
2083
2084
2085 public boolean isExecutable() {
2086 return isExecutable;
2087 }
2088
2089
2090
2091
2092 public boolean isSymbolicLink() {
2093 return isSymbolicLink;
2094 }
2095
2096
2097
2098
2099 public boolean isRegularFile() {
2100 return isRegularFile;
2101 }
2102
2103
2104
2105
2106 public long getCreationTime() {
2107 return creationTime;
2108 }
2109
2110
2111
2112
2113
2114
2115 @Deprecated
2116 public long getLastModifiedTime() {
2117 return lastModifiedInstant.toEpochMilli();
2118 }
2119
2120
2121
2122
2123
2124 public Instant getLastModifiedInstant() {
2125 return lastModifiedInstant;
2126 }
2127
2128 private final boolean isDirectory;
2129
2130 private final boolean isSymbolicLink;
2131
2132 private final boolean isRegularFile;
2133
2134 private final long creationTime;
2135
2136 private final Instant lastModifiedInstant;
2137
2138 private final boolean isExecutable;
2139
2140 private final File file;
2141
2142 private final boolean exists;
2143
2144
2145
2146
2147 protected long length = -1;
2148
2149 final FS fs;
2150
2151 Attributes(FS fs, File file, boolean exists, boolean isDirectory,
2152 boolean isExecutable, boolean isSymbolicLink,
2153 boolean isRegularFile, long creationTime,
2154 Instant lastModifiedInstant, long length) {
2155 this.fs = fs;
2156 this.file = file;
2157 this.exists = exists;
2158 this.isDirectory = isDirectory;
2159 this.isExecutable = isExecutable;
2160 this.isSymbolicLink = isSymbolicLink;
2161 this.isRegularFile = isRegularFile;
2162 this.creationTime = creationTime;
2163 this.lastModifiedInstant = lastModifiedInstant;
2164 this.length = length;
2165 }
2166
2167
2168
2169
2170
2171
2172
2173
2174 public Attributes(File path, FS fs) {
2175 this(fs, path, false, false, false, false, false, 0L, EPOCH, 0L);
2176 }
2177
2178
2179
2180
2181 public long getLength() {
2182 if (length == -1)
2183 return length = file.length();
2184 return length;
2185 }
2186
2187
2188
2189
2190 public String getName() {
2191 return file.getName();
2192 }
2193
2194
2195
2196
2197 public File getFile() {
2198 return file;
2199 }
2200
2201 boolean exists() {
2202 return exists;
2203 }
2204 }
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214 public Attributes getAttributes(File path) {
2215 boolean isDirectory = isDirectory(path);
2216 boolean isFile = !isDirectory && path.isFile();
2217 assert path.exists() == isDirectory || isFile;
2218 boolean exists = isDirectory || isFile;
2219 boolean canExecute = exists && !isDirectory && canExecute(path);
2220 boolean isSymlink = false;
2221 Instant lastModified = exists ? lastModifiedInstant(path) : EPOCH;
2222 long createTime = 0L;
2223 return new Attributes(this, path, exists, isDirectory, canExecute,
2224 isSymlink, isFile, createTime, lastModified, -1);
2225 }
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235 public File normalize(File file) {
2236 return file;
2237 }
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247 public String normalize(String name) {
2248 return name;
2249 }
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263 private static class StreamGobbler implements Runnable {
2264 private InputStream in;
2265
2266 private OutputStream out;
2267
2268 public StreamGobbler(InputStream stream, OutputStream output) {
2269 this.in = stream;
2270 this.out = output;
2271 }
2272
2273 @Override
2274 public void run() {
2275 try {
2276 copy();
2277 } catch (IOException e) {
2278
2279 }
2280 }
2281
2282 void copy() throws IOException {
2283 boolean writeFailure = false;
2284 byte[] buffer = new byte[4096];
2285 int readBytes;
2286 while ((readBytes = in.read(buffer)) != -1) {
2287
2288
2289
2290 if (!writeFailure && out != null) {
2291 try {
2292 out.write(buffer, 0, readBytes);
2293 out.flush();
2294 } catch (IOException e) {
2295 writeFailure = true;
2296 }
2297 }
2298 }
2299 }
2300 }
2301 }