diff options
author | 2014-08-20 16:45:58 -0400 | |
---|---|---|
committer | 2014-08-20 16:45:58 -0400 | |
commit | 679d9c0027ac996eea9f41a6f0f39af436801e89 (patch) | |
tree | d98ae7ba049e2daa4a99e6bcf55054bde5e5ab4d /Annex/Content.hs | |
parent | 86f9d7734d0c638d4c966fd87a5d7e97759e125b (diff) |
reorganize and refactor lock code
Added a convenience Utility.LockFile that is not a windows/posix
portability shim, but still manages to cut down on the boilerplate around
locking.
This commit was sponsored by Johan Herland.
Diffstat (limited to 'Annex/Content.hs')
-rw-r--r-- | Annex/Content.hs | 16 |
1 files changed, 5 insertions, 11 deletions
diff --git a/Annex/Content.hs b/Annex/Content.hs index 2a05c2dac..31a4444af 100644 --- a/Annex/Content.hs +++ b/Annex/Content.hs @@ -56,10 +56,7 @@ import Annex.Perms import Annex.Link import Annex.Content.Direct import Annex.ReplaceFile - -#ifdef mingw32_HOST_OS -import Utility.WinLock -#endif +import Utility.LockFile {- Checks if a given key's content is currently present. -} inAnnex :: Key -> Annex Bool @@ -177,24 +174,21 @@ lockContent key a = do nukeFile lockfile #ifndef mingw32_HOST_OS lock contentfile Nothing = opencontentforlock contentfile >>= dolock - lock _ (Just lockfile) = openforlock lockfile >>= dolock . Just + lock _ (Just lockfile) = createLockFile Nothing lockfile >>= dolock . Just {- Since content files are stored with the write bit disabled, have - to fiddle with permissions to open for an exclusive lock. -} opencontentforlock f = catchMaybeIO $ ifM (doesFileExist f) ( withModifiedFileMode f (`unionFileModes` ownerWriteMode) - (openforlock f) - , openforlock f + (createLockFie Nothing f) + , createLockFile Nothing f ) - openforlock f = openFd f ReadWrite Nothing defaultFileFlags dolock Nothing = return Nothing dolock (Just fd) = do v <- tryIO $ setLock fd (WriteLock, AbsoluteSeek, 0, 0) case v of Left _ -> alreadylocked - Right _ -> do - setFdOption fd CloseOnExec True - return $ Just fd + Right _ -> return $ Just fd unlock mlockfile mfd = do maybe noop cleanuplockfile mlockfile liftIO $ maybe noop closeFd mfd |