2012-02-16 20:36:35 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.ReKey where
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Command
|
|
|
|
import qualified Annex
|
|
|
|
import Types.Key
|
|
|
|
import Annex.Content
|
2012-02-17 02:36:56 +00:00
|
|
|
import qualified Command.Add
|
|
|
|
import Logs.Web
|
2013-04-11 17:35:52 +00:00
|
|
|
import Logs.Location
|
2013-02-15 17:51:50 +00:00
|
|
|
import Utility.CopyFile
|
2012-02-16 20:36:35 +00:00
|
|
|
|
|
|
|
def :: [Command]
|
2012-12-29 18:28:19 +00:00
|
|
|
def = [notDirect $ command "rekey"
|
2012-02-16 20:36:35 +00:00
|
|
|
(paramOptional $ paramRepeating $ paramPair paramPath paramKey)
|
2013-03-24 22:28:21 +00:00
|
|
|
seek SectionPlumbing "change keys used for files"]
|
2012-02-16 20:36:35 +00:00
|
|
|
|
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 :: CommandSeek
|
|
|
|
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
|
|
|
|
go (oldkey, _)
|
|
|
|
| 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
|
|
|
|
present <- inAnnex oldkey
|
|
|
|
_ <- if present
|
2012-02-17 02:36:56 +00:00
|
|
|
then linkKey oldkey newkey
|
2012-02-16 20:36:35 +00:00
|
|
|
else do
|
|
|
|
unlessM (Annex.getState Annex.force) $
|
|
|
|
error $ file ++ " is not available (use --force to override)"
|
|
|
|
return True
|
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. -}
|
2012-02-17 02:36:56 +00:00
|
|
|
linkKey :: Key -> Key -> Annex Bool
|
2012-02-17 04:21:35 +00:00
|
|
|
linkKey oldkey newkey = getViaTmpUnchecked newkey $ \tmp -> do
|
2013-04-04 19:46:33 +00:00
|
|
|
src <- calcRepo $ gitAnnexLocation oldkey
|
2013-06-10 17:10:30 +00:00
|
|
|
liftIO $ ifM (doesFileExist tmp)
|
2013-02-15 17:51:50 +00:00
|
|
|
( return True
|
2013-06-10 17:10:30 +00:00
|
|
|
, createLinkOrCopy src tmp
|
2013-02-15 17:51:50 +00:00
|
|
|
)
|
2012-02-17 02:36:56 +00:00
|
|
|
|
|
|
|
cleanup :: FilePath -> Key -> Key -> CommandCleanup
|
|
|
|
cleanup file oldkey newkey = do
|
|
|
|
-- If the old key had some associated urls, record them for
|
|
|
|
-- the new key as well.
|
|
|
|
urls <- getUrls oldkey
|
|
|
|
unless (null urls) $
|
|
|
|
mapM_ (setUrlPresent newkey) urls
|
|
|
|
|
2012-02-17 02:38:08 +00:00
|
|
|
-- Update symlink to use the new key.
|
|
|
|
liftIO $ removeFile file
|
2013-09-25 20:07:11 +00:00
|
|
|
Command.Add.addLink file newkey Nothing
|
2013-04-11 17:35:52 +00:00
|
|
|
logStatus newkey InfoPresent
|
|
|
|
return True
|