Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration test for ls #931

Merged
merged 4 commits into from
Apr 19, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,13 @@ public DirectoryStream<Path> newDirectoryStream(Path dir, final Filter<? super P
final CloudStoragePath cloudPath = CloudStorageUtil.checkPath(dir);
checkNotNull(filter);
String prefix = cloudPath.toString();
final Iterator<Blob> blobIterator = storage.list(cloudPath.bucket(), Storage.BlobListOption.prefix(prefix), Storage.BlobListOption.fields()).iterateAll();
final Iterator<Blob> blobIterator = storage.list(
cloudPath.bucket(),
Storage.BlobListOption.prefix(prefix),
Storage.BlobListOption.currentDirectory()
// this breaks the listing (bug?)
//Storage.BlobListOption.fields()

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

).iterateAll();
return new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -317,6 +319,29 @@ public void testCopy() throws IOException {
}
}

@Test
public void testListFiles() throws IOException {
try (FileSystem fs = getTestBucket()) {
List<Path> goodPaths = new ArrayList<>();
List<Path> paths = new ArrayList<>();
goodPaths.add(fs.getPath("dir/angel"));
goodPaths.add(fs.getPath("dir/alone"));
paths.add(fs.getPath("dir/dir2/another_angel"));
paths.add(fs.getPath("atroot"));
paths.addAll(goodPaths);
goodPaths.add(fs.getPath("dir/dir2/"));
for (Path path : paths) {
fillFile(storage, path.toString(), SML_SIZE);
}

List<Path> got = new ArrayList<>();
for (Path path : Files.newDirectoryStream(fs.getPath("dir/"))) {
got.add(path);
}
assertThat(got).containsExactlyElementsIn(goodPaths);
}
}

private int readFully(ReadableByteChannel chan, byte[] outputBuf) throws IOException {
ByteBuffer buf = ByteBuffer.wrap(outputBuf);
int sofar = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public Tuple<String, Iterable<Bucket>> list(Map<Option, ?> options) throws Stora
@Override
public Tuple<String, Iterable<StorageObject>> list(String bucket, Map<Option, ?> options)
throws StorageException {
String delimiter = null;
String preprefix = "";
for (Map.Entry<Option, ?> e : options.entrySet()) {
switch (e.getKey()) {
Expand All @@ -102,6 +103,9 @@ public Tuple<String, Iterable<StorageObject>> list(String bucket, Map<Option, ?>
preprefix = preprefix.substring(1);
}
break;
case DELIMITER:
delimiter = (String) e.getValue();
break;
case FIELDS:
// ignore and return all the fields
break;
Expand All @@ -117,17 +121,7 @@ public Tuple<String, Iterable<StorageObject>> list(String bucket, Map<Option, ?>
if (!so.getName().startsWith(prefix)) {
continue;
}
int nextSlash = so.getName().indexOf("/", prefix.length());
if (nextSlash >= 0) {
String folderName = so.getName().substring(0, nextSlash + 1);
if (folders.containsKey(folderName)) {
continue;
}
StorageObject fakeFolder = new StorageObject();
fakeFolder.setName(folderName);
fakeFolder.setBucket(so.getBucket());
fakeFolder.setGeneration(so.getGeneration());
folders.put(folderName, fakeFolder);
if (processedAsFolder(so, delimiter, prefix, folders)) {
continue;
}
values.add(so);
Expand Down Expand Up @@ -333,4 +327,25 @@ private void potentiallyThrow(Map<Option, ?> options) throws UnsupportedOperatio
throw new UnsupportedOperationException();
}
}

// Returns true if this is a folder. Adds it to folders if it isn't already there.
private boolean processedAsFolder(StorageObject so, String delimiter, String prefix, /* inout */ Map<String, StorageObject> folders) {

This comment was marked as spam.

This comment was marked as spam.

if (delimiter == null) {
return false;
}
int nextSlash = so.getName().indexOf(delimiter, prefix.length());
if (nextSlash < 0) {
return false;
}
String folderName = so.getName().substring(0, nextSlash + 1);
if (folders.containsKey(folderName)) {
return true;
}
StorageObject fakeFolder = new StorageObject();
fakeFolder.setName(folderName);
fakeFolder.setBucket(so.getBucket());
fakeFolder.setGeneration(so.getGeneration());
folders.put(folderName, fakeFolder);
return true;
}
}