Skip to content

Commit a8222f3

Browse files
authored
fix: add precondition to delete operations (#1240)
1 parent 7bc7c2d commit a8222f3

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,16 +518,32 @@ public boolean deleteIfExists(Path path) throws IOException {
518518
throw new CloudStoragePseudoDirectoryException(cloudPath);
519519
}
520520

521+
BlobId idWithGeneration = cloudPath.getBlobId();
522+
if (idWithGeneration.getGeneration() == null) {
523+
Storage.BlobGetOption[] options = new BlobGetOption[0];
524+
if (!isNullOrEmpty(userProject)) {
525+
options = new BlobGetOption[] {Storage.BlobGetOption.userProject(userProject)};
526+
}
527+
Blob blob = storage.get(idWithGeneration, options);
528+
if (blob == null) {
529+
// not found
530+
return false;
531+
}
532+
idWithGeneration = blob.getBlobId();
533+
}
534+
521535
final CloudStorageRetryHandler retryHandler =
522536
new CloudStorageRetryHandler(cloudPath.getFileSystem().config());
523537
// Loop will terminate via an exception if all retries are exhausted
524538
while (true) {
525539
try {
526540
if (isNullOrEmpty(userProject)) {
527-
return storage.delete(cloudPath.getBlobId());
541+
return storage.delete(idWithGeneration, Storage.BlobSourceOption.generationMatch());
528542
} else {
529543
return storage.delete(
530-
cloudPath.getBlobId(), Storage.BlobSourceOption.userProject(userProject));
544+
idWithGeneration,
545+
Storage.BlobSourceOption.generationMatch(),
546+
Storage.BlobSourceOption.userProject(userProject));
531547
}
532548
} catch (StorageException exs) {
533549
// Will rethrow a StorageException if all retries/reopens are exhausted

0 commit comments

Comments
 (0)