Limiting the number of copies per trustlevel

The --copies flag now takes an argument of the form:
  trustlevel:number or number

If a trust level is specified the command is limited to files
with at least 'number' copies of this 'trustlevel'.
This commit is contained in:
Nicolas Pouillard 2012-09-23 19:50:31 +02:00
parent c9b3b8829d
commit f0bcc77fb2
2 changed files with 26 additions and 11 deletions

View file

@ -16,6 +16,7 @@ import qualified Utility.Matcher
import qualified Remote import qualified Remote
import qualified Backend import qualified Backend
import Annex.Content import Annex.Content
import Logs.Trust
type Limit = Utility.Matcher.Token (FilePath -> Annex Bool) type Limit = Utility.Matcher.Token (FilePath -> Annex Bool)
@ -83,16 +84,21 @@ addIn name = addLimit $ check $ if name == "." then inAnnex else inremote
{- Adds a limit to skip files not believed to have the specified number {- Adds a limit to skip files not believed to have the specified number
- of copies. -} - of copies. -}
addCopies :: String -> Annex () addCopies :: String -> Annex ()
addCopies num = addCopies trust_num = addLimit . check $ readnum num
case readish num :: Maybe Int of where (num, mayCheckTrust) =
Nothing -> error "bad number for --copies" case split ":" trust_num of
Just n -> addLimit $ check n [trust, num'] -> (num', checkTrust (readtrust trust))
where [num'] -> (num', const (return True))
_ -> bad
readnum = maybe bad id . readish
readtrust = maybe bad id . readTrust
check n = Backend.lookupFile >=> handle n check n = Backend.lookupFile >=> handle n
handle _ Nothing = return False handle _ Nothing = return False
handle n (Just (key, _)) = do handle n (Just (key, _)) = do
us <- Remote.keyLocations key us <- filterM mayCheckTrust =<< Remote.keyLocations key
return $ length us >= n return $ length us >= n
checkTrust t u = (== t) <$> lookupTrust u -- == or >=
bad = error "bad number or trust:number for --copies"
{- Adds a limit to skip files not using a specified key-value backend. -} {- Adds a limit to skip files not using a specified key-value backend. -}
addInBackend :: String -> Annex () addInBackend :: String -> Annex ()

View file

@ -10,6 +10,8 @@ module Logs.Trust (
trustGet, trustGet,
trustSet, trustSet,
trustPartition, trustPartition,
readTrust,
lookupTrust,
) where ) where
import qualified Data.Map as M import qualified Data.Map as M
@ -44,6 +46,10 @@ trustSet uuid@(UUID _) level = do
Annex.changeState $ \s -> s { Annex.trustmap = Nothing } Annex.changeState $ \s -> s { Annex.trustmap = Nothing }
trustSet NoUUID _ = error "unknown UUID; cannot modify trust level" trustSet NoUUID _ = error "unknown UUID; cannot modify trust level"
{- 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. -} {- Partitions a list of UUIDs to those matching a TrustLevel and not. -}
trustPartition :: TrustLevel -> [UUID] -> Annex ([UUID], [UUID]) trustPartition :: TrustLevel -> [UUID] -> Annex ([UUID], [UUID])
trustPartition level ls trustPartition level ls
@ -76,12 +82,15 @@ trustMap = do
where where
configuredtrust r = configuredtrust r =
maybe Nothing (\l -> Just (Types.Remote.uuid r, l)) <$> maybe Nothing (\l -> Just (Types.Remote.uuid r, l)) <$>
maybe Nothing convert <$> maybe Nothing readTrust <$>
getTrustLevel (Types.Remote.repo r) getTrustLevel (Types.Remote.repo r)
convert "trusted" = Just Trusted
convert "untrusted" = Just UnTrusted readTrust :: String -> Maybe TrustLevel
convert "semitrusted" = Just SemiTrusted readTrust "trusted" = Just Trusted
convert _ = Nothing readTrust "untrusted" = Just UnTrusted
readTrust "semitrusted" = Just SemiTrusted
readTrust "dead" = Just DeadTrusted -- NEW CASE
readTrust _ = Nothing
{- The trust.log used to only list trusted repos, without a field for the {- The trust.log used to only list trusted repos, without a field for the
- trust status, which is why this defaults to Trusted. -} - trust status, which is why this defaults to Trusted. -}