2013-03-29 20:17:13 +00:00
|
|
|
{- git-annex file matching
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012-2014 Joey Hess <id@joeyh.name>
|
2013-03-29 20:17:13 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.FileMatcher where
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Limit
|
|
|
|
import Utility.Matcher
|
|
|
|
import Types.Group
|
|
|
|
import Logs.Group
|
|
|
|
import Annex.UUID
|
|
|
|
import qualified Annex
|
2013-05-25 03:07:26 +00:00
|
|
|
import Types.FileMatcher
|
2013-03-29 20:17:13 +00:00
|
|
|
import Git.FilePath
|
2013-04-26 03:44:55 +00:00
|
|
|
import Types.Remote (RemoteConfig)
|
2013-03-29 20:17:13 +00:00
|
|
|
|
|
|
|
import Data.Either
|
|
|
|
import qualified Data.Set as S
|
|
|
|
|
2015-04-11 04:10:34 +00:00
|
|
|
checkFileMatcher :: FileMatcher Annex -> FilePath -> Annex Bool
|
2014-01-23 20:37:08 +00:00
|
|
|
checkFileMatcher matcher file = checkMatcher matcher Nothing (Just file) S.empty True
|
2013-03-29 20:17:13 +00:00
|
|
|
|
2015-04-11 04:10:34 +00:00
|
|
|
checkMatcher :: FileMatcher Annex -> Maybe Key -> AssociatedFile -> AssumeNotPresent -> Bool -> Annex Bool
|
2015-01-28 20:11:28 +00:00
|
|
|
checkMatcher matcher mkey afile notpresent d
|
|
|
|
| isEmpty matcher = return d
|
2014-01-23 20:37:08 +00:00
|
|
|
| otherwise = case (mkey, afile) of
|
|
|
|
(_, Just file) -> go =<< fileMatchInfo file
|
|
|
|
(Just key, _) -> go (MatchingKey key)
|
2015-01-28 20:11:28 +00:00
|
|
|
_ -> return d
|
2014-01-23 20:37:08 +00:00
|
|
|
where
|
|
|
|
go mi = matchMrun matcher $ \a -> a notpresent mi
|
|
|
|
|
|
|
|
fileMatchInfo :: FilePath -> Annex MatchInfo
|
|
|
|
fileMatchInfo file = do
|
|
|
|
matchfile <- getTopFilePath <$> inRepo (toTopFilePath file)
|
2014-02-11 04:39:50 +00:00
|
|
|
return $ MatchingFile FileInfo
|
2014-01-23 20:37:08 +00:00
|
|
|
{ matchFile = matchfile
|
2015-02-06 20:03:02 +00:00
|
|
|
, currFile = file
|
2014-01-23 20:37:08 +00:00
|
|
|
}
|
2013-03-29 20:17:13 +00:00
|
|
|
|
2014-03-29 18:43:34 +00:00
|
|
|
matchAll :: FileMatcher Annex
|
2013-03-29 20:17:13 +00:00
|
|
|
matchAll = generate []
|
|
|
|
|
2014-03-29 18:43:34 +00:00
|
|
|
parsedToMatcher :: [Either String (Token (MatchFiles Annex))] -> Either String (FileMatcher Annex)
|
2013-03-29 20:17:13 +00:00
|
|
|
parsedToMatcher parsed = case partitionEithers parsed of
|
|
|
|
([], vs) -> Right $ generate vs
|
|
|
|
(es, _) -> Left $ unwords $ map ("Parse failure: " ++) es
|
|
|
|
|
2015-12-04 18:57:28 +00:00
|
|
|
exprParser :: FileMatcher Annex -> FileMatcher Annex -> Annex GroupMap -> M.Map UUID RemoteConfig -> Maybe UUID -> String -> [Either String (Token (MatchFiles Annex))]
|
|
|
|
exprParser matchstandard matchgroupwanted getgroupmap configmap mu expr =
|
2013-04-26 03:44:55 +00:00
|
|
|
map parse $ tokenizeMatcher expr
|
|
|
|
where
|
2014-03-14 19:04:33 +00:00
|
|
|
parse = parseToken
|
|
|
|
matchstandard
|
2014-03-15 21:08:55 +00:00
|
|
|
matchgroupwanted
|
2013-04-26 03:44:55 +00:00
|
|
|
(limitPresent mu)
|
|
|
|
(limitInDir preferreddir)
|
2015-12-04 18:57:28 +00:00
|
|
|
getgroupmap
|
2013-04-26 03:44:55 +00:00
|
|
|
preferreddir = fromMaybe "public" $
|
|
|
|
M.lookup "preferreddir" =<< (`M.lookup` configmap) =<< mu
|
|
|
|
|
2015-12-04 18:57:28 +00:00
|
|
|
parseToken :: FileMatcher Annex -> FileMatcher Annex -> MkLimit Annex -> MkLimit Annex -> Annex GroupMap -> String -> Either String (Token (MatchFiles Annex))
|
|
|
|
parseToken matchstandard matchgroupwanted checkpresent checkpreferreddir getgroupmap t
|
2013-04-03 07:52:41 +00:00
|
|
|
| t `elem` tokens = Right $ token t
|
2014-03-15 21:08:55 +00:00
|
|
|
| t == "standard" = call matchstandard
|
|
|
|
| t == "groupwanted" = call matchgroupwanted
|
2013-03-29 20:17:13 +00:00
|
|
|
| t == "present" = use checkpresent
|
2013-04-26 03:44:55 +00:00
|
|
|
| t == "inpreferreddir" = use checkpreferreddir
|
2014-03-14 19:04:33 +00:00
|
|
|
| t == "unused" = Right $ Operation limitUnused
|
2015-06-16 21:03:34 +00:00
|
|
|
| t == "anything" = Right $ Operation limitAnything
|
2013-03-29 20:17:13 +00:00
|
|
|
| otherwise = maybe (Left $ "near " ++ show t) use $ M.lookup k $
|
|
|
|
M.fromList
|
|
|
|
[ ("include", limitInclude)
|
|
|
|
, ("exclude", limitExclude)
|
|
|
|
, ("copies", limitCopies)
|
2014-01-21 22:46:39 +00:00
|
|
|
, ("lackingcopies", limitLackingCopies False)
|
|
|
|
, ("approxlackingcopies", limitLackingCopies True)
|
2013-03-29 20:17:13 +00:00
|
|
|
, ("inbackend", limitInBackend)
|
|
|
|
, ("largerthan", limitSize (>))
|
|
|
|
, ("smallerthan", limitSize (<))
|
2014-02-13 06:24:30 +00:00
|
|
|
, ("metadata", limitMetaData)
|
2015-12-04 18:57:28 +00:00
|
|
|
, ("inallgroup", limitInAllGroup getgroupmap)
|
2013-03-29 20:17:13 +00:00
|
|
|
]
|
|
|
|
where
|
|
|
|
(k, v) = separate (== '=') t
|
2013-04-03 07:52:41 +00:00
|
|
|
use a = Operation <$> a v
|
2014-03-15 21:08:55 +00:00
|
|
|
call sub = Right $ Operation $ \notpresent mi ->
|
|
|
|
matchMrun sub $ \a -> a notpresent mi
|
2013-03-29 20:17:13 +00:00
|
|
|
|
|
|
|
{- This is really dumb tokenization; there's no support for quoted values.
|
|
|
|
- Open and close parens are always treated as standalone tokens;
|
|
|
|
- otherwise tokens must be separated by whitespace. -}
|
|
|
|
tokenizeMatcher :: String -> [String]
|
|
|
|
tokenizeMatcher = filter (not . null ) . concatMap splitparens . words
|
|
|
|
where
|
|
|
|
splitparens = segmentDelim (`elem` "()")
|
|
|
|
|
|
|
|
{- Generates a matcher for files large enough (or meeting other criteria)
|
|
|
|
- to be added to the annex, rather than directly to git. -}
|
2014-03-29 18:43:34 +00:00
|
|
|
largeFilesMatcher :: Annex (FileMatcher Annex)
|
2013-03-29 20:17:13 +00:00
|
|
|
largeFilesMatcher = go =<< annexLargeFiles <$> Annex.getGitConfig
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
go Nothing = return matchAll
|
2013-03-29 20:17:13 +00:00
|
|
|
go (Just expr) = do
|
|
|
|
u <- getUUID
|
2015-12-04 18:57:28 +00:00
|
|
|
-- No need to read remote configs, that's only needed for
|
|
|
|
-- inpreferreddir, which is used in preferred content
|
|
|
|
-- expressions but does not make sense in the
|
|
|
|
-- annex.largefiles expression.
|
|
|
|
let emptyconfig = M.empty
|
2013-04-26 03:44:55 +00:00
|
|
|
either badexpr return $
|
2015-12-04 18:57:28 +00:00
|
|
|
parsedToMatcher $ exprParser matchAll matchAll groupMap emptyconfig (Just u) expr
|
2013-03-29 20:17:13 +00:00
|
|
|
badexpr e = error $ "bad annex.largefiles configuration: " ++ e
|