Skip to content
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

Add close() methods throughout the SecureJar->JarContents chain to allow closing the backing file systems #68

Merged
merged 5 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 8 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,7 @@

import org.jetbrains.annotations.ApiStatus;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.util.Collection;
Expand All @@ -18,7 +19,7 @@
* Convert to a full jar with {@link SecureJar#from(JarContents)}.
*/
@ApiStatus.NonExtendable
public interface JarContents {
public interface JarContents extends AutoCloseable {
/**
* @see SecureJar#getPrimaryPath()
*/
Expand Down Expand Up @@ -69,4 +70,10 @@ static JarContents of(Path fileOrFolder) {
static JarContents of(Collection<Path> filesOrFolders) {
return new JarContentsBuilder().paths(filesOrFolders.toArray(new Path[0])).build();
}

/**
* Closes the underlying resources.
*/
@Override
void close() throws IOException;
}
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;
shartte marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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
5 changes: 5 additions & 0 deletions src/main/java/cpw/mods/jarhandling/impl/JarContentsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,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()) {
shartte marked this conversation as resolved.
Show resolved Hide resolved
shartte marked this conversation as resolved.
Show resolved Hide resolved
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() {
}
}
}
Loading