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 committed Nov 18, 2023
1 parent 420e984 commit 735b625
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 33 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);
}
}
3 changes: 2 additions & 1 deletion src/main/java/cpw/mods/jarhandling/SecureJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cpw.mods.jarhandling.impl.Jar;
import cpw.mods.jarhandling.impl.JarContentsImpl;
import cpw.mods.niofs.union.UnionPathFilter;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
Expand Down Expand Up @@ -103,7 +104,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 BiPredicate<String, String> 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
2 changes: 1 addition & 1 deletion src/main/java/cpw/mods/jarhandling/impl/Jar.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Jar implements SecureJar {

@Deprecated(forRemoval = true, since = "2.1.16")
public Jar(final Supplier<Manifest> defaultManifest, final Function<SecureJar, JarMetadata> metadataFunction, final BiPredicate<String, String> pathfilter, final Path... paths) {
this.contents = new JarContentsImpl(paths, defaultManifest, pathfilter);
this.contents = new JarContentsImpl(paths, defaultManifest, pathfilter == null ? null : pathfilter::test);
this.manifest = contents.getManifest();
this.signingData = contents.signingData;
this.filesystem = contents.filesystem;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/cpw/mods/jarhandling/impl/JarContentsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cpw.mods.jarhandling.SecureJar;
import cpw.mods.niofs.union.UnionFileSystem;
import cpw.mods.niofs.union.UnionFileSystemProvider;
import cpw.mods.niofs.union.UnionPathFilter;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
Expand Down Expand Up @@ -48,7 +49,7 @@ public class JarContentsImpl implements JarContents {
// Cache for repeated getMetaInfServices calls
private List<SecureJar.Provider> providers;

public JarContentsImpl(Path[] paths, Supplier<Manifest> defaultManifest, @Nullable BiPredicate<String, String> pathFilter) {
public JarContentsImpl(Path[] paths, Supplier<Manifest> defaultManifest, @Nullable UnionPathFilter pathFilter) {
var validPaths = Arrays.stream(paths).filter(Files::exists).toArray(Path[]::new);
if (validPaths.length == 0)
throw new UncheckedIOException(new IOException("Invalid paths argument, contained no existing paths: " + Arrays.toString(paths)));
Expand Down Expand Up @@ -202,7 +203,7 @@ public List<SecureJar.Provider> getMetaInfServices() {
if (Files.exists(services)) {
try (var walk = Files.walk(services)) {
this.providers = walk.filter(path->!Files.isDirectory(path))
.map((Path path1) -> SecureJar.Provider.fromPath(path1, filesystem.getFilesystemFilter()))
.map((Path path1) -> SecureJar.Provider.fromPath(path1, filesystem.getFileSystemFilter()))
.toList();
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/cpw/mods/niofs/union/UnionFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.BiPredicate;
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_EXISTS;
private static final MethodHandle ZIPFS_CH;
Expand Down Expand Up @@ -104,15 +106,18 @@ public synchronized Throwable fillInStackTrace() {
private final List<Path> basepaths;
private final int lastElementIndex;
@Nullable
private final BiPredicate<String, String> pathFilter;
private final UnionPathFilter pathFilter;
private final Map<Path, EmbeddedFileSystemMetadata> embeddedFileSystems;

public Path getPrimaryPath() {
return basepaths.get(basepaths.size() - 1);
}

/**
* {@return the filter for this file system, or null if there is none}
*/
@Nullable
public BiPredicate<String, String> getFilesystemFilter() {
public UnionPathFilter getFileSystemFilter() {
return pathFilter;
}

Expand All @@ -123,7 +128,7 @@ String getKey() {
private record EmbeddedFileSystemMetadata(Path path, FileSystem fs, SeekableByteChannel fsCh) {
}

public UnionFileSystem(final UnionFileSystemProvider provider, @Nullable BiPredicate<String, String> 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
40 changes: 17 additions & 23 deletions src/main/java/cpw/mods/niofs/union/UnionFileSystemProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.nio.file.attribute.*;
import java.nio.file.spi.FileSystemProvider;
import java.util.*;
import java.util.function.BiPredicate;
import java.util.stream.Stream;

public class UnionFileSystemProvider extends FileSystemProvider {
Expand Down Expand Up @@ -55,24 +54,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, BiPredicate<String, String>>)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 @@ -87,8 +69,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, BiPredicate<String, String>>)env).getOrDefault("filter", null);
var filter = readFilterFromEnv(env);

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

public UnionFileSystem newFileSystem(@Nullable BiPredicate<String, String> pathfilter, final Path... paths) {
@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]);
return newFileSystemInternal(key, pathfilter, paths);
}

private UnionFileSystem newFileSystemInternal(final String key, @Nullable BiPredicate<String, String> pathfilter, final Path... paths) {
private UnionFileSystem newFileSystemInternal(final String key, @Nullable UnionPathFilter pathfilter, final Path... paths) {
var normpaths = Arrays.stream(paths)
.map(Path::toAbsolutePath)
.map(Path::normalize)
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 @@ -4,7 +4,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 735b625

Please sign in to comment.