factor out nukeFile

This commit is contained in:
Joey Hess 2012-06-06 13:13:13 -04:00
parent 723eb19bbf
commit 993e6459a3
5 changed files with 13 additions and 9 deletions

View file

@ -168,7 +168,7 @@ withTmp :: Key -> (FilePath -> Annex a) -> Annex a
withTmp key action = do withTmp key action = do
tmp <- prepTmp key tmp <- prepTmp key
res <- action tmp res <- action tmp
liftIO $ whenM (doesFileExist tmp) $ liftIO $ removeFile tmp liftIO $ nukeFile tmp
return res return res
{- Checks that there is disk space available to store a given key, {- Checks that there is disk space available to store a given key,

View file

@ -57,13 +57,10 @@ lockDown file = do
createAnnexDirectory tmp createAnnexDirectory tmp
pid <- liftIO getProcessID pid <- liftIO getProcessID
let tmpfile = tmp </> "add" ++ show pid ++ "." ++ takeFileName file let tmpfile = tmp </> "add" ++ show pid ++ "." ++ takeFileName file
nuke tmpfile liftIO $ nukeFile tmpfile
liftIO $ createLink file tmpfile liftIO $ createLink file tmpfile
return tmpfile return tmpfile
nuke :: FilePath -> Annex ()
nuke file = liftIO $ whenM (doesFileExist file) $ removeFile file
{- Moves the file into the annex. -} {- Moves the file into the annex. -}
ingest :: FilePath -> Annex (Maybe Key) ingest :: FilePath -> Annex (Maybe Key)
ingest file = do ingest file = do
@ -75,7 +72,7 @@ ingest file = do
go _ Nothing = return Nothing go _ Nothing = return Nothing
go tmpfile (Just (key, _)) = do go tmpfile (Just (key, _)) = do
handle (undo file key) $ moveAnnex key tmpfile handle (undo file key) $ moveAnnex key tmpfile
nuke file liftIO $ nukeFile file
return $ Just key return $ Just key
perform :: FilePath -> CommandPerform perform :: FilePath -> CommandPerform
@ -86,7 +83,7 @@ perform file = maybe stop (\key -> next $ cleanup file key True) =<< ingest file
undo :: FilePath -> Key -> IOException -> Annex a undo :: FilePath -> Key -> IOException -> Annex a
undo file key e = do undo file key e = do
whenM (inAnnex key) $ do whenM (inAnnex key) $ do
nuke file liftIO $ nukeFile file
handle tryharder $ fromAnnex key file handle tryharder $ fromAnnex key file
logStatus key InfoMissing logStatus key InfoMissing
throw e throw e

View file

@ -40,5 +40,5 @@ perform key = maybe droplocal dropremote =<< Remote.byName =<< from
performOther :: (Key -> Git.Repo -> FilePath) -> Key -> CommandPerform performOther :: (Key -> Git.Repo -> FilePath) -> Key -> CommandPerform
performOther filespec key = do performOther filespec key = do
f <- fromRepo $ filespec key f <- fromRepo $ filespec key
liftIO $ whenM (doesFileExist f) $ removeFile f liftIO $ nukeFile f
next $ return True next $ return True

View file

@ -88,6 +88,13 @@ moveFile src dest = tryIO (rename src dest) >>= onrename
(Left _) -> return False (Left _) -> return False
(Right s) -> return $ isDirectory s (Right s) -> return $ isDirectory s
{- Removes a file, which may or may not exist.
-
- Note that an exception is thrown if the file exists but
- cannot be removed. -}
nukeFile :: FilePath -> IO ()
nukeFile file = whenM (doesFileExist file) $ removeFile file
{- Runs an action in another directory. -} {- Runs an action in another directory. -}
bracketCd :: FilePath -> IO a -> IO a bracketCd :: FilePath -> IO a -> IO a
bracketCd dir a = go =<< getCurrentDirectory bracketCd dir a = go =<< getCurrentDirectory

View file

@ -28,7 +28,7 @@ setup :: Git.Repo -> IO ()
setup = cleanup -- idempotency setup = cleanup -- idempotency
cleanup :: Git.Repo -> IO () cleanup :: Git.Repo -> IO ()
cleanup g = whenM (doesFileExist $ tmpIndex g) $ removeFile $ tmpIndex g cleanup g = nukeFile $ tmpIndex g
parseArgs :: IO [String] parseArgs :: IO [String]
parseArgs = do parseArgs = do