2012-02-16 20:36:35 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2016-01-07 18:51:28 +00:00
|
|
|
- Copyright 2012-2016 Joey Hess <id@joeyh.name>
|
2012-02-16 20:36:35 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.ReKey where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import qualified Annex
|
|
|
|
import Annex.Content
|
2015-12-22 17:23:33 +00:00
|
|
|
import Annex.Ingest
|
2016-01-07 18:51:28 +00:00
|
|
|
import Annex.Link
|
|
|
|
import Annex.Perms
|
|
|
|
import Annex.ReplaceFile
|
2013-04-11 17:35:52 +00:00
|
|
|
import Logs.Location
|
2016-01-07 18:51:28 +00:00
|
|
|
import Git.FilePath
|
|
|
|
import qualified Database.Keys
|
|
|
|
import Annex.InodeSentinal
|
|
|
|
import Utility.InodeCache
|
2012-02-16 20:36:35 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-08 19:08:02 +00:00
|
|
|
cmd = notDirect $
|
|
|
|
command "rekey" SectionPlumbing
|
|
|
|
"change keys used for files"
|
|
|
|
(paramRepeating $ paramPair paramPath paramKey)
|
2016-12-05 16:55:50 +00:00
|
|
|
(seek <$$> optParser)
|
2012-02-16 20:36:35 +00:00
|
|
|
|
2016-12-05 16:55:50 +00:00
|
|
|
data ReKeyOptions = ReKeyOptions
|
|
|
|
{ reKeyThese :: CmdParams
|
|
|
|
, batchOption :: BatchMode
|
|
|
|
}
|
2012-02-16 20:36:35 +00:00
|
|
|
|
2016-12-05 16:55:50 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser ReKeyOptions
|
|
|
|
optParser desc = ReKeyOptions
|
|
|
|
<$> cmdParams desc
|
|
|
|
<*> parseBatchOption
|
|
|
|
|
|
|
|
-- Split on the last space, since a FilePath can contain whitespace,
|
|
|
|
-- but a Key very rarely does.
|
|
|
|
batchParser :: String -> Either String (FilePath, Key)
|
|
|
|
batchParser s = case separate (== ' ') (reverse s) of
|
|
|
|
(rk, rf)
|
|
|
|
| null rk || null rf -> Left "Expected: \"file key\""
|
2019-01-14 17:17:47 +00:00
|
|
|
| otherwise -> case deserializeKey (reverse rk) of
|
2016-12-05 16:55:50 +00:00
|
|
|
Nothing -> Left "bad key"
|
|
|
|
Just k -> Right (reverse rf, k)
|
|
|
|
|
|
|
|
seek :: ReKeyOptions -> CommandSeek
|
|
|
|
seek o = case batchOption o of
|
added -z
Added -z option to git-annex commands that use --batch, useful for
supporting filenames containing newlines.
It only controls input to --batch, the output will still be line delimited
unless --json or etc is used to get some other output. While git often
makes -z affect both input and output, I don't like trying them together,
and making it affect output would have been a significant complication,
and also git-annex output is generally not intended to be machine parsed,
unless using --json or a format option.
Commands that take pairs like "file key" still separate them with a space
in --batch mode. All such commands take care to support filenames with
spaces when parsing that, so there was no need to change it, and it would
have needed significant changes to the batch machinery to separate tose
with a null.
To make fromkey and registerurl support -z, I had to give them a --batch
option. The implicit batch mode they enter when not provided with input
parameters does not support -z as that would have complicated option
parsing. Seemed better to move these toward using the same --batch as
everything else, though the implicit batch mode can still be used.
This commit was sponsored by Ole-Morten Duesund on Patreon.
2018-09-20 20:09:21 +00:00
|
|
|
Batch fmt -> batchInput fmt batchParser (batchCommandAction . start)
|
2018-10-01 18:12:06 +00:00
|
|
|
NoBatch -> withPairs (commandAction . start . parsekey) (reKeyThese o)
|
2016-12-05 16:55:50 +00:00
|
|
|
where
|
|
|
|
parsekey (file, skey) =
|
2019-01-14 17:17:47 +00:00
|
|
|
(file, fromMaybe (giveup "bad key") (deserializeKey skey))
|
2016-12-05 16:55:50 +00:00
|
|
|
|
|
|
|
start :: (FilePath, Key) -> CommandStart
|
|
|
|
start (file, newkey) = ifAnnexed file go stop
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2014-04-17 22:03:39 +00:00
|
|
|
go oldkey
|
2012-11-12 05:05:04 +00:00
|
|
|
| oldkey == newkey = stop
|
|
|
|
| otherwise = do
|
|
|
|
showStart "rekey" file
|
|
|
|
next $ perform file oldkey newkey
|
2012-02-16 20:36:35 +00:00
|
|
|
|
|
|
|
perform :: FilePath -> Key -> Key -> CommandPerform
|
|
|
|
perform file oldkey newkey = do
|
2016-01-07 18:51:28 +00:00
|
|
|
ifM (inAnnex oldkey)
|
|
|
|
( unlessM (linkKey file oldkey newkey) $
|
2018-10-16 19:52:40 +00:00
|
|
|
giveup "failed creating link from old to new key"
|
2016-01-07 18:51:28 +00:00
|
|
|
, unlessM (Annex.getState Annex.force) $
|
2016-11-16 01:29:54 +00:00
|
|
|
giveup $ file ++ " is not available (use --force to override)"
|
2016-01-07 18:51:28 +00:00
|
|
|
)
|
2012-02-17 02:36:56 +00:00
|
|
|
next $ cleanup file oldkey newkey
|
|
|
|
|
2013-06-10 17:10:30 +00:00
|
|
|
{- Make a hard link to the old key content (when supported),
|
|
|
|
- to avoid wasting disk space. -}
|
2016-01-07 18:51:28 +00:00
|
|
|
linkKey :: FilePath -> Key -> Key -> Annex Bool
|
|
|
|
linkKey file oldkey newkey = ifM (isJust <$> isAnnexLink file)
|
|
|
|
{- If the object file is already hardlinked to elsewhere, a hard
|
2018-03-13 18:18:30 +00:00
|
|
|
- link won't be made by getViaTmpFromDisk, but a copy instead.
|
2016-01-07 18:51:28 +00:00
|
|
|
- This avoids hard linking to content linked to an
|
|
|
|
- unlocked file, which would leave the new key unlocked
|
|
|
|
- and vulnerable to corruption. -}
|
2018-06-21 17:34:11 +00:00
|
|
|
( getViaTmpFromDisk RetrievalAllKeysSecure DefaultVerify newkey $ \tmp -> unVerified $ do
|
2016-01-07 18:51:28 +00:00
|
|
|
oldobj <- calcRepo (gitAnnexLocation oldkey)
|
2018-09-05 21:26:12 +00:00
|
|
|
isJust <$> linkOrCopy' (return True) newkey oldobj tmp Nothing
|
2016-01-07 18:51:28 +00:00
|
|
|
, do
|
2018-10-16 21:18:21 +00:00
|
|
|
{- The file being rekeyed is itself an unlocked file; if
|
|
|
|
- it's hard linked to the old key, that link must be broken. -}
|
2016-01-07 18:51:28 +00:00
|
|
|
oldobj <- calcRepo (gitAnnexLocation oldkey)
|
2018-10-16 21:18:21 +00:00
|
|
|
v <- tryNonAsync $ do
|
|
|
|
st <- liftIO $ getFileStatus file
|
|
|
|
when (linkCount st > 1) $ do
|
|
|
|
freezeContent oldobj
|
|
|
|
replaceFile file $ \tmp -> do
|
|
|
|
unlessM (checkedCopyFile oldkey oldobj tmp Nothing) $
|
|
|
|
error "can't lock old key"
|
|
|
|
thawContent tmp
|
2018-10-16 20:45:08 +00:00
|
|
|
ic <- withTSDelta (liftIO . genInodeCache file)
|
2016-01-07 18:51:28 +00:00
|
|
|
case v of
|
|
|
|
Left e -> do
|
|
|
|
warning (show e)
|
|
|
|
return False
|
|
|
|
Right () -> do
|
|
|
|
r <- linkToAnnex newkey file ic
|
|
|
|
return $ case r of
|
|
|
|
LinkAnnexFailed -> False
|
|
|
|
LinkAnnexOk -> True
|
|
|
|
LinkAnnexNoop -> True
|
|
|
|
)
|
2012-02-17 02:36:56 +00:00
|
|
|
|
|
|
|
cleanup :: FilePath -> Key -> Key -> CommandCleanup
|
|
|
|
cleanup file oldkey newkey = do
|
2016-01-07 18:51:28 +00:00
|
|
|
ifM (isJust <$> isAnnexLink file)
|
|
|
|
( do
|
|
|
|
-- Update symlink to use the new key.
|
|
|
|
liftIO $ removeFile file
|
|
|
|
addLink file newkey Nothing
|
|
|
|
, do
|
2016-04-14 18:30:15 +00:00
|
|
|
mode <- liftIO $ catchMaybeIO $ fileMode <$> getFileStatus file
|
2016-01-07 18:51:28 +00:00
|
|
|
liftIO $ whenM (isJust <$> isPointerFile file) $
|
2016-04-14 18:30:15 +00:00
|
|
|
writePointerFile file newkey mode
|
|
|
|
stagePointerFile file mode =<< hashPointerFile newkey
|
2016-01-07 18:51:28 +00:00
|
|
|
Database.Keys.removeAssociatedFile oldkey
|
|
|
|
=<< inRepo (toTopFilePath file)
|
|
|
|
)
|
2016-12-19 22:18:57 +00:00
|
|
|
whenM (inAnnex newkey) $
|
|
|
|
logStatus newkey InfoPresent
|
2013-04-11 17:35:52 +00:00
|
|
|
return True
|