git-annex/Logs/Trust.hs

98 lines
2.9 KiB
Haskell
Raw Normal View History

2012-01-10 19:36:54 +00:00
{- git-annex trust log
-
2012-01-10 19:36:54 +00:00
- Copyright 2010-2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
2011-10-15 20:21:08 +00:00
module Logs.Trust (
module X,
trustLog,
TrustLevel(..),
trustGet,
trustMap,
2011-09-06 21:19:29 +00:00
trustSet,
trustPartition,
2012-11-11 04:24:32 +00:00
trustExclude,
lookupTrust,
trustMapLoad,
trustMapRaw,
) where
import qualified Data.Map as M
2011-10-06 19:55:50 +00:00
import Data.Time.Clock.POSIX
2011-10-05 20:02:51 +00:00
import Common.Annex
import Types.TrustLevel
2011-10-04 04:40:47 +00:00
import qualified Annex.Branch
import qualified Annex
import Logs
2011-10-15 20:21:08 +00:00
import Logs.UUIDBased
import Remote.List
import qualified Types.Remote
import Logs.Trust.Pure as X
2011-10-06 19:55:50 +00:00
{- Returns a list of UUIDs that the trustLog indicates have the
- specified trust level.
- Note that the list can be incomplete for SemiTrusted, since that's
- the default. -}
trustGet :: TrustLevel -> Annex [UUID]
2011-10-06 19:55:50 +00:00
trustGet level = M.keys . M.filter (== level) <$> trustMap
{- Changes the trust level for a uuid in the trustLog. -}
trustSet :: UUID -> TrustLevel -> Annex ()
trustSet uuid@(UUID _) level = do
ts <- liftIO getPOSIXTime
Annex.Branch.change trustLog $
showLog showTrustLog .
changeLog ts uuid level .
parseLog (Just . parseTrustLog)
Annex.changeState $ \s -> s { Annex.trustmap = Nothing }
trustSet NoUUID _ = error "unknown UUID; cannot modify"
{- Returns the TrustLevel of a given repo UUID. -}
lookupTrust :: UUID -> Annex TrustLevel
lookupTrust u = (fromMaybe SemiTrusted . M.lookup u) <$> trustMap
{- Partitions a list of UUIDs to those matching a TrustLevel and not. -}
trustPartition :: TrustLevel -> [UUID] -> Annex ([UUID], [UUID])
trustPartition level ls
| level == SemiTrusted = do
t <- trustGet Trusted
u <- trustGet UnTrusted
d <- trustGet DeadTrusted
let uncandidates = t ++ u ++ d
return $ partition (`notElem` uncandidates) ls
| otherwise = do
candidates <- trustGet level
return $ partition (`elem` candidates) ls
2012-11-11 04:24:32 +00:00
{- Filters UUIDs to those not matching a TrustLevel. -}
2013-04-03 07:52:41 +00:00
trustExclude :: TrustLevel -> [UUID] -> Annex [UUID]
2012-11-11 04:24:32 +00:00
trustExclude level ls = snd <$> trustPartition level ls
{- trustLog in a map, overridden with any values from forcetrust or
- the git config. The map is cached for speed. -}
trustMap :: Annex TrustMap
trustMap = maybe trustMapLoad return =<< Annex.getState Annex.trustmap
{- Loads the map, updating the cache, -}
trustMapLoad :: Annex TrustMap
trustMapLoad = do
overrides <- Annex.getState Annex.forcetrust
logged <- trustMapRaw
configured <- M.fromList . catMaybes
<$> (map configuredtrust <$> remoteList)
let m = M.union overrides $ M.union configured logged
Annex.changeState $ \s -> s { Annex.trustmap = Just m }
return m
2012-11-11 04:51:07 +00:00
where
configuredtrust r = (\l -> Just (Types.Remote.uuid r, l))
=<< readTrustLevel
=<< remoteAnnexTrustLevel (Types.Remote.gitconfig r)
{- Does not include forcetrust or git config values, just those from the
- log file. -}
trustMapRaw :: Annex TrustMap
trustMapRaw = calcTrustMap <$> Annex.Branch.get trustLog