diff options
author | Fabian Meumertzheim <meumertzheim@code-intelligence.com> | 2022-09-08 11:10:09 +0200 |
---|---|---|
committer | Fabian Meumertzheim <fabian@meumertzhe.im> | 2022-09-15 09:09:36 +0200 |
commit | 1bcea20676364b1edc3c730660cc879334cca172 (patch) | |
tree | d9f5fca750a846b3b5661db60229bf491eb19695 /bazel | |
parent | cf96bdb66dadc0021c463083ccbb5636d06fc97c (diff) | |
download | jazzer-api-1bcea20676364b1edc3c730660cc879334cca172.tar.gz |
tools: Support path patterns in strip_jar
Diffstat (limited to 'bazel')
-rw-r--r-- | bazel/tools/java/com/code_intelligence/jazzer/tools/JarStripper.java | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/bazel/tools/java/com/code_intelligence/jazzer/tools/JarStripper.java b/bazel/tools/java/com/code_intelligence/jazzer/tools/JarStripper.java index 2a567c68..eaaabae5 100644 --- a/bazel/tools/java/com/code_intelligence/jazzer/tools/JarStripper.java +++ b/bazel/tools/java/com/code_intelligence/jazzer/tools/JarStripper.java @@ -21,11 +21,13 @@ import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.PathMatcher; import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.TimeZone; import java.util.stream.Collectors; @@ -49,7 +51,7 @@ public class JarStripper { Path inFile = Paths.get(args[0]); Path outFile = Paths.get(args[1]); - Iterable<String> pathsToDelete = + List<String> rawPathsToDelete = Collections.unmodifiableList(Arrays.stream(args).skip(2).collect(Collectors.toList())); try { @@ -76,18 +78,34 @@ public class JarStripper { TimeZone.setDefault(TimeZone.getTimeZone("UTC")); try (FileSystem zipFs = FileSystems.newFileSystem(outUri, ZIP_FS_PROPERTIES)) { - for (String pathToDelete : pathsToDelete) { - // Visit files before the directory they are contained in by sorting in reverse order. - try (Stream<Path> walk = Files.walk(zipFs.getPath(pathToDelete))) { - Iterable<Path> subpaths = - walk.sorted(Comparator.reverseOrder()).collect(Collectors.toList()); - for (Path subpath : subpaths) { - Files.delete(subpath); + String globPattern = String.format("glob:{%s}", + rawPathsToDelete.stream() + .flatMap(pattern -> { + if (pattern.endsWith("/**")) { + // When removing all contents of a directory, also remove the directory itself. + return Stream.of( + pattern, pattern.substring(0, pattern.length() - "/**".length())); + } else { + return Stream.of(pattern); + } + }) + .collect(Collectors.joining(","))); + PathMatcher pathsToDelete = zipFs.getPathMatcher(globPattern); + try (Stream<Path> walk = Files.walk(zipFs.getPath(""))) { + walk.sorted(Comparator.reverseOrder()).filter(pathsToDelete::matches).forEach(path -> { + try { + Files.delete(path); + } catch (IOException e) { + throw new RuntimeException(e); } - } + }); } - } catch (IOException e) { - e.printStackTrace(); + } catch (Throwable e) { + Throwable throwable = e; + if (throwable instanceof RuntimeException) { + throwable = throwable.getCause(); + } + throwable.printStackTrace(); System.exit(1); } } |