more lock file refactoring

This commit is contained in:
Joey Hess 2014-08-20 17:03:04 -04:00
parent d279180266
commit ec7dd0446a
3 changed files with 20 additions and 16 deletions

View file

@ -101,18 +101,16 @@ inAnnexSafe key = inAnnex' (fromMaybe False) (Just False) go key
=<< contentLockFile key =<< contentLockFile key
#ifndef mingw32_HOST_OS #ifndef mingw32_HOST_OS
checkindirect f = liftIO $ openforlock f >>= check is_missing checkindirect f = liftIO $ openExistingLockFile f >>= check is_missing
{- In direct mode, the content file must exist, but {- In direct mode, the content file must exist, but
- the lock file often generally won't exist unless a removal is in - the lock file often generally won't exist unless a removal is in
- process. This does not create the lock file, it only checks for - process. This does not create the lock file, it only checks for
- it. -} - it. -}
checkdirect contentfile lockfile = liftIO $ checkdirect contentfile lockfile = liftIO $
ifM (doesFileExist contentfile) ifM (doesFileExist contentfile)
( openforlock lockfile >>= check is_unlocked ( openExistingLockFile lockfile >>= check is_unlocked
, return is_missing , return is_missing
) )
openforlock f = catchMaybeIO $
openFd f ReadOnly Nothing defaultFileFlags
check _ (Just h) = do check _ (Just h) = do
v <- getLock h (ReadLock, AbsoluteSeek, 0, 0) v <- getLock h (ReadLock, AbsoluteSeek, 0, 0)
closeFd h closeFd h
@ -180,7 +178,7 @@ lockContent key a = do
opencontentforlock f = catchMaybeIO $ ifM (doesFileExist f) opencontentforlock f = catchMaybeIO $ ifM (doesFileExist f)
( withModifiedFileMode f ( withModifiedFileMode f
(`unionFileModes` ownerWriteMode) (`unionFileModes` ownerWriteMode)
(createLockFie Nothing f) (createLockFile Nothing f)
, createLockFile Nothing f , createLockFile Nothing f
) )
dolock Nothing = return Nothing dolock Nothing = return Nothing

View file

@ -17,9 +17,7 @@ import Utility.Metered
import Utility.Percentage import Utility.Percentage
import Utility.QuickCheck import Utility.QuickCheck
import Utility.PID import Utility.PID
#ifdef mingw32_HOST_OS import Utility.LockFile
import Utility.WinLock
#endif
import Data.Time.Clock import Data.Time.Clock
import Data.Time.Clock.POSIX import Data.Time.Clock.POSIX
@ -131,9 +129,7 @@ checkTransfer :: Transfer -> Annex (Maybe TransferInfo)
checkTransfer t = do checkTransfer t = do
tfile <- fromRepo $ transferFile t tfile <- fromRepo $ transferFile t
#ifndef mingw32_HOST_OS #ifndef mingw32_HOST_OS
mode <- annexFileMode mfd <- liftIO $ openExistingLockFile (transferLockFile tfile)
mfd <- liftIO $ catchMaybeIO $
openFd (transferLockFile tfile) ReadOnly (Just mode) defaultFileFlags
case mfd of case mfd of
Nothing -> return Nothing -- failed to open file; not running Nothing -> return Nothing -- failed to open file; not running
Just fd -> do Just fd -> do

View file

@ -6,13 +6,16 @@
-} -}
module Utility.LockFile.Posix ( module Utility.LockFile.Posix (
LockHandle,
lockShared, lockShared,
lockExclusive, lockExclusive,
dropLock, dropLock,
createLockFile, createLockFile,
LockHandle openExistingLockFile,
) where ) where
import Utility.Exception
import System.IO import System.IO
import System.Posix import System.Posix
@ -35,11 +38,18 @@ lock lockreq mode lockfile = do
waitToSetLock l (lockreq, AbsoluteSeek, 0, 0) waitToSetLock l (lockreq, AbsoluteSeek, 0, 0)
return (LockHandle l) return (LockHandle l)
-- Create and opens lock file, does not lock it. -- Create and opens lock file; does not lock it.
-- Close on exec flag is set so child processes do not inherit the lock.
createLockFile :: Maybe FileMode -> LockFile -> IO Fd createLockFile :: Maybe FileMode -> LockFile -> IO Fd
createLockFile mode lockfile = do createLockFile = openLockFile ReadWrite
l <- openFd lockfile ReadWrite mode defaultFileFlags
-- Opens an existing lock file; does not lock it or create it.
openExistingLockFile :: LockFile -> IO (Maybe Fd)
openExistingLockFile = catchMaybeIO . openLockFile ReadOnly Nothing
-- Close on exec flag is set so child processes do not inherit the lock.
openLockFile :: OpenMode -> Maybe FileMode -> LockFile -> IO Fd
openLockFile openmode filemode lockfile = do
l <- openFd lockfile openmode filemode defaultFileFlags
setFdOption l CloseOnExec True setFdOption l CloseOnExec True
return l return l