summaryrefslogtreecommitdiffhomepage
path: root/Storage
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2016-08-11 20:44:07 -0400
committerJoey Hess <joeyh@joeyh.name>2016-08-11 20:44:07 -0400
commita3834e558cf6ae04826b44e65a02ee22286f7952 (patch)
tree571159729a9392579aa993e3f3da8dd58745c0b3 /Storage
parentab3594dacb0461ae5e253544f65c3e3d50eb721d (diff)
downloadkeysafe-a3834e558cf6ae04826b44e65a02ee22286f7952.tar.gz
write via temp file
avoids short reads, and also if a backup program came along while the write was happening, avoids short backups
Diffstat (limited to 'Storage')
-rw-r--r--Storage/LocalFiles.hs8
1 files changed, 7 insertions, 1 deletions
diff --git a/Storage/LocalFiles.hs b/Storage/LocalFiles.hs
index 083a74e..b81ecbe 100644
--- a/Storage/LocalFiles.hs
+++ b/Storage/LocalFiles.hs
@@ -34,11 +34,14 @@ store :: StorableObjectIdent -> Shard -> IO StoreResult
store i s = onError (StoreFailure . show) $ do
dir <- shardDir
createDirectoryIfMissing True dir
- fd <- openFd (dir </> shardFile i) WriteOnly (Just 0o666)
+ let dest = dir </> shardFile i
+ let tmp = dest ++ ".tmp"
+ fd <- openFd tmp WriteOnly (Just 0o666)
(defaultFileFlags { exclusive = True } )
h <- fdToHandle fd
B.hPut h (toByteString s)
hClose h
+ renameFile tmp dest
return StoreSuccess
retrieve :: ShardNum -> StorableObjectIdent -> IO RetrieveResult
@@ -56,6 +59,9 @@ retrieve n i = onError (RetrieveFailure . show) $ do
-- There is no way to set the ctime to the epoch, but setting the other
-- times does at least set it to the current time, which makes all
-- currently stored files look alike.
+--
+-- Note that the contents of shards is never changed, so it's ok to set the
+-- mtime to the epoch; backup programs won't be confused.
obscure :: IO ObscureResult
obscure = onError (ObscureFailure . show) $ do
dir <- shardDir