2012-10-04 19:48:59 +00:00
|
|
|
{- git-annex preferred content matcher configuration
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012-2014 Joey Hess <id@joeyh.name>
|
2012-10-04 19:48:59 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
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,
|
2012-10-04 19:48:59 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
2012-10-10 17:52:24 +00:00
|
|
|
import qualified Data.Set as S
|
2012-10-04 19:48:59 +00:00
|
|
|
import Data.Either
|
|
|
|
|
|
|
|
import Common.Annex
|
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
|
2014-03-29 18:43:34 +00:00
|
|
|
import Utility.Matcher hiding (tokens)
|
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 Types.Group
|
2013-04-26 03:44:55 +00:00
|
|
|
import Types.Remote (RemoteConfig)
|
2012-10-08 19:18:58 +00:00
|
|
|
import Logs.Group
|
2013-04-26 03:44:55 +00:00
|
|
|
import Logs.Remote
|
2014-03-29 18:43:34 +00:00
|
|
|
import Types.FileMatcher
|
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
|
|
|
|
2012-10-08 17:16:53 +00:00
|
|
|
{- Checks if a file is preferred content for the specified repository
|
|
|
|
- (or the current repository if none is specified). -}
|
2014-01-23 20:37:08 +00:00
|
|
|
isPreferredContent :: Maybe UUID -> AssumeNotPresent -> Maybe Key -> AssociatedFile -> Bool -> Annex Bool
|
2014-03-29 19:20:55 +00:00
|
|
|
isPreferredContent = checkMap preferredContentMap
|
|
|
|
|
|
|
|
isRequiredContent :: Maybe UUID -> AssumeNotPresent -> Maybe Key -> AssociatedFile -> Bool -> Annex Bool
|
|
|
|
isRequiredContent = checkMap requiredContentMap
|
|
|
|
|
|
|
|
checkMap :: Annex (FileMatcherMap Annex) -> Maybe UUID -> AssumeNotPresent -> Maybe Key -> AssociatedFile -> Bool -> Annex Bool
|
|
|
|
checkMap getmap mu notpresent mkey afile def = 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
|
2012-12-06 17:22:16 +00:00
|
|
|
Nothing -> return def
|
2014-01-23 20:37:08 +00:00
|
|
|
Just matcher -> checkMatcher matcher mkey afile notpresent def
|
2012-10-08 17:16:53 +00:00
|
|
|
|
2014-03-29 18:43:34 +00:00
|
|
|
preferredContentMap :: Annex (FileMatcherMap Annex)
|
2014-03-29 19:20:55 +00:00
|
|
|
preferredContentMap = maybe (fst <$> preferredRequiredMapsLoad) 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)
|
|
|
|
requiredContentMap = maybe (snd <$> preferredRequiredMapsLoad) return
|
|
|
|
=<< Annex.getState Annex.requiredcontentmap
|
|
|
|
|
|
|
|
preferredRequiredMapsLoad :: Annex (FileMatcherMap Annex, FileMatcherMap Annex)
|
|
|
|
preferredRequiredMapsLoad = do
|
2012-10-08 19:18:58 +00:00
|
|
|
groupmap <- groupMap
|
2013-04-26 03:44:55 +00:00
|
|
|
configmap <- readRemoteLog
|
2014-03-29 19:20:55 +00:00
|
|
|
let genmap l gm = simpleMap
|
|
|
|
. parseLogWithUUID ((Just .) . makeMatcher groupmap configmap gm)
|
|
|
|
<$> Annex.Branch.get l
|
|
|
|
pc <- genmap preferredContentLog =<< groupPreferredContentMapRaw
|
|
|
|
rc <- genmap requiredContentLog M.empty
|
|
|
|
-- Required content is implicitly also preferred content, so OR
|
|
|
|
let m = M.unionWith MOr pc rc
|
|
|
|
Annex.changeState $ \s -> s
|
|
|
|
{ Annex.preferredcontentmap = Just m
|
|
|
|
, Annex.requiredcontentmap = Just rc
|
|
|
|
}
|
|
|
|
return (m, rc)
|
2012-10-04 19:48:59 +00:00
|
|
|
|
|
|
|
{- This intentionally never fails, even on unparsable expressions,
|
2013-12-19 09:57:50 +00:00
|
|
|
- because the configuration is shared among repositories and newer
|
2014-03-20 04:10:12 +00:00
|
|
|
- versions of git-annex may add new features. -}
|
|
|
|
makeMatcher
|
|
|
|
:: GroupMap
|
|
|
|
-> M.Map UUID RemoteConfig
|
|
|
|
-> M.Map Group PreferredContentExpression
|
|
|
|
-> UUID
|
|
|
|
-> PreferredContentExpression
|
2014-03-29 18:43:34 +00:00
|
|
|
-> FileMatcher Annex
|
2014-03-15 21:08:55 +00:00
|
|
|
makeMatcher groupmap configmap groupwantedmap u = go True True
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
2014-03-15 21:08:55 +00:00
|
|
|
go expandstandard expandgroupwanted expr
|
2014-03-29 18:43:34 +00:00
|
|
|
| null (lefts tokens) = generate $ rights tokens
|
2014-03-20 04:10:12 +00:00
|
|
|
| otherwise = unknownMatcher u
|
2014-03-14 19:04:33 +00:00
|
|
|
where
|
2014-03-15 21:08:55 +00:00
|
|
|
tokens = exprParser matchstandard matchgroupwanted groupmap configmap (Just u) expr
|
2014-03-14 19:04:33 +00:00
|
|
|
matchstandard
|
2014-03-20 04:10:12 +00:00
|
|
|
| expandstandard = maybe (unknownMatcher u) (go False False)
|
2014-03-15 21:08:55 +00:00
|
|
|
(standardPreferredContent <$> getStandardGroup mygroups)
|
2014-03-20 04:10:12 +00:00
|
|
|
| otherwise = unknownMatcher u
|
2014-03-15 21:08:55 +00:00
|
|
|
matchgroupwanted
|
2014-03-20 04:10:12 +00:00
|
|
|
| expandgroupwanted = maybe (unknownMatcher u) (go True False)
|
2014-03-15 21:08:55 +00:00
|
|
|
(groupwanted mygroups)
|
2014-03-20 04:10:12 +00:00
|
|
|
| otherwise = unknownMatcher u
|
2014-03-15 21:08:55 +00:00
|
|
|
mygroups = fromMaybe S.empty (u `M.lookup` groupsByUUID groupmap)
|
|
|
|
groupwanted s = case M.elems $ M.filterWithKey (\k _ -> S.member k s) groupwantedmap of
|
|
|
|
[pc] -> Just pc
|
|
|
|
_ -> Nothing
|
2012-10-10 19:15:56 +00:00
|
|
|
|
2014-03-20 04:10:12 +00:00
|
|
|
{- When a preferred content expression cannot be parsed, but is already
|
|
|
|
- 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. -}
|
2014-03-29 18:43:34 +00:00
|
|
|
unknownMatcher :: UUID -> FileMatcher Annex
|
|
|
|
unknownMatcher u = generate [present]
|
2014-03-20 04:10:12 +00:00
|
|
|
where
|
2014-03-29 18:43:34 +00:00
|
|
|
present = Operation $ matchPresent (Just u)
|
2014-03-20 04:10:12 +00:00
|
|
|
|
2012-10-04 19:48:59 +00:00
|
|
|
{- Checks if an expression can be parsed, if not returns Just error -}
|
2014-01-01 23:58:02 +00:00
|
|
|
checkPreferredContentExpression :: PreferredContentExpression -> Maybe String
|
2014-03-14 19:04:33 +00:00
|
|
|
checkPreferredContentExpression expr = case parsedToMatcher tokens of
|
|
|
|
Left e -> Just e
|
|
|
|
Right _ -> Nothing
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
2014-03-15 21:08:55 +00:00
|
|
|
tokens = exprParser matchAll matchAll emptyGroupMap M.empty Nothing expr
|
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
|