2012-07-03 14:58:40 +00:00
|
|
|
{- git-annex assistant transfer watching thread
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.Threads.TransferWatcher where
|
|
|
|
|
2012-07-20 23:29:59 +00:00
|
|
|
import Assistant.Common
|
2012-07-03 14:58:40 +00:00
|
|
|
import Assistant.ThreadedMonad
|
|
|
|
import Assistant.DaemonStatus
|
|
|
|
import Logs.Transfer
|
|
|
|
import Utility.DirWatcher
|
|
|
|
import Utility.Types.DirWatcher
|
|
|
|
|
2012-07-20 23:29:59 +00:00
|
|
|
thisThread :: ThreadName
|
|
|
|
thisThread = "TransferWatcher"
|
|
|
|
|
2012-07-03 14:58:40 +00:00
|
|
|
{- This thread watches for changes to the gitAnnexTransferDir,
|
|
|
|
- and updates the DaemonStatus's map of ongoing transfers. -}
|
2012-07-18 23:13:56 +00:00
|
|
|
transferWatcherThread :: ThreadState -> DaemonStatusHandle -> IO ()
|
|
|
|
transferWatcherThread st dstatus = do
|
2012-07-03 14:58:40 +00:00
|
|
|
g <- runThreadState st $ fromRepo id
|
|
|
|
let dir = gitAnnexTransferDir g
|
|
|
|
createDirectoryIfMissing True dir
|
2012-07-18 23:13:56 +00:00
|
|
|
let hook a = Just $ runHandler st dstatus a
|
2012-07-03 14:58:40 +00:00
|
|
|
let hooks = mkWatchHooks
|
|
|
|
{ addHook = hook onAdd
|
|
|
|
, delHook = hook onDel
|
|
|
|
, errHook = hook onErr
|
|
|
|
}
|
|
|
|
void $ watchDir dir (const False) hooks id
|
2012-07-20 23:29:59 +00:00
|
|
|
debug thisThread ["watching for transfers"]
|
2012-07-03 14:58:40 +00:00
|
|
|
|
2012-07-18 23:13:56 +00:00
|
|
|
type Handler = ThreadState -> DaemonStatusHandle -> FilePath -> Maybe FileStatus -> IO ()
|
2012-07-03 14:58:40 +00:00
|
|
|
|
|
|
|
{- Runs an action handler.
|
|
|
|
-
|
|
|
|
- Exceptions are ignored, otherwise a whole thread could be crashed.
|
|
|
|
-}
|
2012-07-18 23:13:56 +00:00
|
|
|
runHandler :: ThreadState -> DaemonStatusHandle -> Handler -> FilePath -> Maybe FileStatus -> IO ()
|
|
|
|
runHandler st dstatus handler file filestatus = void $ do
|
2012-07-03 14:58:40 +00:00
|
|
|
either print (const noop) =<< tryIO go
|
|
|
|
where
|
2012-07-18 23:13:56 +00:00
|
|
|
go = handler st dstatus file filestatus
|
2012-07-03 14:58:40 +00:00
|
|
|
|
|
|
|
{- Called when there's an error with inotify. -}
|
|
|
|
onErr :: Handler
|
2012-07-18 23:13:56 +00:00
|
|
|
onErr _ _ msg _ = error msg
|
2012-07-03 14:58:40 +00:00
|
|
|
|
2012-07-06 20:30:55 +00:00
|
|
|
{- Called when a new transfer information file is written. -}
|
2012-07-03 14:58:40 +00:00
|
|
|
onAdd :: Handler
|
2012-07-18 23:13:56 +00:00
|
|
|
onAdd st dstatus file _ = case parseTransferFile file of
|
2012-07-03 14:58:40 +00:00
|
|
|
Nothing -> noop
|
2012-07-28 22:02:11 +00:00
|
|
|
Just t -> go t =<< runThreadState st (checkTransfer t)
|
2012-07-05 20:34:20 +00:00
|
|
|
where
|
2012-07-06 22:44:13 +00:00
|
|
|
go _ Nothing = noop -- transfer already finished
|
2012-07-20 23:29:59 +00:00
|
|
|
go t (Just info) = do
|
2012-07-28 22:02:11 +00:00
|
|
|
debug thisThread
|
2012-07-20 23:29:59 +00:00
|
|
|
[ "transfer starting:"
|
|
|
|
, show t
|
|
|
|
]
|
2012-08-10 19:45:00 +00:00
|
|
|
updateTransferInfo dstatus t info
|
2012-07-03 14:58:40 +00:00
|
|
|
|
2012-07-18 23:13:56 +00:00
|
|
|
{- Called when a transfer information file is removed. -}
|
2012-07-03 14:58:40 +00:00
|
|
|
onDel :: Handler
|
2012-07-28 22:02:11 +00:00
|
|
|
onDel _ dstatus file _ = case parseTransferFile file of
|
2012-07-03 14:58:40 +00:00
|
|
|
Nothing -> noop
|
2012-07-20 23:29:59 +00:00
|
|
|
Just t -> do
|
|
|
|
debug thisThread
|
|
|
|
[ "transfer finishing:"
|
|
|
|
, show t
|
|
|
|
]
|
2012-07-28 22:02:11 +00:00
|
|
|
void $ removeTransfer dstatus t
|