convert to using Utility.Lockfile for transfer lock files

Should be no behavior changes, just simplified code.

The only actual difference is it doesn't truncate the lock file.
I think that was a holdover from when transfer info was written to the lock
file.
This commit is contained in:
Joey Hess 2015-05-12 19:36:16 -04:00
parent 643b233860
commit e25ecab7dd

View file

@ -23,9 +23,7 @@ import Logs.Transfer as X
import Annex.Notification as X import Annex.Notification as X
import Annex.Perms import Annex.Perms
import Utility.Metered import Utility.Metered
#ifdef mingw32_HOST_OS
import Utility.LockFile import Utility.LockFile
#endif
import Control.Concurrent import Control.Concurrent
@ -70,14 +68,14 @@ runTransfer' ignorelock t file shouldretry transferobserver transferaction = do
info <- liftIO $ startTransferInfo file info <- liftIO $ startTransferInfo file
(meter, tfile, metervar) <- mkProgressUpdater t info (meter, tfile, metervar) <- mkProgressUpdater t info
mode <- annexFileMode mode <- annexFileMode
(fd, inprogress) <- liftIO $ prep tfile mode info (lck, inprogress) <- liftIO $ prep tfile mode info
if inprogress && not ignorelock if inprogress && not ignorelock
then do then do
showNote "transfer already in progress" showNote "transfer already in progress"
return False return False
else do else do
ok <- retry info metervar $ bracketIO ok <- retry info metervar $ bracketIO
(return fd) (return lck)
(cleanup tfile) (cleanup tfile)
(const $ transferaction meter) (const $ transferaction meter)
transferobserver ok t info transferobserver ok t info
@ -85,25 +83,17 @@ runTransfer' ignorelock t file shouldretry transferobserver transferaction = do
where where
#ifndef mingw32_HOST_OS #ifndef mingw32_HOST_OS
prep tfile mode info = do prep tfile mode info = do
mfd <- catchMaybeIO $ let lck = transferLockFile tfile
openFd (transferLockFile tfile) ReadWrite (Just mode) r <- tryLockExclusive (Just mode) lck
defaultFileFlags { trunc = True } case r of
case mfd of Nothing -> return (Nothing, True)
Nothing -> return (Nothing, False) Just lockhandle -> do
Just fd -> do
setFdOption fd CloseOnExec True
locked <- catchMaybeIO $
setLock fd (WriteLock, AbsoluteSeek, 0, 0)
if isNothing locked
then do
closeFd fd
return (Nothing, True)
else do
void $ tryIO $ writeTransferInfoFile info tfile void $ tryIO $ writeTransferInfoFile info tfile
return (mfd, False) return (Just lockhandle, False)
#else #else
prep tfile _mode info = do prep tfile _mode info = do
v <- catchMaybeIO $ lockExclusive (transferLockFile tfile) let lck = transferLockFile tfile
v <- catchMaybeIO $ lockExclusive lck
case v of case v of
Nothing -> return (Nothing, False) Nothing -> return (Nothing, False)
Just Nothing -> return (Nothing, True) Just Nothing -> return (Nothing, True)
@ -113,10 +103,11 @@ runTransfer' ignorelock t file shouldretry transferobserver transferaction = do
#endif #endif
cleanup _ Nothing = noop cleanup _ Nothing = noop
cleanup tfile (Just lockhandle) = do cleanup tfile (Just lockhandle) = do
let lck = transferLockFile tfile
void $ tryIO $ removeFile tfile void $ tryIO $ removeFile tfile
#ifndef mingw32_HOST_OS #ifndef mingw32_HOST_OS
void $ tryIO $ removeFile $ transferLockFile tfile void $ tryIO $ removeFile lck
closeFd lockhandle dropLock lockhandle
#else #else
{- Windows cannot delete the lockfile until the lock {- Windows cannot delete the lockfile until the lock
- is closed. So it's possible to race with another - is closed. So it's possible to race with another
@ -124,7 +115,7 @@ runTransfer' ignorelock t file shouldretry transferobserver transferaction = do
- so ignore failure to remove. - so ignore failure to remove.
-} -}
dropLock lockhandle dropLock lockhandle
void $ tryIO $ removeFile $ transferLockFile tfile void $ tryIO $ removeFile lck
#endif #endif
retry oldinfo metervar run = do retry oldinfo metervar run = do
v <- tryNonAsync run v <- tryNonAsync run