2011-06-01 21:49:37 +00:00
|
|
|
{- git-annex trust
|
2011-01-26 19:37:16 +00:00
|
|
|
-
|
|
|
|
- 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 (
|
2011-01-26 19:37:16 +00:00
|
|
|
TrustLevel(..),
|
|
|
|
trustGet,
|
2011-09-06 21:19:29 +00:00
|
|
|
trustSet,
|
|
|
|
trustPartition
|
2011-01-26 19:37:16 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
2011-10-06 19:55:50 +00:00
|
|
|
import Data.Time.Clock.POSIX
|
2011-01-26 19:37:16 +00:00
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2011-06-24 01:25:39 +00:00
|
|
|
import Types.TrustLevel
|
2011-10-04 04:40:47 +00:00
|
|
|
import qualified Annex.Branch
|
2011-01-26 19:37:16 +00:00
|
|
|
import qualified Annex
|
2011-10-15 20:21:08 +00:00
|
|
|
import Logs.UUIDBased
|
2011-10-06 19:55:50 +00:00
|
|
|
|
2011-01-26 19:37:16 +00:00
|
|
|
{- Filename of trust.log. -}
|
2011-06-22 21:08:51 +00:00
|
|
|
trustLog :: FilePath
|
|
|
|
trustLog = "trust.log"
|
2011-01-26 19:37:16 +00:00
|
|
|
|
|
|
|
{- Returns a list of UUIDs at the specified trust level. -}
|
|
|
|
trustGet :: TrustLevel -> Annex [UUID]
|
2011-10-06 19:55:50 +00:00
|
|
|
trustGet level = M.keys . M.filter (== level) <$> trustMap
|
2011-01-26 19:37:16 +00:00
|
|
|
|
2011-06-01 21:49:37 +00:00
|
|
|
{- 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. -}
|
2011-06-24 01:25:39 +00:00
|
|
|
trustMap :: Annex TrustMap
|
2011-01-26 19:37:16 +00:00
|
|
|
trustMap = do
|
2011-06-24 01:25:39 +00:00
|
|
|
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
|
2011-06-24 01:25:39 +00:00
|
|
|
Annex.changeState $ \s -> s { Annex.trustmap = Just m }
|
|
|
|
return m
|
2011-01-26 19:37:16 +00:00
|
|
|
|
2011-10-06 19:55:50 +00:00
|
|
|
parseTrust :: String -> Maybe TrustLevel
|
|
|
|
parseTrust s
|
|
|
|
| length w > 0 = readMaybe $ head w
|
|
|
|
-- back-compat; the trust.log used to only list trusted repos
|
|
|
|
| otherwise = Just Trusted
|
2011-01-26 19:37:16 +00:00
|
|
|
where
|
2011-10-06 19:55:50 +00:00
|
|
|
w = words s
|
2011-01-26 19:37:16 +00:00
|
|
|
|
2011-06-22 21:08:51 +00:00
|
|
|
{- Changes the trust level for a uuid in the trustLog. -}
|
2011-01-26 19:37:16 +00:00
|
|
|
trustSet :: UUID -> TrustLevel -> Annex ()
|
|
|
|
trustSet uuid level = do
|
2011-01-26 20:20:28 +00:00
|
|
|
when (null uuid) $
|
|
|
|
error "unknown UUID; cannot modify trust level"
|
2011-10-06 19:55:50 +00:00
|
|
|
ts <- liftIO $ getPOSIXTime
|
2011-10-04 04:40:47 +00:00
|
|
|
Annex.Branch.change trustLog $
|
2011-10-06 19:55:50 +00:00
|
|
|
showLog show . changeLog ts uuid level . parseLog parseTrust
|
2011-10-03 19:41:25 +00:00
|
|
|
Annex.changeState $ \s -> s { Annex.trustmap = Nothing }
|
2011-09-06 21:19:29 +00:00
|
|
|
|
|
|
|
{- Partitions a list of UUIDs to those matching a TrustLevel and not. -}
|
|
|
|
trustPartition :: TrustLevel -> [UUID] -> Annex ([UUID], [UUID])
|
|
|
|
trustPartition level ls = do
|
|
|
|
candidates <- trustGet level
|
|
|
|
return $ partition (`elem` candidates) ls
|