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
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-02-16 20:36:35 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
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
|
make CommandStart return a StartMessage
The goal is to be able to run CommandStart in the main thread when -J is
used, rather than unncessarily passing it off to a worker thread, which
incurs overhead that is signficant when the CommandStart is going to
quickly decide to stop.
To do that, the message it displays needs to be displayed in the worker
thread, after the CommandStart has run.
Also, the change will mean that CommandStart will no longer necessarily
run with the same Annex state as CommandPerform. While its docs already
said it should avoid modifying Annex state, I audited all the
CommandStart code as part of the conversion. (Note that CommandSeek
already sometimes runs with a different Annex state, and that has not been
a source of any problems, so I am not too worried that this change will
lead to breakage going forward.)
The only modification of Annex state I found was it calling
allowMessages in some Commands that default to noMessages. Dealt with
that by adding a startCustomOutput and a startingUsualMessages.
This lets a command start with noMessages and then select the output it
wants for each CommandStart.
One bit of breakage: onlyActionOn has been removed from commands that used it.
The plan is that, since a StartMessage contains an ActionItem,
when a Key can be extracted from that, the parallel job runner can
run onlyActionOn' automatically. Then commands won't need to worry about
this detail. Future work.
Otherwise, this was a fairly straightforward process of making each
CommandStart compile again. Hopefully other behavior changes were mostly
avoided.
In a few cases, a command had a CommandStart that called a CommandPerform
that then called showStart multiple times. I have collapsed those
down to a single start action. The main command to perhaps suffer from it
is Command.Direct, which used to show a start for each file, and no
longer does.
Another minor behavior change is that some commands used showStart
before, but had an associated file and a Key available, so were changed
to ShowStart with an ActionItemAssociatedFile. That will not change the
normal output or behavior, but --json output will now include the key.
This should not break it for anyone using a real json parser.
2019-06-06 19:42:30 +00:00
|
|
|
| otherwise = starting "rekey" (ActionItemWorkTreeFile file) $
|
|
|
|
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
|