git-annex/Logs/Trust.hs

88 lines
2.4 KiB
Haskell
Raw Normal View History

{- git-annex trust
-
- Copyright 2010 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 (
TrustLevel(..),
trustGet,
2011-09-06 21:19:29 +00:00
trustSet,
trustPartition
) 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
2011-10-15 20:21:08 +00:00
import Logs.UUIDBased
2011-10-06 19:55:50 +00:00
{- Filename of trust.log. -}
trustLog :: FilePath
trustLog = "trust.log"
{- 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
{- 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
{- Read the trustLog into a map, overriding with any
2011-09-06 21:19:29 +00:00
- values from forcetrust. The map is cached for speed. -}
trustMap :: Annex TrustMap
trustMap = do
cached <- Annex.getState Annex.trustmap
case cached of
Just m -> return m
Nothing -> do
2011-10-06 19:55:50 +00:00
overrides <- M.fromList <$> Annex.getState Annex.forcetrust
m <- (M.union overrides . simpleMap . parseLog parseTrust) <$>
Annex.Branch.get trustLog
Annex.changeState $ \s -> s { Annex.trustmap = Just m }
return m
2011-10-06 19:55:50 +00:00
parseTrust :: String -> Maybe TrustLevel
parseTrust s
| length w > 0 = Just $ parse $ head w
2011-10-06 19:55:50 +00:00
-- back-compat; the trust.log used to only list trusted repos
2011-11-11 05:52:58 +00:00
| otherwise = Just Trusted
where
2011-10-06 19:55:50 +00:00
w = words s
parse "1" = Trusted
parse "0" = UnTrusted
parse "X" = DeadTrusted
parse _ = SemiTrusted
showTrust :: TrustLevel -> String
showTrust Trusted = "1"
showTrust UnTrusted = "0"
showTrust DeadTrusted = "X"
showTrust SemiTrusted = "?"
{- Changes the trust level for a uuid in the trustLog. -}
trustSet :: UUID -> TrustLevel -> Annex ()
trustSet uuid@(UUID _) level = do
2011-11-11 05:52:58 +00:00
ts <- liftIO getPOSIXTime
2011-10-04 04:40:47 +00:00
Annex.Branch.change trustLog $
showLog showTrust . changeLog ts uuid level . parseLog parseTrust
Annex.changeState $ \s -> s { Annex.trustmap = Nothing }
trustSet NoUUID _ = error "unknown UUID; cannot modify trust level"