diff options
author | Joey Hess <joey@kitenet.net> | 2011-10-04 00:19:42 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2011-10-04 00:19:42 -0400 |
commit | 1a96d4ab35ed5c2af95a1598620cbbd13bc295b3 (patch) | |
tree | 0893a478a8df568c8992db96679029ebadc3985b | |
parent | 8ef2095fa00408ce6729596a42bc0abdc7778098 (diff) |
use bracket to reset environment
In case the exception is caught higher up, don't leave the environment
dirty.
-rw-r--r-- | Branch.hs | 23 |
1 files changed, 11 insertions, 12 deletions
@@ -65,17 +65,13 @@ withIndex' :: Bool -> Annex a -> Annex a withIndex' bootstrapping a = do g <- gitRepo let f = index g - reset <- liftIO $ Git.useIndex f - e <- liftIO $ doesFileExist f - unless e $ do - unless bootstrapping create - liftIO $ createDirectoryIfMissing True $ takeDirectory f - liftIO $ unless bootstrapping $ genIndex g - - r <- a - liftIO reset - return r + bracket (Git.useIndex f) id $ do + unlessM (liftIO $ doesFileExist f) $ do + unless bootstrapping create + liftIO $ createDirectoryIfMissing True $ takeDirectory f + unless bootstrapping $ liftIO $ genIndex g + a withIndexUpdate :: Annex a -> Annex a withIndexUpdate a = update >> withIndex a @@ -332,11 +328,14 @@ lockJournal :: Annex a -> Annex a lockJournal a = do g <- gitRepo let file = gitAnnexJournalLock g - liftIOOp (Control.Exception.bracket (lock file) unlock) run + bracket (lock file) unlock a where lock file = do l <- createFile file stdFileMode waitToSetLock l (WriteLock, AbsoluteSeek, 0, 0) return l unlock = closeFd - run _ = a + +bracket :: IO c -> (c -> IO b) -> Annex a -> Annex a +bracket start cleanup go = + liftIOOp (Control.Exception.bracket start cleanup) (const go) |