2013-03-29 20:17:13 +00:00
|
|
|
{- git-annex file matching
|
|
|
|
-
|
|
|
|
- Copyright 2012, 2013 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- 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
|
2013-10-28 18:05:55 +00:00
|
|
|
import Types.Limit
|
2013-03-29 20:17:13 +00:00
|
|
|
import Logs.Group
|
2013-04-26 03:44:55 +00:00
|
|
|
import Logs.Remote
|
2013-03-29 20:17:13 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
type FileMatcher = Matcher MatchFiles
|
|
|
|
|
|
|
|
checkFileMatcher :: FileMatcher -> FilePath -> Annex Bool
|
|
|
|
checkFileMatcher matcher file = checkFileMatcher' matcher file S.empty True
|
|
|
|
|
|
|
|
checkFileMatcher' :: FileMatcher -> FilePath -> AssumeNotPresent -> Bool -> Annex Bool
|
|
|
|
checkFileMatcher' matcher file notpresent def
|
|
|
|
| isEmpty matcher = return def
|
|
|
|
| otherwise = do
|
|
|
|
matchfile <- getTopFilePath <$> inRepo (toTopFilePath file)
|
2014-01-18 18:51:55 +00:00
|
|
|
let mi = MatchingFile $ FileInfo
|
2013-05-25 03:07:26 +00:00
|
|
|
{ matchFile = matchfile
|
|
|
|
, relFile = file
|
2013-03-29 20:17:13 +00:00
|
|
|
}
|
2014-01-18 18:51:55 +00:00
|
|
|
matchMrun matcher $ \a -> a notpresent mi
|
2013-03-29 20:17:13 +00:00
|
|
|
|
|
|
|
matchAll :: FileMatcher
|
|
|
|
matchAll = generate []
|
|
|
|
|
|
|
|
parsedToMatcher :: [Either String (Token MatchFiles)] -> Either String FileMatcher
|
|
|
|
parsedToMatcher parsed = case partitionEithers parsed of
|
|
|
|
([], vs) -> Right $ generate vs
|
|
|
|
(es, _) -> Left $ unwords $ map ("Parse failure: " ++) es
|
|
|
|
|
2013-04-26 03:44:55 +00:00
|
|
|
exprParser :: GroupMap -> M.Map UUID RemoteConfig -> Maybe UUID -> String -> [Either String (Token MatchFiles)]
|
|
|
|
exprParser groupmap configmap mu expr =
|
|
|
|
map parse $ tokenizeMatcher expr
|
|
|
|
where
|
|
|
|
parse = parseToken
|
|
|
|
(limitPresent mu)
|
|
|
|
(limitInDir preferreddir)
|
|
|
|
groupmap
|
|
|
|
preferreddir = fromMaybe "public" $
|
|
|
|
M.lookup "preferreddir" =<< (`M.lookup` configmap) =<< mu
|
|
|
|
|
|
|
|
parseToken :: MkLimit -> MkLimit -> GroupMap -> String -> Either String (Token MatchFiles)
|
|
|
|
parseToken checkpresent checkpreferreddir groupmap t
|
2013-04-03 07:52:41 +00:00
|
|
|
| t `elem` tokens = Right $ token t
|
2013-03-29 20:17:13 +00:00
|
|
|
| t == "present" = use checkpresent
|
2013-04-26 03:44:55 +00:00
|
|
|
| t == "inpreferreddir" = use checkpreferreddir
|
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)
|
Add and use numcopiesneeded preferred content expression.
* Add numcopiesneeded preferred content expression.
* Client, transfer, incremental backup, and archive repositories
now want to get content that does not yet have enough copies.
This means the asssistant will make copies of files that don't yet
meet the configured numcopies, even to places that would not normally want
the file.
For example, if numcopies is 4, and there are 2 client repos and
2 transfer repos, and 2 removable backup drives, the file will be sent
to both transfer repos in order to make 4 copies. Once a removable drive
get a copy of the file, it will be dropped from one transfer repo or the
other (but not both).
Another example, numcopies is 3 and there is a client that has a backup
removable drive and two small archive repos. Normally once one of the small
archives has a file, it will not be put into the other one. But, to satisfy
numcopies, the assistant will duplicate it into the other small archive
too, if the backup repo is not available to receive the file.
I notice that these examples are fairly unlikely setups .. the old behavior
was not too bad, but it's nice to finally have it really correct.
.. Almost. I have skipped checking the annex.numcopies .gitattributes
out of fear it will be too slow.
This commit was sponsored by Florian Schlegel.
2014-01-20 21:34:58 +00:00
|
|
|
, ("numcopiesneeded", limitNumCopiesNeeded)
|
2013-03-29 20:17:13 +00:00
|
|
|
, ("inbackend", limitInBackend)
|
|
|
|
, ("largerthan", limitSize (>))
|
|
|
|
, ("smallerthan", limitSize (<))
|
|
|
|
, ("inallgroup", limitInAllGroup groupmap)
|
|
|
|
]
|
|
|
|
where
|
|
|
|
(k, v) = separate (== '=') t
|
2013-04-03 07:52:41 +00:00
|
|
|
use a = Operation <$> a v
|
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. -}
|
|
|
|
largeFilesMatcher :: Annex FileMatcher
|
|
|
|
largeFilesMatcher = go =<< annexLargeFiles <$> Annex.getGitConfig
|
|
|
|
where
|
2013-04-03 07:52:41 +00:00
|
|
|
go Nothing = return matchAll
|
2013-03-29 20:17:13 +00:00
|
|
|
go (Just expr) = do
|
2013-04-26 03:44:55 +00:00
|
|
|
gm <- groupMap
|
|
|
|
rc <- readRemoteLog
|
2013-03-29 20:17:13 +00:00
|
|
|
u <- getUUID
|
2013-04-26 03:44:55 +00:00
|
|
|
either badexpr return $
|
|
|
|
parsedToMatcher $ exprParser gm rc (Just u) expr
|
2013-03-29 20:17:13 +00:00
|
|
|
badexpr e = error $ "bad annex.largefiles configuration: " ++ e
|