Skip to content

Commit

Permalink
Add close() methods throughout the SecureJar->JarContents chain to al…
Browse files Browse the repository at this point in the history
…low closing the backing file systems (#68)
  • Loading branch information
shartte authored Jun 2, 2024
1 parent f970393 commit 503aae8
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/main/java/cpw/mods/jarhandling/JarContents.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.jetbrains.annotations.ApiStatus;

import java.io.Closeable;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.util.Collection;
Expand All @@ -18,7 +20,7 @@
* Convert to a full jar with {@link SecureJar#from(JarContents)}.
*/
@ApiStatus.NonExtendable
public interface JarContents {
public interface JarContents extends Closeable {
/**
* @see SecureJar#getPrimaryPath()
*/
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/cpw/mods/jarhandling/SecureJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
import java.security.CodeSigner;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

Expand Down Expand Up @@ -86,6 +82,12 @@ static SecureJar from(JarContents contents, JarMetadata metadata) {
*/
Path getRootPath();

/**
* Closes the underlying file system resources (if any).
* Renders this object unusable.
*/
void close() throws IOException;

/**
* All the functions that are necessary to turn a {@link SecureJar} into a module.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/cpw/mods/jarhandling/VirtualJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cpw.mods.niofs.union.UnionFileSystemProvider;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.lang.module.ModuleDescriptor;
import java.net.URI;
Expand Down Expand Up @@ -106,6 +107,11 @@ public Path getRootPath() {
return dummyFileSystem.getRoot();
}

@Override
public void close() throws IOException {
dummyFileSystem.close();
}

private class VirtualJarModuleDataProvider implements ModuleDataProvider {
@Override
public String name() {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/cpw/mods/jarhandling/impl/Jar.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import cpw.mods.jarhandling.JarMetadata;
import cpw.mods.jarhandling.SecureJar;
import cpw.mods.niofs.union.UnionFileSystem;
import cpw.mods.niofs.union.UnionPathFilter;
import cpw.mods.util.LambdaExceptionUtils;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.lang.module.ModuleDescriptor;
import java.net.URI;
Expand Down Expand Up @@ -104,6 +104,11 @@ public Path getRootPath() {
return filesystem.getPath("");
}

@Override
public void close() throws IOException {
contents.close();
}

@Override
public String toString() {
return "Jar[" + getURI() + "]";
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/cpw/mods/jarhandling/impl/JarContentsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Supplier;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
Expand Down Expand Up @@ -218,4 +217,9 @@ public List<SecureJar.Provider> getMetaInfServices() {
}
return this.providers;
}

@Override
public void close() throws IOException {
filesystem.close();
}
}
17 changes: 16 additions & 1 deletion src/main/java/cpw/mods/niofs/union/UnionFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,23 @@ public UnionFileSystemProvider provider() {
}

@Override
public void close() {
public void close() throws IOException {
provider().removeFileSystem(this);
IOException closeException = null;
for (var embeddedFs : embeddedFileSystems.values()) {
try {
embeddedFs.fs.close();
} catch (IOException e) {
if (closeException != null) {
closeException.addSuppressed(e);
} else {
closeException = e;
}
}
}
if (closeException != null) {
throw closeException;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
import java.lang.module.Configuration;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.lang.module.ResolvedModule;
import java.net.URI;
import java.nio.file.Path;
import java.security.CodeSigner;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -133,5 +131,9 @@ public Path getPath(final String first, final String... rest) {
public Path getRootPath() {
return null;
}

@Override
public void close() {
}
}
}
4 changes: 4 additions & 0 deletions src/test/java/cpw/mods/jarhandling/impl/TestMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,9 @@ public Set<String> getPackagesExcluding(String... excludedRootPackages) {
public List<SecureJar.Provider> getMetaInfServices() {
return List.of();
}

@Override
public void close() {
}
}
}

0 comments on commit 503aae8

Please sign in to comment.