2012-10-04 19:48:59 +00:00
|
|
|
{- git-annex preferred content matcher configuration
|
|
|
|
-
|
2024-09-05 19:22:41 +00:00
|
|
|
- Copyright 2012-2024 Joey Hess <id@joeyh.name>
|
2012-10-04 19:48:59 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-10-04 19:48:59 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Logs.PreferredContent (
|
|
|
|
preferredContentSet,
|
2014-03-29 19:20:55 +00:00
|
|
|
requiredContentSet,
|
2014-03-15 20:17:01 +00:00
|
|
|
groupPreferredContentSet,
|
2012-10-08 17:16:53 +00:00
|
|
|
isPreferredContent,
|
2014-03-29 19:20:55 +00:00
|
|
|
isRequiredContent,
|
2012-10-04 19:48:59 +00:00
|
|
|
preferredContentMap,
|
|
|
|
preferredContentMapRaw,
|
2014-03-29 19:20:55 +00:00
|
|
|
requiredContentMap,
|
|
|
|
requiredContentMapRaw,
|
2014-03-15 20:17:01 +00:00
|
|
|
groupPreferredContentMapRaw,
|
2012-10-04 19:48:59 +00:00
|
|
|
checkPreferredContentExpression,
|
2012-10-10 19:35:10 +00:00
|
|
|
setStandardGroup,
|
2014-05-30 18:03:04 +00:00
|
|
|
defaultStandardGroup,
|
2014-03-29 19:20:55 +00:00
|
|
|
preferredRequiredMapsLoad,
|
2019-05-21 18:38:00 +00:00
|
|
|
preferredRequiredMapsLoad',
|
2020-09-24 19:12:09 +00:00
|
|
|
introspectPreferredRequiredContent,
|
2015-06-17 17:44:19 +00:00
|
|
|
prop_standardGroups_parse,
|
2012-10-04 19:48:59 +00:00
|
|
|
) where
|
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2014-01-02 00:12:20 +00:00
|
|
|
import Logs.PreferredContent.Raw
|
2012-10-04 19:48:59 +00:00
|
|
|
import qualified Annex.Branch
|
|
|
|
import qualified Annex
|
2013-08-29 22:51:22 +00:00
|
|
|
import Logs
|
2012-10-04 19:48:59 +00:00
|
|
|
import Logs.UUIDBased
|
2019-05-14 17:08:51 +00:00
|
|
|
import Utility.Matcher
|
2013-03-29 20:17:13 +00:00
|
|
|
import Annex.FileMatcher
|
2012-10-08 17:16:53 +00:00
|
|
|
import Annex.UUID
|
2012-10-08 19:18:58 +00:00
|
|
|
import Logs.Group
|
2013-04-26 03:44:55 +00:00
|
|
|
import Logs.Remote
|
2012-10-10 20:04:28 +00:00
|
|
|
import Types.StandardGroups
|
2014-03-20 04:10:12 +00:00
|
|
|
import Limit
|
2012-10-04 19:48:59 +00:00
|
|
|
|
2024-09-05 19:22:41 +00:00
|
|
|
import qualified Data.Map as M
|
|
|
|
import qualified Data.Set as S
|
|
|
|
import qualified Data.Attoparsec.ByteString.Lazy as A
|
|
|
|
|
2021-05-25 14:57:06 +00:00
|
|
|
{- Checks if a file is preferred content (or required content) for the
|
|
|
|
- specified repository (or the current repository if none is specified). -}
|
2024-08-23 20:35:12 +00:00
|
|
|
isPreferredContent :: LiveUpdate -> Maybe UUID -> AssumeNotPresent -> Maybe Key -> AssociatedFile -> Bool -> Annex Bool
|
2014-03-29 19:20:55 +00:00
|
|
|
isPreferredContent = checkMap preferredContentMap
|
|
|
|
|
2024-08-23 20:35:12 +00:00
|
|
|
isRequiredContent :: LiveUpdate -> Maybe UUID -> AssumeNotPresent -> Maybe Key -> AssociatedFile -> Bool -> Annex Bool
|
2014-03-29 19:20:55 +00:00
|
|
|
isRequiredContent = checkMap requiredContentMap
|
|
|
|
|
2024-08-23 20:35:12 +00:00
|
|
|
checkMap :: Annex (FileMatcherMap Annex) -> LiveUpdate -> Maybe UUID -> AssumeNotPresent -> Maybe Key -> AssociatedFile -> Bool -> Annex Bool
|
|
|
|
checkMap getmap lu mu notpresent mkey afile d = do
|
2012-10-08 17:16:53 +00:00
|
|
|
u <- maybe getUUID return mu
|
2014-03-29 19:20:55 +00:00
|
|
|
m <- getmap
|
2012-10-08 17:16:53 +00:00
|
|
|
case M.lookup u m of
|
2015-10-06 21:28:20 +00:00
|
|
|
Nothing -> return d
|
2024-08-23 20:35:12 +00:00
|
|
|
Just matcher -> checkMatcher matcher mkey afile lu notpresent (return d) (return d)
|
2012-10-08 17:16:53 +00:00
|
|
|
|
2020-09-24 19:12:09 +00:00
|
|
|
{- Checks if the preferred or required content for the specified repository
|
|
|
|
- (or the current repository if none is specified) contains any terms
|
|
|
|
- that meet the condition. -}
|
|
|
|
introspectPreferredRequiredContent :: (MatchFiles Annex -> Bool) -> Maybe UUID -> Annex Bool
|
|
|
|
introspectPreferredRequiredContent c mu = do
|
|
|
|
u <- maybe getUUID return mu
|
|
|
|
check u preferredContentMap <||> check u requiredContentMap
|
|
|
|
where
|
2023-07-26 18:34:21 +00:00
|
|
|
check u mk = mk >>= return . maybe False (any c . fst) . M.lookup u
|
2020-09-24 19:12:09 +00:00
|
|
|
|
2014-03-29 18:43:34 +00:00
|
|
|
preferredContentMap :: Annex (FileMatcherMap Annex)
|
2019-05-14 18:01:09 +00:00
|
|
|
preferredContentMap = maybe (fst <$> preferredRequiredMapsLoad preferredContentTokens) return
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
=<< Annex.getState Annex.preferredcontentmap
|
|
|
|
|
2014-03-29 19:20:55 +00:00
|
|
|
requiredContentMap :: Annex (FileMatcherMap Annex)
|
2019-05-14 18:01:09 +00:00
|
|
|
requiredContentMap = maybe (snd <$> preferredRequiredMapsLoad preferredContentTokens) return
|
2014-03-29 19:20:55 +00:00
|
|
|
=<< Annex.getState Annex.requiredcontentmap
|
|
|
|
|
2019-05-14 18:01:09 +00:00
|
|
|
preferredRequiredMapsLoad :: (PreferredContentData -> [ParseToken (MatchFiles Annex)]) -> Annex (FileMatcherMap Annex, FileMatcherMap Annex)
|
|
|
|
preferredRequiredMapsLoad mktokens = do
|
importtree: support preferred content expressions needing keys
When importing from a special remote, support preferred content expressions
that use terms that match on keys (eg "present", "copies=1"). Such terms
are ignored when importing, since the key is not known yet.
When "standard" or "groupwanted" is used, the terms in those
expressions also get pruned accordingly.
This does allow setting preferred content to "not (copies=1)" to make a
special remote into a "source" type of repository. Importing from it will
import all files. Then exporting to it will drop all files from it.
In the case of setting preferred content to "present", it's pruned on
import, so everything gets imported from it. Then on export, it's applied,
and everything in it is left on it, and no new content is exported to it.
Since the old behavior on these preferred content expressions was for
importtree to error out, there's no backwards compatability to worry about.
Except that sync/pull/etc will now import where before it errored out.
2023-12-18 20:27:26 +00:00
|
|
|
(pc, rc) <- preferredRequiredMapsLoad' id mktokens
|
2023-07-26 18:34:21 +00:00
|
|
|
let pc' = handleunknown (MatcherDesc "preferred content") pc
|
|
|
|
let rc' = handleunknown (MatcherDesc "required content") rc
|
2019-05-21 18:38:00 +00:00
|
|
|
Annex.changeState $ \s -> s
|
|
|
|
{ Annex.preferredcontentmap = Just pc'
|
|
|
|
, Annex.requiredcontentmap = Just rc'
|
|
|
|
}
|
|
|
|
return (pc', rc')
|
|
|
|
where
|
2023-07-26 18:34:21 +00:00
|
|
|
handleunknown matcherdesc = M.mapWithKey $ \u v ->
|
|
|
|
(either (const $ unknownMatcher u) id v, matcherdesc)
|
2019-05-21 18:38:00 +00:00
|
|
|
|
importtree: support preferred content expressions needing keys
When importing from a special remote, support preferred content expressions
that use terms that match on keys (eg "present", "copies=1"). Such terms
are ignored when importing, since the key is not known yet.
When "standard" or "groupwanted" is used, the terms in those
expressions also get pruned accordingly.
This does allow setting preferred content to "not (copies=1)" to make a
special remote into a "source" type of repository. Importing from it will
import all files. Then exporting to it will drop all files from it.
In the case of setting preferred content to "present", it's pruned on
import, so everything gets imported from it. Then on export, it's applied,
and everything in it is left on it, and no new content is exported to it.
Since the old behavior on these preferred content expressions was for
importtree to error out, there's no backwards compatability to worry about.
Except that sync/pull/etc will now import where before it errored out.
2023-12-18 20:27:26 +00:00
|
|
|
preferredRequiredMapsLoad' :: (Matcher (MatchFiles Annex) -> Matcher (MatchFiles Annex)) -> (PreferredContentData -> [ParseToken (MatchFiles Annex)]) -> Annex (M.Map UUID (Either String (Matcher (MatchFiles Annex))), M.Map UUID (Either String (Matcher (MatchFiles Annex))))
|
|
|
|
preferredRequiredMapsLoad' matcherf mktokens = do
|
2012-10-08 19:18:58 +00:00
|
|
|
groupmap <- groupMap
|
2020-09-22 17:52:26 +00:00
|
|
|
configmap <- remoteConfigMap
|
2019-05-14 18:59:03 +00:00
|
|
|
let genmap l gm =
|
2024-09-05 19:22:41 +00:00
|
|
|
let mk u = makeMatcher groupmap configmap
|
|
|
|
gm u matcherf mktokens (Right (unknownMatcher u))
|
2019-05-14 18:59:03 +00:00
|
|
|
in simpleMap
|
|
|
|
. parseLogOldWithUUID (\u -> mk u . decodeBS <$> A.takeByteString)
|
|
|
|
<$> Annex.Branch.get l
|
2020-04-28 17:31:26 +00:00
|
|
|
gm <- groupPreferredContentMapRaw
|
|
|
|
pc <- genmap preferredContentLog gm
|
|
|
|
rc <- genmap requiredContentLog gm
|
2019-05-21 18:38:00 +00:00
|
|
|
-- Required content is implicitly also preferred content, so combine.
|
|
|
|
let pc' = M.unionWith combiner pc rc
|
|
|
|
return (pc', rc)
|
|
|
|
where
|
|
|
|
combiner (Right a) (Right b) = Right (combineMatchers a b)
|
|
|
|
combiner (Left a) (Left b) = Left (a ++ " " ++ b)
|
|
|
|
combiner (Left a) (Right _) = Left a
|
|
|
|
combiner (Right _) (Left b) = Left b
|
2012-10-04 19:48:59 +00:00
|
|
|
|
2024-09-05 19:22:41 +00:00
|
|
|
{- Parsing preferred content expressions intentionally never fails,
|
2013-12-19 09:57:50 +00:00
|
|
|
- because the configuration is shared among repositories and newer
|
2024-09-05 19:22:41 +00:00
|
|
|
- versions of git-annex may add new features.
|
|
|
|
-
|
|
|
|
- When a preferred content expression cannot be parsed, but is already
|
2014-03-20 04:10:12 +00:00
|
|
|
- in the log (eg, put there by a newer version of git-annex),
|
|
|
|
- the fallback behavior is to match only files that are currently present.
|
|
|
|
-
|
|
|
|
- This avoid unwanted/expensive changes to the content, until the problem
|
|
|
|
- is resolved. -}
|
2023-07-26 18:34:21 +00:00
|
|
|
unknownMatcher :: UUID -> Matcher (MatchFiles Annex)
|
2014-03-29 18:43:34 +00:00
|
|
|
unknownMatcher u = generate [present]
|
2014-03-20 04:10:12 +00:00
|
|
|
where
|
2016-02-03 17:23:34 +00:00
|
|
|
present = Operation $ limitPresent (Just u)
|
2014-03-20 04:10:12 +00:00
|
|
|
|
2012-10-10 19:35:10 +00:00
|
|
|
{- Puts a UUID in a standard group, and sets its preferred content to use
|
2014-05-30 18:03:04 +00:00
|
|
|
- the standard expression for that group (unless preferred content is
|
|
|
|
- already set). -}
|
2012-10-10 19:35:10 +00:00
|
|
|
setStandardGroup :: UUID -> StandardGroup -> Annex ()
|
|
|
|
setStandardGroup u g = do
|
|
|
|
groupSet u $ S.singleton $ fromStandardGroup g
|
2014-05-30 18:03:04 +00:00
|
|
|
unlessM (isJust . M.lookup u <$> preferredContentMap) $
|
2012-10-10 19:35:10 +00:00
|
|
|
preferredContentSet u "standard"
|
2014-05-30 18:03:04 +00:00
|
|
|
|
|
|
|
{- Avoids overwriting the UUID's standard group or preferred content
|
|
|
|
- when it's already been configured. -}
|
|
|
|
defaultStandardGroup :: UUID -> StandardGroup -> Annex ()
|
|
|
|
defaultStandardGroup u g =
|
|
|
|
unlessM (hasgroup <||> haspc) $
|
|
|
|
setStandardGroup u g
|
|
|
|
where
|
|
|
|
hasgroup = not . S.null <$> lookupGroups u
|
|
|
|
haspc = isJust . M.lookup u <$> preferredContentMap
|
2015-06-17 17:44:19 +00:00
|
|
|
|
|
|
|
prop_standardGroups_parse :: Bool
|
|
|
|
prop_standardGroups_parse =
|
|
|
|
all (isNothing . checkPreferredContentExpression . standardPreferredContent)
|
|
|
|
[ minBound .. maxBound]
|