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
|
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2011-01-08 19:54:14 +00:00
|
|
|
import Command
|
2012-06-05 23:51:03 +00:00
|
|
|
import Backend
|
2011-07-05 22:31:46 +00:00
|
|
|
import qualified Types.Key
|
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
|
2011-01-08 19:54:14 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-10 17:18:46 +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
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
seek = withFilesInGit $ whenAnnexed start
|
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
|
|
|
|
newbackend <- choosebackend =<< 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
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
choosebackend Nothing = Prelude.head <$> orderedList
|
|
|
|
choosebackend (Just backend) = return backend
|
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
|
|
|
|
upgradableKey backend key = isNothing (Types.Key.keySize key) || backendupgradable
|
|
|
|
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
|
2013-05-13 18:27:39 +00:00
|
|
|
perform file oldkey oldbackend newbackend = go =<< genkey
|
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
|
2013-05-13 18:27:39 +00:00
|
|
|
checkcontent = Command.Fsck.checkBackend oldbackend oldkey $ Just file
|
|
|
|
finish newkey = stopUnless (Command.ReKey.linkKey oldkey newkey) $
|
2012-11-12 05:05:04 +00:00
|
|
|
next $ Command.ReKey.cleanup file oldkey newkey
|
2015-01-04 16:33:10 +00:00
|
|
|
genkey = case maybe Nothing (\fm -> fm oldkey newbackend (Just file)) (fastMigrate oldbackend) of
|
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
|