Skip to content

Commit ab1d9f4

Browse files
authored
Fix UnionFileSystemProvider#newDirectoryStream to obey contract of FileSystemProvider#newDirectoryStream (#79)
Fixes #78; use of `Files.walk` should now produce `Path`s that are as if resolved from the root path. This required a change to `JarContentsImpl` as it relied on the old (incorrect!) behavior where `walk` returned explicitly relative paths no matter what, as well as some changes to certain tests where similar assumptions were made.
1 parent 8382e57 commit ab1d9f4

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public Set<String> getPackagesExcluding(String... excludedRootPackages) {
168168
@Override
169169
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
170170
if (file.getFileName().toString().endsWith(".class") && attrs.isRegularFile()) {
171-
var pkg = file.getParent().toString().replace('/', '.');
171+
var pkg = JarContentsImpl.this.filesystem.getRoot().relativize(file.getParent()).toString().replace('/', '.');
172172
if (!pkg.isEmpty()) {
173173
packages.add(pkg);
174174
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,11 @@ public DirectoryStream<Path> newDirStream(final UnionPath path, final DirectoryS
404404
if (!fastPathExists(dir)) {
405405
continue;
406406
}
407-
final var isSimple = embeddedFileSystems.containsKey(bp);
408407
final var ds = Files.newDirectoryStream(dir, filter);
409408
closeables.add(ds);
410409
final var currentPaths = StreamSupport.stream(ds.spliterator(), false)
411410
.filter(p -> testFilter(p, bp, null))
412-
.map(other -> fastPath(isSimple ? other : bp.relativize(other)));
411+
.map(other -> path.resolve(fastPath(dir.relativize(other))));
413412
stream = Stream.concat(stream, currentPaths);
414413
}
415414
final Stream<Path> realStream = stream.distinct();

src/test/java/cpw/mods/niofs/union/TestUnionFS.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ void testPathFiltering() {
137137
final var dir2 = Paths.get("src", "test", "resources", "dir2").toAbsolutePath().normalize();
138138
var fsp = (UnionFileSystemProvider)FileSystemProvider.installedProviders().stream().filter(fs-> fs.getScheme().equals("union")).findFirst().orElseThrow();
139139
var ufs = fsp.newFileSystem((path, base)->!path.startsWith("masktest2.txt"), dir1, dir2);
140-
var t1 = ufs.getPath("masktest.txt");
141-
var t3 = ufs.getPath("masktest3.txt");
142-
var t2 = ufs.getPath("masktest2.txt");
140+
var t1 = ufs.getPath("/masktest.txt");
141+
var t3 = ufs.getPath("/masktest3.txt");
142+
var t2 = ufs.getPath("/masktest2.txt");
143143
assertTrue(Files.exists(t1));
144144
assertTrue(Files.exists(t3));
145145
assertTrue(Files.notExists(t2));
146-
var sd1 = ufs.getPath("subdir1");
146+
var sd1 = ufs.getPath("/subdir1");
147147
var sdt1 = sd1.resolve("masktestsd1.txt");
148148
var walk = Set.of(ufs.getRoot(), t1, t3, sd1, sdt1);
149149
assertDoesNotThrow(()-> {
@@ -160,9 +160,9 @@ void testFilteredDuplicate() {
160160
var fsp = (UnionFileSystemProvider)FileSystemProvider.installedProviders().stream().filter(fs-> fs.getScheme().equals("union")).findFirst().orElseThrow();
161161
var all = fsp.newFileSystem((a,b) -> true, dir1);
162162
var all_expected = Set.of(
163-
all.getPath("masktest.txt"),
164-
all.getPath("masktest2.txt"),
165-
all.getPath("subdir1/masktestsd1.txt")
163+
all.getPath("/masktest.txt"),
164+
all.getPath("/masktest2.txt"),
165+
all.getPath("/subdir1/masktestsd1.txt")
166166
);
167167
assertDoesNotThrow(() -> {
168168
try (var walk = Files.walk(all.getRoot())) {
@@ -173,7 +173,7 @@ void testFilteredDuplicate() {
173173

174174
var some = assertDoesNotThrow(() -> fsp.newFileSystem((a,b) -> a.endsWith("/") || a.equals("masktest.txt"), dir1));
175175
var some_expected = Set.of(
176-
some.getPath("masktest.txt")
176+
some.getPath("/masktest.txt")
177177
);
178178
assertDoesNotThrow(() -> {
179179
try (var walk = Files.walk(some.getRoot())) {
@@ -189,7 +189,7 @@ void testNested() {
189189
var fsp = (UnionFileSystemProvider)FileSystemProvider.installedProviders().stream().filter(fs-> fs.getScheme().equals("union")).findFirst().orElseThrow();
190190
var inner = fsp.newFileSystem((a,b) -> a.endsWith("/") || a.equals("masktest.txt"), dir1);
191191
var outer = fsp.newFileSystem((a, b) -> true, inner.getRoot());
192-
var path = outer.getPath("masktest.txt");
192+
var path = outer.getPath("/masktest.txt");
193193
var expected = Set.of(path);
194194
assertDoesNotThrow(() -> {
195195
try (var walk = Files.walk(outer.getRoot())) {
@@ -263,7 +263,7 @@ public void testDirectoryVisitorJar() throws Exception {
263263
final List<String> foundFiles = new ArrayList<>();
264264
assertAll(
265265
StreamSupport.stream(dirStream.spliterator(), false)
266-
.peek(path -> foundFiles.add(path.toString()))
266+
.peek(path -> foundFiles.add(root.relativize(path).toString()))
267267
.map(p-> ()-> {
268268
if (!Files.exists(p)) {
269269
throw new NoSuchFileException(p.toString());

0 commit comments

Comments
 (0)