From be928262b241d40e7a6bf00ed84509c63f117e2b Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sat, 21 Jan 2023 22:49:28 +0900 Subject: [PATCH] Atomically replace objects in filesystem putBlob Fixes gaul/s3proxy#490. This fixes a regression from 41ce90ec360a46cb7b03f7cb8b66e01a113aa5b6. Continue to support Windows logic although it is unclear if this is necessary. --- .../strategy/internal/FilesystemStorageStrategyImpl.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java b/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java index f34b3aaf7c7..3f5b9ea0b87 100644 --- a/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java +++ b/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java @@ -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; @@ -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; @@ -574,7 +576,8 @@ public String putBlob(final String containerName, final Blob blob, BlobAccess ac eTag = actualHashCode.asBytes(); } - if (outputFile.exists()) { + // TODO: is this necessary? + if (isWindows() && outputFile.exists()) { delete(outputFile); } @@ -590,9 +593,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);