2011-09-18 21:47:49 +00:00
|
|
|
{- user-specified limits on files to act on
|
|
|
|
-
|
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
|
|
|
- Copyright 2011-2014 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
|
|
|
|
|
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
|
2014-01-21 22:08:56 +00:00
|
|
|
import Config.NumCopies
|
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
|
2013-05-25 03:07:26 +00:00
|
|
|
import Types.FileMatcher
|
2013-10-28 18:05:55 +00:00
|
|
|
import Types.Limit
|
2014-02-13 06:24:30 +00:00
|
|
|
import Types.MetaData
|
|
|
|
import Logs.MetaData
|
2012-10-01 22:25:11 +00:00
|
|
|
import Logs.Group
|
2014-01-22 20:35:32 +00:00
|
|
|
import Logs.Unused
|
2014-02-06 16:43:56 +00:00
|
|
|
import Logs.Location
|
|
|
|
import Git.Types (RefDate(..))
|
2014-02-21 22:34:34 +00:00
|
|
|
import Utility.Glob
|
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
|
|
|
|
2014-02-11 05:35:11 +00:00
|
|
|
import Data.Time.Clock.POSIX
|
|
|
|
import qualified Data.Set as S
|
|
|
|
import qualified Data.Map as M
|
2013-05-26 22:14:03 +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. -}
|
2014-01-18 18:51:55 +00:00
|
|
|
getMatcher :: Annex (MatchInfo -> Annex Bool)
|
2011-09-19 05:03:16 +00:00
|
|
|
getMatcher = Utility.Matcher.matchM <$> getMatcher'
|
2011-09-19 02:40:31 +00:00
|
|
|
|
2014-01-18 18:51:55 +00:00
|
|
|
getMatcher' :: Annex (Utility.Matcher.Matcher (MatchInfo -> 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)
|
2013-03-12 09:05:33 +00:00
|
|
|
Annex.changeState $ \s ->
|
|
|
|
s { Annex.limit = Right matcher }
|
2011-09-18 21:47:49 +00:00
|
|
|
return matcher
|
|
|
|
|
2011-09-18 22:21:42 +00:00
|
|
|
{- Adds something to the limit list, which is built up reversed. -}
|
2014-01-18 18:51:55 +00:00
|
|
|
add :: Utility.Matcher.Token (MatchInfo -> 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
|
2014-02-21 22:34:34 +00:00
|
|
|
limitInclude glob = Right $ const $ return . matchGlobFile 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
|
2014-02-21 22:34:34 +00:00
|
|
|
limitExclude glob = Right $ const $ return . not . matchGlobFile glob
|
|
|
|
|
|
|
|
matchGlobFile :: String -> (MatchInfo -> Bool)
|
|
|
|
matchGlobFile glob = go
|
|
|
|
where
|
|
|
|
cglob = compileGlob glob CaseSensative -- memoized
|
|
|
|
go (MatchingKey _) = False
|
|
|
|
go (MatchingFile fi) = matchGlob cglob (matchFile fi)
|
2011-09-19 00:14:18 +00:00
|
|
|
|
|
|
|
{- Adds a limit to skip files not believed to be present
|
2014-02-06 16:43:56 +00:00
|
|
|
- in a specfied repository. Optionally on a prior date. -}
|
2011-09-19 00:14:18 +00:00
|
|
|
addIn :: String -> Annex ()
|
2014-03-13 22:51:44 +00:00
|
|
|
addIn s = addLimit =<< mk
|
2012-10-29 02:09:09 +00:00
|
|
|
where
|
2014-02-06 16:43:56 +00:00
|
|
|
(name, date) = separate (== '@') s
|
2014-03-13 22:51:44 +00:00
|
|
|
mk
|
|
|
|
| name == "." = if null date
|
|
|
|
then use inhere
|
|
|
|
else use . inuuid =<< getUUID
|
|
|
|
| otherwise = use . inuuid =<< Remote.nameToUUID name
|
|
|
|
use a = return $ Right $ \notpresent -> checkKey (a notpresent)
|
|
|
|
inuuid u notpresent key
|
2014-02-06 16:43:56 +00:00
|
|
|
| null date = do
|
|
|
|
us <- Remote.keyLocations key
|
|
|
|
return $ u `elem` us && u `S.notMember` notpresent
|
|
|
|
| otherwise = do
|
|
|
|
us <- loggedLocationsHistorical (RefDate date) key
|
|
|
|
return $ u `elem` us
|
2012-10-29 02:09:09 +00:00
|
|
|
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
|
2014-01-18 18:51:55 +00:00
|
|
|
limitPresent u _ = Right $ const $ checkKey $ \key -> do
|
2012-10-19 20:09:21 +00:00
|
|
|
hereu <- getUUID
|
2013-04-03 07:52:41 +00:00
|
|
|
if u == Just hereu || isNothing u
|
2012-10-19 20:09:21 +00:00
|
|
|
then inAnnex key
|
|
|
|
else do
|
|
|
|
us <- Remote.keyLocations key
|
|
|
|
return $ maybe False (`elem` us) u
|
|
|
|
|
2013-04-26 03:44:55 +00:00
|
|
|
{- Limit to content that is in a directory, anywhere in the repository tree -}
|
|
|
|
limitInDir :: FilePath -> MkLimit
|
2014-01-18 18:51:55 +00:00
|
|
|
limitInDir dir = const $ Right $ const go
|
|
|
|
where
|
2014-02-11 05:35:11 +00:00
|
|
|
go (MatchingFile fi) = return $ elem dir $ splitPath $ takeDirectory $ matchFile fi
|
2014-01-18 18:51:55 +00:00
|
|
|
go (MatchingKey _) = return False
|
2013-04-26 03:44:55 +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
|
2013-04-03 03:40:13 +00:00
|
|
|
[v, n] -> case parsetrustspec v of
|
2013-04-03 21:44:34 +00:00
|
|
|
Just checker -> go n $ checktrust checker
|
2012-10-04 19:48:59 +00:00
|
|
|
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"
|
2014-01-18 18:51:55 +00:00
|
|
|
Just n -> Right $ \notpresent -> checkKey $
|
|
|
|
handle n good notpresent
|
|
|
|
handle n good notpresent key = do
|
2012-10-29 02:09:09 +00:00
|
|
|
us <- filter (`S.notMember` notpresent)
|
|
|
|
<$> (filterM good =<< Remote.keyLocations key)
|
|
|
|
return $ length us >= n
|
2013-04-03 21:44:34 +00:00
|
|
|
checktrust checker u = checker <$> lookupTrust u
|
2012-10-29 02:09:09 +00:00
|
|
|
checkgroup g u = S.member g <$> lookupGroups u
|
2013-04-03 03:40:13 +00:00
|
|
|
parsetrustspec s
|
|
|
|
| "+" `isSuffixOf` s = (>=) <$> readTrustLevel (beginning s)
|
|
|
|
| otherwise = (==) <$> readTrustLevel s
|
2011-11-28 21:37:15 +00:00
|
|
|
|
2014-01-21 22:46:39 +00:00
|
|
|
{- Adds a limit to match files that need more copies made. -}
|
|
|
|
addLackingCopies :: Bool -> String -> Annex ()
|
|
|
|
addLackingCopies approx = addLimit . limitLackingCopies approx
|
|
|
|
|
|
|
|
limitLackingCopies :: Bool -> MkLimit
|
|
|
|
limitLackingCopies approx want = case readish want of
|
|
|
|
Just needed -> Right $ \notpresent mi -> flip checkKey mi $
|
|
|
|
handle mi needed notpresent
|
|
|
|
Nothing -> Left "bad value for number of lacking copies"
|
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
|
|
|
where
|
2014-01-21 22:46:39 +00:00
|
|
|
handle mi needed notpresent key = do
|
|
|
|
NumCopies numcopies <- if approx
|
|
|
|
then approxNumCopies
|
|
|
|
else case mi of
|
|
|
|
MatchingKey _ -> approxNumCopies
|
|
|
|
MatchingFile fi -> getGlobalFileNumCopies $ matchFile fi
|
|
|
|
us <- filter (`S.notMember` notpresent)
|
|
|
|
<$> (trustExclude UnTrusted =<< Remote.keyLocations key)
|
|
|
|
return $ numcopies - length us >= needed
|
|
|
|
approxNumCopies = fromMaybe defaultNumCopies <$> getGlobalNumCopies
|
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
|
|
|
|
2014-01-22 20:35:32 +00:00
|
|
|
{- Match keys that are unused.
|
|
|
|
-
|
|
|
|
- This has a nice optimisation: When a file exists,
|
|
|
|
- its key is obviously not unused.
|
|
|
|
-}
|
|
|
|
limitUnused :: MatchFiles
|
|
|
|
limitUnused _ (MatchingFile _) = return False
|
|
|
|
limitUnused _ (MatchingKey k) = S.member k <$> unusedKeys
|
|
|
|
|
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
|
2014-01-18 18:51:55 +00:00
|
|
|
| otherwise = Right $ \notpresent -> checkKey $ check notpresent
|
2012-10-29 02:09:09 +00:00
|
|
|
where
|
|
|
|
want = fromMaybe S.empty $ M.lookup groupname $ uuidsByGroup m
|
2014-01-18 18:51:55 +00:00
|
|
|
check notpresent key
|
2012-10-29 02:09:09 +00:00
|
|
|
-- 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
|
2014-01-18 18:51:55 +00:00
|
|
|
limitInBackend name = Right $ const $ checkKey check
|
2012-10-29 02:09:09 +00:00
|
|
|
where
|
2014-01-18 18:51:55 +00:00
|
|
|
check key = pure $ keyBackendName key == name
|
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"
|
2013-03-29 20:17:13 +00:00
|
|
|
Just sz -> Right $ go sz
|
2012-10-29 02:09:09 +00:00
|
|
|
where
|
2014-01-18 18:51:55 +00:00
|
|
|
go sz _ (MatchingFile fi) = lookupFile fi >>= check fi sz
|
|
|
|
go sz _ (MatchingKey key) = checkkey sz key
|
|
|
|
checkkey sz key = return $ keySize key `vs` Just sz
|
|
|
|
check _ sz (Just (key, _)) = checkkey sz key
|
2013-03-29 20:17:13 +00:00
|
|
|
check fi sz Nothing = do
|
|
|
|
filesize <- liftIO $ catchMaybeIO $
|
|
|
|
fromIntegral . fileSize
|
2013-05-25 03:07:26 +00:00
|
|
|
<$> getFileStatus (relFile fi)
|
2013-03-29 20:17:13 +00:00
|
|
|
return $ filesize `vs` Just sz
|
2012-10-08 17:39:18 +00:00
|
|
|
|
2014-02-13 06:24:30 +00:00
|
|
|
addMetaData :: String -> Annex ()
|
|
|
|
addMetaData = addLimit . limitMetaData
|
|
|
|
|
|
|
|
limitMetaData :: MkLimit
|
|
|
|
limitMetaData s = case parseMetaData s of
|
|
|
|
Left e -> Left e
|
2014-02-21 22:34:34 +00:00
|
|
|
Right (f, v) ->
|
|
|
|
let cglob = compileGlob (fromMetaValue v) CaseInsensative
|
|
|
|
in Right $ const $ checkKey (check f cglob)
|
2014-02-13 06:24:30 +00:00
|
|
|
where
|
2014-02-21 22:34:34 +00:00
|
|
|
check f cglob k = not . S.null
|
|
|
|
. S.filter (matchGlob cglob . fromMetaValue)
|
|
|
|
. metaDataValues f <$> getCurrentMetaData k
|
2014-02-13 06:24:30 +00:00
|
|
|
|
2012-09-25 20:48:24 +00:00
|
|
|
addTimeLimit :: String -> Annex ()
|
|
|
|
addTimeLimit s = do
|
2013-10-08 21:36:55 +00:00
|
|
|
let seconds = maybe (error "bad time-limit") durationToPOSIXTime $
|
|
|
|
parseDuration s
|
2012-09-25 20:48:24 +00:00
|
|
|
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
|
|
|
|
2013-05-25 03:07:26 +00:00
|
|
|
lookupFile :: FileInfo -> Annex (Maybe (Key, Backend))
|
|
|
|
lookupFile = Backend.lookupFile . relFile
|
2014-01-18 18:51:55 +00:00
|
|
|
|
|
|
|
lookupFileKey :: FileInfo -> Annex (Maybe Key)
|
|
|
|
lookupFileKey = (fst <$>) <$$> Backend.lookupFile . relFile
|
|
|
|
|
|
|
|
checkKey :: (Key -> Annex Bool) -> MatchInfo -> Annex Bool
|
|
|
|
checkKey a (MatchingFile fi) = lookupFileKey fi >>= maybe (return False) a
|
|
|
|
checkKey a (MatchingKey k) = a k
|