2013-03-19 20:58:36 +00:00
|
|
|
{- git-annex command, used internally by assistant
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012, 2013 Joey Hess <id@joeyh.name>
|
2013-03-19 20:58:36 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
|
|
|
|
|
|
|
|
module Command.TransferKeys where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import Annex.Content
|
|
|
|
import Logs.Location
|
2014-03-22 14:42:38 +00:00
|
|
|
import Annex.Transfer
|
2013-03-19 20:58:36 +00:00
|
|
|
import qualified Remote
|
2015-04-03 19:33:28 +00:00
|
|
|
import Utility.SimpleProtocol (dupIoHandles)
|
2014-05-19 20:19:33 +00:00
|
|
|
import Git.Types (RemoteName)
|
2016-05-16 18:49:12 +00:00
|
|
|
import qualified Database.Keys
|
2013-03-19 20:58:36 +00:00
|
|
|
|
|
|
|
data TransferRequest = TransferRequest Direction Remote Key AssociatedFile
|
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-08 19:08:02 +00:00
|
|
|
cmd = command "transferkeys" SectionPlumbing "transfers keys"
|
|
|
|
paramNothing (withParams seek)
|
2013-03-19 20:58:36 +00:00
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
seek = withNothing start
|
2013-03-19 20:58:36 +00:00
|
|
|
|
2013-12-11 03:19:18 +00:00
|
|
|
start :: CommandStart
|
2014-04-08 18:02:25 +00:00
|
|
|
start = do
|
2015-04-03 19:33:28 +00:00
|
|
|
(readh, writeh) <- liftIO dupIoHandles
|
2013-12-11 03:19:18 +00:00
|
|
|
runRequests readh writeh runner
|
2013-03-19 20:58:36 +00:00
|
|
|
stop
|
|
|
|
where
|
|
|
|
runner (TransferRequest direction remote key file)
|
2014-03-22 14:42:38 +00:00
|
|
|
| direction == Upload = notifyTransfer direction file $
|
2015-05-12 19:50:03 +00:00
|
|
|
upload (Remote.uuid remote) key file forwardRetry observer $ \p -> do
|
2013-03-19 20:58:36 +00:00
|
|
|
ok <- Remote.storeKey remote key file p
|
|
|
|
when ok $
|
|
|
|
Remote.logStatus remote key InfoPresent
|
|
|
|
return ok
|
2014-03-22 14:42:38 +00:00
|
|
|
| otherwise = notifyTransfer direction file $
|
2015-05-12 19:50:03 +00:00
|
|
|
download (Remote.uuid remote) key file forwardRetry observer $ \p ->
|
2016-05-16 18:49:12 +00:00
|
|
|
getViaTmp (RemoteVerify remote) key $ \t -> do
|
|
|
|
r <- Remote.retrieveKeyFile remote key file t p
|
|
|
|
-- Make sure we get the current
|
|
|
|
-- associated files data for the key,
|
|
|
|
-- not old cached data.
|
|
|
|
Database.Keys.closeDb
|
|
|
|
return r
|
2015-05-12 19:50:03 +00:00
|
|
|
|
2015-07-09 20:05:45 +00:00
|
|
|
observer False t tinfo = recordFailedTransfer t tinfo
|
2015-05-12 19:50:03 +00:00
|
|
|
observer True _ _ = noop
|
2013-03-19 20:58:36 +00:00
|
|
|
|
|
|
|
runRequests
|
|
|
|
:: Handle
|
|
|
|
-> Handle
|
|
|
|
-> (TransferRequest -> Annex Bool)
|
|
|
|
-> Annex ()
|
2013-03-20 17:18:12 +00:00
|
|
|
runRequests readh writeh a = do
|
|
|
|
liftIO $ do
|
|
|
|
hSetBuffering readh NoBuffering
|
|
|
|
fileEncoding readh
|
|
|
|
fileEncoding writeh
|
|
|
|
go =<< readrequests
|
2013-03-19 20:58:36 +00:00
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
go (d:rn:k:f:rest) = do
|
2014-05-19 20:19:33 +00:00
|
|
|
case (deserialize d, deserialize rn, deserialize k, deserialize f) of
|
|
|
|
(Just direction, Just remotename, Just key, Just file) -> do
|
|
|
|
mremote <- Remote.byName' remotename
|
2013-03-19 20:58:36 +00:00
|
|
|
case mremote of
|
2014-05-19 20:19:33 +00:00
|
|
|
Left _ -> sendresult False
|
|
|
|
Right remote -> sendresult =<< a
|
2013-03-19 20:58:36 +00:00
|
|
|
(TransferRequest direction remote key file)
|
|
|
|
_ -> sendresult False
|
|
|
|
go rest
|
2013-04-02 19:18:03 +00:00
|
|
|
go [] = noop
|
|
|
|
go [""] = noop
|
|
|
|
go v = error $ "transferkeys protocol error: " ++ show v
|
2013-03-19 20:58:36 +00:00
|
|
|
|
|
|
|
readrequests = liftIO $ split fieldSep <$> hGetContents readh
|
|
|
|
sendresult b = liftIO $ do
|
|
|
|
hPutStrLn writeh $ serialize b
|
|
|
|
hFlush writeh
|
|
|
|
|
2014-05-19 20:19:33 +00:00
|
|
|
sendRequest :: Transfer -> TransferInfo -> Handle -> IO ()
|
2015-07-09 20:05:45 +00:00
|
|
|
sendRequest t tinfo h = do
|
2013-04-23 00:24:53 +00:00
|
|
|
hPutStr h $ intercalate fieldSep
|
2013-03-19 22:46:29 +00:00
|
|
|
[ serialize (transferDirection t)
|
2014-05-19 20:19:33 +00:00
|
|
|
, maybe (serialize (fromUUID (transferUUID t)))
|
|
|
|
(serialize . Remote.name)
|
2015-07-09 20:05:45 +00:00
|
|
|
(transferRemote tinfo)
|
2013-03-19 22:46:29 +00:00
|
|
|
, serialize (transferKey t)
|
2015-07-09 20:05:45 +00:00
|
|
|
, serialize (associatedFile tinfo)
|
2013-03-20 17:18:12 +00:00
|
|
|
, "" -- adds a trailing null
|
2013-03-19 20:58:36 +00:00
|
|
|
]
|
|
|
|
hFlush h
|
|
|
|
|
2013-03-19 22:46:29 +00:00
|
|
|
readResponse :: Handle -> IO Bool
|
|
|
|
readResponse h = fromMaybe False . deserialize <$> hGetLine h
|
|
|
|
|
2013-03-19 20:58:36 +00:00
|
|
|
fieldSep :: String
|
|
|
|
fieldSep = "\0"
|
|
|
|
|
2014-01-21 20:08:19 +00:00
|
|
|
class TCSerialized a where
|
2013-03-19 20:58:36 +00:00
|
|
|
serialize :: a -> String
|
|
|
|
deserialize :: String -> Maybe a
|
|
|
|
|
2014-01-21 20:08:19 +00:00
|
|
|
instance TCSerialized Bool where
|
2013-03-19 20:58:36 +00:00
|
|
|
serialize True = "1"
|
|
|
|
serialize False = "0"
|
|
|
|
deserialize "1" = Just True
|
|
|
|
deserialize "0" = Just False
|
|
|
|
deserialize _ = Nothing
|
|
|
|
|
2014-01-21 20:08:19 +00:00
|
|
|
instance TCSerialized Direction where
|
2013-03-19 20:58:36 +00:00
|
|
|
serialize Upload = "u"
|
|
|
|
serialize Download = "d"
|
|
|
|
deserialize "u" = Just Upload
|
|
|
|
deserialize "d" = Just Download
|
|
|
|
deserialize _ = Nothing
|
|
|
|
|
2014-01-21 20:08:19 +00:00
|
|
|
instance TCSerialized AssociatedFile where
|
2013-03-19 20:58:36 +00:00
|
|
|
serialize (Just f) = f
|
|
|
|
serialize Nothing = ""
|
|
|
|
deserialize "" = Just Nothing
|
|
|
|
deserialize f = Just $ Just f
|
|
|
|
|
2014-05-19 20:19:33 +00:00
|
|
|
instance TCSerialized RemoteName where
|
|
|
|
serialize n = n
|
|
|
|
deserialize n = Just n
|
2013-03-19 20:58:36 +00:00
|
|
|
|
2014-01-21 20:08:19 +00:00
|
|
|
instance TCSerialized Key where
|
2013-03-19 20:58:36 +00:00
|
|
|
serialize = key2file
|
|
|
|
deserialize = file2key
|