2011-01-08 19:54:14 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2023-12-06 19:38:01 +00:00
|
|
|
- Copyright 2011-2023 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
|
2023-12-06 19:38:01 +00:00
|
|
|
import Logs.Migrate
|
2016-01-07 18:21:12 +00:00
|
|
|
import Logs.MetaData
|
2016-01-07 22:06:20 +00:00
|
|
|
import Logs.Web
|
2023-12-07 21:26:12 +00:00
|
|
|
import Logs.Location
|
2019-06-25 15:37:52 +00:00
|
|
|
import Utility.Metered
|
2023-12-07 22:00:09 +00:00
|
|
|
import qualified Database.Keys
|
|
|
|
import Git.FilePath
|
|
|
|
import Annex.Link
|
2023-12-08 17:23:03 +00:00
|
|
|
import Annex.UUID
|
2011-01-08 19:54:14 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2023-05-04 20:34:35 +00:00
|
|
|
cmd = withAnnexOptions [backendOption, annexedMatchingOptions, jsonOptions] $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "migrate" SectionUtility
|
|
|
|
"switch data to different backend"
|
migrate: New --remove-size option
While intended for converting URL keys added by addurl --fast to be
as if added by addurl --relaxed, it can also be used to remove size
from other types of keys. Although that is not likely to be useful
for checksummed keys, I suppose it could be used for WORM or other
non-checksum keys.
Specifying the --remove-size option does not prevent other migrations
from taking effect if there's a key upgrade to perform, or if the
backend has changed. So --backend=URL needs to be used to prevent
migrating an URL key to the default backend.
Note that it's not possible to use git-annex migrate to convert from a
non-URL key to an URL key, as URL keys cannot be generated, except by
addurl. So while this can get the same effect as --relaxed would have
when addurl --fast was used, when --fast was not used, it won't work, or
if --backend=URL is not used will remove the size but not prevent
checksum verification, which is not useful. Due to this complexity, I
decided not to mention it in the git-annex addurl man page.
Sponsored-by: Jochen Bartl on Patreon
2021-11-12 16:59:30 +00:00
|
|
|
paramPaths (seek <$$> optParser)
|
2011-01-08 19:54:14 +00:00
|
|
|
|
migrate: New --remove-size option
While intended for converting URL keys added by addurl --fast to be
as if added by addurl --relaxed, it can also be used to remove size
from other types of keys. Although that is not likely to be useful
for checksummed keys, I suppose it could be used for WORM or other
non-checksum keys.
Specifying the --remove-size option does not prevent other migrations
from taking effect if there's a key upgrade to perform, or if the
backend has changed. So --backend=URL needs to be used to prevent
migrating an URL key to the default backend.
Note that it's not possible to use git-annex migrate to convert from a
non-URL key to an URL key, as URL keys cannot be generated, except by
addurl. So while this can get the same effect as --relaxed would have
when addurl --fast was used, when --fast was not used, it won't work, or
if --backend=URL is not used will remove the size but not prevent
checksum verification, which is not useful. Due to this complexity, I
decided not to mention it in the git-annex addurl man page.
Sponsored-by: Jochen Bartl on Patreon
2021-11-12 16:59:30 +00:00
|
|
|
data MigrateOptions = MigrateOptions
|
|
|
|
{ migrateThese :: CmdParams
|
2023-12-07 19:50:52 +00:00
|
|
|
, updateOption :: Bool
|
2023-12-08 17:23:03 +00:00
|
|
|
, applyOption :: Bool
|
migrate: New --remove-size option
While intended for converting URL keys added by addurl --fast to be
as if added by addurl --relaxed, it can also be used to remove size
from other types of keys. Although that is not likely to be useful
for checksummed keys, I suppose it could be used for WORM or other
non-checksum keys.
Specifying the --remove-size option does not prevent other migrations
from taking effect if there's a key upgrade to perform, or if the
backend has changed. So --backend=URL needs to be used to prevent
migrating an URL key to the default backend.
Note that it's not possible to use git-annex migrate to convert from a
non-URL key to an URL key, as URL keys cannot be generated, except by
addurl. So while this can get the same effect as --relaxed would have
when addurl --fast was used, when --fast was not used, it won't work, or
if --backend=URL is not used will remove the size but not prevent
checksum verification, which is not useful. Due to this complexity, I
decided not to mention it in the git-annex addurl man page.
Sponsored-by: Jochen Bartl on Patreon
2021-11-12 16:59:30 +00:00
|
|
|
, removeSize :: Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
optParser :: CmdParamsDesc -> Parser MigrateOptions
|
|
|
|
optParser desc = MigrateOptions
|
|
|
|
<$> cmdParams desc
|
2023-12-07 19:50:52 +00:00
|
|
|
<*> switch
|
|
|
|
( long "update"
|
2023-12-08 17:23:03 +00:00
|
|
|
<> help "incrementally apply migrations performed elsewhere"
|
|
|
|
)
|
|
|
|
<*> switch
|
|
|
|
( long "apply"
|
|
|
|
<> help "(re)apply migrations performed elsewhere"
|
2023-12-07 19:50:52 +00:00
|
|
|
)
|
migrate: New --remove-size option
While intended for converting URL keys added by addurl --fast to be
as if added by addurl --relaxed, it can also be used to remove size
from other types of keys. Although that is not likely to be useful
for checksummed keys, I suppose it could be used for WORM or other
non-checksum keys.
Specifying the --remove-size option does not prevent other migrations
from taking effect if there's a key upgrade to perform, or if the
backend has changed. So --backend=URL needs to be used to prevent
migrating an URL key to the default backend.
Note that it's not possible to use git-annex migrate to convert from a
non-URL key to an URL key, as URL keys cannot be generated, except by
addurl. So while this can get the same effect as --relaxed would have
when addurl --fast was used, when --fast was not used, it won't work, or
if --backend=URL is not used will remove the size but not prevent
checksum verification, which is not useful. Due to this complexity, I
decided not to mention it in the git-annex addurl man page.
Sponsored-by: Jochen Bartl on Patreon
2021-11-12 16:59:30 +00:00
|
|
|
<*> switch
|
|
|
|
( long "remove-size"
|
|
|
|
<> help "remove size field from keys"
|
|
|
|
)
|
|
|
|
|
|
|
|
seek :: MigrateOptions -> CommandSeek
|
2023-12-07 19:50:52 +00:00
|
|
|
seek o
|
2023-12-08 17:23:03 +00:00
|
|
|
| updateOption o || applyOption o = do
|
2023-12-07 19:50:52 +00:00
|
|
|
unless (null (migrateThese o)) $
|
2023-12-08 17:23:03 +00:00
|
|
|
error "Cannot combine --update or --apply with files to migrate."
|
2023-12-08 18:18:18 +00:00
|
|
|
seekDistributedMigrations (not (applyOption o))
|
2023-12-07 19:50:52 +00:00
|
|
|
| otherwise = do
|
|
|
|
withFilesInGitAnnex ww seeker =<< workTreeItems ww (migrateThese o)
|
|
|
|
commitMigration
|
2020-05-28 19:55:17 +00:00
|
|
|
where
|
2023-04-25 23:26:20 +00:00
|
|
|
ww = WarnUnmatchLsFiles "migrate"
|
2020-07-13 21:04:02 +00:00
|
|
|
seeker = AnnexedFileSeeker
|
2023-12-06 19:38:01 +00:00
|
|
|
{ startAction = start o
|
2020-07-13 21:04:02 +00:00
|
|
|
, checkContentPresent = Nothing
|
|
|
|
, usesLocationLog = False
|
|
|
|
}
|
2011-01-08 19:54:14 +00:00
|
|
|
|
2023-12-08 18:18:18 +00:00
|
|
|
seekDistributedMigrations :: Bool -> CommandSeek
|
|
|
|
seekDistributedMigrations incremental =
|
|
|
|
streamNewDistributedMigrations incremental $ \oldkey newkey ->
|
|
|
|
-- Not using commandAction because this is not necessarily
|
|
|
|
-- concurrency safe, and also is unlikely to be sped up
|
|
|
|
-- by multiple jobs.
|
|
|
|
void $ includeCommandAction $ update oldkey newkey
|
|
|
|
|
2023-12-06 19:38:01 +00:00
|
|
|
start :: MigrateOptions -> Maybe KeySha -> SeekInput -> RawFilePath -> Key -> CommandStart
|
|
|
|
start o ksha si file key = do
|
2022-06-28 19:28:14 +00:00
|
|
|
forced <- Annex.getRead 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
|
2023-03-27 19:10:46 +00:00
|
|
|
newbackend <- chooseBackend file
|
2023-12-08 20:22:14 +00:00
|
|
|
if (newbackend /= oldbackend || upgradableKey oldbackend || forced) && exists
|
migrate: New --remove-size option
While intended for converting URL keys added by addurl --fast to be
as if added by addurl --relaxed, it can also be used to remove size
from other types of keys. Although that is not likely to be useful
for checksummed keys, I suppose it could be used for WORM or other
non-checksum keys.
Specifying the --remove-size option does not prevent other migrations
from taking effect if there's a key upgrade to perform, or if the
backend has changed. So --backend=URL needs to be used to prevent
migrating an URL key to the default backend.
Note that it's not possible to use git-annex migrate to convert from a
non-URL key to an URL key, as URL keys cannot be generated, except by
addurl. So while this can get the same effect as --relaxed would have
when addurl --fast was used, when --fast was not used, it won't work, or
if --backend=URL is not used will remove the size but not prevent
checksum verification, which is not useful. Due to this complexity, I
decided not to mention it in the git-annex addurl man page.
Sponsored-by: Jochen Bartl on Patreon
2021-11-12 16:59:30 +00:00
|
|
|
then go False oldbackend newbackend
|
2023-12-08 20:22:14 +00:00
|
|
|
else if cantweaksize newbackend oldbackend && exists
|
|
|
|
then go True oldbackend newbackend
|
migrate: New --remove-size option
While intended for converting URL keys added by addurl --fast to be
as if added by addurl --relaxed, it can also be used to remove size
from other types of keys. Although that is not likely to be useful
for checksummed keys, I suppose it could be used for WORM or other
non-checksum keys.
Specifying the --remove-size option does not prevent other migrations
from taking effect if there's a key upgrade to perform, or if the
backend has changed. So --backend=URL needs to be used to prevent
migrating an URL key to the default backend.
Note that it's not possible to use git-annex migrate to convert from a
non-URL key to an URL key, as URL keys cannot be generated, except by
addurl. So while this can get the same effect as --relaxed would have
when addurl --fast was used, when --fast was not used, it won't work, or
if --backend=URL is not used will remove the size but not prevent
checksum verification, which is not useful. Due to this complexity, I
decided not to mention it in the git-annex addurl man page.
Sponsored-by: Jochen Bartl on Patreon
2021-11-12 16:59:30 +00:00
|
|
|
else stop
|
|
|
|
where
|
2023-12-08 20:22:14 +00:00
|
|
|
go onlytweaksize oldbackend newbackend = do
|
2023-12-06 19:38:01 +00:00
|
|
|
keyrec <- case ksha of
|
|
|
|
Just (KeySha s) -> pure (MigrationRecord s)
|
|
|
|
Nothing -> error "internal"
|
migrate: New --remove-size option
While intended for converting URL keys added by addurl --fast to be
as if added by addurl --relaxed, it can also be used to remove size
from other types of keys. Although that is not likely to be useful
for checksummed keys, I suppose it could be used for WORM or other
non-checksum keys.
Specifying the --remove-size option does not prevent other migrations
from taking effect if there's a key upgrade to perform, or if the
backend has changed. So --backend=URL needs to be used to prevent
migrating an URL key to the default backend.
Note that it's not possible to use git-annex migrate to convert from a
non-URL key to an URL key, as URL keys cannot be generated, except by
addurl. So while this can get the same effect as --relaxed would have
when addurl --fast was used, when --fast was not used, it won't work, or
if --backend=URL is not used will remove the size but not prevent
checksum verification, which is not useful. Due to this complexity, I
decided not to mention it in the git-annex addurl man page.
Sponsored-by: Jochen Bartl on Patreon
2021-11-12 16:59:30 +00:00
|
|
|
starting "migrate" (mkActionItem (key, file)) si $
|
2023-12-08 20:22:14 +00:00
|
|
|
perform onlytweaksize o file key keyrec oldbackend newbackend
|
2011-01-08 19:54:14 +00:00
|
|
|
|
2023-12-08 20:22:14 +00:00
|
|
|
cantweaksize newbackend oldbackend
|
|
|
|
| removeSize o = isJust (fromKey keySize key)
|
|
|
|
| newbackend /= oldbackend = False
|
|
|
|
| isNothing (fromKey keySize key) = True
|
|
|
|
| otherwise = False
|
|
|
|
|
|
|
|
upgradableKey oldbackend = maybe False (\a -> a key) (canUpgradeKey oldbackend)
|
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.
|
|
|
|
-}
|
2023-12-06 19:38:01 +00:00
|
|
|
perform :: Bool -> MigrateOptions -> RawFilePath -> Key -> MigrationRecord -> Backend -> Backend -> CommandPerform
|
2023-12-08 20:22:14 +00:00
|
|
|
perform onlytweaksize o file oldkey oldkeyrec 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))
|
2023-12-08 20:22:14 +00:00
|
|
|
| knowngoodcontent = finish =<< tweaksize newkey
|
migrate: New --remove-size option
While intended for converting URL keys added by addurl --fast to be
as if added by addurl --relaxed, it can also be used to remove size
from other types of keys. Although that is not likely to be useful
for checksummed keys, I suppose it could be used for WORM or other
non-checksum keys.
Specifying the --remove-size option does not prevent other migrations
from taking effect if there's a key upgrade to perform, or if the
backend has changed. So --backend=URL needs to be used to prevent
migrating an URL key to the default backend.
Note that it's not possible to use git-annex migrate to convert from a
non-URL key to an URL key, as URL keys cannot be generated, except by
addurl. So while this can get the same effect as --relaxed would have
when addurl --fast was used, when --fast was not used, it won't work, or
if --backend=URL is not used will remove the size but not prevent
checksum verification, which is not useful. Due to this complexity, I
decided not to mention it in the git-annex addurl man page.
Sponsored-by: Jochen Bartl on Patreon
2021-11-12 16:59:30 +00:00
|
|
|
| otherwise = stopUnless checkcontent $
|
2023-12-08 20:22:14 +00:00
|
|
|
finish =<< tweaksize newkey
|
2024-03-09 17:50:30 +00:00
|
|
|
checkcontent = Command.Fsck.checkBackend oldkey 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
|
2023-12-06 19:38:01 +00:00
|
|
|
next $ Command.ReKey.cleanup file newkey $
|
|
|
|
logMigration oldkeyrec
|
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
|
|
|
)
|
2023-12-08 20:22:14 +00:00
|
|
|
genkey _ | onlytweaksize = return $ Just (oldkey, False)
|
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
|
|
|
|
}
|
2023-03-27 19:10:46 +00:00
|
|
|
newkey <- fst <$> genKey source nullMeterUpdate newbackend
|
2020-05-15 16:51:09 +00:00
|
|
|
return $ Just (newkey, False)
|
2024-03-01 20:42:02 +00:00
|
|
|
genkey (Just fm) = fm oldkey newbackend afile True >>= \case
|
2018-10-29 20:26:43 +00:00
|
|
|
Just newkey -> return (Just (newkey, True))
|
|
|
|
Nothing -> genkey Nothing
|
2023-12-08 20:22:14 +00:00
|
|
|
tweaksize k
|
|
|
|
| removeSize o = pure (removesize k)
|
|
|
|
| onlytweaksize = addsize k
|
|
|
|
| otherwise = pure k
|
|
|
|
removesize k = alterKey k $ \kd -> kd { keySize = Nothing }
|
|
|
|
addsize k
|
|
|
|
| fromKey keySize k == Nothing =
|
|
|
|
contentSize k >>= return . \case
|
|
|
|
Just sz -> alterKey k $ \kd -> kd { keySize = Just sz }
|
|
|
|
Nothing -> k
|
|
|
|
| otherwise = return k
|
2017-03-10 17:12:24 +00:00
|
|
|
afile = AssociatedFile (Just file)
|
2023-12-07 19:50:52 +00:00
|
|
|
|
2023-12-07 22:00:09 +00:00
|
|
|
update :: Key -> Key -> CommandStart
|
|
|
|
update oldkey newkey =
|
2023-12-08 17:23:03 +00:00
|
|
|
stopUnless (allowed <&&> available <&&> wanted) $ do
|
2023-12-07 22:00:09 +00:00
|
|
|
ai <- findworktreefile >>= return . \case
|
|
|
|
Just f -> ActionItemAssociatedFile (AssociatedFile (Just f)) newkey
|
|
|
|
Nothing -> ActionItemKey newkey
|
|
|
|
starting "migrate" ai (SeekInput []) $
|
2023-12-08 00:05:42 +00:00
|
|
|
ifM (Command.ReKey.linkKey' v oldkey newkey)
|
2023-12-07 22:00:09 +00:00
|
|
|
( do
|
|
|
|
logStatus newkey InfoPresent
|
|
|
|
next $ return True
|
|
|
|
, next $ return False
|
|
|
|
)
|
|
|
|
where
|
2023-12-08 17:23:03 +00:00
|
|
|
available = (not <$> inAnnex newkey) <&&> inAnnex oldkey
|
|
|
|
|
2023-12-08 16:39:18 +00:00
|
|
|
-- annex.securehashesonly will block adding keys with insecure
|
|
|
|
-- hashes, this check is only to avoid doing extra work and
|
|
|
|
-- displaying a message when it fails.
|
|
|
|
allowed = isNothing <$> checkSecureHashes newkey
|
|
|
|
|
2023-12-08 17:23:03 +00:00
|
|
|
-- If the new key was previous present in this repository, but got
|
|
|
|
-- dropped, assume the user still doesn't want it there.
|
|
|
|
wanted = loggedPreviousLocations newkey >>= \case
|
|
|
|
[] -> pure True
|
|
|
|
us -> do
|
|
|
|
u <- getUUID
|
|
|
|
pure (u `notElem` us)
|
|
|
|
|
2023-12-07 22:00:09 +00:00
|
|
|
findworktreefile = do
|
|
|
|
fs <- Database.Keys.getAssociatedFiles newkey
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
firstM (\f -> (== Just newkey) <$> isAnnexLink f) $
|
|
|
|
map (\f -> simplifyPath (fromTopFilePath f g)) fs
|
2023-12-08 00:05:42 +00:00
|
|
|
|
|
|
|
-- Always verify the content agains the newkey, even if
|
|
|
|
-- annex.verify is unset. This is done to prent bad migration
|
|
|
|
-- information maliciously injected into the git-annex branch
|
|
|
|
-- from populating files with the wrong content.
|
|
|
|
v = AlwaysVerify
|