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
|
|
|
|
|
|
|
|
import Control.Monad.State (liftIO)
|
2011-08-25 04:28:55 +00:00
|
|
|
import Control.Applicative
|
2011-01-08 19:54:14 +00:00
|
|
|
import System.Posix.Files
|
|
|
|
import System.Directory
|
2011-05-16 16:10:08 +00:00
|
|
|
import System.FilePath
|
2011-01-08 19:54:14 +00:00
|
|
|
|
|
|
|
import Command
|
|
|
|
import qualified Annex
|
|
|
|
import qualified Backend
|
2011-07-05 22:31:46 +00:00
|
|
|
import qualified Types.Key
|
2011-01-08 19:54:14 +00:00
|
|
|
import Locations
|
|
|
|
import Types
|
2011-01-16 20:05:05 +00:00
|
|
|
import Content
|
2011-01-08 19:54:14 +00:00
|
|
|
import Messages
|
2011-08-22 20:14:12 +00:00
|
|
|
import Utility.Conditional
|
2011-01-08 19:54:14 +00:00
|
|
|
import qualified Command.Add
|
|
|
|
|
|
|
|
command :: [Command]
|
2011-03-19 22:58:49 +00:00
|
|
|
command = [repoCommand "migrate" paramPath seek "switch data to different backend"]
|
2011-01-08 19:54:14 +00:00
|
|
|
|
|
|
|
seek :: [CommandSeek]
|
|
|
|
seek = [withBackendFilesInGit start]
|
|
|
|
|
|
|
|
start :: CommandStartBackendFile
|
2011-01-12 01:32:38 +00:00
|
|
|
start (file, b) = isAnnexed file $ \(key, oldbackend) -> do
|
2011-01-08 19:54:14 +00:00
|
|
|
exists <- inAnnex key
|
2011-01-12 01:32:38 +00:00
|
|
|
newbackend <- choosebackend b
|
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-08-25 04:28:55 +00:00
|
|
|
choosebackend Nothing = 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
|
|
|
|
upgradableKey key = Types.Key.keySize key == Nothing
|
|
|
|
|
2011-01-26 01:02:34 +00:00
|
|
|
perform :: FilePath -> Key -> Backend Annex -> CommandPerform
|
2011-01-08 19:54:14 +00:00
|
|
|
perform file oldkey newbackend = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
|
|
|
|
-- Store the old backend's cached key in the new backend
|
|
|
|
-- (the file can't be stored as usual, because it's already a symlink).
|
|
|
|
-- The old backend's key is not dropped from it, because there may
|
|
|
|
-- be other files still pointing at that key.
|
2011-01-27 21:00:32 +00:00
|
|
|
let src = gitAnnexLocation g oldkey
|
2011-05-16 16:10:08 +00:00
|
|
|
let tmpfile = gitAnnexTmpDir g </> takeFileName file
|
|
|
|
liftIO $ createLink src tmpfile
|
2011-07-05 22:31:46 +00:00
|
|
|
k <- Backend.genKey tmpfile $ Just newbackend
|
2011-05-16 16:10:08 +00:00
|
|
|
liftIO $ cleantmp tmpfile
|
2011-07-05 22:31:46 +00:00
|
|
|
case k of
|
2011-05-15 06:02:46 +00:00
|
|
|
Nothing -> stop
|
2011-01-08 19:54:14 +00:00
|
|
|
Just (newkey, _) -> do
|
2011-03-22 21:27:04 +00:00
|
|
|
ok <- getViaTmpUnchecked newkey $ \t -> do
|
2011-01-08 19:54:14 +00:00
|
|
|
-- Make a hard link to the old backend's
|
|
|
|
-- cached key, to avoid wasting disk space.
|
2011-05-17 07:10:13 +00:00
|
|
|
liftIO $ unlessM (doesFileExist t) $ createLink src t
|
2011-01-08 19:54:14 +00:00
|
|
|
return True
|
|
|
|
if ok
|
|
|
|
then do
|
|
|
|
-- Update symlink to use the new key.
|
|
|
|
liftIO $ removeFile file
|
2011-08-06 18:57:22 +00:00
|
|
|
next $ Command.Add.cleanup file newkey True
|
2011-05-15 06:02:46 +00:00
|
|
|
else stop
|
2011-05-16 16:10:08 +00:00
|
|
|
where
|
2011-05-17 07:10:13 +00:00
|
|
|
cleantmp t = whenM (doesFileExist t) $ removeFile t
|