From d6b7b5e5e7ca243583e8852edfcf83d57021c9e6 Mon Sep 17 00:00:00 2001 From: Louis Bergelson Date: Wed, 16 Mar 2022 13:25:24 -0400 Subject: [PATCH] fix: prevent crash when checking if a missing file exists #856 (#858) 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 --- .../contrib/nio/CloudStorageConfiguration.java | 2 +- .../cloud/storage/contrib/nio/CloudStoragePath.java | 3 ++- .../cloud/storage/contrib/nio/it/ITGcsNio.java | 12 ++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageConfiguration.java b/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageConfiguration.java index 61359bdf..3ed6d746 100644 --- a/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageConfiguration.java +++ b/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageConfiguration.java @@ -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 retryableHttpCodes = ImmutableList.of(500, 502, 503); private ImmutableList> reopenableExceptions = diff --git a/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStoragePath.java b/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStoragePath.java index b0c5d349..29b32229 100644 --- a/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStoragePath.java +++ b/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStoragePath.java @@ -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; @@ -113,7 +114,7 @@ boolean seemsLikeADirectoryAndUsePseudoDirectories(Storage storage) { } String userProject = fileSystem.config().userProject(); Page list = null; - if (userProject != null) { + if (!Strings.isNullOrEmpty(userProject)) { list = storage.list( this.bucket(), diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java index 55a341d4..4729aa31 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java @@ -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, "");