Skip to content

Commit

Permalink
Merge pull request #333 from ascopes/bugfix/GH-327
Browse files Browse the repository at this point in the history
GH-327: Do not attempt to extract files from unknown archive types.
  • Loading branch information
ascopes authored Aug 19, 2024
2 parents 9ab25d2 + 67ba718 commit ef7f383
Showing 1 changed file with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ public ProtoSourceResolver(
this.protoArchiveExtractor = protoArchiveExtractor;
}

public Collection<ProtoFileListing> createProtoFileListings(
Collection<Path> rootPaths,
ProtoFileFilter filter
) {
return rootPaths
.stream()
// GH-132: Normalize to ensure different paths to the same file do not
// get duplicated across more than one extraction site.
.map(FileUtils::normalize)
// GH-132: Avoid running multiple times on the same location.
.distinct()
.map(path -> concurrentExecutor.submit(() -> createProtoFileListing(path, filter)))
.collect(concurrentExecutor.awaiting())
.stream()
.flatMap(Optional::stream)
.collect(Collectors.toUnmodifiableList());
}

public Optional<ProtoFileListing> createProtoFileListing(
Path rootPath,
ProtoFileFilter filter
Expand All @@ -69,7 +87,7 @@ public Optional<ProtoFileListing> createProtoFileListing(
}

if (Files.isRegularFile(rootPath)) {
return protoArchiveExtractor.extractProtoFiles(rootPath, filter);
return createProtoFileListingForArchive(rootPath, filter);
}

try (var stream = Files.walk(rootPath)) {
Expand All @@ -91,21 +109,23 @@ public Optional<ProtoFileListing> createProtoFileListing(
}
}

public Collection<ProtoFileListing> createProtoFileListings(
Collection<Path> rootPaths,
private Optional<ProtoFileListing> createProtoFileListingForArchive(
Path rootPath,
ProtoFileFilter filter
) {
return rootPaths
.stream()
// GH-132: Normalize to ensure different paths to the same file do not
// get duplicated across more than one extraction site.
.map(FileUtils::normalize)
// GH-132: Avoid running multiple times on the same location.
.distinct()
.map(path -> concurrentExecutor.submit(() -> createProtoFileListing(path, filter)))
.collect(concurrentExecutor.awaiting())
.stream()
.flatMap(Optional::stream)
.collect(Collectors.toUnmodifiableList());
) throws IOException {
var extension = FileUtils.getFileExtension(rootPath)
.map(String::toLowerCase)
.orElse("<none>");

switch (extension) {
case ".ear":
case ".jar":
case ".war":
case ".zip":
return protoArchiveExtractor.extractProtoFiles(rootPath, filter);
default:
log.debug("Ignoring unknown archive type at {}", rootPath);
return Optional.empty();
}
}
}

0 comments on commit ef7f383

Please sign in to comment.