Skip to content

Commit a37aaee

Browse files
jean-philippe-martinchingor13
authored andcommitted
---
yaml --- r: 32415 b: refs/heads/autosynth-errorreporting c: 722508f h: refs/heads/master i: 32413: 0673726 32411: 39cf440 32407: 6998131 32399: f9224c5 32383: 74e9325
1 parent d2c892d commit a37aaee

5 files changed

Lines changed: 66 additions & 4 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ refs/heads/autosynth-bigtable-admin: 6379a2bc712f2736c83de0e009b4d26da4fa82ca
129129
refs/heads/autosynth-containeranalysis: 18d210f81f17cf74864d0db2c29e834302f74f2a
130130
refs/heads/autosynth-datastore: f1efc3dc465174f41041acd56cf29badcec3e5bd
131131
refs/heads/autosynth-dialogflow: 73974cc32e5212aec0126472e0bc1442886fedaf
132-
refs/heads/autosynth-errorreporting: 740b5e96e2ef354745d58ea3a2a163368a69f987
132+
refs/heads/autosynth-errorreporting: 722508f7c606a90355af5682e98d32e00e78d13b
133133
refs/heads/autosynth-firestore: 983c75e4fb1076502c8cac73ef0538bdb10884f3
134134
refs/heads/autosynth-iot: 4025d1804241e74d54950a324dc4f667aeaad4b3
135135
refs/heads/autosynth-kms: 6b65b0f34c12d141031c7288cdc01e550212d0f6

branches/autosynth-errorreporting/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ private SeekableByteChannel newWriteChannel(Path path, Set<? extends OpenOption>
401401
throws IOException {
402402
initStorage();
403403
CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
404-
if (cloudPath.seemsLikeADirectoryAndUsePseudoDirectories(null)) {
404+
boolean allowSlash = options.contains(OptionAllowTrailingSlash.getInstance());
405+
if (!allowSlash && cloudPath.seemsLikeADirectoryAndUsePseudoDirectories(null)) {
405406
throw new CloudStoragePseudoDirectoryException(cloudPath);
406407
}
407408
BlobId file = cloudPath.getBlobId();
@@ -816,8 +817,16 @@ public <A extends BasicFileAttributes> A readAttributes(
816817
}
817818
CloudStorageObjectAttributes ret;
818819
ret = new CloudStorageObjectAttributes(blobInfo);
819-
// if size is 0 it could be a folder
820-
if (ret.size() == 0 && cloudPath.seemsLikeADirectoryAndUsePseudoDirectories(storage)) {
820+
/*
821+
There exists a file with this name, yes. But should we pretend it's a directory?
822+
The web UI will allow the user to "create directories" by creating files
823+
whose name ends in slash (and these files aren't always zero-size).
824+
If we're set to use pseudo directories and the file name looks like a path,
825+
then say it's a directory. We pass null to avoid trying to actually list files;
826+
if the path doesn't end in "/" we'll truthfully say it's a file. Yes it may also be
827+
a directory but we don't want to do a prefix search every time the user stats a file.
828+
*/
829+
if (cloudPath.seemsLikeADirectoryAndUsePseudoDirectories(null)) {
821830
@SuppressWarnings("unchecked")
822831
A result = (A) new CloudStoragePseudoDirectoryAttributes(cloudPath);
823832
return result;

branches/autosynth-errorreporting/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageOptions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,13 @@ public static CloudStorageOption.OpenCopy withChannelReopen(int count) {
9595
return OptionMaxChannelReopens.create(count);
9696
}
9797

98+
/**
99+
* Allows one to use trailing slashes in file names. You really shouldn't (this is here for tests
100+
* only).
101+
*/
102+
static CloudStorageOption.Open allowTrailingSlash() {
103+
return OptionAllowTrailingSlash.getInstance();
104+
}
105+
98106
private CloudStorageOptions() {}
99107
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.storage.contrib.nio;
18+
19+
class OptionAllowTrailingSlash implements CloudStorageOption.Open {
20+
21+
static OptionAllowTrailingSlash instance;
22+
23+
private OptionAllowTrailingSlash() {};
24+
25+
public static synchronized OptionAllowTrailingSlash getInstance() {
26+
if (null == instance) {
27+
instance = new OptionAllowTrailingSlash();
28+
}
29+
return instance;
30+
}
31+
}

branches/autosynth-errorreporting/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,21 @@ public void testSize_trailingSlash_returnsFakePseudoDirectorySize() throws Excep
104104
assertThat(Files.size(Paths.get(URI.create("gs://bucket/wat/")))).isEqualTo(1);
105105
}
106106

107+
@Test
108+
public void test_trailingSlash_isFolder() throws Exception {
109+
Path path = Paths.get(URI.create("gs://bucket/wat/"));
110+
Files.write(path, SINGULARITY.getBytes(UTF_8), CloudStorageOptions.allowTrailingSlash());
111+
assertThat(Files.isDirectory(path)).isTrue();
112+
}
113+
107114
@Test
108115
public void testSize_trailingSlash_disablePseudoDirectories() throws Exception {
109116
try (CloudStorageFileSystem fs = forBucket("doodle", usePseudoDirectories(false))) {
110117
Path path = fs.getPath("wat/");
111118
byte[] rapture = SINGULARITY.getBytes(UTF_8);
112119
Files.write(path, rapture);
113120
assertThat(Files.size(path)).isEqualTo(rapture.length);
121+
Files.delete(path);
114122
}
115123
}
116124

@@ -119,6 +127,7 @@ public void testReadAllBytes() throws Exception {
119127
Path path = Paths.get(URI.create("gs://bucket/wat"));
120128
Files.write(path, SINGULARITY.getBytes(UTF_8));
121129
assertThat(new String(Files.readAllBytes(path), UTF_8)).isEqualTo(SINGULARITY);
130+
Files.delete(path);
122131
}
123132

124133
@Test
@@ -139,6 +148,7 @@ public void testNewByteChannelRead() throws Exception {
139148
buffer.rewind();
140149
assertThat(input.read(buffer)).isEqualTo(-1);
141150
}
151+
Files.delete(path);
142152
}
143153

144154
@Test
@@ -160,6 +170,7 @@ public void testNewByteChannelRead_seeking() throws Exception {
160170
assertThat(input.position()).isEqualTo(5);
161171
assertThat(new String(buffer.array(), UTF_8)).isEqualTo("hello");
162172
}
173+
Files.delete(path);
163174
}
164175

165176
@Test
@@ -667,6 +678,9 @@ public void testCopy_overwriteAttributes() throws Exception {
667678
attributes = Files.readAttributes(target2, CloudStorageFileAttributes.class);
668679
assertThat(attributes.mimeType()).hasValue("text/palfun");
669680
assertThat(attributes.cacheControl()).hasValue("public; max-age=666");
681+
Files.delete(source);
682+
Files.delete(target1);
683+
Files.delete(target2);
670684
}
671685

672686
@Test

0 commit comments

Comments
 (0)