/
CloudStorageFileSystemProvider.java
1043 lines (979 loc) · 39.7 KB
/
CloudStorageFileSystemProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.storage.contrib.nio;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.isNullOrEmpty;
import com.google.api.gax.paging.Page;
import com.google.cloud.storage.Acl;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.CopyWriter;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobGetOption;
import com.google.cloud.storage.Storage.BlobSourceOption;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.StorageOptions;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.collect.AbstractIterator;
import com.google.common.net.UrlEscapers;
import com.google.common.primitives.Ints;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.channels.FileChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.AccessMode;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.CopyOption;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream;
import java.nio.file.DirectoryStream.Filter;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.NoSuchFileException;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.spi.FileSystemProvider;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
import javax.inject.Singleton;
/**
* Google Cloud Storage {@link FileSystemProvider} implementation.
*
* <p><b>Note:</b> This class should never be used directly. This class is instantiated by the
* service loader and called through a standardized API, e.g. {@link java.nio.file.Files}. However
* the javadocs in this class serve as useful documentation for the behavior of the Google Cloud
* Storage NIO library.
*/
@Singleton
@ThreadSafe
public final class CloudStorageFileSystemProvider extends FileSystemProvider {
private Storage storage;
private final StorageOptions storageOptions;
// if non-null, we pay via this project.
private final @Nullable String userProject;
// used only when we create a new instance of CloudStorageFileSystemProvider.
private static StorageOptions futureStorageOptions = StorageOptionsUtil.getDefaultInstance();
private static class LazyPathIterator extends AbstractIterator<Path> {
private final Iterator<Blob> blobIterator;
private final Filter<? super Path> filter;
private final CloudStorageFileSystem fileSystem;
private final String prefix;
// whether to make the paths absolute before returning them.
private final boolean absolutePaths;
LazyPathIterator(
CloudStorageFileSystem fileSystem,
String prefix,
Iterator<Blob> blobIterator,
Filter<? super Path> filter,
boolean absolutePaths) {
this.prefix = prefix;
this.blobIterator = blobIterator;
this.filter = filter;
this.fileSystem = fileSystem;
this.absolutePaths = absolutePaths;
}
@Override
protected Path computeNext() {
while (blobIterator.hasNext()) {
Path path = fileSystem.getPath(blobIterator.next().getName());
try {
if (path.toString().equals(prefix)) {
// do not return ourselves, because that confuses recursive descents.
continue;
}
if (filter.accept(path)) {
if (absolutePaths) {
return path.toAbsolutePath();
}
return path;
}
} catch (IOException ex) {
throw new DirectoryIteratorException(ex);
}
}
return endOfData();
}
}
/**
* Sets options that are only used by the constructor.
*
* <p>Instead of calling this, when possible use CloudStorageFileSystem.forBucket and pass
* StorageOptions as an argument.
*/
@VisibleForTesting
public static void setStorageOptions(@Nullable StorageOptions newStorageOptions) {
futureStorageOptions = newStorageOptions;
}
/**
* Changes the default configuration for every filesystem object created from here on, including
* via SPI. If null then future filesystem objects will have the factory default configuration.
*
* <p>If options are specified later then they override the defaults. Methods that take a whole
* CloudStorageConfiguration (eg. CloudStorageFileSystem.forBucket) will completely override the
* defaults. Methods that take individual options (eg.
* CloudStorageFileSystemProvier.newFileSystem) will override only these options; the rest will be
* taken from the defaults specified here.
*
* <p>This is meant to be done only once, at the beginning of some main program, in order to force
* all libraries to use some settings we like.
*
* <p>Libraries should never call this. If you're a library then, instead, create your own
* filesystem object with the right configuration and pass it along.
*
* @param newDefault new default CloudStorageConfiguration
*/
public static void setDefaultCloudStorageConfiguration(
@Nullable CloudStorageConfiguration newDefault) {
CloudStorageFileSystem.setDefaultCloudStorageConfiguration(newDefault);
}
/**
* Default constructor which should only be called by Java SPI.
*
* @see java.nio.file.FileSystems#getFileSystem(URI)
* @see CloudStorageFileSystem#forBucket(String)
*/
public CloudStorageFileSystemProvider() {
this(
CloudStorageFileSystem.getDefaultCloudStorageConfiguration().userProject(),
futureStorageOptions);
}
/**
* Internal constructor to use the user-provided default config, and a given userProject setting.
*/
CloudStorageFileSystemProvider(@Nullable String userProject) {
this(userProject, futureStorageOptions);
}
/**
* Internal constructor, fully configurable. Note that null options means to use the system
* defaults (NOT the user-provided ones).
*/
CloudStorageFileSystemProvider(
@Nullable String userProject, @Nullable StorageOptions gcsStorageOptions) {
this.storageOptions =
gcsStorageOptions != null
? StorageOptionsUtil.mergeOptionsWithUserAgent(gcsStorageOptions)
: StorageOptionsUtil.getDefaultInstance();
this.userProject = userProject;
}
// Initialize this.storage, once. This may throw an exception if default authentication
// credentials are not available (hence not doing it in the ctor).
private void initStorage() {
if (this.storage == null) {
doInitStorage();
}
}
@Override
public String getScheme() {
return CloudStorageFileSystem.URI_SCHEME;
}
/** Returns Cloud Storage file system, provided a URI with no path, e.g. {@code gs://bucket}. */
@Override
public CloudStorageFileSystem getFileSystem(URI uri) {
initStorage();
return newFileSystem(uri, Collections.<String, Object>emptyMap());
}
/**
* Returns Cloud Storage file system, provided a URI, e.g. {@code gs://bucket}. The URI can
* include a path component (that will be ignored).
*
* @param uri bucket and current working directory, e.g. {@code gs://bucket}
* @param env map of configuration options, whose keys correspond to the method names of {@link
* CloudStorageConfiguration.Builder}. However you are not allowed to set the working
* directory, as that should be provided in the {@code uri}
* @throws IllegalArgumentException if {@code uri} specifies a port, user, query, or fragment, or
* if scheme is not {@value CloudStorageFileSystem#URI_SCHEME}
*/
@Override
public CloudStorageFileSystem newFileSystem(URI uri, Map<String, ?> env) {
checkArgument(
uri.getScheme().equalsIgnoreCase(CloudStorageFileSystem.URI_SCHEME),
"Cloud Storage URIs must have '%s' scheme: %s",
CloudStorageFileSystem.URI_SCHEME,
uri);
checkArgument(
!isNullOrEmpty(uri.getHost()),
"%s:// URIs must have a host: %s",
CloudStorageFileSystem.URI_SCHEME,
uri);
checkArgument(
uri.getPort() == -1
&& isNullOrEmpty(uri.getQuery())
&& isNullOrEmpty(uri.getFragment())
&& isNullOrEmpty(uri.getUserInfo()),
"GCS FileSystem URIs mustn't have: port, userinfo, query, or fragment: %s",
uri);
CloudStorageUtil.checkBucket(uri.getHost());
initStorage();
return new CloudStorageFileSystem(
this,
uri.getHost(),
CloudStorageConfiguration.fromMap(
CloudStorageFileSystem.getDefaultCloudStorageConfiguration(), env));
}
@Override
public CloudStoragePath getPath(URI uri) {
initStorage();
return CloudStoragePath.getPath(
getFileSystem(CloudStorageUtil.stripPathFromUri(uri)), uri.getPath());
}
/** Convenience method: replaces spaces with "%20", builds a URI, and calls getPath(uri). */
public CloudStoragePath getPath(String uriInStringForm) {
String escaped = UrlEscapers.urlFragmentEscaper().escape(uriInStringForm);
return getPath(URI.create(escaped));
}
/**
* Open a file for reading or writing. To read receiver-pays buckets, specify the
* BlobSourceOption.userProject option.
*
* @param path: the path to the file to open or create
* @param options: options specifying how the file is opened, e.g. StandardOpenOption.WRITE or
* BlobSourceOption.userProject
* @param attrs: (not supported, values will be ignored)
* @return
* @throws IOException
*/
@Override
public SeekableByteChannel newByteChannel(
Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
checkNotNull(path);
initStorage();
CloudStorageUtil.checkNotNullArray(attrs);
if (options.contains(StandardOpenOption.WRITE)) {
// TODO: Make our OpenOptions implement FileAttribute. Also remove buffer option.
return newWriteChannel(path, options);
} else {
return newReadChannel(path, options);
}
}
/**
* Open a file for reading OR writing. The {@link FileChannel} that is returned will only allow
* reads or writes depending on the {@link OpenOption}s that are specified. If any of the
* following have been specified, the {@link FileChannel} will be write-only: {@link
* StandardOpenOption#CREATE}
*
* <ul>
* <li>{@link StandardOpenOption#CREATE}
* <li>{@link StandardOpenOption#CREATE_NEW}
* <li>{@link StandardOpenOption#WRITE}
* <li>{@link StandardOpenOption#TRUNCATE_EXISTING}
* </ul>
*
* In all other cases the {@link FileChannel} will be read-only.
*
* @param path The path to the file to open or create
* @param options The options specifying how the file should be opened, and whether the {@link
* FileChannel} should be read-only or write-only.
* @param attrs (not supported, the values will be ignored)
* @throws IOException
*/
@Override
public FileChannel newFileChannel(
Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
checkNotNull(path);
initStorage();
CloudStorageUtil.checkNotNullArray(attrs);
if (options.contains(StandardOpenOption.CREATE_NEW)) {
Files.createFile(path, attrs);
} else if (options.contains(StandardOpenOption.CREATE) && !Files.exists(path)) {
Files.createFile(path, attrs);
}
if (options.contains(StandardOpenOption.WRITE)
|| options.contains(StandardOpenOption.CREATE)
|| options.contains(StandardOpenOption.CREATE_NEW)
|| options.contains(StandardOpenOption.TRUNCATE_EXISTING)) {
return new CloudStorageWriteFileChannel(newWriteChannel(path, options));
} else {
return new CloudStorageReadFileChannel(newReadChannel(path, options));
}
}
private SeekableByteChannel newReadChannel(Path path, Set<? extends OpenOption> options)
throws IOException {
initStorage();
int maxChannelReopens = CloudStorageUtil.getMaxChannelReopensFromPath(path);
List<BlobSourceOption> blobSourceOptions = new ArrayList<>();
for (OpenOption option : options) {
if (option instanceof StandardOpenOption) {
switch ((StandardOpenOption) option) {
case READ:
// Default behavior.
break;
case SPARSE:
case TRUNCATE_EXISTING:
// Ignored by specification.
break;
case WRITE:
throw new IllegalArgumentException("READ+WRITE not supported yet");
case APPEND:
case CREATE:
case CREATE_NEW:
case DELETE_ON_CLOSE:
case DSYNC:
case SYNC:
default:
throw new UnsupportedOperationException(option.toString());
}
} else if (option instanceof OptionMaxChannelReopens) {
maxChannelReopens = ((OptionMaxChannelReopens) option).maxChannelReopens();
} else if (option instanceof BlobSourceOption) {
blobSourceOptions.add((BlobSourceOption) option);
} else {
throw new UnsupportedOperationException(option.toString());
}
}
CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
// passing false since we just want to check if it ends with /
if (cloudPath.seemsLikeADirectoryAndUsePseudoDirectories(null)) {
throw new CloudStoragePseudoDirectoryException(cloudPath);
}
return CloudStorageReadChannel.create(
storage,
cloudPath.getBlobId(),
0,
maxChannelReopens,
cloudPath.getFileSystem().config(),
userProject,
blobSourceOptions.toArray(new BlobSourceOption[blobSourceOptions.size()]));
}
private SeekableByteChannel newWriteChannel(Path path, Set<? extends OpenOption> options)
throws IOException {
initStorage();
CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
boolean allowSlash = options.contains(OptionAllowTrailingSlash.getInstance());
if (!allowSlash && cloudPath.seemsLikeADirectoryAndUsePseudoDirectories(null)) {
throw new CloudStoragePseudoDirectoryException(cloudPath);
}
BlobId file = cloudPath.getBlobId();
BlobInfo.Builder infoBuilder = BlobInfo.newBuilder(file);
List<Storage.BlobWriteOption> writeOptions = new ArrayList<>();
List<Acl> acls = new ArrayList<>();
HashMap<String, String> metas = new HashMap<>();
for (OpenOption option : options) {
if (option instanceof OptionMimeType) {
infoBuilder.setContentType(((OptionMimeType) option).mimeType());
} else if (option instanceof OptionCacheControl) {
infoBuilder.setCacheControl(((OptionCacheControl) option).cacheControl());
} else if (option instanceof OptionContentDisposition) {
infoBuilder.setContentDisposition(((OptionContentDisposition) option).contentDisposition());
} else if (option instanceof OptionContentEncoding) {
infoBuilder.setContentEncoding(((OptionContentEncoding) option).contentEncoding());
} else if (option instanceof OptionUserMetadata) {
OptionUserMetadata opMeta = (OptionUserMetadata) option;
metas.put(opMeta.key(), opMeta.value());
} else if (option instanceof OptionAcl) {
acls.add(((OptionAcl) option).acl());
} else if (option instanceof OptionBlockSize) {
// TODO: figure out how to plumb in block size.
} else if (option instanceof StandardOpenOption) {
switch ((StandardOpenOption) option) {
case CREATE:
case TRUNCATE_EXISTING:
case WRITE:
// Default behavior.
break;
case SPARSE:
// Ignored by specification.
break;
case CREATE_NEW:
writeOptions.add(Storage.BlobWriteOption.doesNotExist());
break;
case READ:
throw new IllegalArgumentException("READ+WRITE not supported yet");
case APPEND:
case DELETE_ON_CLOSE:
case DSYNC:
case SYNC:
default:
throw new UnsupportedOperationException(option.toString());
}
} else if (option instanceof CloudStorageOption) {
// XXX: We need to interpret these later
} else {
throw new UnsupportedOperationException(option.toString());
}
}
if (!isNullOrEmpty(userProject)) {
writeOptions.add(Storage.BlobWriteOption.userProject(userProject));
}
if (!metas.isEmpty()) {
infoBuilder.setMetadata(metas);
}
if (!acls.isEmpty()) {
infoBuilder.setAcl(acls);
}
try {
return new CloudStorageWriteChannel(
storage.writer(
infoBuilder.build(),
writeOptions.toArray(new Storage.BlobWriteOption[writeOptions.size()])));
} catch (StorageException oops) {
throw asIoException(oops);
}
}
@Override
public InputStream newInputStream(Path path, OpenOption... options) throws IOException {
initStorage();
InputStream result = super.newInputStream(path, options);
CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
int blockSize = cloudPath.getFileSystem().config().blockSize();
for (OpenOption option : options) {
if (option instanceof OptionBlockSize) {
blockSize = ((OptionBlockSize) option).size();
}
}
return new BufferedInputStream(result, blockSize);
}
@Override
public boolean deleteIfExists(Path path) throws IOException {
initStorage();
CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
if (cloudPath.seemsLikeADirectoryAndUsePseudoDirectories(storage)) {
// if the "folder" is empty then we're fine, otherwise complain
// that we cannot act on folders.
try (DirectoryStream<Path> paths = Files.newDirectoryStream(path)) {
if (!paths.iterator().hasNext()) {
// "folder" isn't actually there in the first place, so: success!
// (we must return true so delete doesn't freak out)
return true;
}
}
throw new CloudStoragePseudoDirectoryException(cloudPath);
}
final CloudStorageRetryHandler retryHandler =
new CloudStorageRetryHandler(cloudPath.getFileSystem().config());
// Loop will terminate via an exception if all retries are exhausted
while (true) {
try {
if (isNullOrEmpty(userProject)) {
return storage.delete(cloudPath.getBlobId());
} else {
return storage.delete(
cloudPath.getBlobId(), Storage.BlobSourceOption.userProject(userProject));
}
} catch (StorageException exs) {
// Will rethrow a StorageException if all retries/reopens are exhausted
retryHandler.handleStorageException(exs);
// we're being aggressive by retrying even on scenarios where we'd normally reopen.
}
}
}
@Override
public void delete(Path path) throws IOException {
initStorage();
CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
if (!deleteIfExists(cloudPath)) {
throw new NoSuchFileException(cloudPath.toString());
}
}
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
initStorage();
for (CopyOption option : options) {
if (option == StandardCopyOption.ATOMIC_MOVE) {
throw new AtomicMoveNotSupportedException(
source.toString(),
target.toString(),
"Google Cloud Storage does not support atomic move operations.");
}
}
copy(source, target, options);
delete(source);
}
@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
initStorage();
boolean wantCopyAttributes = false;
boolean wantReplaceExisting = false;
// true if the option was set manually (so we shouldn't copy the parent's)
boolean overrideContentType = false;
boolean overrideCacheControl = false;
boolean overrideContentEncoding = false;
boolean overrideContentDisposition = false;
CloudStoragePath toPath = CloudStorageUtil.checkPath(target);
BlobInfo.Builder tgtInfoBuilder = BlobInfo.newBuilder(toPath.getBlobId()).setContentType("");
int blockSize = -1;
for (CopyOption option : options) {
if (option instanceof StandardCopyOption) {
switch ((StandardCopyOption) option) {
case COPY_ATTRIBUTES:
wantCopyAttributes = true;
break;
case REPLACE_EXISTING:
wantReplaceExisting = true;
break;
case ATOMIC_MOVE:
default:
throw new UnsupportedOperationException(option.toString());
}
} else if (option instanceof CloudStorageOption) {
if (option instanceof OptionBlockSize) {
blockSize = ((OptionBlockSize) option).size();
} else if (option instanceof OptionMimeType) {
tgtInfoBuilder.setContentType(((OptionMimeType) option).mimeType());
overrideContentType = true;
} else if (option instanceof OptionCacheControl) {
tgtInfoBuilder.setCacheControl(((OptionCacheControl) option).cacheControl());
overrideCacheControl = true;
} else if (option instanceof OptionContentEncoding) {
tgtInfoBuilder.setContentEncoding(((OptionContentEncoding) option).contentEncoding());
overrideContentEncoding = true;
} else if (option instanceof OptionContentDisposition) {
tgtInfoBuilder.setContentDisposition(
((OptionContentDisposition) option).contentDisposition());
overrideContentDisposition = true;
} else {
throw new UnsupportedOperationException(option.toString());
}
} else {
throw new UnsupportedOperationException(option.toString());
}
}
CloudStoragePath fromPath = CloudStorageUtil.checkPath(source);
blockSize =
blockSize != -1
? blockSize
: Ints.max(
fromPath.getFileSystem().config().blockSize(),
toPath.getFileSystem().config().blockSize());
// TODO: actually use blockSize
if (fromPath.seemsLikeADirectory() && toPath.seemsLikeADirectory()) {
if (fromPath.getFileSystem().config().usePseudoDirectories()
&& toPath.getFileSystem().config().usePseudoDirectories()) {
// NOOP: This would normally create an empty directory.
return;
} else {
checkArgument(
!fromPath.getFileSystem().config().usePseudoDirectories()
&& !toPath.getFileSystem().config().usePseudoDirectories(),
"File systems associated with paths don't agree on pseudo-directories.");
}
}
// We refuse to use paths that end in '/'. If the user puts a folder name
// but without the '/', they'll get whatever error GCS will return normally
// (if any).
if (fromPath.seemsLikeADirectoryAndUsePseudoDirectories(null)) {
throw new CloudStoragePseudoDirectoryException(fromPath);
}
if (toPath.seemsLikeADirectoryAndUsePseudoDirectories(null)) {
throw new CloudStoragePseudoDirectoryException(toPath);
}
final CloudStorageRetryHandler retryHandler =
new CloudStorageRetryHandler(fromPath.getFileSystem().config());
// Loop will terminate via an exception if all retries are exhausted
while (true) {
try {
if (wantCopyAttributes) {
BlobInfo blobInfo;
if (isNullOrEmpty(userProject)) {
blobInfo = storage.get(fromPath.getBlobId());
} else {
blobInfo = storage.get(fromPath.getBlobId(), BlobGetOption.userProject(userProject));
}
if (null == blobInfo) {
throw new NoSuchFileException(fromPath.toString());
}
if (!overrideCacheControl) {
tgtInfoBuilder.setCacheControl(blobInfo.getCacheControl());
}
if (!overrideContentType) {
tgtInfoBuilder.setContentType(blobInfo.getContentType());
}
if (!overrideContentEncoding) {
tgtInfoBuilder.setContentEncoding(blobInfo.getContentEncoding());
}
if (!overrideContentDisposition) {
tgtInfoBuilder.setContentDisposition(blobInfo.getContentDisposition());
}
tgtInfoBuilder.setAcl(blobInfo.getAcl());
tgtInfoBuilder.setMetadata(blobInfo.getMetadata());
}
BlobInfo tgtInfo = tgtInfoBuilder.build();
Storage.CopyRequest.Builder copyReqBuilder =
Storage.CopyRequest.newBuilder().setSource(fromPath.getBlobId());
if (wantReplaceExisting) {
copyReqBuilder = copyReqBuilder.setTarget(tgtInfo);
} else {
copyReqBuilder =
copyReqBuilder.setTarget(tgtInfo, Storage.BlobTargetOption.doesNotExist());
}
if (!isNullOrEmpty(fromPath.getFileSystem().config().userProject())) {
copyReqBuilder =
copyReqBuilder.setSourceOptions(
BlobSourceOption.userProject(fromPath.getFileSystem().config().userProject()));
} else if (!isNullOrEmpty(toPath.getFileSystem().config().userProject())) {
copyReqBuilder =
copyReqBuilder.setSourceOptions(
BlobSourceOption.userProject(toPath.getFileSystem().config().userProject()));
}
CopyWriter copyWriter = storage.copy(copyReqBuilder.build());
copyWriter.getResult();
break;
} catch (StorageException oops) {
try {
// Will rethrow a StorageException if all retries/reopens are exhausted
retryHandler.handleStorageException(oops);
// we're being aggressive by retrying even on scenarios where we'd normally reopen.
} catch (StorageException retriesExhaustedException) {
throw asIoException(retriesExhaustedException);
}
}
}
}
@Override
public boolean isSameFile(Path path, Path path2) {
return CloudStorageUtil.checkPath(path).equals(CloudStorageUtil.checkPath(path2));
}
/** Always returns {@code false}, because Google Cloud Storage doesn't support hidden files. */
@Override
public boolean isHidden(Path path) {
CloudStorageUtil.checkPath(path);
return false;
}
@Override
public void checkAccess(Path path, AccessMode... modes) throws IOException {
initStorage();
for (AccessMode mode : modes) {
switch (mode) {
case READ:
case WRITE:
break;
case EXECUTE:
default:
throw new UnsupportedOperationException(mode.toString());
}
}
final CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
final CloudStorageRetryHandler retryHandler =
new CloudStorageRetryHandler(cloudPath.getFileSystem().config());
// Loop will terminate via an exception if all retries are exhausted
while (true) {
try {
boolean nullId;
if (isNullOrEmpty(userProject)) {
nullId =
storage.get(cloudPath.getBlobId(), Storage.BlobGetOption.fields(Storage.BlobField.ID))
== null;
} else {
nullId =
storage.get(
cloudPath.getBlobId(),
Storage.BlobGetOption.fields(Storage.BlobField.ID),
Storage.BlobGetOption.userProject(userProject))
== null;
}
if (nullId) {
if (cloudPath.seemsLikeADirectoryAndUsePseudoDirectories(storage)) {
// there is no such file, but we're not signalling error because the
// path is a pseudo-directory.
return;
}
throw new NoSuchFileException(path.toString());
}
break;
} catch (StorageException exs) {
// Will rethrow a StorageException if all retries/reopens are exhausted
retryHandler.handleStorageException(exs);
// we're being aggressive by retrying even on scenarios where we'd normally reopen.
} catch (IllegalArgumentException exs) {
if (CloudStorageUtil.checkPath(path).seemsLikeADirectoryAndUsePseudoDirectories(storage)) {
// there is no such file, but we're not signalling error because the
// path is a pseudo-directory.
return;
}
// Other cause for IAE, forward the exception.
throw exs;
}
}
}
@Override
public <A extends BasicFileAttributes> A readAttributes(
Path path, Class<A> type, LinkOption... options) throws IOException {
checkNotNull(type);
CloudStorageUtil.checkNotNullArray(options);
if (type != CloudStorageFileAttributes.class && type != BasicFileAttributes.class) {
throw new UnsupportedOperationException(type.getSimpleName());
}
initStorage();
final CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
final CloudStorageRetryHandler retryHandler =
new CloudStorageRetryHandler(cloudPath.getFileSystem().config());
// Loop will terminate via an exception if all retries are exhausted
while (true) {
try {
BlobInfo blobInfo = null;
try {
BlobId blobId = cloudPath.getBlobId();
// Null or empty name won't give us a file, so skip. But perhaps it's a pseudodir.
if (!isNullOrEmpty(blobId.getName())) {
if (isNullOrEmpty(userProject)) {
blobInfo = storage.get(blobId);
} else {
blobInfo = storage.get(blobId, BlobGetOption.userProject(userProject));
}
}
} catch (IllegalArgumentException ex) {
// the path may be invalid but look like a folder. In that case, return a folder.
if (cloudPath.seemsLikeADirectoryAndUsePseudoDirectories(storage)) {
@SuppressWarnings("unchecked")
A result = (A) new CloudStoragePseudoDirectoryAttributes(cloudPath);
return result;
}
// No? Propagate.
throw ex;
}
// null size indicate a file that we haven't closed yet, so GCS treats it as not there yet.
if (null == blobInfo || blobInfo.getSize() == null) {
if (cloudPath.seemsLikeADirectoryAndUsePseudoDirectories(storage)) {
@SuppressWarnings("unchecked")
A result = (A) new CloudStoragePseudoDirectoryAttributes(cloudPath);
return result;
}
throw new NoSuchFileException(
"gs://" + cloudPath.getBlobId().getBucket() + "/" + cloudPath.getBlobId().getName());
}
CloudStorageObjectAttributes ret;
ret = new CloudStorageObjectAttributes(blobInfo);
/*
There exists a file with this name, yes. But should we pretend it's a directory?
The web UI will allow the user to "create directories" by creating files
whose name ends in slash (and these files aren't always zero-size).
If we're set to use pseudo directories and the file name looks like a path,
then say it's a directory. We pass null to avoid trying to actually list files;
if the path doesn't end in "/" we'll truthfully say it's a file. Yes it may also be
a directory but we don't want to do a prefix search every time the user stats a file.
*/
if (cloudPath.seemsLikeADirectoryAndUsePseudoDirectories(null)) {
@SuppressWarnings("unchecked")
A result = (A) new CloudStoragePseudoDirectoryAttributes(cloudPath);
return result;
}
@SuppressWarnings("unchecked")
A result = (A) ret;
return result;
} catch (StorageException exs) {
// Will rethrow a StorageException if all retries/reopens are exhausted
retryHandler.handleStorageException(exs);
// we're being aggressive by retrying even on scenarios where we'd normally reopen.
}
}
}
@Override
public Map<String, Object> readAttributes(Path path, String attributes, LinkOption... options) {
// TODO(#811): Java 7 NIO defines at least eleven string attributes we'd want to support
// (eg. BasicFileAttributeView and PosixFileAttributeView), so rather than a partial
// implementation we rely on the other overload for now.
throw new UnsupportedOperationException();
}
@Override
public <V extends FileAttributeView> V getFileAttributeView(
Path path, Class<V> type, LinkOption... options) {
checkNotNull(type);
CloudStorageUtil.checkNotNullArray(options);
if (type != CloudStorageFileAttributeView.class && type != BasicFileAttributeView.class) {
throw new UnsupportedOperationException(type.getSimpleName());
}
CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
@SuppressWarnings("unchecked")
V result = (V) new CloudStorageFileAttributeView(storage, cloudPath);
return result;
}
/** Does nothing since Google Cloud Storage uses fake directories. */
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs) {
CloudStorageUtil.checkPath(dir);
CloudStorageUtil.checkNotNullArray(attrs);
}
@Override
public DirectoryStream<Path> newDirectoryStream(
final Path dir, final Filter<? super Path> filter) {
final CloudStoragePath cloudPath = CloudStorageUtil.checkPath(dir);
checkNotNull(filter);
initStorage();
final CloudStorageRetryHandler retryHandler =
new CloudStorageRetryHandler(cloudPath.getFileSystem().config());
// Loop will terminate via an exception if all retries are exhausted
while (true) {
try {
String prePrefix = cloudPath.normalize().toRealPath().toString();
// we can recognize paths without the final "/" as folders,
// but storage.list doesn't do the right thing with those, we need to append a "/".
if (!prePrefix.isEmpty() && !prePrefix.endsWith("/")) {
prePrefix += "/";
}
final String prefix = prePrefix;
Page<Blob> dirList;
if (isNullOrEmpty(userProject)) {
dirList =
storage.list(
cloudPath.bucket(),
Storage.BlobListOption.prefix(prefix),
Storage.BlobListOption.currentDirectory(),
Storage.BlobListOption.fields());
} else {
dirList =
storage.list(
cloudPath.bucket(),
Storage.BlobListOption.prefix(prefix),
Storage.BlobListOption.currentDirectory(),
Storage.BlobListOption.fields(),
Storage.BlobListOption.userProject(userProject));
}
final Iterator<Blob> blobIterator = dirList.iterateAll().iterator();
return new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator() {
return new LazyPathIterator(
cloudPath.getFileSystem(), prefix, blobIterator, filter, dir.isAbsolute());
}
@Override
public void close() throws IOException {
// Does nothing since there's nothing to close. Commenting this method to quiet codacy.
}
};
} catch (StorageException exs) {
// Will rethrow a StorageException if all retries/reopens are exhausted
retryHandler.handleStorageException(exs);
// we're being aggressive by retrying even on scenarios where we'd normally reopen.
}
}
}
/** Throws {@link UnsupportedOperationException} because Cloud Storage objects are immutable. */
@Override
public void setAttribute(Path path, String attribute, Object value, LinkOption... options) {
throw new CloudStorageObjectImmutableException();
}
/**
* Throws {@link UnsupportedOperationException} because this feature hasn't been implemented yet.
*/
@Override
public FileStore getFileStore(Path path) {
throw new UnsupportedOperationException();
}
@Override
public boolean equals(Object other) {
return this == other
|| other instanceof CloudStorageFileSystemProvider
&& Objects.equals(storage, ((CloudStorageFileSystemProvider) other).storage);
}
@Override
public int hashCode() {
return Objects.hash(storage);
}
@Override
public String toString() {
initStorage();
return MoreObjects.toStringHelper(this).add("storage", storage).toString();
}
/**
* @param bucketName the name of the bucket to check
* @return whether requester pays is enabled for that bucket
*/
public boolean requesterPays(String bucketName) {
initStorage();
try {
// instead of true/false, this method returns true/null.
Boolean isRP = storage.get(bucketName).requesterPays();
return isRP != null && isRP.booleanValue();
} catch (StorageException ex) {
if (ex.getCode() == 400 && ex.getMessage().contains("Bucket is requester pays")) {
return true;
}
throw ex;
}
}
/**
* Returns a NEW CloudStorageFileSystemProvider identical to this one, but with userProject
* removed.
*
* <p>Perhaps you want to call this is you realize you'll be working on a bucket that is not
* requester-pays.
*/
public CloudStorageFileSystemProvider withNoUserProject() {
return new CloudStorageFileSystemProvider("", this.storageOptions);
}
/** Returns the project that is assigned to this provider. */
public String getProject() {
initStorage();
return storage.getOptions().getProjectId();
}
/**
* Lists the project's buckets. But use the one in CloudStorageFileSystem.
*
* <p>Example of listing buckets, specifying the page size and a name prefix.
*
* <pre>{@code
* String prefix = "bucket_";