2014-07-10 04:32:23 +00:00
|
|
|
{- git-annex lock files.
|
|
|
|
-
|
2020-06-17 19:13:52 +00:00
|
|
|
- Copyright 2012-2020 Joey Hess <id@joeyh.name>
|
2014-07-10 04:32:23 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2014-07-10 04:32:23 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# 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,
|
2019-01-17 19:40:44 +00:00
|
|
|
withSharedLock,
|
2014-07-10 04:32:23 +00:00
|
|
|
withExclusiveLock,
|
2019-03-04 21:50:41 +00:00
|
|
|
takeExclusiveLock,
|
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-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
|
2020-10-28 21:25:59 +00:00
|
|
|
import qualified System.FilePath.ByteString as P
|
2014-07-10 04:32:23 +00:00
|
|
|
|
|
|
|
{- Create a specified lock file, and takes a shared lock, which is retained
|
2015-05-18 20:23:07 +00:00
|
|
|
- in the cache. -}
|
2020-10-29 14:33:12 +00:00
|
|
|
lockFileCached :: RawFilePath -> Annex ()
|
2015-05-18 20:23:07 +00:00
|
|
|
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
|
2023-04-27 19:57:50 +00:00
|
|
|
lockhandle <- 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
|
|
|
|
2020-10-29 14:33:12 +00:00
|
|
|
unlockFile :: RawFilePath -> 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
|
|
|
|
2020-10-29 14:33:12 +00:00
|
|
|
fromLockCache :: RawFilePath -> Annex (Maybe LockHandle)
|
2015-05-18 20:23:07 +00:00
|
|
|
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
|
|
|
|
2019-01-17 19:40:44 +00:00
|
|
|
{- Runs an action with a shared lock held. If an exclusive lock is held,
|
|
|
|
- blocks until it becomes free. -}
|
2022-08-11 20:57:44 +00:00
|
|
|
withSharedLock :: RawFilePath -> Annex a -> Annex a
|
|
|
|
withSharedLock lockfile a = debugLocks $ do
|
2020-10-28 21:25:59 +00:00
|
|
|
createAnnexDirectory $ P.takeDirectory lockfile
|
2019-01-17 19:40:44 +00:00
|
|
|
mode <- annexFileMode
|
2020-10-29 14:33:12 +00:00
|
|
|
bracket (lock mode lockfile) (liftIO . dropLock) (const a)
|
2019-01-17 19:40:44 +00:00
|
|
|
where
|
|
|
|
#ifndef mingw32_HOST_OS
|
2023-04-27 19:57:50 +00:00
|
|
|
lock mode = lockShared (Just mode)
|
2019-01-17 19:40:44 +00:00
|
|
|
#else
|
|
|
|
lock _mode = liftIO . waitToLock . lockShared
|
|
|
|
#endif
|
|
|
|
|
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. -}
|
2022-08-11 20:57:44 +00:00
|
|
|
withExclusiveLock :: RawFilePath -> Annex a -> Annex a
|
|
|
|
withExclusiveLock lockfile a = bracket
|
|
|
|
(takeExclusiveLock lockfile)
|
2019-03-04 21:50:41 +00:00
|
|
|
(liftIO . dropLock)
|
|
|
|
(const a)
|
|
|
|
|
|
|
|
{- Takes an exclusive lock, blocking until it's free. -}
|
2022-08-11 20:57:44 +00:00
|
|
|
takeExclusiveLock :: RawFilePath -> Annex LockHandle
|
|
|
|
takeExclusiveLock lockfile = debugLocks $ do
|
2020-10-28 21:25:59 +00:00
|
|
|
createAnnexDirectory $ P.takeDirectory lockfile
|
2014-07-10 04:32:23 +00:00
|
|
|
mode <- annexFileMode
|
2020-10-29 14:33:12 +00:00
|
|
|
lock mode lockfile
|
2014-07-10 04:32:23 +00:00
|
|
|
where
|
|
|
|
#ifndef mingw32_HOST_OS
|
2023-04-27 19:57:50 +00:00
|
|
|
lock 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. -}
|
2022-08-11 20:57:44 +00:00
|
|
|
tryExclusiveLock :: RawFilePath -> Annex a -> Annex (Maybe a)
|
|
|
|
tryExclusiveLock lockfile a = debugLocks $ do
|
2020-10-28 21:25:59 +00:00
|
|
|
createAnnexDirectory $ P.takeDirectory lockfile
|
2015-02-17 17:04:22 +00:00
|
|
|
mode <- annexFileMode
|
2020-10-29 14:33:12 +00:00
|
|
|
bracket (lock mode lockfile) (liftIO . unlock) go
|
2015-02-17 17:04:22 +00:00
|
|
|
where
|
|
|
|
#ifndef mingw32_HOST_OS
|
2023-04-27 19:57:50 +00:00
|
|
|
lock mode = tryLockExclusive (Just mode)
|
2015-02-17 17:04:22 +00:00
|
|
|
#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
|