From 67ba7183dcd8c380e02f54a11273ef17940b0744 Mon Sep 17 00:00:00 2001 From: Ashley <73482956+ascopes@users.noreply.github.com> Date: Mon, 19 Aug 2024 13:10:06 +0100 Subject: [PATCH] GH-327: Do not attempt to extract files from unknown archive types. --- .../sources/ProtoSourceResolver.java | 52 +++++++++++++------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/protobuf-maven-plugin/src/main/java/io/github/ascopes/protobufmavenplugin/sources/ProtoSourceResolver.java b/protobuf-maven-plugin/src/main/java/io/github/ascopes/protobufmavenplugin/sources/ProtoSourceResolver.java index 00029a67..e733dce9 100644 --- a/protobuf-maven-plugin/src/main/java/io/github/ascopes/protobufmavenplugin/sources/ProtoSourceResolver.java +++ b/protobuf-maven-plugin/src/main/java/io/github/ascopes/protobufmavenplugin/sources/ProtoSourceResolver.java @@ -59,6 +59,24 @@ public ProtoSourceResolver( this.protoArchiveExtractor = protoArchiveExtractor; } + public Collection createProtoFileListings( + Collection 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 createProtoFileListing( Path rootPath, ProtoFileFilter filter @@ -69,7 +87,7 @@ public Optional createProtoFileListing( } if (Files.isRegularFile(rootPath)) { - return protoArchiveExtractor.extractProtoFiles(rootPath, filter); + return createProtoFileListingForArchive(rootPath, filter); } try (var stream = Files.walk(rootPath)) { @@ -91,21 +109,23 @@ public Optional createProtoFileListing( } } - public Collection createProtoFileListings( - Collection rootPaths, + private Optional 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(""); + + 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(); + } } }