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)
|
|
|
|
(withParams seek)
|
2012-02-16 20:36:35 +00:00
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
seek = withPairs start
|
2012-02-16 20:36:35 +00:00
|
|
|
|
|
|
|
start :: (FilePath, String) -> CommandStart
|
|
|
|
start (file, keyname) = ifAnnexed file go stop
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
newkey = fromMaybe (error "bad key") $ file2key keyname
|
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) $
|
|
|
|
error "failed"
|
|
|
|
, unlessM (Annex.getState Annex.force) $
|
|
|
|
error $ file ++ " is not available (use --force to override)"
|
|
|
|
)
|
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)
|
|
|
|
linkOrCopy' (return True) newkey oldobj tmp
|
|
|
|
, 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 ->
|
|
|
|
unlessM (checkedCopyFile oldkey file tmp) $
|
|
|
|
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
|
|
|
|
liftIO $ whenM (isJust <$> isPointerFile file) $
|
|
|
|
writeFile file (formatPointer newkey)
|
|
|
|
stagePointerFile file =<< hashPointerFile newkey
|
|
|
|
Database.Keys.removeAssociatedFile oldkey
|
|
|
|
=<< inRepo (toTopFilePath file)
|
|
|
|
)
|
|
|
|
|
2013-04-11 17:35:52 +00:00
|
|
|
logStatus newkey InfoPresent
|
|
|
|
return True
|