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\""
|
|
|
|
| otherwise -> case file2key (reverse rk) of
|
|
|
|
Nothing -> Left "bad key"
|
|
|
|
Just k -> Right (reverse rf, k)
|
|
|
|
|
|
|
|
seek :: ReKeyOptions -> CommandSeek
|
|
|
|
seek o = case batchOption o of
|
|
|
|
Batch -> batchInput batchParser (batchCommandAction . start)
|
|
|
|
NoBatch -> withPairs (start . parsekey) (reKeyThese o)
|
|
|
|
where
|
|
|
|
parsekey (file, skey) =
|
|
|
|
(file, fromMaybe (giveup "bad key") (file2key skey))
|
|
|
|
|
|
|
|
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) $
|
2016-11-22 05:16:18 +00:00
|
|
|
giveup "failed"
|
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
|
|
|
|
- link won't be made by getViaTmp', but a copy instead.
|
|
|
|
- This avoids hard linking to content linked to an
|
|
|
|
- unlocked file, which would leave the new key unlocked
|
|
|
|
- and vulnerable to corruption. -}
|
|
|
|
( getViaTmp' DefaultVerify newkey $ \tmp -> unVerified $ do
|
|
|
|
oldobj <- calcRepo (gitAnnexLocation oldkey)
|
2016-04-14 18:30:15 +00:00
|
|
|
linkOrCopy' (return True) newkey oldobj tmp Nothing
|
2016-01-07 18:51:28 +00:00
|
|
|
, do
|
|
|
|
ic <- withTSDelta (liftIO . genInodeCache file)
|
|
|
|
{- The file being rekeyed is itself an unlocked file, so if
|
|
|
|
- it's linked to the old key, that link must be broken. -}
|
|
|
|
oldobj <- calcRepo (gitAnnexLocation oldkey)
|
|
|
|
v <- tryNonAsync $ modifyContent oldobj $ do
|
|
|
|
replaceFile oldobj $ \tmp ->
|
2016-04-14 18:30:15 +00:00
|
|
|
unlessM (checkedCopyFile oldkey file tmp Nothing) $
|
2016-01-07 18:51:28 +00:00
|
|
|
error "can't lock old key"
|
|
|
|
freezeContent oldobj
|
|
|
|
oldic <- withTSDelta (liftIO . genInodeCache oldobj)
|
|
|
|
whenM (isUnmodified oldkey oldobj) $
|
|
|
|
Database.Keys.addInodeCaches oldkey (catMaybes [oldic])
|
|
|
|
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
|