Skip to content

Commit 312eb11

Browse files
sharttemarchermans
andauthored
Pass basePath as Path during path filtering (#70)
We're using the path filter mostly for filtering out resources / class from Minecraft jars. The basePath is important to unconditionally include certain resources from specific basepaths of the union filesystem (client-extra jar). Not having to do string matching on the basepath makes this more robust. p.s.: This change is also motivated by SJH normalizing the base path such that it becomes a relative path, but only on Linux. So, `/tmp/blah.jar` becomes `tmp/blah.jar`. Co-authored-by: Marc Hermans <marc.hermans@ldtteam.com>
1 parent 5daff13 commit 312eb11

6 files changed

Lines changed: 19 additions & 18 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
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;
@@ -135,13 +136,13 @@ record Provider(String serviceName, List<String> providers) {
135136
/**
136137
* Helper method to parse service provider implementations from a {@link Path}.
137138
*/
138-
public static Provider fromPath(final Path path, final BiPredicate<String, String> pkgFilter) {
139+
public static Provider fromPath(final Path path, final UnionPathFilter pkgFilter) {
139140
final var sname = path.getFileName().toString();
140141
try {
141142
var entries = Files.readAllLines(path).stream()
142143
.map(String::trim)
143144
.filter(l-> !l.isEmpty() && !l.startsWith("#")) // We support comments :)
144-
.filter(p-> pkgFilter == null || pkgFilter.test(p.replace('.','/'), ""))
145+
.filter(p-> pkgFilter == null || pkgFilter.test(p.replace('.','/'), path.getRoot()))
145146
.toList();
146147
return new Provider(sname, entries);
147148
} catch (IOException e) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cpw.mods.jarhandling.JarMetadata;
44
import cpw.mods.jarhandling.SecureJar;
55
import cpw.mods.niofs.union.UnionFileSystem;
6+
import cpw.mods.niofs.union.UnionPathFilter;
67
import cpw.mods.util.LambdaExceptionUtils;
78
import org.jetbrains.annotations.Nullable;
89

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

Lines changed: 2 additions & 1 deletion
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;
@@ -49,7 +50,7 @@ public class JarContentsImpl implements JarContents {
4950
// Cache for repeated getMetaInfServices calls
5051
private List<SecureJar.Provider> providers;
5152

52-
public JarContentsImpl(Path[] paths, Supplier<Manifest> defaultManifest, @Nullable BiPredicate<String, String> pathFilter) {
53+
public JarContentsImpl(Path[] paths, Supplier<Manifest> defaultManifest, @Nullable UnionPathFilter pathFilter) {
5354
var validPaths = Arrays.stream(paths).filter(Files::exists).toArray(Path[]::new);
5455
if (validPaths.length == 0)
5556
throw new UncheckedIOException(new IOException("Invalid paths argument, contained no existing paths: " + Arrays.toString(paths)));

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.util.Map;
3232
import java.util.Optional;
3333
import java.util.Set;
34-
import java.util.function.BiPredicate;
3534
import java.util.function.Function;
3635
import java.util.stream.Collectors;
3736
import java.util.stream.IntStream;
@@ -97,15 +96,15 @@ public synchronized Throwable fillInStackTrace() {
9796
private final List<Path> basepaths;
9897
private final int lastElementIndex;
9998
@Nullable
100-
private final BiPredicate<String, String> pathFilter;
99+
private final UnionPathFilter pathFilter;
101100
private final Map<Path, EmbeddedFileSystemMetadata> embeddedFileSystems;
102101

103102
public Path getPrimaryPath() {
104103
return basepaths.get(basepaths.size() - 1);
105104
}
106105

107106
@Nullable
108-
public BiPredicate<String, String> getFilesystemFilter() {
107+
public UnionPathFilter getFilesystemFilter() {
109108
return pathFilter;
110109
}
111110

@@ -116,7 +115,7 @@ String getKey() {
116115
private record EmbeddedFileSystemMetadata(Path path, FileSystem fs, SeekableByteChannel fsCh) {
117116
}
118117

119-
public UnionFileSystem(final UnionFileSystemProvider provider, @Nullable BiPredicate<String, String> pathFilter, final String key, final Path... basepaths) {
118+
public UnionFileSystem(final UnionFileSystemProvider provider, @Nullable UnionPathFilter pathFilter, final String key, final Path... basepaths) {
120119
this.pathFilter = pathFilter;
121120
this.provider = provider;
122121
this.key = key;
@@ -454,9 +453,6 @@ private boolean testFilter(final Path path, final Path basePath, @Nullable Basic
454453
sPath += '/';
455454
if (sPath.length() > 1 && sPath.startsWith("/"))
456455
sPath = sPath.substring(1);
457-
String sBasePath = basePath.toString().replace('\\', '/');
458-
if (sBasePath.length() > 1 && sBasePath.startsWith("/"))
459-
sBasePath = sBasePath.substring(1);
460-
return pathFilter.test(sPath, sBasePath);
456+
return pathFilter.test(sPath, basePath);
461457
}
462458
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected Path uriToPath(URI uri) {
6565

6666
/**
6767
* Invoked by FileSystems.newFileSystem, Only returns a value if env contains one of more of:
68-
* "filter": BiPredicate<String, String> - A filter to apply to the opened path
68+
* "filter": UnionPathFilter - A filter to apply to the opened path
6969
* "additional": List<Path> - Additional paths to join together
7070
* If none specified, throws IllegalArgumentException
7171
* If uri.getScheme() is not "union" throws IllegalArgumentException
@@ -76,7 +76,7 @@ public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) throws
7676
@SuppressWarnings("unchecked")
7777
var additional = ((Map<String, List<Path>>)env).getOrDefault("additional", List.<Path>of());
7878
@SuppressWarnings("unchecked")
79-
var filter = ((Map<String, BiPredicate<String, String>>)env).getOrDefault("filter", null);
79+
var filter = ((Map<String, UnionPathFilter>)env).getOrDefault("filter", null);
8080

8181
if (filter == null && additional.isEmpty())
8282
throw new IllegalArgumentException("Missing additional and/or filter");
@@ -95,7 +95,7 @@ public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) throws
9595

9696
/**
9797
* Invoked by FileSystems.newFileSystem, Only returns a value if env contains one of more of:
98-
* "filter": BiPredicate<String, String> - A filter to apply to the opened path
98+
* "filter": UnionPathFilter - A filter to apply to the opened path
9999
* "additional": List<Path> - Additional paths to join together
100100
* If none specified, throws UnsupportedOperationException instead of IllegalArgumentException
101101
* so that FileSystems.newFileSystem will search for the next provider.
@@ -106,7 +106,7 @@ public FileSystem newFileSystem(final Path path, final Map<String, ?> env) throw
106106
@SuppressWarnings("unchecked")
107107
var additional = ((Map<String, List<Path>>)env).getOrDefault("additional", List.<Path>of());
108108
@SuppressWarnings("unchecked")
109-
var filter = ((Map<String, BiPredicate<String, String>>)env).getOrDefault("filter", null);
109+
var filter = ((Map<String, UnionPathFilter>)env).getOrDefault("filter", null);
110110

111111
if (filter == null && additional.isEmpty())
112112
throw new UnsupportedOperationException("Missing additional and/or filter");
@@ -119,13 +119,13 @@ public FileSystem newFileSystem(final Path path, final Map<String, ?> env) throw
119119
}
120120
}
121121

122-
public UnionFileSystem newFileSystem(@Nullable BiPredicate<String, String> pathfilter, final Path... paths) {
122+
public UnionFileSystem newFileSystem(@Nullable UnionPathFilter pathfilter, final Path... paths) {
123123
if (paths.length == 0) throw new IllegalArgumentException("Need at least one path");
124124
var key = makeKey(paths[0]);
125125
return newFileSystemInternal(key, pathfilter, paths);
126126
}
127127

128-
private UnionFileSystem newFileSystemInternal(final String key, @Nullable BiPredicate<String, String> pathfilter, final Path... paths) {
128+
private UnionFileSystem newFileSystemInternal(final String key, @Nullable UnionPathFilter pathfilter, final Path... paths) {
129129
var normpaths = Arrays.stream(paths)
130130
.map(Path::toAbsolutePath)
131131
.map(Path::normalize)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cpw.mods.niofs.union;
22

3+
import java.nio.file.Path;
4+
35
/**
46
* Filter for paths in a {@link UnionFileSystem}.
57
*/
@@ -12,5 +14,5 @@ public interface UnionPathFilter {
1214
* @param basePath the base path, i.e. one of the root paths the filesystem is built out of
1315
* @return {@code true} to include the entry, {@code} false to exclude it
1416
*/
15-
boolean test(String entry, String basePath);
17+
boolean test(String entry, Path basePath);
1618
}

0 commit comments

Comments
 (0)