2011-03-16 05:23:20 +00:00
|
|
|
{- git-annex v1 -> v2 upgrade support
|
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Upgrade.V1 where
|
|
|
|
|
|
|
|
import System.Posix.Types
|
2011-03-28 13:27:28 +00:00
|
|
|
import Data.Char
|
2011-03-16 05:23:20 +00:00
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2011-06-02 01:56:04 +00:00
|
|
|
import Types.Key
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2011-10-15 20:21:08 +00:00
|
|
|
import Logs.Presence
|
2011-10-04 04:40:47 +00:00
|
|
|
import qualified Annex.Queue
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
|
|
|
import qualified Git.LsFiles as LsFiles
|
2011-03-16 05:23:20 +00:00
|
|
|
import Backend
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Version
|
2011-09-23 22:13:24 +00:00
|
|
|
import Utility.FileMode
|
2011-10-16 04:31:25 +00:00
|
|
|
import Utility.TempFile
|
2011-06-23 06:30:20 +00:00
|
|
|
import qualified Upgrade.V2
|
2011-03-16 15:53:46 +00:00
|
|
|
|
2011-03-16 06:35:48 +00:00
|
|
|
-- v2 adds hashing of filenames of content and location log files.
|
|
|
|
-- Key information is encoded in filenames differently, so
|
|
|
|
-- both content and location log files move around, and symlinks
|
|
|
|
-- to content need to be changed.
|
|
|
|
--
|
|
|
|
-- When upgrading a v1 key to v2, file size metadata ought to be
|
|
|
|
-- added to the key (unless it is a WORM key, which encoded
|
|
|
|
-- mtime:size in v1). This can only be done when the file content
|
|
|
|
-- is present. Since upgrades need to happen consistently,
|
|
|
|
-- (so that two repos get changed the same way by the upgrade, and
|
|
|
|
-- will merge), that metadata cannot be added on upgrade.
|
|
|
|
--
|
|
|
|
-- Note that file size metadata
|
|
|
|
-- will only be used for detecting situations where git-annex
|
|
|
|
-- would run out of disk space, so if some keys don't have it,
|
|
|
|
-- the impact is minor. At least initially. It could be used in the
|
|
|
|
-- future by smart auto-repo balancing code, etc.
|
|
|
|
--
|
|
|
|
-- Anyway, since v2 plans ahead for other metadata being included
|
|
|
|
-- in keys, there should probably be a way to update a key.
|
|
|
|
-- Something similar to the migrate subcommand could be used,
|
|
|
|
-- and users could then run that at their leisure.
|
2011-03-16 05:23:20 +00:00
|
|
|
|
|
|
|
upgrade :: Annex Bool
|
|
|
|
upgrade = do
|
2011-07-19 18:07:23 +00:00
|
|
|
showAction "v1 to v2"
|
2011-11-08 19:34:10 +00:00
|
|
|
|
2012-03-16 05:59:07 +00:00
|
|
|
ifM (fromRepo Git.repoIsLocalBare)
|
|
|
|
( do
|
2011-03-16 17:16:52 +00:00
|
|
|
moveContent
|
2011-03-16 20:07:33 +00:00
|
|
|
setVersion
|
2012-03-16 05:59:07 +00:00
|
|
|
, do
|
2011-03-16 17:16:52 +00:00
|
|
|
moveContent
|
|
|
|
updateSymlinks
|
|
|
|
moveLocationLogs
|
|
|
|
|
2012-04-27 17:23:52 +00:00
|
|
|
Annex.Queue.flush
|
2011-03-16 20:07:33 +00:00
|
|
|
setVersion
|
2012-03-16 05:59:07 +00:00
|
|
|
)
|
2011-06-23 06:30:20 +00:00
|
|
|
|
|
|
|
Upgrade.V2.upgrade
|
2011-03-16 05:23:20 +00:00
|
|
|
|
2011-03-16 06:35:48 +00:00
|
|
|
moveContent :: Annex ()
|
|
|
|
moveContent = do
|
2011-07-19 18:07:23 +00:00
|
|
|
showAction "moving content"
|
2011-03-28 13:27:28 +00:00
|
|
|
files <- getKeyFilesPresent1
|
|
|
|
forM_ files move
|
2011-03-16 06:35:48 +00:00
|
|
|
where
|
2011-03-28 13:27:28 +00:00
|
|
|
move f = do
|
|
|
|
let k = fileKey1 (takeFileName f)
|
2011-03-16 06:35:48 +00:00
|
|
|
let d = parentDir f
|
|
|
|
liftIO $ allowWrite d
|
|
|
|
liftIO $ allowWrite f
|
|
|
|
moveAnnex k f
|
|
|
|
liftIO $ removeDirectory d
|
2011-03-16 05:23:20 +00:00
|
|
|
|
2011-03-16 06:35:48 +00:00
|
|
|
updateSymlinks :: Annex ()
|
|
|
|
updateSymlinks = do
|
2011-07-19 18:07:23 +00:00
|
|
|
showAction "updating symlinks"
|
2011-11-08 19:34:10 +00:00
|
|
|
top <- fromRepo Git.workTree
|
|
|
|
files <- inRepo $ LsFiles.inRepo [top]
|
2011-07-15 07:12:05 +00:00
|
|
|
forM_ files fixlink
|
2011-03-16 06:35:48 +00:00
|
|
|
where
|
2011-03-16 19:10:15 +00:00
|
|
|
fixlink f = do
|
2011-03-16 06:35:48 +00:00
|
|
|
r <- lookupFile1 f
|
|
|
|
case r of
|
2012-04-22 03:32:33 +00:00
|
|
|
Nothing -> noop
|
2011-03-16 06:35:48 +00:00
|
|
|
Just (k, _) -> do
|
|
|
|
link <- calcGitLink f k
|
2011-03-16 19:10:15 +00:00
|
|
|
liftIO $ removeFile f
|
|
|
|
liftIO $ createSymbolicLink link f
|
2011-10-04 04:40:47 +00:00
|
|
|
Annex.Queue.add "add" [Param "--"] [f]
|
2011-03-16 05:23:20 +00:00
|
|
|
|
2011-03-16 06:35:48 +00:00
|
|
|
moveLocationLogs :: Annex ()
|
|
|
|
moveLocationLogs = do
|
2011-07-19 18:07:23 +00:00
|
|
|
showAction "moving location logs"
|
2011-03-16 14:56:59 +00:00
|
|
|
logkeys <- oldlocationlogs
|
|
|
|
forM_ logkeys move
|
|
|
|
where
|
|
|
|
oldlocationlogs = do
|
2011-11-08 19:34:10 +00:00
|
|
|
dir <- fromRepo Upgrade.V2.gitStateDir
|
2012-03-16 05:59:07 +00:00
|
|
|
ifM (liftIO $ doesDirectoryExist dir)
|
|
|
|
( mapMaybe oldlog2key
|
|
|
|
<$> (liftIO $ getDirectoryContents dir)
|
|
|
|
, return []
|
|
|
|
)
|
2011-03-16 14:56:59 +00:00
|
|
|
move (l, k) = do
|
2011-11-08 19:34:10 +00:00
|
|
|
dest <- fromRepo $ logFile2 k
|
2011-11-11 05:52:58 +00:00
|
|
|
dir <- fromRepo Upgrade.V2.gitStateDir
|
2011-03-16 14:56:59 +00:00
|
|
|
let f = dir </> l
|
|
|
|
liftIO $ createDirectoryIfMissing True (parentDir dest)
|
|
|
|
-- could just git mv, but this way deals with
|
2011-03-16 15:53:46 +00:00
|
|
|
-- log files that are not checked into git,
|
|
|
|
-- as well as merging with already upgraded
|
|
|
|
-- logs that have been pulled from elsewhere
|
2011-08-20 00:05:08 +00:00
|
|
|
old <- liftIO $ readLog1 f
|
|
|
|
new <- liftIO $ readLog1 dest
|
|
|
|
liftIO $ writeLog1 dest (old++new)
|
2011-10-04 04:40:47 +00:00
|
|
|
Annex.Queue.add "add" [Param "--"] [dest]
|
|
|
|
Annex.Queue.add "add" [Param "--"] [f]
|
|
|
|
Annex.Queue.add "rm" [Param "--quiet", Param "-f", Param "--"] [f]
|
2011-03-16 14:56:59 +00:00
|
|
|
|
|
|
|
oldlog2key :: FilePath -> Maybe (FilePath, Key)
|
2012-03-16 05:59:07 +00:00
|
|
|
oldlog2key l
|
|
|
|
| drop len l == ".log" && sane = Just (l, k)
|
|
|
|
| otherwise = Nothing
|
|
|
|
where
|
|
|
|
len = length l - 4
|
|
|
|
k = readKey1 (take len l)
|
|
|
|
sane = (not . null $ keyName k) && (not . null $ keyBackendName k)
|
2011-03-16 06:35:48 +00:00
|
|
|
|
|
|
|
-- WORM backend keys: "WORM:mtime:size:filename"
|
|
|
|
-- all the rest: "backend:key"
|
2011-03-28 13:27:28 +00:00
|
|
|
--
|
|
|
|
-- If the file looks like "WORM:XXX-...", then it was created by mixing
|
|
|
|
-- v2 and v1; that infelicity is worked around by treating the value
|
|
|
|
-- as the v2 key that it is.
|
2011-03-16 05:23:20 +00:00
|
|
|
readKey1 :: String -> Key
|
2012-03-16 05:59:07 +00:00
|
|
|
readKey1 v
|
|
|
|
| mixup = fromJust $ readKey $ join ":" $ Prelude.tail bits
|
|
|
|
| otherwise = Key
|
|
|
|
{ keyName = n
|
|
|
|
, keyBackendName = b
|
|
|
|
, keySize = s
|
|
|
|
, keyMtime = t
|
|
|
|
}
|
2011-03-16 05:23:20 +00:00
|
|
|
where
|
|
|
|
bits = split ":" v
|
2011-12-15 22:11:42 +00:00
|
|
|
b = Prelude.head bits
|
2011-03-16 05:23:20 +00:00
|
|
|
n = join ":" $ drop (if wormy then 3 else 1) bits
|
|
|
|
t = if wormy
|
2011-12-15 22:11:42 +00:00
|
|
|
then Just (Prelude.read (bits !! 1) :: EpochTime)
|
2011-03-16 05:23:20 +00:00
|
|
|
else Nothing
|
|
|
|
s = if wormy
|
2011-12-15 22:11:42 +00:00
|
|
|
then Just (Prelude.read (bits !! 2) :: Integer)
|
2011-03-16 05:23:20 +00:00
|
|
|
else Nothing
|
2011-12-15 22:11:42 +00:00
|
|
|
wormy = Prelude.head bits == "WORM"
|
|
|
|
mixup = wormy && isUpper (Prelude.head $ bits !! 1)
|
2011-03-16 05:23:20 +00:00
|
|
|
|
2011-03-16 06:35:48 +00:00
|
|
|
showKey1 :: Key -> String
|
|
|
|
showKey1 Key { keyName = n , keyBackendName = b, keySize = s, keyMtime = t } =
|
|
|
|
join ":" $ filter (not . null) [b, showifhere t, showifhere s, n]
|
|
|
|
where
|
|
|
|
showifhere Nothing = ""
|
|
|
|
showifhere (Just v) = show v
|
|
|
|
|
|
|
|
keyFile1 :: Key -> FilePath
|
|
|
|
keyFile1 key = replace "/" "%" $ replace "%" "&s" $ replace "&" "&a" $ showKey1 key
|
|
|
|
|
|
|
|
fileKey1 :: FilePath -> Key
|
|
|
|
fileKey1 file = readKey1 $
|
|
|
|
replace "&a" "&" $ replace "&s" "%" $ replace "%" "/" file
|
|
|
|
|
2011-08-20 00:05:08 +00:00
|
|
|
writeLog1 :: FilePath -> [LogLine] -> IO ()
|
2011-11-08 03:21:22 +00:00
|
|
|
writeLog1 file ls = viaTmp writeFile file (showLog ls)
|
2011-08-20 00:05:08 +00:00
|
|
|
|
|
|
|
readLog1 :: FilePath -> IO [LogLine]
|
2011-11-11 00:24:24 +00:00
|
|
|
readLog1 file = catchDefaultIO (parseLog <$> readFileStrict file) []
|
2011-03-16 06:35:48 +00:00
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
lookupFile1 :: FilePath -> Annex (Maybe (Key, Backend))
|
2011-03-16 05:23:20 +00:00
|
|
|
lookupFile1 file = do
|
2012-02-03 20:47:24 +00:00
|
|
|
tl <- liftIO $ tryIO getsymlink
|
2011-03-16 05:23:20 +00:00
|
|
|
case tl of
|
|
|
|
Left _ -> return Nothing
|
2011-07-05 22:31:46 +00:00
|
|
|
Right l -> makekey l
|
2011-03-16 05:23:20 +00:00
|
|
|
where
|
2011-08-25 04:28:55 +00:00
|
|
|
getsymlink = takeFileName <$> readSymbolicLink file
|
2011-07-15 07:12:05 +00:00
|
|
|
makekey l = case maybeLookupBackendName bname of
|
|
|
|
Nothing -> do
|
|
|
|
unless (null kname || null bname ||
|
|
|
|
not (isLinkToAnnex l)) $
|
|
|
|
warning skip
|
|
|
|
return Nothing
|
|
|
|
Just backend -> return $ Just (k, backend)
|
2011-03-16 05:23:20 +00:00
|
|
|
where
|
|
|
|
k = fileKey1 l
|
|
|
|
bname = keyBackendName k
|
|
|
|
kname = keyName k
|
|
|
|
skip = "skipping " ++ file ++
|
|
|
|
" (unknown backend " ++ bname ++ ")"
|
|
|
|
|
2011-03-28 13:27:28 +00:00
|
|
|
getKeyFilesPresent1 :: Annex [FilePath]
|
2011-11-08 19:34:10 +00:00
|
|
|
getKeyFilesPresent1 = getKeyFilesPresent1' =<< fromRepo gitAnnexObjectDir
|
2011-03-28 13:27:28 +00:00
|
|
|
getKeyFilesPresent1' :: FilePath -> Annex [FilePath]
|
2012-03-16 05:59:07 +00:00
|
|
|
getKeyFilesPresent1' dir =
|
|
|
|
ifM (liftIO $ doesDirectoryExist dir)
|
|
|
|
( do
|
2011-03-28 13:27:28 +00:00
|
|
|
dirs <- liftIO $ getDirectoryContents dir
|
|
|
|
let files = map (\d -> dir ++ "/" ++ d ++ "/" ++ takeFileName d) dirs
|
|
|
|
liftIO $ filterM present files
|
2012-03-16 05:59:07 +00:00
|
|
|
, return []
|
|
|
|
)
|
2011-03-16 05:23:20 +00:00
|
|
|
where
|
2011-03-28 13:27:28 +00:00
|
|
|
present f = do
|
2012-02-03 20:47:24 +00:00
|
|
|
result <- tryIO $ getFileStatus f
|
2011-03-16 05:23:20 +00:00
|
|
|
case result of
|
|
|
|
Right s -> return $ isRegularFile s
|
|
|
|
Left _ -> return False
|
2011-08-20 00:05:08 +00:00
|
|
|
|
|
|
|
logFile1 :: Git.Repo -> Key -> String
|
|
|
|
logFile1 repo key = Upgrade.V2.gitStateDir repo ++ keyFile1 key ++ ".log"
|
|
|
|
|
2011-11-08 19:34:10 +00:00
|
|
|
logFile2 :: Key -> Git.Repo -> String
|
2011-08-20 00:05:08 +00:00
|
|
|
logFile2 = logFile' hashDirLower
|
|
|
|
|
2011-11-08 19:34:10 +00:00
|
|
|
logFile' :: (Key -> FilePath) -> Key -> Git.Repo -> String
|
|
|
|
logFile' hasher key repo =
|
2011-08-20 00:05:08 +00:00
|
|
|
gitStateDir repo ++ hasher key ++ keyFile key ++ ".log"
|
|
|
|
|
|
|
|
stateDir :: FilePath
|
2011-09-21 03:24:48 +00:00
|
|
|
stateDir = addTrailingPathSeparator ".git-annex"
|
2011-08-20 00:05:08 +00:00
|
|
|
|
|
|
|
gitStateDir :: Git.Repo -> FilePath
|
|
|
|
gitStateDir repo = addTrailingPathSeparator $ Git.workTree repo </> stateDir
|