2012-10-01 19:12:04 +00:00
|
|
|
{- git-annex group log
|
|
|
|
-
|
2019-01-09 18:00:35 +00:00
|
|
|
- Copyright 2012, 2019 Joey Hess <id@joeyh.name>
|
2012-10-01 19:12:04 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-10-01 19:12:04 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Logs.Group (
|
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
|
|
|
groupLog,
|
2012-10-01 19:17:21 +00:00
|
|
|
groupChange,
|
2012-10-04 19:48:59 +00:00
|
|
|
groupSet,
|
2012-10-01 19:12:04 +00:00
|
|
|
lookupGroups,
|
|
|
|
groupMap,
|
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
|
|
|
groupMapLoad,
|
2012-10-10 23:13:49 +00:00
|
|
|
getStandardGroup,
|
2013-04-03 21:01:40 +00:00
|
|
|
inUnwantedGroup
|
2012-10-01 19:12:04 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import qualified Data.Set as S
|
convert old uuid-based log parsers to attoparsec
This preserves the workaround for the old bug that caused NoUUID items
to be stored in the log, prefixing log lines with " ". It's now handled
implicitly, by using takeWhile1 (/= ' ') to get the uuid.
There is a behavior change from the old parser, which split the value
into words and then recombined it. That meant that "foo bar" and "foo\tbar"
came out as "foo bar". That behavior was not documented, and seems
surprising; it meant that after a git-annex describe here "foo bar",
you wouldn't get that same string back out when git-annex displayed repo
descriptions.
Otoh, some other parsers relied on the old behavior, and the attoparsec
rewrites had to deal with the issue themselves...
For group.log, there are some edge cases around the user providing a
group name with a leading or trailing space. The old parser would ignore
such excess whitespace. The new parser does too, because the alternative
is to refuse to parse something like " group1 group2 " due to excess
whitespace, which would be even more confusing behavior.
The only git-annex branch log file that is not converted to attoparsec
and bytestring-builder now is transitions.log.
2019-01-10 18:39:36 +00:00
|
|
|
import qualified Data.Attoparsec.ByteString as A
|
|
|
|
import qualified Data.Attoparsec.ByteString.Char8 as A8
|
2019-01-09 18:00:35 +00:00
|
|
|
import Data.ByteString.Builder
|
2012-10-01 19:12:04 +00:00
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2013-08-29 22:51:22 +00:00
|
|
|
import Logs
|
2012-10-01 19:12:04 +00:00
|
|
|
import qualified Annex.Branch
|
|
|
|
import qualified Annex
|
|
|
|
import Logs.UUIDBased
|
|
|
|
import Types.Group
|
2012-10-10 20:04:28 +00:00
|
|
|
import Types.StandardGroups
|
2012-10-01 19:12:04 +00:00
|
|
|
|
|
|
|
{- Returns the groups of a given repo UUID. -}
|
|
|
|
lookupGroups :: UUID -> Annex (S.Set Group)
|
2012-10-02 17:45:30 +00:00
|
|
|
lookupGroups u = (fromMaybe S.empty . M.lookup u) . groupsByUUID <$> groupMap
|
2012-10-01 19:12:04 +00:00
|
|
|
|
2012-10-01 19:17:21 +00:00
|
|
|
{- Applies a set modifier to change the groups for a uuid in the groupLog. -}
|
|
|
|
groupChange :: UUID -> (S.Set Group -> S.Set Group) -> Annex ()
|
|
|
|
groupChange uuid@(UUID _) modifier = do
|
|
|
|
curr <- lookupGroups uuid
|
2017-08-14 17:55:38 +00:00
|
|
|
c <- liftIO currentVectorClock
|
2012-10-01 19:12:04 +00:00
|
|
|
Annex.Branch.change groupLog $
|
2019-02-21 17:43:21 +00:00
|
|
|
buildLogOld buildGroup . changeLog c uuid (modifier curr) . parseLogOld parseGroup
|
2013-04-08 20:40:34 +00:00
|
|
|
|
|
|
|
-- The changed group invalidates the preferred content cache.
|
|
|
|
Annex.changeState $ \s -> s
|
|
|
|
{ Annex.groupmap = Nothing
|
|
|
|
, Annex.preferredcontentmap = Nothing
|
|
|
|
}
|
2012-10-04 19:48:59 +00:00
|
|
|
groupChange NoUUID _ = error "unknown UUID; cannot modify"
|
|
|
|
|
2019-01-09 18:00:35 +00:00
|
|
|
buildGroup :: S.Set Group -> Builder
|
|
|
|
buildGroup = go . S.toList
|
|
|
|
where
|
|
|
|
go [] = mempty
|
|
|
|
go (g:gs) = bld g <> mconcat [ charUtf8 ' ' <> bld g' | g' <- gs ]
|
2019-01-09 19:00:43 +00:00
|
|
|
bld (Group g) = byteString g
|
|
|
|
|
convert old uuid-based log parsers to attoparsec
This preserves the workaround for the old bug that caused NoUUID items
to be stored in the log, prefixing log lines with " ". It's now handled
implicitly, by using takeWhile1 (/= ' ') to get the uuid.
There is a behavior change from the old parser, which split the value
into words and then recombined it. That meant that "foo bar" and "foo\tbar"
came out as "foo bar". That behavior was not documented, and seems
surprising; it meant that after a git-annex describe here "foo bar",
you wouldn't get that same string back out when git-annex displayed repo
descriptions.
Otoh, some other parsers relied on the old behavior, and the attoparsec
rewrites had to deal with the issue themselves...
For group.log, there are some edge cases around the user providing a
group name with a leading or trailing space. The old parser would ignore
such excess whitespace. The new parser does too, because the alternative
is to refuse to parse something like " group1 group2 " due to excess
whitespace, which would be even more confusing behavior.
The only git-annex branch log file that is not converted to attoparsec
and bytestring-builder now is transitions.log.
2019-01-10 18:39:36 +00:00
|
|
|
parseGroup :: A.Parser (S.Set Group)
|
|
|
|
parseGroup = S.fromList <$> go []
|
|
|
|
where
|
|
|
|
go l = (A.endOfInput *> pure l)
|
|
|
|
<|> ((getgroup <* A8.char ' ') >>= go . (:l))
|
|
|
|
<|> ((:l) <$> getgroup)
|
|
|
|
-- allow extra writespace before or after a group name
|
|
|
|
<|> (A8.char ' ' >>= const (go l))
|
|
|
|
getgroup = Group <$> A8.takeWhile1 (/= ' ')
|
2019-01-09 18:00:35 +00:00
|
|
|
|
2012-10-04 19:48:59 +00:00
|
|
|
groupSet :: UUID -> S.Set Group -> Annex ()
|
|
|
|
groupSet u g = groupChange u (const g)
|
2012-10-01 19:12:04 +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-01 19:12:04 +00:00
|
|
|
groupMap :: Annex GroupMap
|
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
|
|
|
groupMap = maybe groupMapLoad return =<< Annex.getState Annex.groupmap
|
|
|
|
|
|
|
|
{- Loads the map, updating the cache. -}
|
|
|
|
groupMapLoad :: Annex GroupMap
|
|
|
|
groupMapLoad = do
|
2019-02-21 17:43:21 +00:00
|
|
|
m <- makeGroupMap . simpleMap . parseLogOld parseGroup
|
|
|
|
<$> Annex.Branch.get groupLog
|
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.changeState $ \s -> s { Annex.groupmap = Just m }
|
|
|
|
return m
|
2012-10-02 17:45:30 +00:00
|
|
|
|
|
|
|
makeGroupMap :: M.Map UUID (S.Set Group) -> GroupMap
|
|
|
|
makeGroupMap byuuid = GroupMap byuuid bygroup
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
bygroup = M.fromListWith S.union $
|
2013-04-03 07:52:41 +00:00
|
|
|
concatMap explode $ M.toList byuuid
|
2012-11-11 04:51:07 +00:00
|
|
|
explode (u, s) = map (\g -> (g, S.singleton u)) (S.toList s)
|
2012-10-10 20:04:28 +00:00
|
|
|
|
|
|
|
{- If a repository is in exactly one standard group, returns it. -}
|
2012-10-10 20:23:41 +00:00
|
|
|
getStandardGroup :: S.Set Group -> Maybe StandardGroup
|
2013-04-03 07:52:41 +00:00
|
|
|
getStandardGroup s = case mapMaybe toStandardGroup $ S.toList s of
|
2012-10-10 20:23:41 +00:00
|
|
|
[g] -> Just g
|
|
|
|
_ -> Nothing
|
2013-04-03 21:01:40 +00:00
|
|
|
|
|
|
|
inUnwantedGroup :: UUID -> Annex Bool
|
|
|
|
inUnwantedGroup u = elem UnwantedGroup
|
|
|
|
. mapMaybe toStandardGroup . S.toList <$> lookupGroups u
|