2012-01-10 19:36:54 +00:00
|
|
|
{- git-annex trust log
|
2011-01-26 19:37:16 +00:00
|
|
|
-
|
2012-01-10 19:36:54 +00:00
|
|
|
- Copyright 2010-2012 Joey Hess <joey@kitenet.net>
|
2011-01-26 19:37:16 +00:00
|
|
|
-
|
|
|
|
- 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,
|
2012-01-10 03:31:44 +00:00
|
|
|
trustPartition,
|
2012-09-23 17:50:31 +00:00
|
|
|
lookupTrust,
|
2012-10-03 21:04:52 +00:00
|
|
|
trustMapRaw,
|
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
|
2012-01-10 17:11:16 +00:00
|
|
|
import Remote.List
|
|
|
|
import Config
|
|
|
|
import qualified Types.Remote
|
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
|
|
|
|
2011-11-18 17:22:48 +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. -}
|
2011-01-26 19:37:16 +00:00
|
|
|
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
|
|
|
|
2012-01-10 03:31:44 +00:00
|
|
|
{- 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 $
|
2012-10-03 21:04:52 +00:00
|
|
|
showLog showTrustLog .
|
|
|
|
changeLog ts uuid level .
|
|
|
|
parseLog (Just . parseTrustLog)
|
2012-01-10 03:31:44 +00:00
|
|
|
Annex.changeState $ \s -> s { Annex.trustmap = Nothing }
|
|
|
|
trustSet NoUUID _ = error "unknown UUID; cannot modify trust level"
|
|
|
|
|
2012-09-23 17:50:31 +00:00
|
|
|
{- Returns the TrustLevel of a given repo UUID. -}
|
|
|
|
lookupTrust :: UUID -> Annex TrustLevel
|
|
|
|
lookupTrust u = (fromMaybe SemiTrusted . M.lookup u) <$> trustMap
|
|
|
|
|
2011-11-18 17:22:48 +00:00
|
|
|
{- 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
|
2011-12-02 23:21:56 +00:00
|
|
|
d <- trustGet DeadTrusted
|
|
|
|
let uncandidates = t ++ u ++ d
|
2011-11-18 17:22:48 +00:00
|
|
|
return $ partition (`notElem` uncandidates) ls
|
|
|
|
| otherwise = do
|
|
|
|
candidates <- trustGet level
|
|
|
|
return $ partition (`elem` candidates) ls
|
|
|
|
|
2011-06-01 21:49:37 +00:00
|
|
|
{- Read the trustLog into a map, overriding with any
|
2012-01-10 17:11:16 +00:00
|
|
|
- values from forcetrust or the git config. 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
|
2012-01-10 03:31:44 +00:00
|
|
|
overrides <- Annex.getState Annex.forcetrust
|
2012-10-03 21:04:52 +00:00
|
|
|
logged <- trustMapRaw
|
|
|
|
configured <- M.fromList . catMaybes
|
|
|
|
<$> (mapM configuredtrust =<< remoteList)
|
2012-01-10 17:11:16 +00:00
|
|
|
let m = M.union overrides $ M.union configured logged
|
2011-06-24 01:25:39 +00:00
|
|
|
Annex.changeState $ \s -> s { Annex.trustmap = Just m }
|
|
|
|
return m
|
2012-01-10 17:11:16 +00:00
|
|
|
where
|
|
|
|
configuredtrust r =
|
|
|
|
maybe Nothing (\l -> Just (Types.Remote.uuid r, l)) <$>
|
2012-10-03 21:04:52 +00:00
|
|
|
maybe Nothing readTrustLevel
|
|
|
|
<$> getTrustLevel (Types.Remote.repo r)
|
2012-09-23 17:50:31 +00:00
|
|
|
|
2012-10-03 21:04:52 +00:00
|
|
|
trustMapRaw :: Annex TrustMap
|
|
|
|
trustMapRaw = simpleMap . parseLog (Just . parseTrustLog)
|
|
|
|
<$> Annex.Branch.get trustLog
|
2011-01-26 19:37:16 +00:00
|
|
|
|
2011-12-15 22:11:42 +00:00
|
|
|
{- The trust.log used to only list trusted repos, without a field for the
|
|
|
|
- trust status, which is why this defaults to Trusted. -}
|
2012-10-03 21:04:52 +00:00
|
|
|
parseTrustLog :: String -> TrustLevel
|
|
|
|
parseTrustLog s = maybe Trusted parse $ headMaybe $ words s
|
2011-01-26 19:37:16 +00:00
|
|
|
where
|
2011-11-08 03:21:22 +00:00
|
|
|
parse "1" = Trusted
|
|
|
|
parse "0" = UnTrusted
|
2011-12-02 20:59:55 +00:00
|
|
|
parse "X" = DeadTrusted
|
2011-11-08 03:21:22 +00:00
|
|
|
parse _ = SemiTrusted
|
|
|
|
|
2012-10-03 21:04:52 +00:00
|
|
|
showTrustLog :: TrustLevel -> String
|
|
|
|
showTrustLog Trusted = "1"
|
|
|
|
showTrustLog UnTrusted = "0"
|
|
|
|
showTrustLog DeadTrusted = "X"
|
|
|
|
showTrustLog SemiTrusted = "?"
|