diff options
author | Valerij Fredriksen <valerijf@verizonmedia.com> | 2020-04-24 10:57:03 +0200 |
---|---|---|
committer | Valerij Fredriksen <valerijf@verizonmedia.com> | 2020-04-24 11:03:21 +0200 |
commit | 59684f45818f16dfffeacec1a9dcb3bd7aa80ed0 (patch) | |
tree | 0648099e2ac5cb3de99af2f69cc225e94def4bda /node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileFinder.java | |
parent | e83700c6f617f5fbcb78be9f7a1289523aee987d (diff) |
Implement FileFinder::prune
Diffstat (limited to 'node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileFinder.java')
-rw-r--r-- | node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileFinder.java | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileFinder.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileFinder.java index a41281d0479..f9a369aab27 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileFinder.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileFinder.java @@ -14,6 +14,8 @@ import java.nio.file.attribute.BasicFileAttributes; import java.time.Duration; import java.time.Instant; import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; @@ -35,6 +37,7 @@ public class FileFinder { private static final Logger logger = Logger.getLogger(FileFinder.class.getName()); private final Path basePath; + private final Set<Path> pruned = new HashSet<>(); private Predicate<FileAttributes> matcher; private int maxDepth = Integer.MAX_VALUE; @@ -72,6 +75,27 @@ public class FileFinder { } /** + * Path for which whole directory tree will be skipped, including the path itself. + * The path must be under {@code basePath} or be relative to {@code basePath}. + */ + public FileFinder prune(Path path) { + if (!path.isAbsolute()) + path = basePath.resolve(path); + + if (!path.startsWith(basePath)) + throw new IllegalArgumentException("Prune path " + path + " is not under base path " + basePath); + + this.pruned.add(path); + return this; + } + + /** Convenience method for pruning multiple paths, see {@link #prune(Path)}. */ + public FileFinder prune(Collection<Path> paths) { + paths.forEach(this::prune); + return this; + } + + /** * Maximum depth (relative to basePath) where contents should be matched with the given filters. * Default is unlimited. */ @@ -145,6 +169,8 @@ public class FileFinder { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { + if (pruned.contains(dir)) return FileVisitResult.SKIP_SUBTREE; + currentLevel++; FileAttributes attributes = new FileAttributes(dir, attrs); |