Skip to content

Commit

Permalink
SJH 3.0: Replace BiPredicate by UnionPathFilter in UnionFS and hide S…
Browse files Browse the repository at this point in the history
…ecureJar impl details
  • Loading branch information
Technici4n authored and shartte committed Jun 2, 2024
1 parent 503aae8 commit 98f98fb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/main/java/cpw/mods/jarhandling/JarContentsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public JarContentsBuilder pathFilter(@Nullable UnionPathFilter pathFilter) {
* Builds the jar.
*/
public JarContents build() {
return new JarContentsImpl(paths, defaultManifest, pathFilter == null ? null : pathFilter::test);
return new JarContentsImpl(paths, defaultManifest, pathFilter);
}
}
2 changes: 1 addition & 1 deletion src/main/java/cpw/mods/jarhandling/SecureJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ record Provider(String serviceName, List<String> providers) {
/**
* Helper method to parse service provider implementations from a {@link Path}.
*/
public static Provider fromPath(final Path path, final UnionPathFilter pkgFilter) {
public static Provider fromPath(Path path, @Nullable UnionPathFilter pkgFilter) {
final var sname = path.getFileName().toString();
try {
var entries = Files.readAllLines(path).stream()
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/cpw/mods/niofs/union/UnionFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
* Can be instantiated using the public methods in {@link UnionFileSystemProvider}.
*/
public class UnionFileSystem extends FileSystem {
private static final MethodHandle ZIPFS_CH;
private static final MethodHandle FCI_UNINTERUPTIBLE;
Expand Down Expand Up @@ -83,7 +88,7 @@ private static class UncheckedIOException extends java.io.UncheckedIOException {
public UncheckedIOException(final IOException cause) {
super(cause);
}

public UncheckedIOException(final String message, final IOException cause) {
super(message, cause);
}
Expand All @@ -107,6 +112,9 @@ public Path getPrimaryPath() {
return basepaths.get(basepaths.size() - 1);
}

/**
* {@return the filter for this file system, or null if there is none}
*/
@Nullable
public UnionPathFilter getFilesystemFilter() {
return pathFilter;
Expand All @@ -119,7 +127,7 @@ String getKey() {
private record EmbeddedFileSystemMetadata(Path path, FileSystem fs, SeekableByteChannel fsCh) {
}

public UnionFileSystem(final UnionFileSystemProvider provider, @Nullable UnionPathFilter pathFilter, final String key, final Path... basepaths) {
UnionFileSystem(UnionFileSystemProvider provider, @Nullable UnionPathFilter pathFilter, String key, Path... basepaths) {
this.pathFilter = pathFilter;
this.provider = provider;
this.key = key;
Expand Down
36 changes: 15 additions & 21 deletions src/main/java/cpw/mods/niofs/union/UnionFileSystemProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.stream.Stream;

public class UnionFileSystemProvider extends FileSystemProvider {
Expand Down Expand Up @@ -73,24 +72,7 @@ protected Path uriToPath(URI uri) {
*/
@Override
public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) throws IOException {
@SuppressWarnings("unchecked")
var additional = ((Map<String, List<Path>>)env).getOrDefault("additional", List.<Path>of());
@SuppressWarnings("unchecked")
var filter = ((Map<String, UnionPathFilter>)env).getOrDefault("filter", null);

if (filter == null && additional.isEmpty())
throw new IllegalArgumentException("Missing additional and/or filter");

if (filter == null)
filter = (p, b) -> true;

var path = uriToPath(uri);
var key = makeKey(path);
try {
return newFileSystemInternal(key, filter, Stream.concat(Stream.of(path), additional.stream()).toArray(Path[]::new));
} catch (UncheckedIOException e) {
throw e.getCause();
}
return newFileSystem(uriToPath(uri), env);
}

/**
Expand All @@ -105,8 +87,7 @@ public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) throws
public FileSystem newFileSystem(final Path path, final Map<String, ?> env) throws IOException {
@SuppressWarnings("unchecked")
var additional = ((Map<String, List<Path>>)env).getOrDefault("additional", List.<Path>of());
@SuppressWarnings("unchecked")
var filter = ((Map<String, UnionPathFilter>)env).getOrDefault("filter", null);
var filter = readFilterFromEnv(env);

if (filter == null && additional.isEmpty())
throw new UnsupportedOperationException("Missing additional and/or filter");
Expand All @@ -119,6 +100,19 @@ public FileSystem newFileSystem(final Path path, final Map<String, ?> env) throw
}
}

@Nullable
private static UnionPathFilter readFilterFromEnv(Map<String, ?> env) {
Object filter = env.get("filter");

if (filter == null) {
return null;
} else if (filter instanceof UnionPathFilter unionPathFilter) {
return unionPathFilter;
} else {
throw new IllegalArgumentException("Unknown type for \"filter\" UnionFileSystem env var: " + filter.getClass().getName());
}
}

public UnionFileSystem newFileSystem(@Nullable UnionPathFilter pathfilter, final Path... paths) {
if (paths.length == 0) throw new IllegalArgumentException("Need at least one path");
var key = makeKey(paths[0]);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

module cpw.mods.securejarhandler {
exports cpw.mods.jarhandling;
exports cpw.mods.jarhandling.impl; // TODO - Bump version, and remove this export, you don't need our implementation
exports cpw.mods.cl;
exports cpw.mods.niofs.union;
requires jdk.unsupported;
Expand Down

0 comments on commit 98f98fb

Please sign in to comment.