summaryrefslogtreecommitdiff
path: root/Command/RecvKey.hs
blob: 041e104a75080faaff983cf4df890f5cd9d0ade3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{- git-annex command
 -
 - Copyright 2010 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Command.RecvKey where

import Common.Annex
import Command
import CmdLine
import Annex.Content
import Annex
import Utility.Rsync
import Logs.Transfer
import Command.SendKey (fieldTransfer)
import qualified Fields
import qualified Types.Key
import qualified Types.Backend
import qualified Backend

def :: [Command]
def = [noCommit $ command "recvkey" paramKey seek
	SectionPlumbing "runs rsync in server mode to receive content"]

seek :: [CommandSeek]
seek = [withKeys start]

start :: Key -> CommandStart
start key = ifM (inAnnex key)
	( error "key is already present in annex"
	, fieldTransfer Download key $ \_p -> do
		ifM (getViaTmp key go)
			( do
				-- forcibly quit after receiving one key,
				-- and shutdown cleanly
				_ <- shutdown True
				return True
			, return False
			)
	)
  where
	go tmp = do
		opts <- filterRsyncSafeOptions . maybe [] words
			<$> getField "RsyncOptions"
		ifM (liftIO $ rsyncServerReceive (map Param opts) tmp)
			( ifM (isJust <$> Fields.getField Fields.direct)
				( directcheck tmp
				, return True
				)
			, return False
			)
	{- 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
				size' <- fromIntegral . fileSize
       	        	        	<$> liftIO (getFileStatus tmp)
				return $ size == size'
		if oksize
			then case Backend.maybeLookupBackendName (Types.Key.keyBackendName key) of
				Nothing -> return False
				Just backend -> maybe (return True) (\a -> a key tmp)
					(Types.Backend.fsckKey backend)
			else return False