2013-03-19 22:46:29 +00:00
|
|
|
{- A pool of "git-annex transferkeys" processes
|
|
|
|
-
|
|
|
|
- Copyright 2013 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-11-12 18:54:02 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2013-03-19 22:46:29 +00:00
|
|
|
module Assistant.TransferrerPool where
|
|
|
|
|
|
|
|
import Assistant.Common
|
|
|
|
import Assistant.Types.TransferrerPool
|
|
|
|
import Logs.Transfer
|
2013-12-01 18:56:37 +00:00
|
|
|
import Utility.Batch
|
2013-11-12 18:54:02 +00:00
|
|
|
|
|
|
|
#ifndef mingw32_HOST_OS
|
2013-03-19 22:46:29 +00:00
|
|
|
import qualified Command.TransferKeys as T
|
2013-11-12 18:54:02 +00:00
|
|
|
#endif
|
2013-03-19 22:46:29 +00:00
|
|
|
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
import System.Process (create_group)
|
|
|
|
import Control.Exception (throw)
|
|
|
|
import Control.Concurrent
|
|
|
|
|
|
|
|
{- Runs an action with a Transferrer from the pool. -}
|
2013-12-01 19:37:51 +00:00
|
|
|
withTransferrer :: FilePath -> BatchCommandMaker -> TransferrerPool -> (Transferrer -> IO a) -> IO a
|
|
|
|
withTransferrer program batchmaker pool a = do
|
|
|
|
t <- maybe (mkTransferrer program batchmaker) (checkTransferrer program batchmaker)
|
2013-03-19 22:46:29 +00:00
|
|
|
=<< atomically (tryReadTChan pool)
|
|
|
|
v <- tryNonAsync $ a t
|
|
|
|
unlessM (putback t) $
|
|
|
|
void $ forkIO $ stopTransferrer t
|
|
|
|
either throw return v
|
|
|
|
where
|
|
|
|
putback t = atomically $ ifM (isEmptyTChan pool)
|
|
|
|
( do
|
|
|
|
writeTChan pool t
|
|
|
|
return True
|
|
|
|
, return False
|
|
|
|
)
|
|
|
|
|
|
|
|
{- Requests that a Transferrer perform a Transfer, and waits for it to
|
|
|
|
- finish. -}
|
|
|
|
performTransfer :: Transferrer -> Transfer -> AssociatedFile -> IO Bool
|
|
|
|
performTransfer transferrer t f = catchBoolIO $ do
|
2013-11-12 18:54:02 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-03-19 22:46:29 +00:00
|
|
|
T.sendRequest t f (transferrerWrite transferrer)
|
|
|
|
T.readResponse (transferrerRead transferrer)
|
2013-11-12 18:54:02 +00:00
|
|
|
#else
|
|
|
|
error "TODO performTransfer not implemented on Windows"
|
|
|
|
#endif
|
2013-03-19 22:46:29 +00:00
|
|
|
|
|
|
|
{- Starts a new git-annex transferkeys process, setting up a pipe
|
|
|
|
- that will be used to communicate with it. -}
|
2013-12-01 19:37:51 +00:00
|
|
|
mkTransferrer :: FilePath -> BatchCommandMaker -> IO Transferrer
|
|
|
|
mkTransferrer program batchmaker = do
|
2013-11-12 18:54:02 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-03-19 22:46:29 +00:00
|
|
|
(myread, twrite) <- createPipe
|
|
|
|
(tread, mywrite) <- createPipe
|
|
|
|
mapM_ (\fd -> setFdOption fd CloseOnExec True) [myread, mywrite]
|
|
|
|
let params =
|
|
|
|
[ Param "transferkeys"
|
|
|
|
, Param "--readfd", Param $ show tread
|
|
|
|
, Param "--writefd", Param $ show twrite
|
|
|
|
]
|
2013-12-01 18:56:37 +00:00
|
|
|
{- It runs as a batch job. -}
|
2013-12-01 19:37:51 +00:00
|
|
|
let (program', params') = batchmaker (program, params)
|
2013-03-19 22:46:29 +00:00
|
|
|
{- It's put into its own group so that the whole group can be
|
|
|
|
- killed to stop a transfer. -}
|
2013-12-01 18:56:37 +00:00
|
|
|
(_, _, _, pid) <- createProcess (proc program' $ toCommand params')
|
2013-03-19 22:46:29 +00:00
|
|
|
{ create_group = True }
|
|
|
|
closeFd twrite
|
|
|
|
closeFd tread
|
|
|
|
myreadh <- fdToHandle myread
|
|
|
|
mywriteh <- fdToHandle mywrite
|
2013-03-20 17:18:12 +00:00
|
|
|
fileEncoding myreadh
|
|
|
|
fileEncoding mywriteh
|
2013-03-19 22:46:29 +00:00
|
|
|
return $ Transferrer
|
|
|
|
{ transferrerRead = myreadh
|
|
|
|
, transferrerWrite = mywriteh
|
|
|
|
, transferrerHandle = pid
|
|
|
|
}
|
2013-11-12 18:54:02 +00:00
|
|
|
#else
|
|
|
|
error "TODO mkTransferrer not implemented on Windows"
|
|
|
|
#endif
|
2013-03-19 22:46:29 +00:00
|
|
|
|
|
|
|
{- Checks if a Transferrer is still running. If not, makes a new one. -}
|
2013-12-01 19:37:51 +00:00
|
|
|
checkTransferrer :: FilePath -> BatchCommandMaker -> Transferrer -> IO Transferrer
|
|
|
|
checkTransferrer program batchmaker t =
|
|
|
|
maybe (return t) (const $ mkTransferrer program batchmaker)
|
|
|
|
=<< getProcessExitCode (transferrerHandle t)
|
2013-03-19 22:46:29 +00:00
|
|
|
|
|
|
|
{- Closing the fds will stop the transferrer. -}
|
|
|
|
stopTransferrer :: Transferrer -> IO ()
|
|
|
|
stopTransferrer t = do
|
|
|
|
hClose $ transferrerRead t
|
|
|
|
hClose $ transferrerWrite t
|
|
|
|
void $ waitForProcess $ transferrerHandle t
|