2011-01-08 19:54:14 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2011-01-08 20:09:17 +00:00
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
2011-01-08 19:54:14 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Migrate where
|
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2011-01-08 19:54:14 +00:00
|
|
|
import Command
|
|
|
|
import qualified Backend
|
2011-07-05 22:31:46 +00:00
|
|
|
import qualified Types.Key
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2011-01-08 19:54:14 +00:00
|
|
|
import qualified Command.Add
|
2011-10-15 20:36:56 +00:00
|
|
|
import Logs.Web
|
2011-01-08 19:54:14 +00:00
|
|
|
|
2011-10-29 19:19:05 +00:00
|
|
|
def :: [Command]
|
|
|
|
def = [command "migrate" paramPaths seek "switch data to different backend"]
|
2011-01-08 19:54:14 +00:00
|
|
|
|
|
|
|
seek :: [CommandSeek]
|
2012-02-14 03:42:44 +00:00
|
|
|
seek = [withFilesInGit $ whenAnnexed start]
|
2011-01-08 19:54:14 +00:00
|
|
|
|
2012-02-14 03:42:44 +00:00
|
|
|
start :: FilePath -> (Key, Backend) -> CommandStart
|
|
|
|
start file (key, oldbackend) = do
|
2011-01-08 19:54:14 +00:00
|
|
|
exists <- inAnnex key
|
2012-02-14 03:42:44 +00:00
|
|
|
newbackend <- choosebackend =<< Backend.chooseBackend file
|
2011-07-05 22:31:46 +00:00
|
|
|
if (newbackend /= oldbackend || upgradableKey key) && exists
|
2011-01-08 19:54:14 +00:00
|
|
|
then do
|
|
|
|
showStart "migrate" file
|
2011-05-15 06:02:46 +00:00
|
|
|
next $ perform file key newbackend
|
|
|
|
else stop
|
2011-01-12 01:32:38 +00:00
|
|
|
where
|
2011-12-15 22:11:42 +00:00
|
|
|
choosebackend Nothing = Prelude.head <$> Backend.orderedList
|
2011-01-12 01:32:38 +00:00
|
|
|
choosebackend (Just backend) = return backend
|
2011-01-08 19:54:14 +00:00
|
|
|
|
2011-07-05 22:31:46 +00:00
|
|
|
{- Checks if a key is upgradable to a newer representation. -}
|
|
|
|
{- Ideally, all keys have file size metadata. Old keys may not. -}
|
|
|
|
upgradableKey :: Key -> Bool
|
2011-09-21 03:24:48 +00:00
|
|
|
upgradableKey key = isNothing $ Types.Key.keySize key
|
2011-07-05 22:31:46 +00:00
|
|
|
|
2011-11-19 19:16:38 +00:00
|
|
|
{- Store the old backend's key in the new backend
|
|
|
|
- The old backend's key is not dropped from it, because there may
|
|
|
|
- be other files still pointing at that key.
|
|
|
|
-
|
|
|
|
- Use the same filename as the file for the temp file name, to support
|
|
|
|
- backends that allow the filename to influence the keys they
|
|
|
|
- generate.
|
|
|
|
-}
|
2011-12-31 08:11:39 +00:00
|
|
|
perform :: FilePath -> Key -> Backend -> CommandPerform
|
2011-01-08 19:54:14 +00:00
|
|
|
perform file oldkey newbackend = do
|
2011-11-29 02:43:51 +00:00
|
|
|
src <- inRepo $ gitAnnexLocation oldkey
|
2011-11-11 05:52:58 +00:00
|
|
|
tmp <- fromRepo gitAnnexTmpDir
|
2011-11-08 19:34:10 +00:00
|
|
|
let tmpfile = tmp </> takeFileName file
|
2011-11-17 22:29:28 +00:00
|
|
|
cleantmp tmpfile
|
2011-05-16 16:10:08 +00:00
|
|
|
liftIO $ createLink src tmpfile
|
2011-07-05 22:31:46 +00:00
|
|
|
k <- Backend.genKey tmpfile $ Just newbackend
|
2011-11-17 22:29:28 +00:00
|
|
|
cleantmp tmpfile
|
2011-07-05 22:31:46 +00:00
|
|
|
case k of
|
2011-05-15 06:02:46 +00:00
|
|
|
Nothing -> stop
|
2012-02-16 20:36:35 +00:00
|
|
|
Just (newkey, _) -> stopUnless (linkKey src newkey) $
|
|
|
|
next $ cleanup file oldkey newkey
|
2011-05-16 16:10:08 +00:00
|
|
|
where
|
2011-11-17 22:29:28 +00:00
|
|
|
cleantmp t = liftIO $ whenM (doesFileExist t) $ removeFile t
|
2012-02-16 20:36:35 +00:00
|
|
|
|
|
|
|
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
|