diff options
Diffstat (limited to 'Command')
-rw-r--r-- | Command/Migrate.hs | 39 | ||||
-rw-r--r-- | Command/ReKey.hs | 46 |
2 files changed, 68 insertions, 17 deletions
diff --git a/Command/Migrate.hs b/Command/Migrate.hs index c6b0f086c..795ecc265 100644 --- a/Command/Migrate.hs +++ b/Command/Migrate.hs @@ -58,22 +58,27 @@ perform file oldkey newbackend = do cleantmp tmpfile case k of Nothing -> stop - Just (newkey, _) -> stopUnless (link src newkey) $ do - -- Update symlink to use the new key. - liftIO $ removeFile file - - -- 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 - - next $ Command.Add.cleanup file newkey True + Just (newkey, _) -> stopUnless (linkKey src newkey) $ + next $ cleanup file oldkey newkey where cleantmp t = liftIO $ whenM (doesFileExist t) $ removeFile t - link src newkey = getViaTmpUnchecked newkey $ \t -> do - -- Make a hard link to the old backend's - -- cached key, to avoid wasting disk space. - liftIO $ unlessM (doesFileExist t) $ createLink src t - return True + +linkKey :: FilePath -> Key -> Annex Bool +linkKey src newkey = getViaTmpUnchecked newkey $ \t -> do + -- Make a hard link to the old backend's + -- cached key, to avoid wasting disk space. + liftIO $ unlessM (doesFileExist t) $ createLink src t + return True + +cleanup :: FilePath -> Key -> Key -> CommandCleanup +cleanup file oldkey newkey = do + -- Update symlink to use the new key. + liftIO $ removeFile file + + -- 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 + + Command.Add.cleanup file newkey True diff --git a/Command/ReKey.hs b/Command/ReKey.hs new file mode 100644 index 000000000..9c3625aef --- /dev/null +++ b/Command/ReKey.hs @@ -0,0 +1,46 @@ +{- 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 +import qualified Command.Migrate + +def :: [Command] +def = [command "rekey" + (paramOptional $ paramRepeating $ paramPair paramPath paramKey) + seek "change keys used for files"] + +seek :: [CommandSeek] +seek = [withPairs start] + +start :: (FilePath, String) -> CommandStart +start (file, keyname) = ifAnnexed file go stop + where + newkey = fromMaybe (error "bad key") $ readKey keyname + go (oldkey, _) + | oldkey == newkey = stop + | otherwise = do + showStart "rekey" file + next $ perform file oldkey newkey + +perform :: FilePath -> Key -> Key -> CommandPerform +perform file oldkey newkey = do + present <- inAnnex oldkey + _ <- if present + then do + src <- inRepo $ gitAnnexLocation oldkey + Command.Migrate.linkKey src newkey + else do + unlessM (Annex.getState Annex.force) $ + error $ file ++ " is not available (use --force to override)" + return True + next $ Command.Migrate.cleanup file oldkey newkey |