Skip to content
Open
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
58 changes: 29 additions & 29 deletions src/main/java/org/codehaus/plexus/archiver/AbstractArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.attribute.FileTime;
Expand Down Expand Up @@ -473,6 +472,9 @@ public boolean hasNext() {

try {
ioResourceIter = currentResourceCollection.resources.getResources();
// Register ownership before reading, since iteration can fail before
// exhaustion.
addCloseable(ioResourceIter);
} catch (final IOException e) {
throw new ArchiverException(e.getMessage(), e);
}
Expand All @@ -487,12 +489,6 @@ public boolean hasNext() {
final PlexusIoResource resource = (PlexusIoResource) ioResourceIter.next();
nextEntry = asArchiveEntry(currentResourceCollection, resource);
} else {
// this will leak handles in the IO iterator if the iterator is not fully consumed.
// alternately we'd have to make this method return a Closeable iterator back
// to the client and ditch the whole issue onto the client.
// this does not really make any sense either, might equally well change the
// api into something that is not broken by design.
addCloseable(ioResourceIter);
ioResourceIter = null;
}
}
Expand Down Expand Up @@ -552,20 +548,19 @@ private String normalizedForDuplicateCheck(ArchiveEntry entry) {
};
}

/** Reaches the owning collection through permission and proxy wrappers before releasing its resources. */
private static void closeIfCloseable(Object resource) throws IOException {
if (resource instanceof AddedResourceCollection collection) {
resource = collection.resources;
}
while (resource instanceof PlexusIoProxyResourceCollection collection) {
resource = collection.getSrc();
}
if (resource instanceof Closeable closeable) {
closeable.close();
}
}

private static void closeQuietlyIfCloseable(Object resource) {
try {
closeIfCloseable(resource);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
public File getDestFile() {
return destFile;
Expand Down Expand Up @@ -832,25 +827,30 @@ private void addCloseable(Object maybeCloseable) {
}
}

private void closeIterators() {
for (Closeable closeable : closeables) {
closeQuietlyIfCloseable(closeable);
}
}

protected abstract void close() throws IOException;

/** Releases iterators and owning collections on success or failure, attempting every registered close. */
protected void cleanUp() throws IOException {
closeIterators();

for (Object resource : resources) {
if (resource instanceof PlexusIoProxyResourceCollection collection) {
resource = collection.getSrc();
List<Object> pending = new ArrayList<>(closeables);
pending.addAll(resources);
// Detach ownership before closing so a failure cannot retain readers across archive operations.
closeables.clear();
resources.clear();
IOException failure = null;
for (Object resource : pending) {
try {
closeIfCloseable(resource);
} catch (IOException e) {
if (failure == null) {
failure = e;
} else if (failure != e) {
failure.addSuppressed(e);
}
}

closeIfCloseable(resource);
}
resources.clear();
if (failure != null) {
throw failure;
}
}

protected abstract void execute() throws ArchiverException, IOException;
Expand Down
Loading