| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package org.apache.commons.io.file; |
| 19 |
|
| 20 |
import java.io.IOException; |
| 21 |
import java.nio.file.FileVisitResult; |
| 22 |
import java.nio.file.Files; |
| 23 |
import java.nio.file.LinkOption; |
| 24 |
import java.nio.file.Path; |
| 25 |
import java.nio.file.attribute.BasicFileAttributes; |
| 26 |
import java.util.Arrays; |
| 27 |
import java.util.Objects; |
| 28 |
|
| 29 |
import org.apache.commons.io.file.Counters.PathCounters; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class CleaningPathVisitor extends CountingPathVisitor { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public static CountingPathVisitor withBigIntegerCounters() { |
| 44 |
return new CleaningPathVisitor(Counters.bigIntegerPathCounters()); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static CountingPathVisitor withLongCounters() { |
| 53 |
return new CleaningPathVisitor(Counters.longPathCounters()); |
| 54 |
} |
| 55 |
|
| 56 |
private final String[] skip; |
| 57 |
private final boolean overrideReadOnly; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public CleaningPathVisitor(final PathCounters pathCounter, final DeleteOption[] deleteOption, final String... skip) { |
| 68 |
super(pathCounter); |
| 69 |
final String[] temp = skip != null ? skip.clone() : EMPTY_STRING_ARRAY; |
| 70 |
Arrays.sort(temp); |
| 71 |
this.skip = temp; |
| 72 |
this.overrideReadOnly = StandardDeleteOption.overrideReadOnly(deleteOption); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public CleaningPathVisitor(final PathCounters pathCounter, final String... skip) { |
| 82 |
this(pathCounter, PathUtils.EMPTY_DELETE_OPTION_ARRAY, skip); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private boolean accept(final Path path) { |
| 92 |
return Arrays.binarySearch(skip, PathUtils.getFileNameString(path)) < 0; |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public boolean equals(final Object obj) { |
| 97 |
if (this == obj) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
if (!super.equals(obj)) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
if (getClass() != obj.getClass()) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
final CleaningPathVisitor other = (CleaningPathVisitor) obj; |
| 107 |
return overrideReadOnly == other.overrideReadOnly && Arrays.equals(skip, other.skip); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public int hashCode() { |
| 112 |
final int prime = 31; |
| 113 |
int result = super.hashCode(); |
| 114 |
result = prime * result + Arrays.hashCode(skip); |
| 115 |
result = prime * result + Objects.hash(overrideReadOnly); |
| 116 |
return result; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attributes) throws IOException { |
| 121 |
super.preVisitDirectory(dir, attributes); |
| 122 |
return accept(dir) ? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE; |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException { |
| 127 |
|
| 128 |
if (accept(file) && Files.exists(file, LinkOption.NOFOLLOW_LINKS)) { |
| 129 |
if (overrideReadOnly) { |
| 130 |
PathUtils.setReadOnly(file, false, LinkOption.NOFOLLOW_LINKS); |
| 131 |
} |
| 132 |
Files.deleteIfExists(file); |
| 133 |
} |
| 134 |
updateFileCounters(file, attributes); |
| 135 |
return FileVisitResult.CONTINUE; |
| 136 |
} |
| 137 |
} |