Skip to content

Commit

Permalink
fix: check generation for nullability before incrementing it (#888)
Browse files Browse the repository at this point in the history
  • Loading branch information
domrde committed Apr 20, 2022
1 parent 34e1c59 commit cadf081
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,10 @@ public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws Storage
// if this is a new file, set generation to 1, else increment the existing generation
long generation = 1;
if (metadata.containsKey(destKey)) {
generation = metadata.get(destKey).getGeneration() + 1;
Long storedGeneration = metadata.get(destKey).getGeneration();
if (null != storedGeneration) {
generation = storedGeneration + 1;
}
}

checkGeneration(destKey, generationMatch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,28 @@ public void testStorageOptionIsSerializable_customOptions() throws Exception {
assertThat(ois.readObject()).isEqualTo(storageOptions);
}
}

@Test
public void testCopyOperationOverwritesExistingFile() {
String bucket = "bucket";
String original = "original";
String replacement = "replacement";
byte[] originalContent = "original content".getBytes();
byte[] replacementContent = "replacement content".getBytes();

localStorageService.create(BlobInfo.newBuilder(bucket, original).build(), originalContent);
localStorageService.create(
BlobInfo.newBuilder(bucket, replacement).build(), replacementContent);

final Storage.CopyRequest request =
Storage.CopyRequest.newBuilder()
.setSource(BlobId.of(bucket, replacement))
.setTarget(BlobId.of(bucket, original))
.build();

localStorageService.copy(request).getResult();

assertThat(localStorageService.readAllBytes(BlobId.of(bucket, original)))
.isEqualTo(replacementContent);
}
}

0 comments on commit cadf081

Please sign in to comment.