Add path filter support to PathRemover

This allows for simplifying FilePath to use PathRemover rather than duplicating logic.

Signed-off-by: Matt Sicker <boards@gmail.com>
This commit is contained in:
Matt Sicker committed 2019-01-10 13:40:17 -06:00
1 parent 301ba7af51
commit 59880c2fcd
4 files changed
+47 -19

No files matched your search

+28 -5
View File
@@ -75,6 +75,7 @@ import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
@@ -241,7 +242,18 @@ public class Util {
* if the operation fails.
*/
public static void deleteContentsRecursive(@Nonnull File file) throws IOException {
newPathRemover().forceRemoveDirectoryContents(fileToPath(file));
deleteContentsRecursive(fileToPath(file), path -> true);
}
/**
* Deletes the given directory contents (but not the directory itself) recursively using a filter.
* @param path a directory to delete
* @param pathFilter a predicate that when evaluated to true will delete that path; when false, will ignore that path
* @throws IOException if the operation fails
*/
@Restricted(NoExternalUse.class)
public static void deleteContentsRecursive(@Nonnull Path path, @Nonnull Predicate<Path> pathFilter) throws IOException {
newPathRemover(pathFilter).forceRemoveDirectoryContents(path);
}
/**
@@ -252,7 +264,7 @@ public class Util {
* @throws IOException if it exists but could not be successfully deleted
*/
public static void deleteFile(@Nonnull File f) throws IOException {
newPathRemover().forceRemoveFile(fileToPath(f));
newPathRemover(path -> true).forceRemoveFile(fileToPath(f));
}
/**
@@ -264,7 +276,18 @@ public class Util {
* if the operation fails.
*/
public static void deleteRecursive(@Nonnull File dir) throws IOException {
newPathRemover().forceRemoveRecursive(fileToPath(dir));
deleteRecursive(fileToPath(dir), path -> true);
}
/**
* Deletes the given directory and contents recursively using a filter.
* @param dir a directory to delete
* @param pathFilter a predicate that when evaluated to true will delete that path; when false, will ignore that path
* @throws IOException if the operation fails
*/
@Restricted(NoExternalUse.class)
public static void deleteRecursive(@Nonnull Path dir, @Nonnull Predicate<Path> pathFilter) throws IOException {
newPathRemover(pathFilter).forceRemoveRecursive(dir);
}
/*
@@ -1560,8 +1583,8 @@ public class Util {
@Restricted(value = NoExternalUse.class)
static boolean GC_AFTER_FAILED_DELETE = SystemProperties.getBoolean(Util.class.getName() + ".performGCOnFailedDelete");
private static PathRemover newPathRemover() {
return PathRemover.newRobustRemover(DELETION_MAX - 1, GC_AFTER_FAILED_DELETE, WAIT_BETWEEN_DELETION_RETRIES);
private static PathRemover newPathRemover(@Nonnull Predicate<Path> pathFilter) {
return PathRemover.newFilteredRobustRemover(pathFilter, DELETION_MAX - 1, GC_AFTER_FAILED_DELETE, WAIT_BETWEEN_DELETION_RETRIES);
}
/**