diff options
author | Joey Hess <joeyh@joeyh.name> | 2016-08-12 00:23:18 -0400 |
---|---|---|
committer | Joey Hess <joeyh@joeyh.name> | 2016-08-12 00:23:18 -0400 |
commit | 71a1bdf294b2ec84115f98d2f8a214d5ac8b396f (patch) | |
tree | c2398d8b7833c00f3a7d46d722a286c55c6decae /Storage | |
parent | 63aa819fe5e5a8d72b202f6f0e0f407fa149961f (diff) | |
download | keysafe-71a1bdf294b2ec84115f98d2f8a214d5ac8b396f.tar.gz |
don't allow overwrite of object file
Diffstat (limited to 'Storage')
-rw-r--r-- | Storage/LocalFiles.hs | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/Storage/LocalFiles.hs b/Storage/LocalFiles.hs index fd77ba1..f34a8b5 100644 --- a/Storage/LocalFiles.hs +++ b/Storage/LocalFiles.hs @@ -36,14 +36,18 @@ store i s = onError (StoreFailure . show) $ do dir <- shardDir createDirectoryIfMissing True dir 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 + exists <- doesFileExist dest + if exists + then return $ StoreFailure "file already exists" + else do + 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 retrieve n i = onError (RetrieveFailure . show) $ do |