Skip to content

Commit

Permalink
Atomically replace objects in filesystem putBlob
Browse files Browse the repository at this point in the history
Fixes gaul/s3proxy#490.  This fixes a regression from
41ce90e that was not possible on
Windows before taking a dependency on Java 7.
  • Loading branch information
gaul committed Jan 21, 2023
1 parent 7e50ae6 commit 2eb561d
Showing 1 changed file with 3 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static java.nio.file.Files.createDirectories;
import static java.nio.file.Files.getFileAttributeView;
import static java.nio.file.Files.getPosixFilePermissions;
import static java.nio.file.Files.move;
import static java.nio.file.Files.probeContentType;
import static java.nio.file.Files.readAttributes;
import static java.nio.file.Files.setPosixFilePermissions;
Expand All @@ -44,6 +45,7 @@
import java.nio.file.AccessDeniedException;
import java.nio.file.DirectoryStream;
import java.nio.file.NoSuchFileException;
import java.nio.file.StandardCopyOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
Expand Down Expand Up @@ -574,10 +576,6 @@ public String putBlob(final String containerName, final Blob blob, BlobAccess ac
eTag = actualHashCode.asBytes();
}

if (outputFile.exists()) {
delete(outputFile);
}

UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(tmpPath);
if (view != null) {
try {
Expand All @@ -590,9 +588,7 @@ public String putBlob(final String containerName, final Blob blob, BlobAccess ac

setBlobAccess(containerName, tmpBlobName, access);

if (!tmpFile.renameTo(outputFile)) {
throw new IOException("Could not rename file " + tmpFile + " to " + outputFile);
}
move(tmpPath, outputFile.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
tmpFile = null;

return base16().lowerCase().encode(eTag);
Expand Down

0 comments on commit 2eb561d

Please sign in to comment.