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
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-01-08 19:54:14 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
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
|
2019-06-25 15:37:52 +00:00
|
|
|
import Utility.Metered
|
2011-01-08 19:54:14 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2019-08-26 19:52:19 +00:00
|
|
|
cmd = 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
|
2020-07-10 19:40:06 +00:00
|
|
|
seek = withFilesInGitAnnex ww (commandAction' start)
|
2020-05-28 19:55:17 +00:00
|
|
|
<=< workTreeItems ww
|
|
|
|
where
|
|
|
|
ww = WarnUnmatchLsFiles
|
2011-01-08 19:54:14 +00:00
|
|
|
|
2019-12-05 18:36:43 +00:00
|
|
|
start :: RawFilePath -> Key -> CommandStart
|
2014-04-17 22:03:39 +00:00
|
|
|
start file key = do
|
2015-03-23 16:11:16 +00:00
|
|
|
forced <- Annex.getState Annex.force
|
2019-12-05 18:36:43 +00:00
|
|
|
v <- Backend.getBackend (fromRawFilePath file) key
|
2014-04-17 22:03:39 +00:00
|
|
|
case v of
|
|
|
|
Nothing -> stop
|
|
|
|
Just oldbackend -> do
|
|
|
|
exists <- inAnnex key
|
2017-05-09 19:04:07 +00:00
|
|
|
newbackend <- maybe defaultBackend return
|
2019-12-05 18:36:43 +00:00
|
|
|
=<< chooseBackend (fromRawFilePath file)
|
2015-03-23 16:11:16 +00:00
|
|
|
if (newbackend /= oldbackend || upgradableKey oldbackend key || forced) && exists
|
make CommandStart return a StartMessage
The goal is to be able to run CommandStart in the main thread when -J is
used, rather than unncessarily passing it off to a worker thread, which
incurs overhead that is signficant when the CommandStart is going to
quickly decide to stop.
To do that, the message it displays needs to be displayed in the worker
thread, after the CommandStart has run.
Also, the change will mean that CommandStart will no longer necessarily
run with the same Annex state as CommandPerform. While its docs already
said it should avoid modifying Annex state, I audited all the
CommandStart code as part of the conversion. (Note that CommandSeek
already sometimes runs with a different Annex state, and that has not been
a source of any problems, so I am not too worried that this change will
lead to breakage going forward.)
The only modification of Annex state I found was it calling
allowMessages in some Commands that default to noMessages. Dealt with
that by adding a startCustomOutput and a startingUsualMessages.
This lets a command start with noMessages and then select the output it
wants for each CommandStart.
One bit of breakage: onlyActionOn has been removed from commands that used it.
The plan is that, since a StartMessage contains an ActionItem,
when a Key can be extracted from that, the parallel job runner can
run onlyActionOn' automatically. Then commands won't need to worry about
this detail. Future work.
Otherwise, this was a fairly straightforward process of making each
CommandStart compile again. Hopefully other behavior changes were mostly
avoided.
In a few cases, a command had a CommandStart that called a CommandPerform
that then called showStart multiple times. I have collapsed those
down to a single start action. The main command to perhaps suffer from it
is Command.Direct, which used to show a start for each file, and no
longer does.
Another minor behavior change is that some commands used showStart
before, but had an associated file and a Key available, so were changed
to ShowStart with an ActionItemAssociatedFile. That will not change the
normal output or behavior, but --json output will now include the key.
This should not break it for anyone using a real json parser.
2019-06-06 19:42:30 +00:00
|
|
|
then starting "migrate" (mkActionItem (key, file)) $
|
|
|
|
perform file key oldbackend newbackend
|
2014-04-17 22:03:39 +00:00
|
|
|
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
|
2019-11-22 20:24:04 +00:00
|
|
|
upgradableKey backend key = isNothing (fromKey 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.
|
|
|
|
-}
|
2019-12-05 18:36:43 +00:00
|
|
|
perform :: RawFilePath -> 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
|
2019-03-18 19:53:54 +00:00
|
|
|
checkcontent = Command.Fsck.checkBackend oldbackend oldkey Command.Fsck.KeyPresent 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-10-29 20:26:43 +00:00
|
|
|
genkey Nothing = do
|
|
|
|
content <- calcRepo $ gitAnnexLocation oldkey
|
|
|
|
let source = KeySource
|
2020-02-21 13:34:59 +00:00
|
|
|
{ keyFilename = file
|
|
|
|
, contentLocation = content
|
2018-10-29 20:26:43 +00:00
|
|
|
, inodeCache = Nothing
|
|
|
|
}
|
2020-05-15 16:51:09 +00:00
|
|
|
newkey <- fst <$> genKey source nullMeterUpdate (Just newbackend)
|
|
|
|
return $ Just (newkey, False)
|
2018-09-24 16:07:46 +00:00
|
|
|
genkey (Just fm) = fm oldkey newbackend afile >>= \case
|
2018-10-29 20:26:43 +00:00
|
|
|
Just newkey -> return (Just (newkey, True))
|
|
|
|
Nothing -> genkey Nothing
|
2017-03-10 17:12:24 +00:00
|
|
|
afile = AssociatedFile (Just file)
|