Skip to content

Commit 98f98fb

Browse files
Technici4nshartte
authored andcommitted
SJH 3.0: Replace BiPredicate by UnionPathFilter in UnionFS and hide SecureJar impl details
1 parent 503aae8 commit 98f98fb

File tree

5 files changed

+27
-26
lines changed

5 files changed

+27
-26
lines changed

Diff for: src/main/java/cpw/mods/jarhandling/JarContentsBuilder.java

+1-1
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
}

Diff for: src/main/java/cpw/mods/jarhandling/SecureJar.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ record Provider(String serviceName, List<String> providers) {
138138
/**
139139
* Helper method to parse service provider implementations from a {@link Path}.
140140
*/
141-
public static Provider fromPath(final Path path, final UnionPathFilter pkgFilter) {
141+
public static Provider fromPath(Path path, @Nullable UnionPathFilter pkgFilter) {
142142
final var sname = path.getFileName().toString();
143143
try {
144144
var entries = Files.readAllLines(path).stream()

Diff for: src/main/java/cpw/mods/niofs/union/UnionFileSystem.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@
3131
import java.util.Map;
3232
import java.util.Optional;
3333
import java.util.Set;
34+
import java.util.Spliterator;
35+
import java.util.Spliterators;
3436
import java.util.function.Function;
3537
import java.util.stream.Collectors;
3638
import java.util.stream.IntStream;
3739
import java.util.stream.Stream;
3840
import java.util.stream.StreamSupport;
3941

42+
/**
43+
* Can be instantiated using the public methods in {@link UnionFileSystemProvider}.
44+
*/
4045
public class UnionFileSystem extends FileSystem {
4146
private static final MethodHandle ZIPFS_CH;
4247
private static final MethodHandle FCI_UNINTERUPTIBLE;
@@ -83,7 +88,7 @@ private static class UncheckedIOException extends java.io.UncheckedIOException {
8388
public UncheckedIOException(final IOException cause) {
8489
super(cause);
8590
}
86-
91+
8792
public UncheckedIOException(final String message, final IOException cause) {
8893
super(message, cause);
8994
}
@@ -107,6 +112,9 @@ public Path getPrimaryPath() {
107112
return basepaths.get(basepaths.size() - 1);
108113
}
109114

115+
/**
116+
* {@return the filter for this file system, or null if there is none}
117+
*/
110118
@Nullable
111119
public UnionPathFilter getFilesystemFilter() {
112120
return pathFilter;
@@ -119,7 +127,7 @@ String getKey() {
119127
private record EmbeddedFileSystemMetadata(Path path, FileSystem fs, SeekableByteChannel fsCh) {
120128
}
121129

122-
public UnionFileSystem(final UnionFileSystemProvider provider, @Nullable UnionPathFilter pathFilter, final String key, final Path... basepaths) {
130+
UnionFileSystem(UnionFileSystemProvider provider, @Nullable UnionPathFilter pathFilter, String key, Path... basepaths) {
123131
this.pathFilter = pathFilter;
124132
this.provider = provider;
125133
this.key = key;

Diff for: src/main/java/cpw/mods/niofs/union/UnionFileSystemProvider.java

+15-21
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.List;
3030
import java.util.Map;
3131
import java.util.Set;
32-
import java.util.function.BiPredicate;
3332
import java.util.stream.Stream;
3433

3534
public class UnionFileSystemProvider extends FileSystemProvider {
@@ -73,24 +72,7 @@ protected Path uriToPath(URI uri) {
7372
*/
7473
@Override
7574
public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) throws IOException {
76-
@SuppressWarnings("unchecked")
77-
var additional = ((Map<String, List<Path>>)env).getOrDefault("additional", List.<Path>of());
78-
@SuppressWarnings("unchecked")
79-
var filter = ((Map<String, UnionPathFilter>)env).getOrDefault("filter", null);
80-
81-
if (filter == null && additional.isEmpty())
82-
throw new IllegalArgumentException("Missing additional and/or filter");
83-
84-
if (filter == null)
85-
filter = (p, b) -> true;
86-
87-
var path = uriToPath(uri);
88-
var key = makeKey(path);
89-
try {
90-
return newFileSystemInternal(key, filter, Stream.concat(Stream.of(path), additional.stream()).toArray(Path[]::new));
91-
} catch (UncheckedIOException e) {
92-
throw e.getCause();
93-
}
75+
return newFileSystem(uriToPath(uri), env);
9476
}
9577

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

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

103+
@Nullable
104+
private static UnionPathFilter readFilterFromEnv(Map<String, ?> env) {
105+
Object filter = env.get("filter");
106+
107+
if (filter == null) {
108+
return null;
109+
} else if (filter instanceof UnionPathFilter unionPathFilter) {
110+
return unionPathFilter;
111+
} else {
112+
throw new IllegalArgumentException("Unknown type for \"filter\" UnionFileSystem env var: " + filter.getClass().getName());
113+
}
114+
}
115+
122116
public UnionFileSystem newFileSystem(@Nullable UnionPathFilter pathfilter, final Path... paths) {
123117
if (paths.length == 0) throw new IllegalArgumentException("Need at least one path");
124118
var key = makeKey(paths[0]);

Diff for: src/main/java/module-info.java

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

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

0 commit comments

Comments
 (0)