Skip to content

Commit 735b625

Browse files
committed
SJH 3.0: Replace BiPredicate by UnionPathFilter in UnionFS and hide SecureJar impl details
1 parent 420e984 commit 735b625

File tree

7 files changed

+33
-33
lines changed

7 files changed

+33
-33
lines changed

src/main/java/cpw/mods/jarhandling/JarContentsBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ public JarContentsBuilder pathFilter(@Nullable UnionPathFilter pathFilter) {
5353
* Builds the jar.
5454
*/
5555
public JarContents build() {
56-
return new JarContentsImpl(paths, defaultManifest, pathFilter == null ? null : pathFilter::test);
56+
return new JarContentsImpl(paths, defaultManifest, pathFilter);
5757
}
5858
}

src/main/java/cpw/mods/jarhandling/SecureJar.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cpw.mods.jarhandling.impl.Jar;
44
import cpw.mods.jarhandling.impl.JarContentsImpl;
5+
import cpw.mods.niofs.union.UnionPathFilter;
56
import org.jetbrains.annotations.Nullable;
67

78
import java.io.IOException;
@@ -103,7 +104,7 @@ record Provider(String serviceName, List<String> providers) {
103104
/**
104105
* Helper method to parse service provider implementations from a {@link Path}.
105106
*/
106-
public static Provider fromPath(final Path path, final BiPredicate<String, String> pkgFilter) {
107+
public static Provider fromPath(Path path, @Nullable UnionPathFilter pkgFilter) {
107108
final var sname = path.getFileName().toString();
108109
try {
109110
var entries = Files.readAllLines(path).stream()

src/main/java/cpw/mods/jarhandling/impl/Jar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class Jar implements SecureJar {
3232

3333
@Deprecated(forRemoval = true, since = "2.1.16")
3434
public Jar(final Supplier<Manifest> defaultManifest, final Function<SecureJar, JarMetadata> metadataFunction, final BiPredicate<String, String> pathfilter, final Path... paths) {
35-
this.contents = new JarContentsImpl(paths, defaultManifest, pathfilter);
35+
this.contents = new JarContentsImpl(paths, defaultManifest, pathfilter == null ? null : pathfilter::test);
3636
this.manifest = contents.getManifest();
3737
this.signingData = contents.signingData;
3838
this.filesystem = contents.filesystem;

src/main/java/cpw/mods/jarhandling/impl/JarContentsImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import cpw.mods.jarhandling.SecureJar;
55
import cpw.mods.niofs.union.UnionFileSystem;
66
import cpw.mods.niofs.union.UnionFileSystemProvider;
7+
import cpw.mods.niofs.union.UnionPathFilter;
78
import org.jetbrains.annotations.Nullable;
89

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

51-
public JarContentsImpl(Path[] paths, Supplier<Manifest> defaultManifest, @Nullable BiPredicate<String, String> pathFilter) {
52+
public JarContentsImpl(Path[] paths, Supplier<Manifest> defaultManifest, @Nullable UnionPathFilter pathFilter) {
5253
var validPaths = Arrays.stream(paths).filter(Files::exists).toArray(Path[]::new);
5354
if (validPaths.length == 0)
5455
throw new UncheckedIOException(new IOException("Invalid paths argument, contained no existing paths: " + Arrays.toString(paths)));
@@ -202,7 +203,7 @@ public List<SecureJar.Provider> getMetaInfServices() {
202203
if (Files.exists(services)) {
203204
try (var walk = Files.walk(services)) {
204205
this.providers = walk.filter(path->!Files.isDirectory(path))
205-
.map((Path path1) -> SecureJar.Provider.fromPath(path1, filesystem.getFilesystemFilter()))
206+
.map((Path path1) -> SecureJar.Provider.fromPath(path1, filesystem.getFileSystemFilter()))
206207
.toList();
207208
} catch (IOException e) {
208209
throw new UncheckedIOException(e);

src/main/java/cpw/mods/niofs/union/UnionFileSystem.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@
3434
import java.util.Set;
3535
import java.util.Spliterator;
3636
import java.util.Spliterators;
37-
import java.util.function.BiPredicate;
3837
import java.util.function.Function;
3938
import java.util.stream.Collectors;
4039
import java.util.stream.IntStream;
4140
import java.util.stream.Stream;
4241
import java.util.stream.StreamSupport;
4342

43+
/**
44+
* Can be instantiated using the public methods in {@link UnionFileSystemProvider}.
45+
*/
4446
public class UnionFileSystem extends FileSystem {
4547
private static final MethodHandle ZIPFS_EXISTS;
4648
private static final MethodHandle ZIPFS_CH;
@@ -104,15 +106,18 @@ public synchronized Throwable fillInStackTrace() {
104106
private final List<Path> basepaths;
105107
private final int lastElementIndex;
106108
@Nullable
107-
private final BiPredicate<String, String> pathFilter;
109+
private final UnionPathFilter pathFilter;
108110
private final Map<Path, EmbeddedFileSystemMetadata> embeddedFileSystems;
109111

110112
public Path getPrimaryPath() {
111113
return basepaths.get(basepaths.size() - 1);
112114
}
113115

116+
/**
117+
* {@return the filter for this file system, or null if there is none}
118+
*/
114119
@Nullable
115-
public BiPredicate<String, String> getFilesystemFilter() {
120+
public UnionPathFilter getFileSystemFilter() {
116121
return pathFilter;
117122
}
118123

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

126-
public UnionFileSystem(final UnionFileSystemProvider provider, @Nullable BiPredicate<String, String> pathFilter, final String key, final Path... basepaths) {
131+
UnionFileSystem(UnionFileSystemProvider provider, @Nullable UnionPathFilter pathFilter, String key, Path... basepaths) {
127132
this.pathFilter = pathFilter;
128133
this.provider = provider;
129134
this.key = key;

src/main/java/cpw/mods/niofs/union/UnionFileSystemProvider.java

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.nio.file.attribute.*;
1212
import java.nio.file.spi.FileSystemProvider;
1313
import java.util.*;
14-
import java.util.function.BiPredicate;
1514
import java.util.stream.Stream;
1615

1716
public class UnionFileSystemProvider extends FileSystemProvider {
@@ -55,24 +54,7 @@ protected Path uriToPath(URI uri) {
5554
*/
5655
@Override
5756
public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) throws IOException {
58-
@SuppressWarnings("unchecked")
59-
var additional = ((Map<String, List<Path>>)env).getOrDefault("additional", List.<Path>of());
60-
@SuppressWarnings("unchecked")
61-
var filter = ((Map<String, BiPredicate<String, String>>)env).getOrDefault("filter", null);
62-
63-
if (filter == null && additional.isEmpty())
64-
throw new IllegalArgumentException("Missing additional and/or filter");
65-
66-
if (filter == null)
67-
filter = (p, b) -> true;
68-
69-
var path = uriToPath(uri);
70-
var key = makeKey(path);
71-
try {
72-
return newFileSystemInternal(key, filter, Stream.concat(Stream.of(path), additional.stream()).toArray(Path[]::new));
73-
} catch (UncheckedIOException e) {
74-
throw e.getCause();
75-
}
57+
return newFileSystem(uriToPath(uri), env);
7658
}
7759

7860
/**
@@ -87,8 +69,7 @@ public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) throws
8769
public FileSystem newFileSystem(final Path path, final Map<String, ?> env) throws IOException {
8870
@SuppressWarnings("unchecked")
8971
var additional = ((Map<String, List<Path>>)env).getOrDefault("additional", List.<Path>of());
90-
@SuppressWarnings("unchecked")
91-
var filter = ((Map<String, BiPredicate<String, String>>)env).getOrDefault("filter", null);
72+
var filter = readFilterFromEnv(env);
9273

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

104-
public UnionFileSystem newFileSystem(@Nullable BiPredicate<String, String> pathfilter, final Path... paths) {
85+
@Nullable
86+
private static UnionPathFilter readFilterFromEnv(Map<String, ?> env) {
87+
Object filter = env.get("filter");
88+
89+
if (filter == null) {
90+
return null;
91+
} else if (filter instanceof UnionPathFilter unionPathFilter) {
92+
return unionPathFilter;
93+
} else {
94+
throw new IllegalArgumentException("Unknown type for \"filter\" UnionFileSystem env var: " + filter.getClass().getName());
95+
}
96+
}
97+
98+
public UnionFileSystem newFileSystem(@Nullable UnionPathFilter pathfilter, final Path... paths) {
10599
if (paths.length == 0) throw new IllegalArgumentException("Need at least one path");
106100
var key = makeKey(paths[0]);
107101
return newFileSystemInternal(key, pathfilter, paths);
108102
}
109103

110-
private UnionFileSystem newFileSystemInternal(final String key, @Nullable BiPredicate<String, String> pathfilter, final Path... paths) {
104+
private UnionFileSystem newFileSystemInternal(final String key, @Nullable UnionPathFilter pathfilter, final Path... paths) {
111105
var normpaths = Arrays.stream(paths)
112106
.map(Path::toAbsolutePath)
113107
.map(Path::normalize)

src/main/java/module-info.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

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

0 commit comments

Comments
 (0)