2014-07-10 04:32:23 +00:00
|
|
|
{- git-annex lock files.
|
|
|
|
-
|
2015-02-17 17:04:22 +00:00
|
|
|
- Copyright 2012-2015 Joey Hess <id@joeyh.name>
|
2014-07-10 04:32:23 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
|
|
|
module Annex.LockFile (
|
2015-05-18 20:23:07 +00:00
|
|
|
lockFileCached,
|
2014-07-10 04:32:23 +00:00
|
|
|
unlockFile,
|
2015-05-18 20:23:07 +00:00
|
|
|
getLockCache,
|
2017-05-11 21:33:18 +00:00
|
|
|
fromLockCache,
|
2014-07-10 04:32:23 +00:00
|
|
|
withExclusiveLock,
|
2015-02-17 17:04:22 +00:00
|
|
|
tryExclusiveLock,
|
2014-07-10 04:32:23 +00:00
|
|
|
) where
|
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2014-07-10 04:32:23 +00:00
|
|
|
import Annex
|
2015-05-18 20:23:07 +00:00
|
|
|
import Types.LockCache
|
2014-07-10 04:32:23 +00:00
|
|
|
import qualified Git
|
2014-07-14 19:55:48 +00:00
|
|
|
import Annex.Perms
|
2015-11-12 22:05:45 +00:00
|
|
|
import Annex.LockPool
|
2014-07-10 04:32:23 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
|
|
|
{- Create a specified lock file, and takes a shared lock, which is retained
|
2015-05-18 20:23:07 +00:00
|
|
|
- in the cache. -}
|
|
|
|
lockFileCached :: FilePath -> Annex ()
|
|
|
|
lockFileCached file = go =<< fromLockCache file
|
2014-07-10 04:32:23 +00:00
|
|
|
where
|
|
|
|
go (Just _) = noop -- already locked
|
|
|
|
go Nothing = do
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
mode <- annexFileMode
|
2015-11-12 22:05:45 +00:00
|
|
|
lockhandle <- noUmask mode $ lockShared (Just mode) file
|
2014-07-10 04:32:23 +00:00
|
|
|
#else
|
|
|
|
lockhandle <- liftIO $ waitToLock $ lockShared file
|
|
|
|
#endif
|
2015-05-18 20:23:07 +00:00
|
|
|
changeLockCache $ M.insert file lockhandle
|
2014-07-10 04:32:23 +00:00
|
|
|
|
|
|
|
unlockFile :: FilePath -> Annex ()
|
2015-05-18 20:23:07 +00:00
|
|
|
unlockFile file = maybe noop go =<< fromLockCache file
|
2014-07-10 04:32:23 +00:00
|
|
|
where
|
|
|
|
go lockhandle = do
|
|
|
|
liftIO $ dropLock lockhandle
|
2015-05-18 20:23:07 +00:00
|
|
|
changeLockCache $ M.delete file
|
2014-07-10 04:32:23 +00:00
|
|
|
|
2015-05-18 20:23:07 +00:00
|
|
|
getLockCache :: Annex LockCache
|
|
|
|
getLockCache = getState lockcache
|
2014-07-10 04:32:23 +00:00
|
|
|
|
2015-05-18 20:23:07 +00:00
|
|
|
fromLockCache :: FilePath -> Annex (Maybe LockHandle)
|
|
|
|
fromLockCache file = M.lookup file <$> getLockCache
|
2014-07-10 04:32:23 +00:00
|
|
|
|
2015-05-18 20:23:07 +00:00
|
|
|
changeLockCache :: (LockCache -> LockCache) -> Annex ()
|
|
|
|
changeLockCache a = do
|
|
|
|
m <- getLockCache
|
|
|
|
changeState $ \s -> s { lockcache = a m }
|
2014-07-10 04:32:23 +00:00
|
|
|
|
|
|
|
{- Runs an action with an exclusive lock held. If the lock is already
|
|
|
|
- held, blocks until it becomes free. -}
|
|
|
|
withExclusiveLock :: (Git.Repo -> FilePath) -> Annex a -> Annex a
|
2018-11-19 19:00:24 +00:00
|
|
|
withExclusiveLock getlockfile a = debugLocks $ do
|
2014-07-10 04:32:23 +00:00
|
|
|
lockfile <- fromRepo getlockfile
|
|
|
|
createAnnexDirectory $ takeDirectory lockfile
|
|
|
|
mode <- annexFileMode
|
2015-11-12 22:05:45 +00:00
|
|
|
bracket (lock mode lockfile) (liftIO . dropLock) (const a)
|
2014-07-10 04:32:23 +00:00
|
|
|
where
|
|
|
|
#ifndef mingw32_HOST_OS
|
2014-08-20 20:45:58 +00:00
|
|
|
lock mode = noUmask mode . lockExclusive (Just mode)
|
2014-07-10 04:32:23 +00:00
|
|
|
#else
|
2015-11-12 22:05:45 +00:00
|
|
|
lock _mode = liftIO . waitToLock . lockExclusive
|
2014-07-10 04:32:23 +00:00
|
|
|
#endif
|
2015-02-17 17:04:22 +00:00
|
|
|
|
|
|
|
{- Tries to take an exclusive lock and run an action. If the lock is
|
|
|
|
- already held, returns Nothing. -}
|
|
|
|
tryExclusiveLock :: (Git.Repo -> FilePath) -> Annex a -> Annex (Maybe a)
|
2018-11-19 19:00:24 +00:00
|
|
|
tryExclusiveLock getlockfile a = debugLocks $ do
|
2015-02-17 17:04:22 +00:00
|
|
|
lockfile <- fromRepo getlockfile
|
|
|
|
createAnnexDirectory $ takeDirectory lockfile
|
|
|
|
mode <- annexFileMode
|
2015-11-12 22:05:45 +00:00
|
|
|
bracket (lock mode lockfile) (liftIO . unlock) go
|
2015-02-17 17:04:22 +00:00
|
|
|
where
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
lock mode = noUmask mode . tryLockExclusive (Just mode)
|
|
|
|
#else
|
2015-11-12 22:05:45 +00:00
|
|
|
lock _mode = liftIO . lockExclusive
|
2015-02-17 17:04:22 +00:00
|
|
|
#endif
|
|
|
|
unlock = maybe noop dropLock
|
|
|
|
go Nothing = return Nothing
|
|
|
|
go (Just _) = Just <$> a
|