Repository: jclouds Updated Branches: refs/heads/master 4ce357ba6 -> f706d9c13
JCLOUDS-835: Atomically put filesystem blobs Write to a temporary file and rename to the desired destination to better match object store semantics. Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/f706d9c1 Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/f706d9c1 Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/f706d9c1 Branch: refs/heads/master Commit: f706d9c13a78f360d5e0c94c74251a892ce0e66e Parents: 4ce357b Author: Andrew Gaul <[email protected]> Authored: Fri Nov 20 14:37:43 2015 -0800 Committer: Andrew Gaul <[email protected]> Committed: Fri Nov 20 14:44:02 2015 -0800 ---------------------------------------------------------------------- .../internal/FilesystemStorageStrategyImpl.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jclouds/blob/f706d9c1/apis/filesystem/src/main/java/org/jclouds/filesystem/strategy/internal/FilesystemStorageStrategyImpl.java ---------------------------------------------------------------------- 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 1f14d68..bb4e940 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 @@ -43,6 +43,7 @@ import java.util.Collection; import java.util.Date; import java.util.Map; import java.util.Set; +import java.util.UUID; import javax.annotation.Resource; import javax.inject.Inject; @@ -450,13 +451,14 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy { return putDirectoryBlob(containerName, blob); } File outputFile = getFileForBlobKey(containerName, blobKey); + // TODO: should we use a known suffix to filter these out during list? + File tmpFile = getFileForBlobKey(containerName, blobKey + "-" + UUID.randomUUID()); Path outputPath = outputFile.toPath(); HashingInputStream his = null; try { - Files.createParentDirs(outputFile); + Files.createParentDirs(tmpFile); his = new HashingInputStream(Hashing.md5(), payload.openStream()); - delete(outputFile); - Files.asByteSink(outputFile).writeFrom(his); + Files.asByteSink(tmpFile).writeFrom(his); HashCode actualHashCode = his.hash(); HashCode expectedHashCode = payload.getContentMetadata().getContentMD5AsHashCode(); if (expectedHashCode != null && !actualHashCode.equals(expectedHashCode)) { @@ -465,6 +467,8 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy { } payload.getContentMetadata().setContentMD5(actualHashCode); + tmpFile.renameTo(outputFile); + UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(outputPath); if (view != null) { try { @@ -477,11 +481,11 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy { setBlobAccess(containerName, blobKey, BlobAccess.PRIVATE); return base16().lowerCase().encode(actualHashCode.asBytes()); } catch (IOException ex) { - if (outputFile != null) { + if (tmpFile != null) { try { - delete(outputFile); + delete(tmpFile); } catch (IOException e) { - logger.debug("Could not delete %s: %s", outputFile, e); + logger.debug("Could not delete %s: %s", tmpFile, e); } } throw ex;
