Adds support for glob 'cleanup_pattern' to facilitate removal of obsolete resources

(cherry picked from commit 515ed2e049dbc4e76d4e35d89ec2c674e87d3e6d)
This commit is contained in:
Giedrius Zavadskis
2021-03-29 10:30:19 +03:00
parent 55c544c68b
commit 79420067b2
3 changed files with 105 additions and 0 deletions
@@ -362,6 +362,13 @@ public class Application
return _digest.getDigest(resource);
}
/**
* Returns a list of the cleanup patterns used by application.
*/
public List<String> cleanupPatterns() {
return _cleanupPatterns;
}
/**
* Returns a list of all the active {@link Resource} objects used by this application (code and
* non-code).
@@ -594,6 +601,7 @@ public class Application
initJava(config);
initTracking(config);
initResources(config);
initCleanupPatterns(config);
initArgs(config);
return config;
}
@@ -753,6 +761,22 @@ public class Application
}
}
/**
* Reads the cleanup patterns from {@code config} into this instance.
*/
public void initCleanupPatterns (Config config) {
// clear our arrays as we may be reinitializing
_cleanupPatterns.clear();
// parse cleanup patterns
String[] patterns = config.getMultiValue("cleanup_pattern");
if (patterns == null) {
return;
}
_cleanupPatterns.addAll(Arrays.asList(patterns));
}
/**
* Reads the command line arg info from {@code config} into this instance.
*/
@@ -1771,6 +1795,7 @@ public class Application
protected List<Resource> _codes = new ArrayList<>();
protected List<Resource> _resources = new ArrayList<>();
protected List<String> _cleanupPatterns = new ArrayList<>();
protected List<String> _cpdirs = new ArrayList<>();
protected int _verifyTimeout = 60;
@@ -6,6 +6,8 @@
package com.threerings.getdown.util;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.jar.*;
import java.util.zip.*;
@@ -236,4 +238,44 @@ public final class FileUtil
}
}
}
/**
* Returns files as Paths from path {@code basePath}, filtered Glob pattern {@code globPattern}.
*/
public static Set<Path> getFilePathsByGlob(String basePath, String globPattern) {
//massaging glob pattern
final String globPatternUnixSeparator = globPattern.replace("\\", "/");
final String glob = String.format("glob:%s%s%s",
basePath.replace("\\", "/"),
globPatternUnixSeparator.startsWith("/") ? "" : "/",
globPatternUnixSeparator);
final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(glob);
final Set<Path> foundFilePaths = new HashSet<>();
try {
Files.walkFileTree(Paths.get(basePath), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path,
BasicFileAttributes attrs) {
if (pathMatcher.matches(path) && !Files.isDirectory(path)) {
foundFilePaths.add(path);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
return FileVisitResult.CONTINUE;
}
});
} catch (Exception e) {
log.warning(String.format("Failed to get file list with path %s with glob %s",
basePath, globPattern), "error", e);
return Collections.emptySet();
}
return foundFilePaths;
}
}