Skip to content

Commit

Permalink
fix: prevent crash when checking if a missing file exists #856
Browse files Browse the repository at this point in the history
Fixes a crash that occurred when autoDetectRequesterPays is set and
a Files.exists() call is made on a file that doesn't exist.

Refs: #856
  • Loading branch information
lbergelson committed Mar 15, 2022
1 parent 03f4ec7 commit 16908f9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static final class Builder {
private int blockSize = CloudStorageFileSystem.BLOCK_SIZE_DEFAULT;
private int maxChannelReopens = 0;
private @Nullable String userProject = null;
// This of this as "clear userProject if not RequesterPays"
// Think of this as "clear userProject if not RequesterPays"
private boolean useUserProjectOnlyForRequesterPaysBuckets = false;
private ImmutableList<Integer> retryableHttpCodes = ImmutableList.of(500, 502, 503);
private ImmutableList<Class<? extends Exception>> reopenableExceptions =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.common.base.Strings;
import com.google.common.collect.UnmodifiableIterator;
import java.io.File;
import java.net.URI;
Expand Down Expand Up @@ -113,7 +114,7 @@ boolean seemsLikeADirectoryAndUsePseudoDirectories(Storage storage) {
}
String userProject = fileSystem.config().userProject();
Page<Blob> list = null;
if (userProject != null) {
if (!Strings.isNullOrEmpty(userProject)) {
list =
storage.list(
this.bucket(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ public void testAutodetectWhenNotRequesterPays() throws IOException {
"");
}

@Test
public void testFilesExistDoesntCrashWhenRequesterPays() throws IOException {
CloudStorageConfiguration config =
CloudStorageConfiguration.builder()
.autoDetectRequesterPays(true)
.userProject(project)
.build();
CloudStorageFileSystem testBucket =
CloudStorageFileSystem.forBucket(BUCKET, config, storageOptions);
Assert.assertFalse(Files.exists(testBucket.getPath("path")));
}

@Test
public void testAutoDetectNoUserProject() throws IOException {
CloudStorageFileSystem testBucket = getRequesterPaysBucket(false, "");
Expand Down

0 comments on commit 16908f9

Please sign in to comment.