generalized safeWriteFile to viaTmp

This commit is contained in:
Joey Hess 2011-06-30 00:42:09 -04:00
parent d72fb5acc2
commit 2cda9d0a0f
4 changed files with 9 additions and 8 deletions

View file

@ -55,7 +55,7 @@ gitPreCommitHookWrite repo = do
if exists if exists
then warning $ "pre-commit hook (" ++ hook ++ ") already exists, not configuring" then warning $ "pre-commit hook (" ++ hook ++ ") already exists, not configuring"
else liftIO $ do else liftIO $ do
safeWriteFile hook preCommitScript viaTmp writeFile hook preCommitScript
p <- getPermissions hook p <- getPermissions hook
setPermissions hook $ p {executable = True} setPermissions hook $ p {executable = True}
where where

View file

@ -86,7 +86,7 @@ checkRemoteUnused' r = do
writeUnusedFile :: FilePath -> [(Int, Key)] -> Annex () writeUnusedFile :: FilePath -> [(Int, Key)] -> Annex ()
writeUnusedFile prefix l = do writeUnusedFile prefix l = do
g <- Annex.gitRepo g <- Annex.gitRepo
liftIO $ safeWriteFile (gitAnnexUnusedLog prefix g) $ liftIO $ viaTmp writeFile (gitAnnexUnusedLog prefix g) $
unlines $ map (\(n, k) -> show n ++ " " ++ show k) l unlines $ map (\(n, k) -> show n ++ " " ++ show k) l
table :: [(Int, Key)] -> [String] table :: [(Int, Key)] -> [String]

View file

@ -128,7 +128,7 @@ gitAttributesUnWrite repo = do
let attributes = Git.attributes repo let attributes = Git.attributes repo
whenM (doesFileExist attributes) $ do whenM (doesFileExist attributes) $ do
c <- readFileStrict attributes c <- readFileStrict attributes
liftIO $ safeWriteFile attributes $ unlines $ liftIO $ viaTmp writeFile attributes $ unlines $
filter (\l -> not $ l `elem` attrLines) $ lines c filter (\l -> not $ l `elem` attrLines) $ lines c
Git.run repo "add" [File attributes] Git.run repo "add" [File attributes]

View file

@ -22,7 +22,7 @@ module Utility (
shellUnEscape, shellUnEscape,
unsetFileMode, unsetFileMode,
readMaybe, readMaybe,
safeWriteFile, viaTmp,
dirContains, dirContains,
dirContents, dirContents,
myHomeDir, myHomeDir,
@ -243,13 +243,14 @@ readMaybe s = case reads s of
((x,_):_) -> Just x ((x,_):_) -> Just x
_ -> Nothing _ -> Nothing
{- Writes a file using a temp file that is renamed atomically into place. -} {- Runs an action like writeFile, writing to a tmp file first and
safeWriteFile :: FilePath -> String -> IO () - then moving it into place. -}
safeWriteFile file content = do viaTmp :: (FilePath -> String -> IO ()) -> FilePath -> String -> IO ()
viaTmp a file content = do
pid <- getProcessID pid <- getProcessID
let tmpfile = file ++ ".tmp" ++ show pid let tmpfile = file ++ ".tmp" ++ show pid
createDirectoryIfMissing True (parentDir file) createDirectoryIfMissing True (parentDir file)
writeFile tmpfile content a tmpfile content
renameFile tmpfile file renameFile tmpfile file
{- Lists the contents of a directory. {- Lists the contents of a directory.