Skip to content

Commit

Permalink
fix: prevent crash when checking if a missing file exists googleapis#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 doens't exist.

Refs: googleapis#856
  • Loading branch information
lbergelson committed Mar 11, 2022
1 parent e0e9a9e commit 19dbf0a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 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 @@ -987,7 +987,7 @@ public boolean requesterPays(String bucketName) {
* requester-pays.
*/
public CloudStorageFileSystemProvider withNoUserProject() {
return new CloudStorageFileSystemProvider("", this.storageOptions);
return new CloudStorageFileSystemProvider(null, this.storageOptions);
}

/** Returns the project that is assigned to this provider. */
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 19dbf0a

Please sign in to comment.