2010-12-31 17:39:30 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2010 Joey Hess <id@joeyh.name>
|
2010-12-31 17:39:30 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.RecvKey where
|
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2010-12-31 17:39:30 +00:00
|
|
|
import Command
|
2011-01-16 20:05:05 +00:00
|
|
|
import CmdLine
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2013-03-29 00:34:07 +00:00
|
|
|
import Annex
|
2012-09-19 18:28:32 +00:00
|
|
|
import Utility.Rsync
|
2012-07-02 05:31:10 +00:00
|
|
|
import Logs.Transfer
|
2012-08-23 19:22:23 +00:00
|
|
|
import Command.SendKey (fieldTransfer)
|
2014-01-26 20:32:55 +00:00
|
|
|
import qualified CmdLine.GitAnnexShell.Fields as Fields
|
2013-01-11 19:43:09 +00:00
|
|
|
import qualified Types.Key
|
|
|
|
import qualified Types.Backend
|
|
|
|
import qualified Backend
|
2010-12-31 17:39:30 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-08 19:08:02 +00:00
|
|
|
cmd = noCommit $ command "recvkey" SectionPlumbing
|
|
|
|
"runs rsync in server mode to receive content"
|
|
|
|
paramKey (withParams seek)
|
2010-12-31 17:39:30 +00:00
|
|
|
|
2015-07-08 16:33:27 +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 = withKeys start
|
2010-12-31 17:39:30 +00:00
|
|
|
|
2011-09-15 20:50:49 +00:00
|
|
|
start :: Key -> CommandStart
|
2014-08-04 13:16:47 +00:00
|
|
|
start key = fieldTransfer Download key $ \_p ->
|
|
|
|
ifM (getViaTmp key go)
|
|
|
|
( do
|
|
|
|
-- forcibly quit after receiving one key,
|
|
|
|
-- and shutdown cleanly
|
|
|
|
_ <- shutdown True
|
|
|
|
return True
|
|
|
|
, return False
|
|
|
|
)
|
2013-01-11 19:43:09 +00:00
|
|
|
where
|
2013-03-29 00:34:07 +00:00
|
|
|
go tmp = do
|
2013-03-30 23:05:51 +00:00
|
|
|
opts <- filterRsyncSafeOptions . maybe [] words
|
|
|
|
<$> getField "RsyncOptions"
|
2013-05-06 21:30:57 +00:00
|
|
|
ok <- liftIO $ rsyncServerReceive (map Param opts) tmp
|
|
|
|
|
|
|
|
-- The file could have been received with permissions that
|
|
|
|
-- do not allow reading it, so this is done before the
|
|
|
|
-- directcheck.
|
|
|
|
freezeContent tmp
|
|
|
|
|
|
|
|
if ok
|
|
|
|
then ifM (isJust <$> Fields.getField Fields.direct)
|
2013-03-29 00:34:07 +00:00
|
|
|
( directcheck tmp
|
|
|
|
, return True
|
|
|
|
)
|
2013-05-06 21:30:57 +00:00
|
|
|
else return False
|
2013-01-11 19:43:09 +00:00
|
|
|
{- If the sending repository uses direct mode, the file
|
|
|
|
- it sends could be modified as it's sending it. So check
|
|
|
|
- that the right size file was received, and that the key/value
|
|
|
|
- Backend is happy with it. -}
|
|
|
|
directcheck tmp = do
|
|
|
|
oksize <- case Types.Key.keySize key of
|
|
|
|
Nothing -> return True
|
|
|
|
Just size -> do
|
2015-01-20 20:58:48 +00:00
|
|
|
size' <- liftIO $ getFileSize tmp
|
2013-01-11 19:43:09 +00:00
|
|
|
return $ size == size'
|
|
|
|
if oksize
|
|
|
|
then case Backend.maybeLookupBackendName (Types.Key.keyBackendName key) of
|
2013-10-01 18:10:45 +00:00
|
|
|
Nothing -> do
|
|
|
|
warning "recvkey: received key from direct mode repository using unknown backend; cannot check; discarding"
|
|
|
|
return False
|
|
|
|
Just backend -> maybe (return True) runfsck
|
2013-01-11 19:43:09 +00:00
|
|
|
(Types.Backend.fsckKey backend)
|
2013-10-01 18:10:45 +00:00
|
|
|
else do
|
|
|
|
warning "recvkey: received key with wrong size; discarding"
|
|
|
|
return False
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
runfsck check = ifM (check key tmp)
|
2013-10-01 18:10:45 +00:00
|
|
|
( return True
|
|
|
|
, do
|
|
|
|
warning "recvkey: received key from direct mode repository seems to have changed as it was transferred; discarding"
|
|
|
|
return False
|
|
|
|
)
|