2017-08-31 19:41:48 +00:00
|
|
|
{- git-annex export log
|
|
|
|
-
|
2019-01-30 16:36:30 +00:00
|
|
|
- Copyright 2017-2019 Joey Hess <id@joeyh.name>
|
2017-08-31 19:41:48 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2017-08-31 19:41:48 +00:00
|
|
|
-}
|
|
|
|
|
2019-12-09 17:49:05 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2019-01-30 16:36:30 +00:00
|
|
|
module Logs.Export (
|
|
|
|
Exported,
|
|
|
|
mkExported,
|
|
|
|
ExportParticipants,
|
|
|
|
ExportChange(..),
|
|
|
|
getExport,
|
|
|
|
exportedTreeishes,
|
|
|
|
incompleteExportedTreeishes,
|
|
|
|
recordExport,
|
|
|
|
recordExportBeginning,
|
2019-05-20 20:37:04 +00:00
|
|
|
logExportExcluded,
|
|
|
|
getExportExcluded,
|
2019-01-30 16:36:30 +00:00
|
|
|
) where
|
2017-08-31 19:41:48 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import qualified Annex.Branch
|
|
|
|
import qualified Git
|
2017-09-06 17:39:33 +00:00
|
|
|
import Git.Sha
|
2017-08-31 22:06:49 +00:00
|
|
|
import Git.FilePath
|
2017-08-31 19:41:48 +00:00
|
|
|
import Logs
|
2017-09-12 21:45:52 +00:00
|
|
|
import Logs.MapLog
|
2019-05-20 20:37:04 +00:00
|
|
|
import Logs.File
|
|
|
|
import qualified Git.LsTree
|
|
|
|
import qualified Git.Tree
|
2017-08-31 19:41:48 +00:00
|
|
|
import Annex.UUID
|
|
|
|
|
2019-01-10 17:23:42 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
import qualified Data.Attoparsec.ByteString.Lazy as A
|
|
|
|
import qualified Data.Attoparsec.ByteString.Char8 as A8
|
2019-01-09 17:06:37 +00:00
|
|
|
import Data.ByteString.Builder
|
2019-11-26 19:27:22 +00:00
|
|
|
import Data.Either
|
|
|
|
import Data.Char
|
2019-01-09 17:06:37 +00:00
|
|
|
|
2019-01-30 16:36:30 +00:00
|
|
|
-- This constuctor is not itself exported to other modules, to enforce
|
|
|
|
-- consistent use of exportedTreeishes.
|
2017-09-06 17:39:33 +00:00
|
|
|
data Exported = Exported
|
|
|
|
{ exportedTreeish :: Git.Ref
|
|
|
|
, incompleteExportedTreeish :: [Git.Ref]
|
|
|
|
}
|
2017-09-12 20:59:04 +00:00
|
|
|
deriving (Eq, Show)
|
2017-09-06 17:39:33 +00:00
|
|
|
|
2019-01-30 16:36:30 +00:00
|
|
|
mkExported :: Git.Ref -> [Git.Ref] -> Exported
|
|
|
|
mkExported = Exported
|
|
|
|
|
|
|
|
-- | Get the list of exported treeishes.
|
|
|
|
--
|
|
|
|
-- If the list contains multiple items, there was an export conflict,
|
|
|
|
-- and different trees were exported to the same special remote.
|
|
|
|
exportedTreeishes :: [Exported] -> [Git.Ref]
|
|
|
|
exportedTreeishes = nub . map exportedTreeish
|
|
|
|
|
|
|
|
-- | Treeishes that started to be exported, but were not finished.
|
|
|
|
incompleteExportedTreeishes :: [Exported] -> [Git.Ref]
|
|
|
|
incompleteExportedTreeishes = concatMap incompleteExportedTreeish
|
|
|
|
|
|
|
|
|
2017-09-12 21:45:52 +00:00
|
|
|
data ExportParticipants = ExportParticipants
|
|
|
|
{ exportFrom :: UUID
|
|
|
|
, exportTo :: UUID
|
|
|
|
}
|
2019-01-10 17:23:42 +00:00
|
|
|
deriving (Eq, Ord, Show)
|
2017-09-12 21:45:52 +00:00
|
|
|
|
|
|
|
data ExportChange = ExportChange
|
|
|
|
{ oldTreeish :: [Git.Ref]
|
|
|
|
, newTreeish :: Git.Ref
|
|
|
|
}
|
|
|
|
|
2017-09-06 17:39:33 +00:00
|
|
|
-- | Get what's been exported to a special remote.
|
|
|
|
getExport :: UUID -> Annex [Exported]
|
2017-09-12 21:45:52 +00:00
|
|
|
getExport remoteuuid = nub . mapMaybe get . M.toList . simpleMap
|
|
|
|
. parseExportLog
|
2017-08-31 19:41:48 +00:00
|
|
|
<$> Annex.Branch.get exportLog
|
|
|
|
where
|
2017-09-12 21:45:52 +00:00
|
|
|
get (ep, exported)
|
|
|
|
| exportTo ep == remoteuuid = Just exported
|
2017-08-31 19:41:48 +00:00
|
|
|
| otherwise = Nothing
|
|
|
|
|
|
|
|
-- | Record a change in what's exported to a special remote.
|
|
|
|
--
|
2017-09-06 17:39:33 +00:00
|
|
|
-- This is called before an export begins uploading new files to the
|
|
|
|
-- remote, but after it's cleaned up any files that need to be deleted
|
|
|
|
-- from the old treeish.
|
|
|
|
--
|
2017-08-31 19:41:48 +00:00
|
|
|
-- Any entries in the log for the oldTreeish will be updated to the
|
|
|
|
-- newTreeish. This way, when multiple repositories are exporting to
|
|
|
|
-- the same special remote, there's no conflict as long as they move
|
|
|
|
-- forward in lock-step.
|
2017-08-31 22:06:49 +00:00
|
|
|
--
|
|
|
|
-- Also, the newTreeish is grafted into the git-annex branch. This is done
|
|
|
|
-- to ensure that it's available later.
|
2017-08-31 19:41:48 +00:00
|
|
|
recordExport :: UUID -> ExportChange -> Annex ()
|
|
|
|
recordExport remoteuuid ec = do
|
|
|
|
c <- liftIO currentVectorClock
|
|
|
|
u <- getUUID
|
2017-09-12 21:45:52 +00:00
|
|
|
let ep = ExportParticipants { exportFrom = u, exportTo = remoteuuid }
|
|
|
|
let exported = Exported (newTreeish ec) []
|
2017-08-31 19:41:48 +00:00
|
|
|
Annex.Branch.change exportLog $
|
2019-01-09 17:06:37 +00:00
|
|
|
buildExportLog
|
2017-09-12 21:45:52 +00:00
|
|
|
. changeMapLog c ep exported
|
2017-08-31 19:41:48 +00:00
|
|
|
. M.mapWithKey (updateothers c u)
|
2019-01-10 17:23:42 +00:00
|
|
|
. parseExportLog
|
2017-08-31 19:41:48 +00:00
|
|
|
where
|
2017-09-12 21:45:52 +00:00
|
|
|
updateothers c u ep le@(LogEntry _ exported@(Exported { exportedTreeish = t }))
|
|
|
|
| u == exportFrom ep || remoteuuid /= exportTo ep || t `notElem` oldTreeish ec = le
|
|
|
|
| otherwise = LogEntry c (exported { exportedTreeish = newTreeish ec })
|
2017-09-06 17:39:33 +00:00
|
|
|
|
|
|
|
-- | Record the beginning of an export, to allow cleaning up from
|
|
|
|
-- interrupted exports.
|
|
|
|
--
|
|
|
|
-- This is called before any changes are made to the remote.
|
|
|
|
recordExportBeginning :: UUID -> Git.Ref -> Annex ()
|
|
|
|
recordExportBeginning remoteuuid newtree = do
|
|
|
|
c <- liftIO currentVectorClock
|
|
|
|
u <- getUUID
|
2017-09-12 21:45:52 +00:00
|
|
|
let ep = ExportParticipants { exportFrom = u, exportTo = remoteuuid }
|
|
|
|
old <- fromMaybe (Exported emptyTree [])
|
|
|
|
. M.lookup ep . simpleMap
|
|
|
|
. parseExportLog
|
2017-09-06 17:39:33 +00:00
|
|
|
<$> Annex.Branch.get exportLog
|
2017-09-12 18:19:26 +00:00
|
|
|
let new = old { incompleteExportedTreeish = nub (newtree:incompleteExportedTreeish old) }
|
2017-09-06 17:39:33 +00:00
|
|
|
Annex.Branch.change exportLog $
|
2019-01-09 17:06:37 +00:00
|
|
|
buildExportLog
|
2017-09-12 21:45:52 +00:00
|
|
|
. changeMapLog c ep new
|
2019-01-10 17:23:42 +00:00
|
|
|
. parseExportLog
|
2019-02-22 15:16:22 +00:00
|
|
|
Annex.Branch.rememberTreeish newtree (asTopFilePath "export.tree")
|
2017-08-31 19:41:48 +00:00
|
|
|
|
2019-01-10 17:23:42 +00:00
|
|
|
parseExportLog :: L.ByteString -> MapLog ExportParticipants Exported
|
|
|
|
parseExportLog = parseMapLog exportParticipantsParser exportedParser
|
2017-09-12 21:45:52 +00:00
|
|
|
|
2019-01-09 17:06:37 +00:00
|
|
|
buildExportLog :: MapLog ExportParticipants Exported -> Builder
|
|
|
|
buildExportLog = buildMapLog buildExportParticipants buildExported
|
2017-09-12 21:45:52 +00:00
|
|
|
|
2019-01-09 17:06:37 +00:00
|
|
|
buildExportParticipants :: ExportParticipants -> Builder
|
2019-01-09 18:00:35 +00:00
|
|
|
buildExportParticipants ep =
|
|
|
|
buildUUID (exportFrom ep) <> sep <> buildUUID (exportTo ep)
|
2019-01-09 17:06:37 +00:00
|
|
|
where
|
|
|
|
sep = charUtf8 ':'
|
2017-08-31 19:41:48 +00:00
|
|
|
|
2019-01-10 17:23:42 +00:00
|
|
|
exportParticipantsParser :: A.Parser ExportParticipants
|
|
|
|
exportParticipantsParser = ExportParticipants
|
|
|
|
<$> (toUUID <$> A8.takeWhile1 (/= ':'))
|
|
|
|
<* A8.char ':'
|
|
|
|
<*> (toUUID <$> A8.takeWhile1 (const True))
|
2019-01-09 17:06:37 +00:00
|
|
|
|
|
|
|
buildExported :: Exported -> Builder
|
|
|
|
buildExported exported = go (exportedTreeish exported : incompleteExportedTreeish exported)
|
|
|
|
where
|
|
|
|
go [] = mempty
|
|
|
|
go (r:rs) = rref r <> mconcat [ charUtf8 ' ' <> rref r' | r' <- rs ]
|
2020-04-07 21:41:09 +00:00
|
|
|
rref = byteString . Git.fromRef'
|
2017-08-31 19:41:48 +00:00
|
|
|
|
2019-01-10 17:23:42 +00:00
|
|
|
exportedParser :: A.Parser Exported
|
|
|
|
exportedParser = Exported <$> refparser <*> many refparser
|
|
|
|
where
|
2020-04-07 21:41:09 +00:00
|
|
|
refparser = (Git.Ref <$> A8.takeWhile1 (/= ' ') )
|
2019-01-10 17:23:42 +00:00
|
|
|
<* ((const () <$> A8.char ' ') <|> A.endOfInput)
|
2019-05-20 20:37:04 +00:00
|
|
|
|
|
|
|
logExportExcluded :: UUID -> ((Git.Tree.TreeItem -> IO ()) -> Annex a) -> Annex a
|
|
|
|
logExportExcluded u a = do
|
|
|
|
logf <- fromRepo $ gitAnnexExportExcludeLog u
|
|
|
|
withLogHandle logf $ \logh -> do
|
|
|
|
liftIO $ hSetNewlineMode logh noNewlineTranslation
|
|
|
|
a (writer logh)
|
|
|
|
where
|
|
|
|
writer logh = hPutStrLn logh
|
|
|
|
. Git.LsTree.formatLsTree
|
|
|
|
. Git.Tree.treeItemToLsTreeItem
|
|
|
|
|
|
|
|
getExportExcluded :: UUID -> Annex [Git.Tree.TreeItem]
|
|
|
|
getExportExcluded u = do
|
|
|
|
logf <- fromRepo $ gitAnnexExportExcludeLog u
|
2019-11-26 19:27:22 +00:00
|
|
|
liftIO $ catchDefaultIO [] $ parser <$> L.readFile logf
|
2019-05-20 20:37:04 +00:00
|
|
|
where
|
2019-11-26 19:27:22 +00:00
|
|
|
parser = map Git.Tree.lsTreeItemToTreeItem
|
|
|
|
. rights
|
|
|
|
. map Git.LsTree.parseLsTree
|
|
|
|
. L.split (fromIntegral $ ord '\n')
|