/
MoreFiles.java
800 lines (725 loc) · 31.5 KB
/
MoreFiles.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
* Copyright (C) 2013 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.io;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.graph.SuccessorsFunction;
import com.google.common.graph.Traverser;
import com.google.common.io.ByteSource.AsCharSource;
import com.google.j2objc.annotations.J2ObjCIncompatible;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.channels.SeekableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.NoSuchFileException;
import java.nio.file.NotDirectoryException;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.SecureDirectoryStream;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
/**
* Static utilities for use with {@link Path} instances, intended to complement {@link Files}.
*
* <p>Many methods provided by Guava's {@code Files} class for {@link java.io.File} instances are
* now available via the JDK's {@link java.nio.file.Files} class for {@code Path} - check the JDK's
* class if a sibling method from {@code Files} appears to be missing from this class.
*
* @since 21.0
* @author Colin Decker
*/
@Beta
@GwtIncompatible
@J2ObjCIncompatible // java.nio.file
public final class MoreFiles {
private MoreFiles() {}
/**
* Returns a view of the given {@code path} as a {@link ByteSource}.
*
* <p>Any {@linkplain OpenOption open options} provided are used when opening streams to the file
* and may affect the behavior of the returned source and the streams it provides. See {@link
* StandardOpenOption} for the standard options that may be provided. Providing no options is
* equivalent to providing the {@link StandardOpenOption#READ READ} option.
*/
public static ByteSource asByteSource(Path path, OpenOption... options) {
return new PathByteSource(path, options);
}
private static final class PathByteSource extends ByteSource {
private static final LinkOption[] FOLLOW_LINKS = {};
private final Path path;
private final OpenOption[] options;
private final boolean followLinks;
private PathByteSource(Path path, OpenOption... options) {
this.path = checkNotNull(path);
this.options = options.clone();
this.followLinks = followLinks(this.options);
// TODO(cgdecker): validate the provided options... for example, just WRITE seems wrong
}
private static boolean followLinks(OpenOption[] options) {
for (OpenOption option : options) {
if (option == NOFOLLOW_LINKS) {
return false;
}
}
return true;
}
@Override
public InputStream openStream() throws IOException {
return Files.newInputStream(path, options);
}
private BasicFileAttributes readAttributes() throws IOException {
return Files.readAttributes(
path,
BasicFileAttributes.class,
followLinks ? FOLLOW_LINKS : new LinkOption[] {NOFOLLOW_LINKS});
}
@Override
public Optional<Long> sizeIfKnown() {
BasicFileAttributes attrs;
try {
attrs = readAttributes();
} catch (IOException e) {
// Failed to get attributes; we don't know the size.
return Optional.absent();
}
// Don't return a size for directories or symbolic links; their sizes are implementation
// specific and they can't be read as bytes using the read methods anyway.
if (attrs.isDirectory() || attrs.isSymbolicLink()) {
return Optional.absent();
}
return Optional.of(attrs.size());
}
@Override
public long size() throws IOException {
BasicFileAttributes attrs = readAttributes();
// Don't return a size for directories or symbolic links; their sizes are implementation
// specific and they can't be read as bytes using the read methods anyway.
if (attrs.isDirectory()) {
throw new IOException("can't read: is a directory");
} else if (attrs.isSymbolicLink()) {
throw new IOException("can't read: is a symbolic link");
}
return attrs.size();
}
@Override
public byte[] read() throws IOException {
try (SeekableByteChannel channel = Files.newByteChannel(path, options)) {
return ByteStreams.toByteArray(Channels.newInputStream(channel), channel.size());
}
}
@Override
public CharSource asCharSource(Charset charset) {
if (options.length == 0) {
// If no OpenOptions were passed, delegate to Files.lines, which could have performance
// advantages. (If OpenOptions were passed we can't, because Files.lines doesn't have an
// overload taking OpenOptions, meaning we can't guarantee the same behavior w.r.t. things
// like following/not following symlinks.
return new AsCharSource(charset) {
@SuppressWarnings("FilesLinesLeak") // the user needs to close it in this case
@Override
public Stream<String> lines() throws IOException {
return Files.lines(path, charset);
}
};
}
return super.asCharSource(charset);
}
@Override
public String toString() {
return "MoreFiles.asByteSource(" + path + ", " + Arrays.toString(options) + ")";
}
}
/**
* Returns a view of the given {@code path} as a {@link ByteSink}.
*
* <p>Any {@linkplain OpenOption open options} provided are used when opening streams to the file
* and may affect the behavior of the returned sink and the streams it provides. See {@link
* StandardOpenOption} for the standard options that may be provided. Providing no options is
* equivalent to providing the {@link StandardOpenOption#CREATE CREATE}, {@link
* StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING} and {@link StandardOpenOption#WRITE
* WRITE} options.
*/
public static ByteSink asByteSink(Path path, OpenOption... options) {
return new PathByteSink(path, options);
}
private static final class PathByteSink extends ByteSink {
private final Path path;
private final OpenOption[] options;
private PathByteSink(Path path, OpenOption... options) {
this.path = checkNotNull(path);
this.options = options.clone();
// TODO(cgdecker): validate the provided options... for example, just READ seems wrong
}
@Override
public OutputStream openStream() throws IOException {
return Files.newOutputStream(path, options);
}
@Override
public String toString() {
return "MoreFiles.asByteSink(" + path + ", " + Arrays.toString(options) + ")";
}
}
/**
* Returns a view of the given {@code path} as a {@link CharSource} using the given {@code
* charset}.
*
* <p>Any {@linkplain OpenOption open options} provided are used when opening streams to the file
* and may affect the behavior of the returned source and the streams it provides. See {@link
* StandardOpenOption} for the standard options that may be provided. Providing no options is
* equivalent to providing the {@link StandardOpenOption#READ READ} option.
*/
public static CharSource asCharSource(Path path, Charset charset, OpenOption... options) {
return asByteSource(path, options).asCharSource(charset);
}
/**
* Returns a view of the given {@code path} as a {@link CharSink} using the given {@code charset}.
*
* <p>Any {@linkplain OpenOption open options} provided are used when opening streams to the file
* and may affect the behavior of the returned sink and the streams it provides. See {@link
* StandardOpenOption} for the standard options that may be provided. Providing no options is
* equivalent to providing the {@link StandardOpenOption#CREATE CREATE}, {@link
* StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING} and {@link StandardOpenOption#WRITE
* WRITE} options.
*/
public static CharSink asCharSink(Path path, Charset charset, OpenOption... options) {
return asByteSink(path, options).asCharSink(charset);
}
/**
* Returns an immutable list of paths to the files contained in the given directory.
*
* @throws NoSuchFileException if the file does not exist <i>(optional specific exception)</i>
* @throws NotDirectoryException if the file could not be opened because it is not a directory
* <i>(optional specific exception)</i>
* @throws IOException if an I/O error occurs
*/
public static ImmutableList<Path> listFiles(Path dir) throws IOException {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
return ImmutableList.copyOf(stream);
} catch (DirectoryIteratorException e) {
throw e.getCause();
}
}
/**
* Returns a {@link Traverser} instance for the file and directory tree. The returned traverser
* starts from a {@link Path} and will return all files and directories it encounters.
*
* <p>The returned traverser attempts to avoid following symbolic links to directories. However,
* the traverser cannot guarantee that it will not follow symbolic links to directories as it is
* possible for a directory to be replaced with a symbolic link between checking if the file is a
* directory and actually reading the contents of that directory.
*
* <p>If the {@link Path} passed to one of the traversal methods does not exist or is not a
* directory, no exception will be thrown and the returned {@link Iterable} will contain a single
* element: that path.
*
* <p>{@link DirectoryIteratorException} may be thrown when iterating {@link Iterable} instances
* created by this traverser if an {@link IOException} is thrown by a call to {@link
* #listFiles(Path)}.
*
* <p>Example: {@code MoreFiles.fileTraverser().breadthFirst("/")} may return files with the
* following paths: {@code ["/", "/etc", "/home", "/usr", "/etc/config.txt", "/etc/fonts", ...]}
*
* @since 23.5
*/
public static Traverser<Path> fileTraverser() {
return Traverser.forTree(FILE_TREE);
}
private static final SuccessorsFunction<Path> FILE_TREE =
new SuccessorsFunction<Path>() {
@Override
public Iterable<Path> successors(Path path) {
return fileTreeChildren(path);
}
};
private static Iterable<Path> fileTreeChildren(Path dir) {
if (Files.isDirectory(dir, NOFOLLOW_LINKS)) {
try {
return listFiles(dir);
} catch (IOException e) {
// the exception thrown when iterating a DirectoryStream if an I/O exception occurs
throw new DirectoryIteratorException(e);
}
}
return ImmutableList.of();
}
/**
* Returns a predicate that returns the result of {@link java.nio.file.Files#isDirectory(Path,
* LinkOption...)} on input paths with the given link options.
*/
public static Predicate<Path> isDirectory(LinkOption... options) {
final LinkOption[] optionsCopy = options.clone();
return new Predicate<Path>() {
@Override
public boolean apply(Path input) {
return Files.isDirectory(input, optionsCopy);
}
@Override
public String toString() {
return "MoreFiles.isDirectory(" + Arrays.toString(optionsCopy) + ")";
}
};
}
/** Returns whether or not the file with the given name in the given dir is a directory. */
private static boolean isDirectory(
SecureDirectoryStream<Path> dir, Path name, LinkOption... options) throws IOException {
return dir.getFileAttributeView(name, BasicFileAttributeView.class, options)
.readAttributes()
.isDirectory();
}
/**
* Returns a predicate that returns the result of {@link java.nio.file.Files#isRegularFile(Path,
* LinkOption...)} on input paths with the given link options.
*/
public static Predicate<Path> isRegularFile(LinkOption... options) {
final LinkOption[] optionsCopy = options.clone();
return new Predicate<Path>() {
@Override
public boolean apply(Path input) {
return Files.isRegularFile(input, optionsCopy);
}
@Override
public String toString() {
return "MoreFiles.isRegularFile(" + Arrays.toString(optionsCopy) + ")";
}
};
}
/**
* Returns true if the files located by the given paths exist, are not directories, and contain
* the same bytes.
*
* @throws IOException if an I/O error occurs
* @since 22.0
*/
public static boolean equal(Path path1, Path path2) throws IOException {
checkNotNull(path1);
checkNotNull(path2);
if (Files.isSameFile(path1, path2)) {
return true;
}
/*
* Some operating systems may return zero as the length for files denoting system-dependent
* entities such as devices or pipes, in which case we must fall back on comparing the bytes
* directly.
*/
ByteSource source1 = asByteSource(path1);
ByteSource source2 = asByteSource(path2);
long len1 = source1.sizeIfKnown().or(0L);
long len2 = source2.sizeIfKnown().or(0L);
if (len1 != 0 && len2 != 0 && len1 != len2) {
return false;
}
return source1.contentEquals(source2);
}
/**
* Like the unix command of the same name, creates an empty file or updates the last modified
* timestamp of the existing file at the given path to the current system time.
*/
public static void touch(Path path) throws IOException {
checkNotNull(path);
try {
Files.setLastModifiedTime(path, FileTime.fromMillis(System.currentTimeMillis()));
} catch (NoSuchFileException e) {
try {
Files.createFile(path);
} catch (FileAlreadyExistsException ignore) {
// The file didn't exist when we called setLastModifiedTime, but it did when we called
// createFile, so something else created the file in between. The end result is
// what we wanted: a new file that probably has its last modified time set to approximately
// now. Or it could have an arbitrary last modified time set by the creator, but that's no
// different than if another process set its last modified time to something else after we
// created it here.
}
}
}
/**
* Creates any necessary but nonexistent parent directories of the specified path. Note that if
* this operation fails, it may have succeeded in creating some (but not all) of the necessary
* parent directories. The parent directory is created with the given {@code attrs}.
*
* @throws IOException if an I/O error occurs, or if any necessary but nonexistent parent
* directories of the specified file could not be created.
*/
public static void createParentDirectories(Path path, FileAttribute<?>... attrs)
throws IOException {
// Interestingly, unlike File.getCanonicalFile(), Path/Files provides no way of getting the
// canonical (absolute, normalized, symlinks resolved, etc.) form of a path to a nonexistent
// file. getCanonicalFile() can at least get the canonical form of the part of the path which
// actually exists and then append the normalized remainder of the path to that.
Path normalizedAbsolutePath = path.toAbsolutePath().normalize();
Path parent = normalizedAbsolutePath.getParent();
if (parent == null) {
// The given directory is a filesystem root. All zero of its ancestors exist. This doesn't
// mean that the root itself exists -- consider x:\ on a Windows machine without such a
// drive -- or even that the caller can create it, but this method makes no such guarantees
// even for non-root files.
return;
}
// Check if the parent is a directory first because createDirectories will fail if the parent
// exists and is a symlink to a directory... we'd like for this to succeed in that case.
// (I'm kind of surprised that createDirectories would fail in that case; doesn't seem like
// what you'd want to happen.)
if (!Files.isDirectory(parent)) {
Files.createDirectories(parent, attrs);
if (!Files.isDirectory(parent)) {
throw new IOException("Unable to create parent directories of " + path);
}
}
}
/**
* Returns the <a href="http://en.wikipedia.org/wiki/Filename_extension">file extension</a> for
* the file at the given path, or the empty string if the file has no extension. The result does
* not include the '{@code .}'.
*
* <p><b>Note:</b> This method simply returns everything after the last '{@code .}' in the file's
* name as determined by {@link Path#getFileName}. It does not account for any filesystem-specific
* behavior that the {@link Path} API does not already account for. For example, on NTFS it will
* report {@code "txt"} as the extension for the filename {@code "foo.exe:.txt"} even though NTFS
* will drop the {@code ":.txt"} part of the name when the file is actually created on the
* filesystem due to NTFS's <a href="https://goo.gl/vTpJi4">Alternate Data Streams</a>.
*/
public static String getFileExtension(Path path) {
Path name = path.getFileName();
// null for empty paths and root-only paths
if (name == null) {
return "";
}
String fileName = name.toString();
int dotIndex = fileName.lastIndexOf('.');
return dotIndex == -1 ? "" : fileName.substring(dotIndex + 1);
}
/**
* Returns the file name without its <a
* href="http://en.wikipedia.org/wiki/Filename_extension">file extension</a> or path. This is
* similar to the {@code basename} unix command. The result does not include the '{@code .}'.
*/
public static String getNameWithoutExtension(Path path) {
Path name = path.getFileName();
// null for empty paths and root-only paths
if (name == null) {
return "";
}
String fileName = name.toString();
int dotIndex = fileName.lastIndexOf('.');
return dotIndex == -1 ? fileName : fileName.substring(0, dotIndex);
}
/**
* Deletes the file or directory at the given {@code path} recursively. Deletes symbolic links,
* not their targets (subject to the caveat below).
*
* <p>If an I/O exception occurs attempting to read, open or delete any file under the given
* directory, this method skips that file and continues. All such exceptions are collected and,
* after attempting to delete all files, an {@code IOException} is thrown containing those
* exceptions as {@linkplain Throwable#getSuppressed() suppressed exceptions}.
*
* <h2>Warning: Security of recursive deletes</h2>
*
* <p>On a file system that supports symbolic links and does <i>not</i> support {@link
* SecureDirectoryStream}, it is possible for a recursive delete to delete files and directories
* that are <i>outside</i> the directory being deleted. This can happen if, after checking that a
* file is a directory (and not a symbolic link), that directory is replaced by a symbolic link to
* an outside directory before the call that opens the directory to read its entries.
*
* <p>By default, this method throws {@link InsecureRecursiveDeleteException} if it can't
* guarantee the security of recursive deletes. If you wish to allow the recursive deletes anyway,
* pass {@link RecursiveDeleteOption#ALLOW_INSECURE} to this method to override that behavior.
*
* @throws NoSuchFileException if {@code path} does not exist <i>(optional specific exception)</i>
* @throws InsecureRecursiveDeleteException if the security of recursive deletes can't be
* guaranteed for the file system and {@link RecursiveDeleteOption#ALLOW_INSECURE} was not
* specified
* @throws IOException if {@code path} or any file in the subtree rooted at it can't be deleted
* for any reason
*/
public static void deleteRecursively(Path path, RecursiveDeleteOption... options)
throws IOException {
Path parentPath = getParentPath(path);
if (parentPath == null) {
throw new FileSystemException(path.toString(), null, "can't delete recursively");
}
Collection<IOException> exceptions = null; // created lazily if needed
try {
boolean sdsSupported = false;
try (DirectoryStream<Path> parent = Files.newDirectoryStream(parentPath)) {
if (parent instanceof SecureDirectoryStream) {
sdsSupported = true;
exceptions =
deleteRecursivelySecure((SecureDirectoryStream<Path>) parent, path.getFileName());
}
}
if (!sdsSupported) {
checkAllowsInsecure(path, options);
exceptions = deleteRecursivelyInsecure(path);
}
} catch (IOException e) {
if (exceptions == null) {
throw e;
} else {
exceptions.add(e);
}
}
if (exceptions != null) {
throwDeleteFailed(path, exceptions);
}
}
/**
* Deletes all files within the directory at the given {@code path} {@linkplain #deleteRecursively
* recursively}. Does not delete the directory itself. Deletes symbolic links, not their targets
* (subject to the caveat below). If {@code path} itself is a symbolic link to a directory, that
* link is followed and the contents of the directory it targets are deleted.
*
* <p>If an I/O exception occurs attempting to read, open or delete any file under the given
* directory, this method skips that file and continues. All such exceptions are collected and,
* after attempting to delete all files, an {@code IOException} is thrown containing those
* exceptions as {@linkplain Throwable#getSuppressed() suppressed exceptions}.
*
* <h2>Warning: Security of recursive deletes</h2>
*
* <p>On a file system that supports symbolic links and does <i>not</i> support {@link
* SecureDirectoryStream}, it is possible for a recursive delete to delete files and directories
* that are <i>outside</i> the directory being deleted. This can happen if, after checking that a
* file is a directory (and not a symbolic link), that directory is replaced by a symbolic link to
* an outside directory before the call that opens the directory to read its entries.
*
* <p>By default, this method throws {@link InsecureRecursiveDeleteException} if it can't
* guarantee the security of recursive deletes. If you wish to allow the recursive deletes anyway,
* pass {@link RecursiveDeleteOption#ALLOW_INSECURE} to this method to override that behavior.
*
* @throws NoSuchFileException if {@code path} does not exist <i>(optional specific exception)</i>
* @throws NotDirectoryException if the file at {@code path} is not a directory <i>(optional
* specific exception)</i>
* @throws InsecureRecursiveDeleteException if the security of recursive deletes can't be
* guaranteed for the file system and {@link RecursiveDeleteOption#ALLOW_INSECURE} was not
* specified
* @throws IOException if one or more files can't be deleted for any reason
*/
public static void deleteDirectoryContents(Path path, RecursiveDeleteOption... options)
throws IOException {
Collection<IOException> exceptions = null; // created lazily if needed
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
if (stream instanceof SecureDirectoryStream) {
SecureDirectoryStream<Path> sds = (SecureDirectoryStream<Path>) stream;
exceptions = deleteDirectoryContentsSecure(sds);
} else {
checkAllowsInsecure(path, options);
exceptions = deleteDirectoryContentsInsecure(stream);
}
} catch (IOException e) {
if (exceptions == null) {
throw e;
} else {
exceptions.add(e);
}
}
if (exceptions != null) {
throwDeleteFailed(path, exceptions);
}
}
/**
* Secure recursive delete using {@code SecureDirectoryStream}. Returns a collection of exceptions
* that occurred or null if no exceptions were thrown.
*/
@NullableDecl
private static Collection<IOException> deleteRecursivelySecure(
SecureDirectoryStream<Path> dir, Path path) {
Collection<IOException> exceptions = null;
try {
if (isDirectory(dir, path, NOFOLLOW_LINKS)) {
try (SecureDirectoryStream<Path> childDir = dir.newDirectoryStream(path, NOFOLLOW_LINKS)) {
exceptions = deleteDirectoryContentsSecure(childDir);
}
// If exceptions is not null, something went wrong trying to delete the contents of the
// directory, so we shouldn't try to delete the directory as it will probably fail.
if (exceptions == null) {
dir.deleteDirectory(path);
}
} else {
dir.deleteFile(path);
}
return exceptions;
} catch (IOException e) {
return addException(exceptions, e);
}
}
/**
* Secure method for deleting the contents of a directory using {@code SecureDirectoryStream}.
* Returns a collection of exceptions that occurred or null if no exceptions were thrown.
*/
@NullableDecl
private static Collection<IOException> deleteDirectoryContentsSecure(
SecureDirectoryStream<Path> dir) {
Collection<IOException> exceptions = null;
try {
for (Path path : dir) {
exceptions = concat(exceptions, deleteRecursivelySecure(dir, path.getFileName()));
}
return exceptions;
} catch (DirectoryIteratorException e) {
return addException(exceptions, e.getCause());
}
}
/**
* Insecure recursive delete for file systems that don't support {@code SecureDirectoryStream}.
* Returns a collection of exceptions that occurred or null if no exceptions were thrown.
*/
@NullableDecl
private static Collection<IOException> deleteRecursivelyInsecure(Path path) {
Collection<IOException> exceptions = null;
try {
if (Files.isDirectory(path, NOFOLLOW_LINKS)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
exceptions = deleteDirectoryContentsInsecure(stream);
}
}
// If exceptions is not null, something went wrong trying to delete the contents of the
// directory, so we shouldn't try to delete the directory as it will probably fail.
if (exceptions == null) {
Files.delete(path);
}
return exceptions;
} catch (IOException e) {
return addException(exceptions, e);
}
}
/**
* Simple, insecure method for deleting the contents of a directory for file systems that don't
* support {@code SecureDirectoryStream}. Returns a collection of exceptions that occurred or null
* if no exceptions were thrown.
*/
@NullableDecl
private static Collection<IOException> deleteDirectoryContentsInsecure(
DirectoryStream<Path> dir) {
Collection<IOException> exceptions = null;
try {
for (Path entry : dir) {
exceptions = concat(exceptions, deleteRecursivelyInsecure(entry));
}
return exceptions;
} catch (DirectoryIteratorException e) {
return addException(exceptions, e.getCause());
}
}
/**
* Returns a path to the parent directory of the given path. If the path actually has a parent
* path, this is simple. Otherwise, we need to do some trickier things. Returns null if the path
* is a root or is the empty path.
*/
@NullableDecl
private static Path getParentPath(Path path) {
Path parent = path.getParent();
// Paths that have a parent:
if (parent != null) {
// "/foo" ("/")
// "foo/bar" ("foo")
// "C:\foo" ("C:\")
// "\foo" ("\" - current drive for process on Windows)
// "C:foo" ("C:" - working dir of drive C on Windows)
return parent;
}
// Paths that don't have a parent:
if (path.getNameCount() == 0) {
// "/", "C:\", "\" (no parent)
// "" (undefined, though typically parent of working dir)
// "C:" (parent of working dir of drive C on Windows)
//
// For working dir paths ("" and "C:"), return null because:
// A) it's not specified that "" is the path to the working directory.
// B) if we're getting this path for recursive delete, it's typically not possible to
// delete the working dir with a relative path anyway, so it's ok to fail.
// C) if we're getting it for opening a new SecureDirectoryStream, there's no need to get
// the parent path anyway since we can safely open a DirectoryStream to the path without
// worrying about a symlink.
return null;
} else {
// "foo" (working dir)
return path.getFileSystem().getPath(".");
}
}
/** Checks that the given options allow an insecure delete, throwing an exception if not. */
private static void checkAllowsInsecure(Path path, RecursiveDeleteOption[] options)
throws InsecureRecursiveDeleteException {
if (!Arrays.asList(options).contains(RecursiveDeleteOption.ALLOW_INSECURE)) {
throw new InsecureRecursiveDeleteException(path.toString());
}
}
/**
* Adds the given exception to the given collection, creating the collection if it's null. Returns
* the collection.
*/
private static Collection<IOException> addException(
@NullableDecl Collection<IOException> exceptions, IOException e) {
if (exceptions == null) {
exceptions = new ArrayList<>(); // don't need Set semantics
}
exceptions.add(e);
return exceptions;
}
/**
* Concatenates the contents of the two given collections of exceptions. If either collection is
* null, the other collection is returned. Otherwise, the elements of {@code other} are added to
* {@code exceptions} and {@code exceptions} is returned.
*/
@NullableDecl
private static Collection<IOException> concat(
@NullableDecl Collection<IOException> exceptions,
@NullableDecl Collection<IOException> other) {
if (exceptions == null) {
return other;
} else if (other != null) {
exceptions.addAll(other);
}
return exceptions;
}
/**
* Throws an exception indicating that one or more files couldn't be deleted. The thrown exception
* contains all the exceptions in the given collection as suppressed exceptions.
*/
private static void throwDeleteFailed(Path path, Collection<IOException> exceptions)
throws FileSystemException {
// TODO(cgdecker): Should there be a custom exception type for this?
// Also, should we try to include the Path of each file we may have failed to delete rather
// than just the exceptions that occurred?
FileSystemException deleteFailed =
new FileSystemException(
path.toString(),
null,
"failed to delete one or more files; see suppressed exceptions for details");
for (IOException e : exceptions) {
deleteFailed.addSuppressed(e);
}
throw deleteFailed;
}
}