2011-09-18 21:47:49 +00:00
|
|
|
{- user-specified limits on files to act on
|
|
|
|
-
|
2012-10-04 19:48:59 +00:00
|
|
|
- Copyright 2011,2012 Joey Hess <joey@kitenet.net>
|
2011-09-18 21:47:49 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Limit where
|
|
|
|
|
|
|
|
import Text.Regex.PCRE.Light.Char8
|
|
|
|
import System.Path.WildMatch
|
2012-09-25 20:48:24 +00:00
|
|
|
import Data.Time.Clock.POSIX
|
2012-10-01 22:25:11 +00:00
|
|
|
import qualified Data.Set as S
|
2012-10-08 19:18:58 +00:00
|
|
|
import qualified Data.Map as M
|
2011-09-18 21:47:49 +00:00
|
|
|
|
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-10-05 20:52:44 +00:00
|
|
|
import Annex.UUID
|
2012-09-23 17:50:31 +00:00
|
|
|
import Logs.Trust
|
2012-10-03 21:04:52 +00:00
|
|
|
import Types.TrustLevel
|
2012-10-08 17:39:18 +00:00
|
|
|
import Types.Key
|
2012-10-08 19:18:58 +00:00
|
|
|
import Types.Group
|
2012-10-01 22:25:11 +00:00
|
|
|
import Logs.Group
|
2012-09-25 20:48:24 +00:00
|
|
|
import Utility.HumanTime
|
2012-10-08 17:39:18 +00:00
|
|
|
import Utility.DataUnits
|
2011-09-18 21:47:49 +00:00
|
|
|
|
2012-10-17 20:01:09 +00:00
|
|
|
type MatchFiles = AssumeNotPresent -> Annex.FileInfo -> Annex Bool
|
2012-10-05 20:52:44 +00:00
|
|
|
type MkLimit = String -> Either String MatchFiles
|
|
|
|
type AssumeNotPresent = S.Set UUID
|
2011-09-18 21:47:49 +00:00
|
|
|
|
2011-09-19 00:41:51 +00:00
|
|
|
{- Checks if there are user-specified limits. -}
|
|
|
|
limited :: Annex Bool
|
2012-12-06 17:22:16 +00:00
|
|
|
limited = (not . Utility.Matcher.isEmpty) <$> 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. -}
|
2012-10-17 20:01:09 +00:00
|
|
|
getMatcher :: Annex (Annex.FileInfo -> Annex Bool)
|
2011-09-19 05:03:16 +00:00
|
|
|
getMatcher = Utility.Matcher.matchM <$> getMatcher'
|
2011-09-19 02:40:31 +00:00
|
|
|
|
2012-10-17 20:01:09 +00:00
|
|
|
getMatcher' :: Annex (Utility.Matcher.Matcher (Annex.FileInfo -> Annex Bool))
|
2011-09-19 02:40:31 +00:00
|
|
|
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. -}
|
2012-10-17 20:01:09 +00:00
|
|
|
add :: Utility.Matcher.Token (Annex.FileInfo -> Annex Bool) -> Annex ()
|
2011-09-19 05:57:12 +00:00
|
|
|
add l = Annex.changeState $ \s -> s { Annex.limit = prepend $ Annex.limit s }
|
2012-10-29 02:09:09 +00:00
|
|
|
where
|
|
|
|
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. -}
|
2012-10-05 20:52:44 +00:00
|
|
|
addLimit :: Either String MatchFiles -> Annex ()
|
|
|
|
addLimit = either error (\l -> add $ Utility.Matcher.Operation $ l S.empty)
|
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 ()
|
2012-10-04 19:48:59 +00:00
|
|
|
addInclude = addLimit . limitInclude
|
|
|
|
|
|
|
|
limitInclude :: MkLimit
|
2012-10-05 20:52:44 +00:00
|
|
|
limitInclude glob = Right $ const $ return . matchglob glob
|
2011-12-22 17:53:06 +00:00
|
|
|
|
|
|
|
{- Add a limit to skip files that match the glob. -}
|
2011-09-19 00:14:18 +00:00
|
|
|
addExclude :: String -> Annex ()
|
2012-10-04 19:48:59 +00:00
|
|
|
addExclude = addLimit . limitExclude
|
|
|
|
|
|
|
|
limitExclude :: MkLimit
|
2012-10-05 20:52:44 +00:00
|
|
|
limitExclude glob = Right $ const $ return . not . matchglob glob
|
2011-12-22 17:53:06 +00:00
|
|
|
|
2012-10-17 20:01:09 +00:00
|
|
|
matchglob :: String -> Annex.FileInfo -> Bool
|
|
|
|
matchglob glob (Annex.FileInfo { Annex.matchFile = f }) =
|
|
|
|
isJust $ match cregex f []
|
2012-10-29 02:09:09 +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 ()
|
2012-10-04 19:48:59 +00:00
|
|
|
addIn = addLimit . limitIn
|
|
|
|
|
|
|
|
limitIn :: MkLimit
|
2012-10-05 20:52:44 +00:00
|
|
|
limitIn name = Right $ \notpresent -> check $
|
|
|
|
if name == "."
|
|
|
|
then inhere notpresent
|
|
|
|
else inremote notpresent
|
2012-10-29 02:09:09 +00:00
|
|
|
where
|
|
|
|
check a = lookupFile >=> handle a
|
|
|
|
handle _ Nothing = return False
|
|
|
|
handle a (Just (key, _)) = a key
|
|
|
|
inremote notpresent key = do
|
|
|
|
u <- Remote.nameToUUID name
|
|
|
|
us <- Remote.keyLocations key
|
|
|
|
return $ u `elem` us && u `S.notMember` notpresent
|
|
|
|
inhere notpresent key
|
|
|
|
| S.null notpresent = inAnnex key
|
|
|
|
| otherwise = do
|
|
|
|
u <- getUUID
|
|
|
|
if u `S.member` notpresent
|
|
|
|
then return False
|
|
|
|
else inAnnex key
|
2011-09-19 00:23:08 +00:00
|
|
|
|
2012-10-19 20:09:21 +00:00
|
|
|
{- Limit to content that is currently present on a uuid. -}
|
|
|
|
limitPresent :: Maybe UUID -> MkLimit
|
2012-10-20 19:30:11 +00:00
|
|
|
limitPresent u _ = Right $ const $ check $ \key -> do
|
2012-10-19 20:09:21 +00:00
|
|
|
hereu <- getUUID
|
|
|
|
if u == Just hereu || u == Nothing
|
|
|
|
then inAnnex key
|
|
|
|
else do
|
|
|
|
us <- Remote.keyLocations key
|
|
|
|
return $ maybe False (`elem` us) u
|
2012-10-29 02:09:09 +00:00
|
|
|
where
|
|
|
|
check a = lookupFile >=> handle a
|
|
|
|
handle _ Nothing = return False
|
|
|
|
handle a (Just (key, _)) = a key
|
2012-10-19 20:09:21 +00:00
|
|
|
|
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-10-04 19:48:59 +00:00
|
|
|
addCopies = addLimit . limitCopies
|
|
|
|
|
|
|
|
limitCopies :: MkLimit
|
|
|
|
limitCopies want = case split ":" want of
|
|
|
|
[v, n] -> case readTrustLevel v of
|
|
|
|
Just trust -> go n $ checktrust trust
|
|
|
|
Nothing -> go n $ checkgroup v
|
|
|
|
[n] -> go n $ const $ return True
|
|
|
|
_ -> Left "bad value for copies"
|
2012-10-29 02:09:09 +00:00
|
|
|
where
|
|
|
|
go num good = case readish num of
|
|
|
|
Nothing -> Left "bad number for copies"
|
|
|
|
Just n -> Right $ \notpresent f ->
|
|
|
|
lookupFile f >>= handle n good notpresent
|
|
|
|
handle _ _ _ Nothing = return False
|
|
|
|
handle n good notpresent (Just (key, _)) = do
|
|
|
|
us <- filter (`S.notMember` notpresent)
|
|
|
|
<$> (filterM good =<< Remote.keyLocations key)
|
|
|
|
return $ length us >= n
|
|
|
|
checktrust t u = (== t) <$> lookupTrust u
|
|
|
|
checkgroup g u = S.member g <$> lookupGroups u
|
2011-11-28 21:37:15 +00:00
|
|
|
|
2012-10-08 19:18:58 +00:00
|
|
|
{- Adds a limit to skip files not believed to be present in all
|
|
|
|
- repositories in the specified group. -}
|
2012-10-10 16:59:45 +00:00
|
|
|
addInAllGroup :: String -> Annex ()
|
|
|
|
addInAllGroup groupname = do
|
2012-10-08 19:18:58 +00:00
|
|
|
m <- groupMap
|
2012-10-10 16:59:45 +00:00
|
|
|
addLimit $ limitInAllGroup m groupname
|
2012-10-08 19:18:58 +00:00
|
|
|
|
2012-10-10 16:59:45 +00:00
|
|
|
limitInAllGroup :: GroupMap -> MkLimit
|
|
|
|
limitInAllGroup m groupname
|
2012-10-08 19:18:58 +00:00
|
|
|
| S.null want = Right $ const $ const $ return True
|
2012-10-17 20:01:09 +00:00
|
|
|
| otherwise = Right $ \notpresent -> lookupFile >=> check notpresent
|
2012-10-29 02:09:09 +00:00
|
|
|
where
|
|
|
|
want = fromMaybe S.empty $ M.lookup groupname $ uuidsByGroup m
|
|
|
|
check _ Nothing = return False
|
|
|
|
check notpresent (Just (key, _))
|
|
|
|
-- optimisation: Check if a wanted uuid is notpresent.
|
|
|
|
| not (S.null (S.intersection want notpresent)) = return False
|
|
|
|
| otherwise = do
|
|
|
|
present <- S.fromList <$> Remote.keyLocations key
|
|
|
|
return $ S.null $ want `S.difference` present
|
2012-10-08 19:18:58 +00:00
|
|
|
|
2011-11-28 21:37:15 +00:00
|
|
|
{- Adds a limit to skip files not using a specified key-value backend. -}
|
|
|
|
addInBackend :: String -> Annex ()
|
2012-10-04 19:48:59 +00:00
|
|
|
addInBackend = addLimit . limitInBackend
|
|
|
|
|
|
|
|
limitInBackend :: MkLimit
|
2012-10-17 20:01:09 +00:00
|
|
|
limitInBackend name = Right $ const $ lookupFile >=> check
|
2012-10-29 02:09:09 +00:00
|
|
|
where
|
|
|
|
wanted = Backend.lookupBackendName name
|
|
|
|
check = return . maybe False ((==) wanted . snd)
|
2012-09-25 20:48:24 +00:00
|
|
|
|
2012-10-08 17:39:18 +00:00
|
|
|
{- Adds a limit to skip files that are too large or too small -}
|
|
|
|
addLargerThan :: String -> Annex ()
|
|
|
|
addLargerThan = addLimit . limitSize (>)
|
|
|
|
|
|
|
|
addSmallerThan :: String -> Annex ()
|
|
|
|
addSmallerThan = addLimit . limitSize (<)
|
|
|
|
|
|
|
|
limitSize :: (Maybe Integer -> Maybe Integer -> Bool) -> MkLimit
|
|
|
|
limitSize vs s = case readSize dataUnits s of
|
|
|
|
Nothing -> Left "bad size"
|
2012-10-17 20:01:09 +00:00
|
|
|
Just sz -> Right $ const $ lookupFile >=> check sz
|
2012-10-29 02:09:09 +00:00
|
|
|
where
|
|
|
|
check _ Nothing = return False
|
|
|
|
check sz (Just (key, _)) = return $ keySize key `vs` Just sz
|
2012-10-08 17:39:18 +00:00
|
|
|
|
2012-09-25 20:48:24 +00:00
|
|
|
addTimeLimit :: String -> Annex ()
|
|
|
|
addTimeLimit s = do
|
|
|
|
let seconds = fromMaybe (error "bad time-limit") $ parseDuration s
|
|
|
|
start <- liftIO getPOSIXTime
|
|
|
|
let cutoff = start + seconds
|
2012-10-05 20:52:44 +00:00
|
|
|
addLimit $ Right $ const $ const $ do
|
2012-09-25 20:48:24 +00:00
|
|
|
now <- liftIO getPOSIXTime
|
|
|
|
if now > cutoff
|
|
|
|
then do
|
|
|
|
warning $ "Time limit (" ++ s ++ ") reached!"
|
|
|
|
liftIO $ exitWith $ ExitFailure 101
|
|
|
|
else return True
|
2012-10-17 20:01:09 +00:00
|
|
|
|
|
|
|
lookupFile :: Annex.FileInfo -> Annex (Maybe (Key, Backend))
|
|
|
|
lookupFile = Backend.lookupFile . Annex.relFile
|