aboutsummaryrefslogtreecommitdiff
path: root/Command/LockContent.hs
blob: 202ba20d1c64b23be8bf8eb7317434a41f1b50b6 (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
{- git-annex-shell command
 -
 - Copyright 2015 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Command.LockContent where

import Command
import Annex.Content
import Remote.Helper.Ssh (contentLockedMarker)
import Utility.SimpleProtocol

cmd :: Command
cmd = noCommit $ 
	command "lockcontent" SectionPlumbing 
		"locks key's content in the annex, preventing it being dropped"
		paramKey
		(withParams seek)

seek :: CmdParams -> CommandSeek
seek = withWords start

-- First, lock the content. Then, make sure the content is actually
-- present, and print out "OK". Wait for the caller to send a line before
-- dropping the lock.
start :: [String] -> CommandStart
start [ks] = do
	ok <- lockContentShared k (const locksuccess)
		`catchNonAsync` (const $ return False)
	liftIO $ if ok
		then exitSuccess
		else exitFailure
  where
	k = fromMaybe (giveup "bad key") (file2key ks)
	locksuccess = ifM (inAnnex k)
		( liftIO $ do
			putStrLn contentLockedMarker
			hFlush stdout
			_ <- getProtocolLine stdin
			return True
		, return False
		)
start _ = giveup "Specify exactly 1 key."