From 263c4140583aeddd2c1e52a40d5fcc411f3d18d1 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 20 Aug 2014 18:56:25 -0400 Subject: more lock file refactoring Also fixes a test suite failures introduced in recent commits, where inAnnexSafe failed in indirect mode, since it tried to open the lock file ReadWrite. This is why the new checkLocked opens it ReadOnly. This commit was sponsored by Chad Horohoe. --- Annex/Content.hs | 32 +++++++++++++++++--------------- Annex/Ssh.hs | 11 +++++------ 2 files changed, 22 insertions(+), 21 deletions(-) (limited to 'Annex') diff --git a/Annex/Content.hs b/Annex/Content.hs index b3c62ee0a..90ab7db58 100644 --- a/Annex/Content.hs +++ b/Annex/Content.hs @@ -101,23 +101,21 @@ inAnnexSafe key = inAnnex' (fromMaybe False) (Just False) go key =<< contentLockFile key #ifndef mingw32_HOST_OS - checkindirect f = liftIO $ openExistingLockFile f >>= check is_missing + checkindirect contentfile = liftIO $ checkOr is_missing contentfile {- In direct mode, the content file must exist, but - - 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 - - it. -} + - the lock file generally won't exist unless a removal is in + - process. -} checkdirect contentfile lockfile = liftIO $ ifM (doesFileExist contentfile) - ( openExistingLockFile lockfile >>= check is_unlocked + ( checkOr is_unlocked lockfile , return is_missing ) - check _ (Just h) = do - v <- getLock h (ReadLock, AbsoluteSeek, 0, 0) - closeFd h + checkOr def lockfile = do + v <- checkLocked lockfile return $ case v of - Just _ -> is_locked - Nothing -> is_unlocked - check def Nothing = return def + Nothing -> def + Just True -> is_locked + Just False -> is_unlocked #else checkindirect f = liftIO $ ifM (doesFileExist f) ( do @@ -161,7 +159,7 @@ lockContent key a = do contentfile <- calcRepo $ gitAnnexLocation key lockfile <- contentLockFile key maybe noop setuplockfile lockfile - bracket (liftIO $ lock contentfile lockfile) (unlock lockfile) (const a) + bracket (lock contentfile lockfile) (unlock lockfile) (const a) where alreadylocked = error "content is locked" setuplockfile lockfile = modifyContent lockfile $ @@ -171,8 +169,11 @@ lockContent key a = do void $ liftIO $ tryIO $ nukeFile lockfile #ifndef mingw32_HOST_OS - lock contentfile Nothing = opencontentforlock contentfile >>= dolock - lock _ (Just lockfile) = createLockFile Nothing lockfile >>= dolock . Just + lock contentfile Nothing = liftIO $ + opencontentforlock contentfile >>= dolock + lock _ (Just lockfile) = do + mode <- annexFileMode + liftIO $ createLockFile mode 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 = catchDefaultIO Nothing $ @@ -189,7 +190,8 @@ lockContent key a = do maybe noop cleanuplockfile mlockfile liftIO $ maybe noop closeFd mfd #else - lock _ (Just lockfile) = maybe alreadylocked (return . Just) =<< lockExclusive lockfile + lock _ (Just lockfile) = liftIO $ + maybe alreadylocked (return . Just) =<< lockExclusive lockfile lock _ Nothing = return Nothing unlock mlockfile mlockhandle = do liftIO $ maybe noop dropLock mlockhandle diff --git a/Annex/Ssh.hs b/Annex/Ssh.hs index 2b1b809ff..ad636b4aa 100644 --- a/Annex/Ssh.hs +++ b/Annex/Ssh.hs @@ -152,13 +152,12 @@ sshCleanup = mapM_ cleanup =<< enumSocketFiles let lockfile = socket2lock socketfile unlockFile lockfile mode <- annexFileMode - fd <- liftIO $ noUmask mode $ createLockFile (Just mode) lockfile - v <- liftIO $ tryIO $ - setLock fd (WriteLock, AbsoluteSeek, 0, 0) + v <- liftIO $ noUmask mode $ tryLockExclusive (Just mode) lockfile case v of - Left _ -> noop - Right _ -> forceStopSsh socketfile - liftIO $ closeFd fd + Nothing -> noop + Just lck -> do + forceStopSsh socketfile + liftIO $ dropLock lck #else forceStopSsh socketfile #endif -- cgit v1.2.3