-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation of durable file rename #60
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,12 +82,14 @@ module UnliftIO.IO.File | |
, withBinaryFileDurable | ||
, withBinaryFileDurableAtomic | ||
, ensureFileDurable | ||
, renameFileDurable | ||
) | ||
where | ||
|
||
import Data.ByteString as B (ByteString, writeFile) | ||
import Control.Monad.IO.Unlift | ||
import UnliftIO.IO (Handle, IOMode(..), withBinaryFile) | ||
import UnliftIO.Directory (renameFile) | ||
|
||
#if WINDOWS | ||
|
||
|
@@ -102,6 +104,8 @@ withBinaryFileDurable = withBinaryFile | |
withBinaryFileDurableAtomic = withBinaryFile | ||
withBinaryFileAtomic = withBinaryFile | ||
|
||
renameFileDurable = renameFile | ||
|
||
#else | ||
|
||
import qualified Data.ByteString as B (hPut) | ||
|
@@ -119,8 +123,30 @@ writeBinaryFileAtomic fp bytes = | |
withBinaryFileDurable = Posix.withBinaryFileDurable | ||
withBinaryFileDurableAtomic = Posix.withBinaryFileDurableAtomic | ||
withBinaryFileAtomic = Posix.withBinaryFileAtomic | ||
|
||
renameFileDurable = Posix.renameFileDurable | ||
#endif | ||
|
||
-- | When a file is renamed, it is necessary to execute @fsync()@ on the | ||
-- directory that contains the file now and afterwards on the directory where | ||
-- the file was before, so that the rename is durable. | ||
-- | ||
-- Remark: This is also atomic if both locations of the file are on the same | ||
-- filesystem. However, it could happen that the operation leads to data loss, | ||
-- if a crash happens after the rename and before the first fsync finishes. This | ||
-- is because on an async filesystem the write of the old directory might | ||
-- already written to disk and the change on the new directory is not. It the | ||
-- function call returns, the change is durable. Nevertheless, this will not | ||
-- happen on filesystems using journaling, that is, allmost all modern filesystems. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it not a bit confusing to say "This is also atomic"? Because it should be only atomic on the same filesystem --
|
||
-- | ||
-- === Cross-Platform support | ||
-- | ||
-- This function is a noop on Windows platforms. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this text needs to be adjusted: On |
||
-- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback, added those. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Will do! |
||
-- @since 0.2.14 | ||
renameFileDurable :: MonadIO m => FilePath -> FilePath -> m () | ||
-- Implementation is at the top of the module | ||
|
||
-- | After a file is closed, this function opens it again and executes @fsync()@ | ||
-- internally on both the file and the directory that contains it. Note that this function | ||
-- is intended to work around the non-durability of existing file APIs, as opposed to | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ module UnliftIO.IO.File.Posix | |
, withBinaryFileDurableAtomic | ||
, withBinaryFileAtomic | ||
, ensureFileDurable | ||
, renameFileDurable | ||
) | ||
where | ||
|
||
|
@@ -39,6 +40,7 @@ import System.IO.Error (ioeGetErrorType, isAlreadyExistsError, | |
import qualified System.Posix.Files as Posix | ||
import System.Posix.Internals (CFilePath, c_close, c_safe_open, withFilePath) | ||
import System.Posix.Types (CMode(..), Fd(..), FileMode) | ||
import UnliftIO.Directory (renameFile) | ||
import UnliftIO.Exception | ||
import UnliftIO.IO | ||
import UnliftIO.MVar | ||
|
@@ -580,3 +582,13 @@ withBinaryFileAtomic filePath iomode action = | |
liftIO $ atomicTempFileRename Nothing mFileMode eTmpFile filePath | ||
pure res | ||
|
||
|
||
-- | See `renameFileDurable` | ||
renameFileDurable :: | ||
MonadIO m => FilePath -> FilePath -> m () | ||
renameFileDurable oldName newName = | ||
liftIO $ withDirectory (takeDirectory oldName) $ \oldDirFd -> | ||
withDirectory (takeDirectory newName) $ \newDirFd -> do | ||
renameFile oldName newName | ||
fsyncDirectoryFd "renameFileDurable" newDirFd | ||
fsyncDirectoryFd "renameFileDurable" oldDirFd | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possible optimisation: For the common case that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another possible improvement could be to use That would be even better than what we have here, because then the That is not necessary for this PR though, having a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@snoyberg FYI Looks like the
0.2.13
went missing.