Skip to content

Commit d6b7b5e

Browse files
authored
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
1 parent 03f4ec7 commit d6b7b5e

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static final class Builder {
123123
private int blockSize = CloudStorageFileSystem.BLOCK_SIZE_DEFAULT;
124124
private int maxChannelReopens = 0;
125125
private @Nullable String userProject = null;
126-
// This of this as "clear userProject if not RequesterPays"
126+
// Think of this as "clear userProject if not RequesterPays"
127127
private boolean useUserProjectOnlyForRequesterPaysBuckets = false;
128128
private ImmutableList<Integer> retryableHttpCodes = ImmutableList.of(500, 502, 503);
129129
private ImmutableList<Class<? extends Exception>> reopenableExceptions =

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.cloud.storage.Blob;
2424
import com.google.cloud.storage.BlobId;
2525
import com.google.cloud.storage.Storage;
26+
import com.google.common.base.Strings;
2627
import com.google.common.collect.UnmodifiableIterator;
2728
import java.io.File;
2829
import java.net.URI;
@@ -113,7 +114,7 @@ boolean seemsLikeADirectoryAndUsePseudoDirectories(Storage storage) {
113114
}
114115
String userProject = fileSystem.config().userProject();
115116
Page<Blob> list = null;
116-
if (userProject != null) {
117+
if (!Strings.isNullOrEmpty(userProject)) {
117118
list =
118119
storage.list(
119120
this.bucket(),

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,18 @@ public void testAutodetectWhenNotRequesterPays() throws IOException {
327327
"");
328328
}
329329

330+
@Test
331+
public void testFilesExistDoesntCrashWhenRequesterPays() throws IOException {
332+
CloudStorageConfiguration config =
333+
CloudStorageConfiguration.builder()
334+
.autoDetectRequesterPays(true)
335+
.userProject(project)
336+
.build();
337+
CloudStorageFileSystem testBucket =
338+
CloudStorageFileSystem.forBucket(BUCKET, config, storageOptions);
339+
Assert.assertFalse(Files.exists(testBucket.getPath("path")));
340+
}
341+
330342
@Test
331343
public void testAutoDetectNoUserProject() throws IOException {
332344
CloudStorageFileSystem testBucket = getRequesterPaysBucket(false, "");

0 commit comments

Comments
 (0)