2011-01-08 19:54:14 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2011 Joey Hess <id@joeyh.name>
|
2011-01-08 19:54:14 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Migrate where
|
|
|
|
|
|
|
|
import Command
|
2012-06-05 23:51:03 +00:00
|
|
|
import Backend
|
2014-07-10 21:06:04 +00:00
|
|
|
import Types.Backend (canUpgradeKey, fastMigrate)
|
2012-06-20 20:07:14 +00:00
|
|
|
import Types.KeySource
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2012-02-17 02:36:56 +00:00
|
|
|
import qualified Command.ReKey
|
2012-09-14 04:18:18 +00:00
|
|
|
import qualified Command.Fsck
|
2015-03-23 16:11:16 +00:00
|
|
|
import qualified Annex
|
2016-01-07 18:21:12 +00:00
|
|
|
import Logs.MetaData
|
2016-01-07 22:06:20 +00:00
|
|
|
import Logs.Web
|
2011-01-08 19:54:14 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2018-02-19 18:28:17 +00:00
|
|
|
cmd = notDirect $ withGlobalOptions [annexedMatchingOptions] $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "migrate" SectionUtility
|
|
|
|
"switch data to different backend"
|
|
|
|
paramPaths (withParams seek)
|
2011-01-08 19:54:14 +00:00
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
2018-10-01 18:12:06 +00:00
|
|
|
seek = withFilesInGit (commandAction . (whenAnnexed start)) <=< workTreeItems
|
2011-01-08 19:54:14 +00:00
|
|
|
|
2014-04-17 22:03:39 +00:00
|
|
|
start :: FilePath -> Key -> CommandStart
|
|
|
|
start file key = do
|
2015-03-23 16:11:16 +00:00
|
|
|
forced <- Annex.getState Annex.force
|
2014-04-17 22:03:39 +00:00
|
|
|
v <- Backend.getBackend file key
|
|
|
|
case v of
|
|
|
|
Nothing -> stop
|
|
|
|
Just oldbackend -> do
|
|
|
|
exists <- inAnnex key
|
2017-05-09 19:04:07 +00:00
|
|
|
newbackend <- maybe defaultBackend return
|
|
|
|
=<< chooseBackend file
|
2015-03-23 16:11:16 +00:00
|
|
|
if (newbackend /= oldbackend || upgradableKey oldbackend key || forced) && exists
|
2014-04-17 22:03:39 +00:00
|
|
|
then do
|
|
|
|
showStart "migrate" file
|
|
|
|
next $ perform file key oldbackend newbackend
|
|
|
|
else stop
|
2011-01-08 19:54:14 +00:00
|
|
|
|
2012-12-20 19:43:14 +00:00
|
|
|
{- Checks if a key is upgradable to a newer representation.
|
|
|
|
-
|
|
|
|
- Reasons for migration:
|
|
|
|
- - Ideally, all keys have file size metadata. Old keys may not.
|
|
|
|
- - Something has changed in the backend, such as a bug fix.
|
|
|
|
-}
|
|
|
|
upgradableKey :: Backend -> Key -> Bool
|
2016-01-20 20:36:33 +00:00
|
|
|
upgradableKey backend key = isNothing (keySize key) || backendupgradable
|
2012-12-20 19:43:14 +00:00
|
|
|
where
|
2014-07-10 21:06:04 +00:00
|
|
|
backendupgradable = maybe False (\a -> a key) (canUpgradeKey backend)
|
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
|
2013-05-13 18:27:39 +00:00
|
|
|
- be other files still pointing at that key.
|
|
|
|
-
|
|
|
|
- To ensure that the data we have for the old key is valid, it's
|
|
|
|
- fscked here. First we generate the new key. This ensures that the
|
|
|
|
- data cannot get corrupted after the fsck but before the new key is
|
|
|
|
- generated.
|
|
|
|
-}
|
2012-09-14 04:18:18 +00:00
|
|
|
perform :: FilePath -> Key -> Backend -> Backend -> CommandPerform
|
2018-09-24 16:07:46 +00:00
|
|
|
perform file oldkey oldbackend newbackend = go =<< genkey (fastMigrate oldbackend)
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
go Nothing = stop
|
2014-07-10 21:06:04 +00:00
|
|
|
go (Just (newkey, knowngoodcontent))
|
|
|
|
| knowngoodcontent = finish newkey
|
|
|
|
| otherwise = stopUnless checkcontent $ finish newkey
|
2017-03-10 17:12:24 +00:00
|
|
|
checkcontent = Command.Fsck.checkBackend oldbackend oldkey Command.Fsck.KeyLocked afile
|
2016-01-07 18:51:28 +00:00
|
|
|
finish newkey = ifM (Command.ReKey.linkKey file oldkey newkey)
|
|
|
|
( do
|
2017-10-16 16:54:00 +00:00
|
|
|
_ <- copyMetaData oldkey newkey
|
2016-01-07 22:06:20 +00:00
|
|
|
-- If the old key had some associated urls, record them for
|
|
|
|
-- the new key as well.
|
|
|
|
urls <- getUrls oldkey
|
2018-10-04 21:33:25 +00:00
|
|
|
forM_ urls $ \url ->
|
|
|
|
setUrlPresent newkey url
|
2016-01-07 18:51:28 +00:00
|
|
|
next $ Command.ReKey.cleanup file oldkey newkey
|
2018-10-16 19:52:40 +00:00
|
|
|
, giveup "failed creating link from old to new key"
|
2016-01-07 18:51:28 +00:00
|
|
|
)
|
2018-09-24 16:07:46 +00:00
|
|
|
genkey Nothing = return Nothing
|
|
|
|
genkey (Just fm) = fm oldkey newbackend afile >>= \case
|
2014-07-10 21:06:04 +00:00
|
|
|
Just newkey -> return $ Just (newkey, True)
|
|
|
|
Nothing -> do
|
|
|
|
content <- calcRepo $ gitAnnexLocation oldkey
|
|
|
|
let source = KeySource
|
|
|
|
{ keyFilename = file
|
|
|
|
, contentLocation = content
|
|
|
|
, inodeCache = Nothing
|
|
|
|
}
|
|
|
|
v <- genKey source (Just newbackend)
|
|
|
|
return $ case v of
|
|
|
|
Just (newkey, _) -> Just (newkey, False)
|
|
|
|
_ -> Nothing
|
2017-03-10 17:12:24 +00:00
|
|
|
afile = AssociatedFile (Just file)
|