Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add glob 'cleanup_pattern' to remove obsolete resources #253

Merged
merged 2 commits into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions core/src/main/java/com/threerings/getdown/data/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,13 @@ public String getDigest (Resource resource)
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).
Expand Down Expand Up @@ -594,6 +601,7 @@ public Config init (boolean checkPlatform) throws IOException
initJava(config);
initTracking(config);
initResources(config);
initCleanupPatterns(config);
initArgs(config);
return config;
}
Expand Down Expand Up @@ -753,6 +761,22 @@ public void initResources (Config config) throws IOException {
}
}

/**
* 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.
*/
Expand Down Expand Up @@ -1771,6 +1795,7 @@ protected static String encodePath (String path)

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;
Expand Down
42 changes: 42 additions & 0 deletions core/src/main/java/com/threerings/getdown/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -236,4 +238,44 @@ public static void walkTree (File root, Visitor visitor)
}
}
}

/**
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -342,6 +343,13 @@ protected void getdown ()
if (_app.verifyMetadata(this)) {
log.info("Application requires update.");
update();

// do resource cleanup
final List<String> cleanupPatterns = _app.cleanupPatterns();
if (!cleanupPatterns.isEmpty()) {
cleanupResources(cleanupPatterns);
}

// loop back again and reverify the metadata
continue;
}
Expand Down Expand Up @@ -679,6 +687,37 @@ protected void download (Collection<Resource> resources)
}
}

/**
* Removes files/resources by glob pattern with exclusion of resources defined in Getdown.txt
*/
void cleanupResources(List<String> cleanupPatterns) {
//get list of files from glob patterns
final String applicationPath = _app.getAppDir().getAbsolutePath();
final Set<Path> filesFromGlob = new HashSet<>();
for (String cleanupPattern : cleanupPatterns) {
filesFromGlob.addAll(FileUtil.getFilePathsByGlob(applicationPath, cleanupPattern));
}

//filter out files contained in resources
Set<Path> rsrcs = new HashSet<>();
for (Resource resource : _app.getAllActiveResources()) {
rsrcs.add(resource.getLocal().toPath().toAbsolutePath());
}

final Set<Path> cleanupSet = new HashSet<>();
for (Path path : filesFromGlob) {
if (!rsrcs.contains(path)) {
cleanupSet.add(path);
}
}

if (!cleanupSet.isEmpty()) {
for (Path path : cleanupSet) {
path.toFile().delete();
}
}
}

/**
* Called to launch the application if everything is determined to be ready to go.
*/
Expand Down