2015-11-12 21:47:31 +00:00
|
|
|
{- Wraps Utility.LockPool, making pid locks be used when git-annex is so
|
|
|
|
- configured.
|
|
|
|
-
|
2021-12-03 21:20:21 +00:00
|
|
|
- Copyright 2015-2021 Joey Hess <id@joeyh.name>
|
2015-11-12 21:47:31 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2015-11-12 21:47:31 +00:00
|
|
|
-}
|
|
|
|
|
2015-11-12 22:05:45 +00:00
|
|
|
module Annex.LockPool.PosixOrPid (
|
|
|
|
LockFile,
|
|
|
|
LockHandle,
|
|
|
|
lockShared,
|
|
|
|
lockExclusive,
|
|
|
|
tryLockShared,
|
|
|
|
tryLockExclusive,
|
|
|
|
dropLock,
|
|
|
|
checkLocked,
|
|
|
|
LockStatus(..),
|
|
|
|
getLockStatus,
|
|
|
|
checkSaneLock,
|
|
|
|
) where
|
2015-11-12 21:47:31 +00:00
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Common
|
|
|
|
import Types
|
2015-11-12 21:47:31 +00:00
|
|
|
import qualified Annex
|
|
|
|
import qualified Utility.LockPool.Posix as Posix
|
|
|
|
import qualified Utility.LockPool.PidLock as Pid
|
2015-11-16 19:35:41 +00:00
|
|
|
import qualified Utility.LockPool.LockHandle as H
|
2023-04-27 19:57:50 +00:00
|
|
|
import Utility.FileMode
|
2015-11-16 19:35:41 +00:00
|
|
|
import Utility.LockPool.LockHandle (LockHandle, dropLock)
|
2015-11-12 21:47:31 +00:00
|
|
|
import Utility.LockFile.Posix (openLockFile)
|
2021-12-03 21:20:21 +00:00
|
|
|
import Utility.LockPool.STM (LockFile, LockMode(..))
|
2015-11-12 21:47:31 +00:00
|
|
|
import Utility.LockFile.LockStatus
|
2020-07-01 15:21:10 +00:00
|
|
|
import Config (pidLockFile)
|
2020-08-26 17:05:34 +00:00
|
|
|
import Messages (warning)
|
2023-04-12 21:18:29 +00:00
|
|
|
import Git.Quote
|
2015-11-12 21:47:31 +00:00
|
|
|
|
|
|
|
import System.Posix
|
|
|
|
|
2023-04-27 19:57:50 +00:00
|
|
|
lockShared :: Maybe ModeSetter -> LockFile -> Annex LockHandle
|
2021-12-03 21:20:21 +00:00
|
|
|
lockShared m f = pidLock m f LockShared $ Posix.lockShared m f
|
2015-11-12 21:47:31 +00:00
|
|
|
|
2023-04-27 19:57:50 +00:00
|
|
|
lockExclusive :: Maybe ModeSetter -> LockFile -> Annex LockHandle
|
2021-12-03 21:20:21 +00:00
|
|
|
lockExclusive m f = pidLock m f LockExclusive $ Posix.lockExclusive m f
|
2015-11-12 21:47:31 +00:00
|
|
|
|
2023-04-27 19:57:50 +00:00
|
|
|
tryLockShared :: Maybe ModeSetter -> LockFile -> Annex (Maybe LockHandle)
|
2021-12-03 21:20:21 +00:00
|
|
|
tryLockShared m f = tryPidLock m f LockShared $ Posix.tryLockShared m f
|
2015-11-12 21:47:31 +00:00
|
|
|
|
2023-04-27 19:57:50 +00:00
|
|
|
tryLockExclusive :: Maybe ModeSetter -> LockFile -> Annex (Maybe LockHandle)
|
2021-12-03 21:20:21 +00:00
|
|
|
tryLockExclusive m f = tryPidLock m f LockExclusive $ Posix.tryLockExclusive m f
|
2015-11-12 21:47:31 +00:00
|
|
|
|
|
|
|
checkLocked :: LockFile -> Annex (Maybe Bool)
|
2016-03-01 16:12:57 +00:00
|
|
|
checkLocked f = Posix.checkLocked f `pidLockCheck` checkpid
|
|
|
|
where
|
2017-11-15 20:55:38 +00:00
|
|
|
checkpid pidlock = Pid.checkLocked pidlock >>= \case
|
|
|
|
-- Only return true when the posix lock file exists.
|
|
|
|
Just _ -> Posix.checkLocked f
|
|
|
|
Nothing -> return Nothing
|
2015-11-12 21:47:31 +00:00
|
|
|
|
|
|
|
getLockStatus :: LockFile -> Annex LockStatus
|
|
|
|
getLockStatus f = Posix.getLockStatus f
|
|
|
|
`pidLockCheck` Pid.getLockStatus
|
|
|
|
|
2015-11-16 19:35:41 +00:00
|
|
|
checkSaneLock :: LockFile -> LockHandle -> Annex Bool
|
|
|
|
checkSaneLock f h = H.checkSaneLock f h
|
|
|
|
`pidLockCheck` flip Pid.checkSaneLock h
|
|
|
|
|
2015-11-12 21:47:31 +00:00
|
|
|
pidLockCheck :: IO a -> (LockFile -> IO a) -> Annex a
|
2018-11-19 19:00:24 +00:00
|
|
|
pidLockCheck posixcheck pidcheck = debugLocks $
|
2015-11-12 21:47:31 +00:00
|
|
|
liftIO . maybe posixcheck pidcheck =<< pidLockFile
|
|
|
|
|
2023-04-27 19:57:50 +00:00
|
|
|
pidLock :: Maybe ModeSetter -> LockFile -> LockMode -> IO LockHandle -> Annex LockHandle
|
2021-12-03 21:20:21 +00:00
|
|
|
pidLock m f lockmode posixlock = debugLocks $ go =<< pidLockFile
|
2015-11-12 21:47:31 +00:00
|
|
|
where
|
|
|
|
go Nothing = liftIO posixlock
|
|
|
|
go (Just pidlock) = do
|
|
|
|
timeout <- annexPidLockTimeout <$> Annex.getGitConfig
|
2020-08-26 17:05:34 +00:00
|
|
|
liftIO $ dummyPosixLock m f
|
filter out control characters in warning messages
Converted warning and similar to use StringContainingQuotedPath. Most
warnings are static strings, some do refer to filepaths that need to be
quoted, and others don't need quoting.
Note that, since quote filters out control characters of even
UnquotedString, this makes all warnings safe, even when an attacker
sneaks in a control character in some other way.
When json is being output, no quoting is done, since json gets its own
quoting.
This does, as a side effect, make warning messages in json output not
be indented. The indentation is only needed to offset warning messages
underneath the display of the file they apply to, so that's ok.
Sponsored-by: Brett Eisenberg on Patreon
2023-04-10 18:47:32 +00:00
|
|
|
Pid.waitLock f lockmode timeout pidlock (warning . UnquotedString)
|
2015-11-12 21:47:31 +00:00
|
|
|
|
2023-04-27 19:57:50 +00:00
|
|
|
tryPidLock :: Maybe ModeSetter -> LockFile -> LockMode -> IO (Maybe LockHandle) -> Annex (Maybe LockHandle)
|
2021-12-03 21:20:21 +00:00
|
|
|
tryPidLock m f lockmode posixlock = debugLocks $ liftIO . go =<< pidLockFile
|
2015-11-12 21:47:31 +00:00
|
|
|
where
|
|
|
|
go Nothing = posixlock
|
|
|
|
go (Just pidlock) = do
|
|
|
|
dummyPosixLock m f
|
2021-12-03 21:20:21 +00:00
|
|
|
Pid.tryLock f lockmode pidlock
|
2015-11-12 21:47:31 +00:00
|
|
|
|
|
|
|
-- The posix lock file is created even when using pid locks, in order to
|
|
|
|
-- avoid complicating any code that might expect to be able to see that
|
2016-03-01 20:22:47 +00:00
|
|
|
-- lock file. But, it's not locked.
|
2023-04-27 19:57:50 +00:00
|
|
|
dummyPosixLock :: Maybe ModeSetter -> LockFile -> IO ()
|
2020-06-09 17:48:48 +00:00
|
|
|
dummyPosixLock m f = bracket (openLockFile ReadLock m f) closeFd (const noop)
|