-
Notifications
You must be signed in to change notification settings - Fork 2
/
KeepGoingDeletionVisitor.java
91 lines (82 loc) · 2.6 KB
/
KeepGoingDeletionVisitor.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
package com.github.fge.filesystem.deletion;
import com.github.fge.filesystem.MoreFiles;
import com.github.fge.filesystem.RecursionMode;
import com.github.fge.filesystem.exceptions.RecursiveDeletionException;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.spi.FileSystemProvider;
import java.util.Objects;
/**
* Deletion {@link FileVisitor} for {@link RecursionMode#KEEP_GOING keep going}
* operation
*
* <p>This visitor will collect all {@link IOException}s it encounters into the
* {@link RecursiveDeletionException} argument (as a {@link
* Throwable#addSuppressed(Throwable) suppressed} exception).</p>
*
* @see MoreFiles#deleteRecursive(Path, RecursionMode)
*/
public final class KeepGoingDeletionVisitor
implements FileVisitor<Path>
{
private final FileSystemProvider provider;
private final RecursiveDeletionException exception;
/**
* Constructor
*
* @param victim the path to delete recursively
* @param exception the exception to add suppressed exceptions to
*/
public KeepGoingDeletionVisitor(final Path victim,
final RecursiveDeletionException exception)
{
provider = Objects.requireNonNull(victim).getFileSystem().provider();
this.exception = Objects.requireNonNull(exception);
}
@Override
public FileVisitResult preVisitDirectory(final Path dir,
final BasicFileAttributes attrs)
throws IOException
{
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(final Path file,
final BasicFileAttributes attrs)
throws IOException
{
try {
provider.delete(file);
} catch (IOException ioException) {
exception.addSuppressed(ioException);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(final Path file,
final IOException exc)
throws IOException
{
exception.addSuppressed(exc);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(final Path dir,
final IOException exc)
throws IOException
{
if (exc != null) {
exception.addSuppressed(exc);
return FileVisitResult.CONTINUE;
}
try {
provider.delete(dir);
} catch (IOException ioException) {
exception.addSuppressed(ioException);
}
return FileVisitResult.CONTINUE;
}
}