2012-10-04 19:48:59 +00:00
|
|
|
{- git-annex preferred content matcher configuration
|
|
|
|
-
|
2014-03-15 20:17:01 +00:00
|
|
|
- Copyright 2012-2014 Joey Hess <joey@kitenet.net>
|
2012-10-04 19:48:59 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Logs.PreferredContent (
|
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
|
|
|
preferredContentLog,
|
2012-10-04 19:48:59 +00:00
|
|
|
preferredContentSet,
|
2014-03-15 20:17:01 +00:00
|
|
|
groupPreferredContentSet,
|
2012-10-08 17:16:53 +00:00
|
|
|
isPreferredContent,
|
2012-10-04 19:48:59 +00:00
|
|
|
preferredContentMap,
|
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
|
|
|
preferredContentMapLoad,
|
2012-10-04 19:48:59 +00:00
|
|
|
preferredContentMapRaw,
|
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,
|
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
|
|
|
|
import qualified Utility.Matcher
|
2013-03-29 20:17:13 +00:00
|
|
|
import Annex.FileMatcher
|
2012-10-08 17:16:53 +00:00
|
|
|
import Annex.UUID
|
2013-10-28 18:05:55 +00:00
|
|
|
import Types.Limit
|
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
|
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
|
|
|
|
isPreferredContent mu notpresent mkey afile def = do
|
2012-10-08 17:16:53 +00:00
|
|
|
u <- maybe getUUID return mu
|
|
|
|
m <- preferredContentMap
|
|
|
|
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
|
|
|
|
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
|
|
|
{- The map is cached for speed. -}
|
2012-10-04 19:48:59 +00:00
|
|
|
preferredContentMap :: Annex Annex.PreferredContentMap
|
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
|
|
|
preferredContentMap = maybe preferredContentMapLoad return
|
|
|
|
=<< Annex.getState Annex.preferredcontentmap
|
|
|
|
|
|
|
|
{- Loads the map, updating the cache. -}
|
|
|
|
preferredContentMapLoad :: Annex Annex.PreferredContentMap
|
|
|
|
preferredContentMapLoad = do
|
2012-10-08 19:18:58 +00:00
|
|
|
groupmap <- groupMap
|
2013-04-26 03:44:55 +00:00
|
|
|
configmap <- readRemoteLog
|
2014-03-15 21:08:55 +00:00
|
|
|
groupwantedmap <- groupPreferredContentMapRaw
|
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
|
|
|
m <- simpleMap
|
2014-03-15 21:08:55 +00:00
|
|
|
. parseLogWithUUID ((Just .) . makeMatcher groupmap configmap groupwantedmap)
|
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.Branch.get preferredContentLog
|
|
|
|
Annex.changeState $ \s -> s { Annex.preferredcontentmap = Just m }
|
|
|
|
return m
|
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
|
|
|
|
-> FileMatcher
|
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-14 19:04:33 +00:00
|
|
|
| null (lefts tokens) = Utility.Matcher.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. -}
|
|
|
|
unknownMatcher :: UUID -> FileMatcher
|
|
|
|
unknownMatcher u = Utility.Matcher.generate [present]
|
|
|
|
where
|
|
|
|
present = Utility.Matcher.Operation $ matchPresent (Just u)
|
|
|
|
|
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
|
|
|
|
- the standard expression for that group, unless something is already set. -}
|
|
|
|
setStandardGroup :: UUID -> StandardGroup -> Annex ()
|
|
|
|
setStandardGroup u g = do
|
|
|
|
groupSet u $ S.singleton $ fromStandardGroup g
|
|
|
|
m <- preferredContentMap
|
|
|
|
unless (isJust $ M.lookup u m) $
|
|
|
|
preferredContentSet u "standard"
|