-
Notifications
You must be signed in to change notification settings - Fork 0
fix: enhance metadata handling and extraction process, improve test c… #47
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package dev.ayagmar.quarkusforge.api; | ||
|
|
||
| public enum MetadataSource { | ||
| LIVE("live"), | ||
| SNAPSHOT("bundled snapshot"), | ||
| CACHE("cache"); | ||
|
|
||
| private final String label; | ||
|
|
||
| MetadataSource(String label) { | ||
| this.label = label; | ||
| } | ||
|
|
||
| public String label() { | ||
| return label; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,8 @@ | |
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.CancellationException; | ||
| import java.util.function.BooleanSupplier; | ||
| import java.util.zip.ZipEntry; | ||
| import java.util.zip.ZipInputStream; | ||
|
|
||
|
|
@@ -37,9 +39,19 @@ public SafeZipExtractor(ArchiveSafetyPolicy safetyPolicy) { | |
|
|
||
| public ExtractionResult extract( | ||
| Path zipFile, Path outputDirectory, OverwritePolicy overwritePolicy) { | ||
| return extract(zipFile, outputDirectory, overwritePolicy, () -> false); | ||
| } | ||
|
|
||
| public ExtractionResult extract( | ||
| Path zipFile, | ||
| Path outputDirectory, | ||
| OverwritePolicy overwritePolicy, | ||
| BooleanSupplier cancelled) { | ||
| Objects.requireNonNull(zipFile); | ||
| Objects.requireNonNull(outputDirectory); | ||
| Objects.requireNonNull(overwritePolicy); | ||
| Objects.requireNonNull(cancelled); | ||
|
Comment on lines
+45
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fail fast when
💡 Minimal fix Objects.requireNonNull(zipFile);
Objects.requireNonNull(outputDirectory);
Objects.requireNonNull(overwritePolicy);
Objects.requireNonNull(cancelled);
+ throwIfCancelled(cancelled);
Map<String, ZipEntryMetadata> metadataByEntry = ZipCentralDirectoryReader.read(zipFile);🤖 Prompt for AI Agents |
||
| throwIfCancelled(cancelled); | ||
|
|
||
| Map<String, ZipEntryMetadata> metadataByEntry = ZipCentralDirectoryReader.read(zipFile); | ||
| validateEntryMetadata(metadataByEntry); | ||
|
|
@@ -65,7 +77,11 @@ public ExtractionResult extract( | |
| Path stagingContent = stagingRoot.resolve("content"); | ||
| try { | ||
| Files.createDirectories(stagingContent); | ||
| ExtractionResult stagedResult = extractIntoStaging(zipFile, stagingContent, metadataByEntry); | ||
| ExtractionResult stagedResult = | ||
| extractIntoStaging(zipFile, stagingContent, metadataByEntry, cancelled); | ||
| if (cancelled.getAsBoolean()) { | ||
| throw new CancellationException("Generation cancelled during extraction"); | ||
| } | ||
| Path extractedRoot = stagedResult.extractedRoot(); | ||
| applyOverwritePolicy(extractedRoot, absoluteTarget, overwritePolicy); | ||
| return new ExtractionResult( | ||
|
|
@@ -129,7 +145,10 @@ private void validateEntryMetadata(Map<String, ZipEntryMetadata> metadataByEntry | |
| } | ||
|
|
||
| private ExtractionResult extractIntoStaging( | ||
| Path zipFile, Path stagingContent, Map<String, ZipEntryMetadata> metadataByEntry) | ||
| Path zipFile, | ||
| Path stagingContent, | ||
| Map<String, ZipEntryMetadata> metadataByEntry, | ||
| BooleanSupplier cancelled) | ||
| throws IOException { | ||
| Set<String> seenEntries = new LinkedHashSet<>(); | ||
| Set<String> topLevelSegments = new LinkedHashSet<>(); | ||
|
|
@@ -139,6 +158,7 @@ private ExtractionResult extractIntoStaging( | |
| try (ZipInputStream zipInputStream = new ZipInputStream(Files.newInputStream(zipFile))) { | ||
| ZipEntry zipEntry; | ||
| while ((zipEntry = zipInputStream.getNextEntry()) != null) { | ||
| throwIfCancelled(cancelled); | ||
| String normalizedName = normalizeEntryName(zipEntry.getName()); | ||
| ZipEntryMetadata metadata = metadataByEntry.get(normalizedName); | ||
| if (metadata == null) { | ||
|
|
@@ -176,7 +196,8 @@ private ExtractionResult extractIntoStaging( | |
| destination, | ||
| normalizedName, | ||
| metadata.uncompressedSize(), | ||
| extractedBytes); | ||
| extractedBytes, | ||
| cancelled); | ||
| long copied = copyResult.entryBytes(); | ||
| extractedBytes = copyResult.totalExtractedBytes(); | ||
|
|
||
|
|
@@ -255,7 +276,8 @@ private CopyResult copyEntry( | |
| Path destination, | ||
| String entryName, | ||
| long expectedUncompressedSize, | ||
| long extractedBytesSoFar) | ||
| long extractedBytesSoFar, | ||
| BooleanSupplier cancelled) | ||
| throws IOException { | ||
| long copied = 0L; | ||
| long totalExtractedBytes = extractedBytesSoFar; | ||
|
|
@@ -265,6 +287,7 @@ private CopyResult copyEntry( | |
| destination, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) { | ||
| int read; | ||
| while ((read = inputStream.read(buffer)) != -1) { | ||
| throwIfCancelled(cancelled); | ||
| long nextCopied = safeAdd(copied, read); | ||
| if (nextCopied > expectedUncompressedSize) { | ||
| throw new ArchiveException( | ||
|
|
@@ -290,6 +313,12 @@ private CopyResult copyEntry( | |
| return new CopyResult(copied, totalExtractedBytes); | ||
| } | ||
|
|
||
| private static void throwIfCancelled(BooleanSupplier cancelled) { | ||
| if (cancelled.getAsBoolean()) { | ||
| throw new CancellationException("Generation cancelled during extraction"); | ||
| } | ||
| } | ||
|
|
||
| static String normalizeEntryName(String rawName) { | ||
| if (rawName == null || rawName.isBlank()) { | ||
| throw new ArchiveException("ZIP entry name must not be blank"); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.