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-02-15 17:51:50 +00:00
|
|
|
import Config
|
|
|
|
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)
|
|
|
|
seek "change keys used for files"]
|
|
|
|
|
|
|
|
seek :: [CommandSeek]
|
|
|
|
seek = [withPairs start]
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
{- Make a hard link to the old key content, to avoid wasting disk space. -}
|
|
|
|
linkKey :: Key -> Key -> Annex Bool
|
2012-02-17 04:21:35 +00:00
|
|
|
linkKey oldkey newkey = getViaTmpUnchecked newkey $ \tmp -> do
|
2012-02-17 02:36:56 +00:00
|
|
|
src <- inRepo $ gitAnnexLocation oldkey
|
2013-02-15 17:51:50 +00:00
|
|
|
ifM (liftIO $ doesFileExist tmp)
|
|
|
|
( return True
|
|
|
|
, ifM crippledFileSystem
|
|
|
|
( liftIO $ copyFileExternal src tmp
|
|
|
|
, do
|
|
|
|
liftIO $ createLink src tmp
|
|
|
|
return True
|
|
|
|
)
|
|
|
|
)
|
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
|
2012-02-17 02:36:56 +00:00
|
|
|
Command.Add.cleanup file newkey True
|