2011-09-18 21:47:49 +00:00
|
|
|
{- user-specified limits on files to act on
|
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Limit where
|
|
|
|
|
|
|
|
import Text.Regex.PCRE.Light.Char8
|
|
|
|
import System.Path.WildMatch
|
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2011-10-04 02:24:57 +00:00
|
|
|
import qualified Annex
|
2011-09-18 21:47:49 +00:00
|
|
|
import qualified Utility.Matcher
|
2011-09-19 00:14:18 +00:00
|
|
|
import qualified Remote
|
|
|
|
import qualified Backend
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2012-09-23 17:50:31 +00:00
|
|
|
import Logs.Trust
|
2011-09-18 21:47:49 +00:00
|
|
|
|
|
|
|
type Limit = Utility.Matcher.Token (FilePath -> Annex Bool)
|
|
|
|
|
2011-09-19 00:41:51 +00:00
|
|
|
{- Checks if there are user-specified limits. -}
|
|
|
|
limited :: Annex Bool
|
2011-09-19 02:40:31 +00:00
|
|
|
limited = (not . Utility.Matcher.matchesAny) <$> getMatcher'
|
2011-09-19 00:41:51 +00:00
|
|
|
|
2011-09-18 21:47:49 +00:00
|
|
|
{- Gets a matcher for the user-specified limits. The matcher is cached for
|
|
|
|
- speed; once it's obtained the user-specified limits can't change. -}
|
2011-09-19 02:40:31 +00:00
|
|
|
getMatcher :: Annex (FilePath -> Annex Bool)
|
2011-09-19 05:03:16 +00:00
|
|
|
getMatcher = Utility.Matcher.matchM <$> getMatcher'
|
2011-09-19 02:40:31 +00:00
|
|
|
|
|
|
|
getMatcher' :: Annex (Utility.Matcher.Matcher (FilePath -> Annex Bool))
|
|
|
|
getMatcher' = do
|
2011-09-18 21:47:49 +00:00
|
|
|
m <- Annex.getState Annex.limit
|
|
|
|
case m of
|
|
|
|
Right r -> return r
|
|
|
|
Left l -> do
|
|
|
|
let matcher = Utility.Matcher.generate (reverse l)
|
|
|
|
Annex.changeState $ \s -> s { Annex.limit = Right matcher }
|
|
|
|
return matcher
|
|
|
|
|
2011-09-18 22:21:42 +00:00
|
|
|
{- Adds something to the limit list, which is built up reversed. -}
|
2011-09-18 21:47:49 +00:00
|
|
|
add :: Limit -> Annex ()
|
2011-09-19 05:57:12 +00:00
|
|
|
add l = Annex.changeState $ \s -> s { Annex.limit = prepend $ Annex.limit s }
|
2011-09-18 21:47:49 +00:00
|
|
|
where
|
2011-09-19 05:57:12 +00:00
|
|
|
prepend (Left ls) = Left $ l:ls
|
|
|
|
prepend _ = error "internal"
|
2011-09-18 21:47:49 +00:00
|
|
|
|
|
|
|
{- Adds a new token. -}
|
2011-09-20 04:49:40 +00:00
|
|
|
addToken :: String -> Annex ()
|
|
|
|
addToken = add . Utility.Matcher.token
|
|
|
|
|
|
|
|
{- Adds a new limit. -}
|
|
|
|
addLimit :: (FilePath -> Annex Bool) -> Annex ()
|
|
|
|
addLimit = add . Utility.Matcher.Operation
|
2011-09-18 21:47:49 +00:00
|
|
|
|
|
|
|
{- Add a limit to skip files that do not match the glob. -}
|
2011-12-22 17:53:06 +00:00
|
|
|
addInclude :: String -> Annex ()
|
|
|
|
addInclude glob = addLimit $ return . matchglob glob
|
|
|
|
|
|
|
|
{- Add a limit to skip files that match the glob. -}
|
2011-09-19 00:14:18 +00:00
|
|
|
addExclude :: String -> Annex ()
|
2011-12-22 17:53:06 +00:00
|
|
|
addExclude glob = addLimit $ return . not . matchglob glob
|
|
|
|
|
|
|
|
matchglob :: String -> FilePath -> Bool
|
|
|
|
matchglob glob f = isJust $ match cregex f []
|
2011-09-18 21:47:49 +00:00
|
|
|
where
|
|
|
|
cregex = compile regex []
|
|
|
|
regex = '^':wildToRegex glob
|
2011-09-19 00:14:18 +00:00
|
|
|
|
|
|
|
{- Adds a limit to skip files not believed to be present
|
2011-09-19 05:57:12 +00:00
|
|
|
- in a specfied repository. -}
|
2011-09-19 00:14:18 +00:00
|
|
|
addIn :: String -> Annex ()
|
2011-10-11 18:43:45 +00:00
|
|
|
addIn name = addLimit $ check $ if name == "." then inAnnex else inremote
|
2011-09-19 00:14:18 +00:00
|
|
|
where
|
2011-11-01 03:39:55 +00:00
|
|
|
check a = Backend.lookupFile >=> handle a
|
2011-09-19 00:14:18 +00:00
|
|
|
handle _ Nothing = return False
|
2011-09-19 05:52:17 +00:00
|
|
|
handle a (Just (key, _)) = a key
|
2011-10-11 18:43:45 +00:00
|
|
|
inremote key = do
|
|
|
|
u <- Remote.nameToUUID name
|
2012-01-10 17:11:16 +00:00
|
|
|
us <- Remote.keyLocations key
|
2011-09-19 00:14:18 +00:00
|
|
|
return $ u `elem` us
|
2011-09-19 00:23:08 +00:00
|
|
|
|
|
|
|
{- Adds a limit to skip files not believed to have the specified number
|
|
|
|
- of copies. -}
|
|
|
|
addCopies :: String -> Annex ()
|
2012-09-23 17:50:31 +00:00
|
|
|
addCopies trust_num = addLimit . check $ readnum num
|
2012-09-23 19:42:05 +00:00
|
|
|
where
|
|
|
|
(num, mayCheckTrust) = case split ":" trust_num of
|
|
|
|
[trust, num'] -> (num', checkTrust (readtrust trust))
|
|
|
|
[num'] -> (num', const (return True))
|
|
|
|
_ -> bad
|
|
|
|
readnum = maybe bad id . readish
|
|
|
|
readtrust = maybe bad id . readTrust
|
2011-11-01 03:39:55 +00:00
|
|
|
check n = Backend.lookupFile >=> handle n
|
2011-09-19 00:23:08 +00:00
|
|
|
handle _ Nothing = return False
|
|
|
|
handle n (Just (key, _)) = do
|
2012-09-23 17:50:31 +00:00
|
|
|
us <- filterM mayCheckTrust =<< Remote.keyLocations key
|
2011-09-19 00:23:08 +00:00
|
|
|
return $ length us >= n
|
2012-09-23 19:42:05 +00:00
|
|
|
checkTrust t u = (== t) <$> lookupTrust u
|
|
|
|
bad = error "bad number or trust:number for --copies"
|
2011-11-28 21:37:15 +00:00
|
|
|
|
|
|
|
{- Adds a limit to skip files not using a specified key-value backend. -}
|
|
|
|
addInBackend :: String -> Annex ()
|
|
|
|
addInBackend name = addLimit $ Backend.lookupFile >=> check
|
|
|
|
where
|
|
|
|
wanted = Backend.lookupBackendName name
|
|
|
|
check = return . maybe False ((==) wanted . snd)
|