Skip to content

Commit da3724f

Browse files
Fix directory listing with leading slash
newDirectoryStream("dir1/") and newDirectoryStream("/dir1/") should return the same result. Added a test to show they didn't, then fixed it so the test passes.
1 parent 68ce84d commit da3724f

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ public DirectoryStream<Path> newDirectoryStream(Path dir, final Filter<? super P
598598
final CloudStoragePath cloudPath = CloudStorageUtil.checkPath(dir);
599599
checkNotNull(filter);
600600
initStorage();
601-
String prefix = cloudPath.toString();
601+
String prefix = cloudPath.toRealPath().toString();
602602
final Iterator<Blob> blobIterator = storage.list(cloudPath.bucket(),
603603
Storage.BlobListOption.prefix(prefix), Storage.BlobListOption.currentDirectory(),
604604
Storage.BlobListOption.fields()).iterateAll();

google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ public void testListFiles() throws IOException {
158158
got.add(path);
159159
}
160160
assertThat(got).containsExactlyElementsIn(goodPaths);
161+
162+
// Must also work with relative path
163+
got.clear();
164+
for (Path path : Files.newDirectoryStream(fs.getPath("dir/"))) {
165+
got.add(path);
166+
}
167+
assertThat(got).containsExactlyElementsIn(goodPaths);
161168
}
162169
}
163170

google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ public void testListFiles() throws IOException {
347347
}
348348

349349
List<Path> got = new ArrayList<>();
350+
for (Path path : Files.newDirectoryStream(fs.getPath("/dir/"))) {
351+
got.add(path);
352+
}
353+
assertThat(got).containsExactlyElementsIn(goodPaths);
354+
355+
// Must also work with relative path
356+
got.clear();
350357
for (Path path : Files.newDirectoryStream(fs.getPath("dir/"))) {
351358
got.add(path);
352359
}

0 commit comments

Comments
 (0)