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.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Trust (
|
|
|
|
TrustLevel(..),
|
|
|
|
trustLog,
|
|
|
|
trustGet,
|
2011-09-06 21:19:29 +00:00
|
|
|
trustSet,
|
|
|
|
trustPartition
|
2011-01-26 19:37:16 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Monad.State
|
|
|
|
import qualified Data.Map as M
|
2011-09-06 21:19:29 +00:00
|
|
|
import Data.List
|
2011-01-26 19:37:16 +00:00
|
|
|
|
2011-06-24 01:25:39 +00:00
|
|
|
import Types.TrustLevel
|
2011-06-22 21:08:51 +00:00
|
|
|
import qualified Branch
|
2011-01-26 19:37:16 +00:00
|
|
|
import Types
|
|
|
|
import UUID
|
|
|
|
import qualified Annex
|
|
|
|
|
|
|
|
{- 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]
|
|
|
|
trustGet level = do
|
|
|
|
m <- trustMap
|
|
|
|
return $ M.keys $ M.filter (== level) m
|
|
|
|
|
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
|
|
|
|
overrides <- Annex.getState Annex.forcetrust
|
|
|
|
l <- Branch.get trustLog
|
|
|
|
let m = M.fromList $ trustMapParse l ++ overrides
|
|
|
|
Annex.changeState $ \s -> s { Annex.trustmap = Just m }
|
|
|
|
return m
|
2011-01-26 19:37:16 +00:00
|
|
|
|
|
|
|
{- Trust map parser. -}
|
2011-06-01 21:49:37 +00:00
|
|
|
trustMapParse :: String -> [(UUID, TrustLevel)]
|
|
|
|
trustMapParse s = map pair $ filter (not . null) $ lines s
|
2011-01-26 19:37:16 +00:00
|
|
|
where
|
|
|
|
pair l
|
|
|
|
| length w > 1 = (w !! 0, read (w !! 1) :: TrustLevel)
|
|
|
|
-- for back-compat; the trust log used to only
|
|
|
|
-- list trusted uuids
|
|
|
|
| otherwise = (w !! 0, Trusted)
|
|
|
|
where
|
|
|
|
w = words l
|
|
|
|
|
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-01-26 19:37:16 +00:00
|
|
|
m <- trustMap
|
|
|
|
when (M.lookup uuid m /= Just level) $ do
|
|
|
|
let m' = M.insert uuid level m
|
2011-06-22 21:08:51 +00:00
|
|
|
Branch.change trustLog (serialize m')
|
2011-06-24 01:25:39 +00:00
|
|
|
Annex.changeState $ \s -> s { Annex.trustmap = Just m' }
|
2011-01-26 19:37:16 +00:00
|
|
|
where
|
|
|
|
serialize m = unlines $ map showpair $ M.toList m
|
|
|
|
showpair (u, t) = u ++ " " ++ show t
|
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
|