fixes for transfer resume

Fix resuming of downloads, which do not have a transfer info file to read.

When checking upload progress, use the MVar, rather than re-reading
the info file.

Catch exceptions in the transfer action. Required a tryAnnex.
This commit is contained in:
Joey Hess 2012-09-24 13:16:50 -04:00
parent 364b40e5fc
commit 3887432c54
3 changed files with 34 additions and 20 deletions

View file

@ -8,12 +8,13 @@
module Annex.Exception ( module Annex.Exception (
bracketIO, bracketIO,
handle, handle,
tryAnnex,
throw, throw,
) where ) where
import Control.Exception.Lifted (handle) import Control.Exception.Lifted (handle, try)
import Control.Monad.Trans.Control (liftBaseOp) import Control.Monad.Trans.Control (liftBaseOp)
import Control.Exception hiding (handle, throw) import Control.Exception hiding (handle, try, throw)
import Common.Annex import Common.Annex
@ -22,6 +23,10 @@ bracketIO :: IO c -> (c -> IO b) -> Annex a -> Annex a
bracketIO setup cleanup go = bracketIO setup cleanup go =
liftBaseOp (Control.Exception.bracket setup cleanup) (const go) liftBaseOp (Control.Exception.bracket setup cleanup) (const go)
{- try in the Annex monad -}
tryAnnex :: Annex a -> Annex (Either SomeException a)
tryAnnex = try
{- Throws an exception in the Annex monad. -} {- Throws an exception in the Annex monad. -}
throw :: Control.Exception.Exception e => e -> Annex a throw :: Control.Exception.Exception e => e -> Annex a
throw = liftIO . throwIO throw = liftIO . throwIO

View file

@ -47,7 +47,7 @@ start (k:[]) = do
, transferKey = key , transferKey = key
} }
info <- liftIO $ startTransferInfo file info <- liftIO $ startTransferInfo file
(update, tfile) <- mkProgressUpdater t info (update, tfile, _) <- mkProgressUpdater t info
liftIO $ mapM_ void liftIO $ mapM_ void
[ tryIO $ forever $ do [ tryIO $ forever $ do
bytes <- readish <$> getLine bytes <- readish <$> getLine

View file

@ -100,9 +100,10 @@ download u key file shouldretry a = runTransfer (Transfer Download u key) file s
runTransfer :: Transfer -> Maybe FilePath -> RetryDecider -> (MeterUpdate -> Annex Bool) -> Annex Bool runTransfer :: Transfer -> Maybe FilePath -> RetryDecider -> (MeterUpdate -> Annex Bool) -> Annex Bool
runTransfer t file shouldretry a = do runTransfer t file shouldretry a = do
info <- liftIO $ startTransferInfo file info <- liftIO $ startTransferInfo file
(meter, tfile) <- mkProgressUpdater t info (meter, tfile, metervar) <- mkProgressUpdater t info
mode <- annexFileMode mode <- annexFileMode
ok <- retry tfile info $ bracketIO (prep tfile mode info) (cleanup tfile) (a meter) ok <- retry info metervar $
bracketIO (prep tfile mode info) (cleanup tfile) (a meter)
unless ok $ failed info unless ok $ failed info
return ok return ok
where where
@ -123,26 +124,34 @@ runTransfer t file shouldretry a = do
failedtfile <- fromRepo $ failedTransferFile t failedtfile <- fromRepo $ failedTransferFile t
createAnnexDirectory $ takeDirectory failedtfile createAnnexDirectory $ takeDirectory failedtfile
liftIO $ writeTransferInfoFile info failedtfile liftIO $ writeTransferInfoFile info failedtfile
retry tfile oldinfo run = do retry oldinfo metervar run = do
ok <- run v <- tryAnnex run
if ok case v of
then return ok Right b -> return b
else do Left _ -> do
v <- liftIO $ readTransferInfoFile Nothing tfile b <- getbytescomplete metervar
case v of let newinfo = oldinfo { bytesComplete = Just b }
Nothing -> return ok if shouldretry oldinfo newinfo
Just newinfo -> if shouldretry oldinfo newinfo then retry newinfo metervar run
then retry tfile newinfo run else return False
else return ok getbytescomplete metervar
| transferDirection t == Upload =
liftIO $ readMVar metervar
| otherwise = do
f <- fromRepo $ gitAnnexTmpLocation (transferKey t)
liftIO $ catchDefaultIO 0 $
fromIntegral . fileSize
<$> getFileStatus f
{- Generates a callback that can be called as transfer progresses to update {- Generates a callback that can be called as transfer progresses to update
- the transfer info file. Also returns the file it'll be updating. -} - the transfer info file. Also returns the file it'll be updating, and a
mkProgressUpdater :: Transfer -> TransferInfo -> Annex (MeterUpdate, FilePath) - MVar that can be used to read the number of bytesComplete. -}
mkProgressUpdater :: Transfer -> TransferInfo -> Annex (MeterUpdate, FilePath, MVar Integer)
mkProgressUpdater t info = do mkProgressUpdater t info = do
tfile <- fromRepo $ transferFile t tfile <- fromRepo $ transferFile t
createAnnexDirectory $ takeDirectory tfile createAnnexDirectory $ takeDirectory tfile
mvar <- liftIO $ newMVar 0 mvar <- liftIO $ newMVar 0
return (liftIO . updater tfile mvar, tfile) return (liftIO . updater tfile mvar, tfile, mvar)
where where
updater tfile mvar bytes = modifyMVar_ mvar $ \oldbytes -> do updater tfile mvar bytes = modifyMVar_ mvar $ \oldbytes -> do
if (bytes - oldbytes >= mindelta) if (bytes - oldbytes >= mindelta)
@ -268,7 +277,7 @@ writeTransferInfo info = unlines
] ]
readTransferInfoFile :: (Maybe ProcessID) -> FilePath -> IO (Maybe TransferInfo) readTransferInfoFile :: (Maybe ProcessID) -> FilePath -> IO (Maybe TransferInfo)
readTransferInfoFile mpid tfile = do readTransferInfoFile mpid tfile = catchDefaultIO Nothing $ do
h <- openFile tfile ReadMode h <- openFile tfile ReadMode
fileEncoding h fileEncoding h
hClose h `after` (readTransferInfo mpid <$> hGetContentsStrict h) hClose h `after` (readTransferInfo mpid <$> hGetContentsStrict h)