2011-01-08 15:54:14 -04:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-01-21 12:50:09 -04:00
|
|
|
- Copyright 2011 Joey Hess <id@joeyh.name>
|
2011-01-08 15:54:14 -04:00
|
|
|
-
|
2019-03-13 15:48:14 -04:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-01-08 15:54:14 -04:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Migrate where
|
|
|
|
|
|
|
|
import Command
|
2012-06-05 19:51:03 -04:00
|
|
|
import Backend
|
2014-07-10 17:06:04 -04:00
|
|
|
import Types.Backend (canUpgradeKey, fastMigrate)
|
2012-06-20 16:07:14 -04:00
|
|
|
import Types.KeySource
|
2011-10-04 00:40:47 -04:00
|
|
|
import Annex.Content
|
2012-02-16 22:36:56 -04:00
|
|
|
import qualified Command.ReKey
|
2012-09-14 00:18:18 -04:00
|
|
|
import qualified Command.Fsck
|
2015-03-23 12:11:16 -04:00
|
|
|
import qualified Annex
|
2016-01-07 14:21:12 -04:00
|
|
|
import Logs.MetaData
|
2016-01-07 18:06:20 -04:00
|
|
|
import Logs.Web
|
2011-01-08 15:54:14 -04:00
|
|
|
|
2015-07-08 12:33:27 -04:00
|
|
|
cmd :: Command
|
2018-02-19 14:28:17 -04:00
|
|
|
cmd = notDirect $ withGlobalOptions [annexedMatchingOptions] $
|
2015-07-08 15:08:02 -04:00
|
|
|
command "migrate" SectionUtility
|
|
|
|
"switch data to different backend"
|
|
|
|
paramPaths (withParams seek)
|
2011-01-08 15:54:14 -04:00
|
|
|
|
2015-07-08 15:08:02 -04:00
|
|
|
seek :: CmdParams -> CommandSeek
|
2018-10-01 14:12:06 -04:00
|
|
|
seek = withFilesInGit (commandAction . (whenAnnexed start)) <=< workTreeItems
|
2011-01-08 15:54:14 -04:00
|
|
|
|
2014-04-17 18:03:39 -04:00
|
|
|
start :: FilePath -> Key -> CommandStart
|
|
|
|
start file key = do
|
2015-03-23 12:11:16 -04:00
|
|
|
forced <- Annex.getState Annex.force
|
2014-04-17 18:03:39 -04:00
|
|
|
v <- Backend.getBackend file key
|
|
|
|
case v of
|
|
|
|
Nothing -> stop
|
|
|
|
Just oldbackend -> do
|
|
|
|
exists <- inAnnex key
|
2017-05-09 15:04:07 -04:00
|
|
|
newbackend <- maybe defaultBackend return
|
|
|
|
=<< chooseBackend file
|
2015-03-23 12:11:16 -04:00
|
|
|
if (newbackend /= oldbackend || upgradableKey oldbackend key || forced) && exists
|
2014-04-17 18:03:39 -04:00
|
|
|
then do
|
|
|
|
showStart "migrate" file
|
|
|
|
next $ perform file key oldbackend newbackend
|
|
|
|
else stop
|
2011-01-08 15:54:14 -04:00
|
|
|
|
2012-12-20 15:43:14 -04: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 16:36:33 -04:00
|
|
|
upgradableKey backend key = isNothing (keySize key) || backendupgradable
|
2012-12-20 15:43:14 -04:00
|
|
|
where
|
2014-07-10 17:06:04 -04:00
|
|
|
backendupgradable = maybe False (\a -> a key) (canUpgradeKey backend)
|
2011-07-05 18:31:46 -04:00
|
|
|
|
2011-11-19 15:16:38 -04: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 14:27:39 -04: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 00:18:18 -04:00
|
|
|
perform :: FilePath -> Key -> Backend -> Backend -> CommandPerform
|
2018-09-24 12:07:46 -04:00
|
|
|
perform file oldkey oldbackend newbackend = go =<< genkey (fastMigrate oldbackend)
|
2012-11-12 01:05:04 -04:00
|
|
|
where
|
2014-10-09 14:53:13 -04:00
|
|
|
go Nothing = stop
|
2014-07-10 17:06:04 -04:00
|
|
|
go (Just (newkey, knowngoodcontent))
|
|
|
|
| knowngoodcontent = finish newkey
|
|
|
|
| otherwise = stopUnless checkcontent $ finish newkey
|
2019-03-18 15:53:54 -04:00
|
|
|
checkcontent = Command.Fsck.checkBackend oldbackend oldkey Command.Fsck.KeyPresent afile
|
2016-01-07 14:51:28 -04:00
|
|
|
finish newkey = ifM (Command.ReKey.linkKey file oldkey newkey)
|
|
|
|
( do
|
2017-10-16 12:54:00 -04:00
|
|
|
_ <- copyMetaData oldkey newkey
|
2016-01-07 18:06:20 -04:00
|
|
|
-- If the old key had some associated urls, record them for
|
|
|
|
-- the new key as well.
|
|
|
|
urls <- getUrls oldkey
|
2018-10-04 17:33:25 -04:00
|
|
|
forM_ urls $ \url ->
|
|
|
|
setUrlPresent newkey url
|
2016-01-07 14:51:28 -04:00
|
|
|
next $ Command.ReKey.cleanup file oldkey newkey
|
2018-10-16 15:52:40 -04:00
|
|
|
, giveup "failed creating link from old to new key"
|
2016-01-07 14:51:28 -04:00
|
|
|
)
|
2018-10-29 16:26:43 -04:00
|
|
|
genkey 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
|
2018-09-24 12:07:46 -04:00
|
|
|
genkey (Just fm) = fm oldkey newbackend afile >>= \case
|
2018-10-29 16:26:43 -04:00
|
|
|
Just newkey -> return (Just (newkey, True))
|
|
|
|
Nothing -> genkey Nothing
|
2017-03-10 13:12:24 -04:00
|
|
|
afile = AssociatedFile (Just file)
|