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)
|
|
|
|
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
|
|
|
|
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-05-17 07:10:13 +00:00
|
|
|
import Utility
|
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-03-23 21:57:10 +00:00
|
|
|
upgradable <- Backend.upgradableKey oldbackend key
|
|
|
|
if (newbackend /= oldbackend || upgradable) && 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
|
|
|
|
choosebackend Nothing = do
|
|
|
|
backends <- Backend.list
|
|
|
|
return $ head backends
|
|
|
|
choosebackend (Just backend) = return backend
|
2011-01-08 19:54:14 +00:00
|
|
|
|
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
|
|
|
|
stored <- Backend.storeFileKey tmpfile $ Just newbackend
|
|
|
|
liftIO $ cleantmp tmpfile
|
2011-01-08 19:54:14 +00:00
|
|
|
case stored 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-05-15 06:02:46 +00:00
|
|
|
next $ Command.Add.cleanup file newkey
|
|
|
|
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
|