Skip to content

Commit

Permalink
fix: add precondition to delete operations (googleapis#1240)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWhitehead authored Aug 17, 2023
1 parent 2b1bbe3 commit 57c65c6
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -518,16 +518,32 @@ public boolean deleteIfExists(Path path) throws IOException {
throw new CloudStoragePseudoDirectoryException(cloudPath);
}

BlobId idWithGeneration = cloudPath.getBlobId();
if (idWithGeneration.getGeneration() == null) {
Storage.BlobGetOption[] options = new BlobGetOption[0];
if (!isNullOrEmpty(userProject)) {
options = new BlobGetOption[] {Storage.BlobGetOption.userProject(userProject)};
}
Blob blob = storage.get(idWithGeneration, options);
if (blob == null) {
// not found
return false;
}
idWithGeneration = blob.getBlobId();
}

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());
return storage.delete(idWithGeneration, Storage.BlobSourceOption.generationMatch());
} else {
return storage.delete(
cloudPath.getBlobId(), Storage.BlobSourceOption.userProject(userProject));
idWithGeneration,
Storage.BlobSourceOption.generationMatch(),
Storage.BlobSourceOption.userProject(userProject));
}
} catch (StorageException exs) {
// Will rethrow a StorageException if all retries/reopens are exhausted
Expand Down

0 comments on commit 57c65c6

Please sign in to comment.