2014-01-19 21:35:36 +00:00
|
|
|
{- dropping of unwanted content
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012-2014 Joey Hess <id@joeyh.name>
|
2014-01-19 21:35:36 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2014-01-19 21:35:36 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.Drop where
|
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2016-01-26 18:28:43 +00:00
|
|
|
import qualified Annex
|
2014-01-19 21:35:36 +00:00
|
|
|
import Logs.Trust
|
2015-04-30 18:02:56 +00:00
|
|
|
import Annex.NumCopies
|
2018-12-18 17:58:12 +00:00
|
|
|
import Types.Remote (uuid, appendonly, config)
|
2014-01-19 21:35:36 +00:00
|
|
|
import qualified Remote
|
|
|
|
import qualified Command.Drop
|
|
|
|
import Command
|
|
|
|
import Annex.Wanted
|
|
|
|
import Config
|
|
|
|
import Annex.Content.Direct
|
2015-12-26 19:09:53 +00:00
|
|
|
import qualified Database.Keys
|
2016-01-05 21:22:19 +00:00
|
|
|
import Git.FilePath
|
2014-01-19 21:35:36 +00:00
|
|
|
|
|
|
|
import qualified Data.Set as S
|
|
|
|
import System.Log.Logger (debugM)
|
|
|
|
|
|
|
|
type Reason = String
|
|
|
|
|
|
|
|
{- Drop a key from local and/or remote when allowed by the preferred content
|
|
|
|
- and numcopies settings.
|
|
|
|
-
|
2018-08-30 15:23:57 +00:00
|
|
|
- Skips trying to drop from remotes that are appendonly, since those drops
|
2018-12-18 17:58:12 +00:00
|
|
|
- would presumably fail. Also skips dropping from exporttree remotes,
|
|
|
|
- which don't allow dropping individual keys.
|
2018-08-30 15:23:57 +00:00
|
|
|
-
|
2014-01-20 17:31:03 +00:00
|
|
|
- The UUIDs are ones where the content is believed to be present.
|
|
|
|
- The Remote list can include other remotes that do not have the content;
|
|
|
|
- only ones that match the UUIDs will be dropped from.
|
2015-10-14 16:33:02 +00:00
|
|
|
-
|
|
|
|
- If allowed to drop fromhere, that drop will be done last. This is done
|
|
|
|
- because local drops do not need any LockedCopy evidence, and so dropping
|
|
|
|
- from local last allows the content to be removed from more remotes.
|
2014-01-19 21:35:36 +00:00
|
|
|
-
|
2015-10-08 20:55:11 +00:00
|
|
|
- A VerifiedCopy can be provided as an optimisation when eg, a key
|
|
|
|
- has just been uploaded to a remote.
|
2014-01-19 21:35:36 +00:00
|
|
|
-
|
|
|
|
- In direct mode, all associated files are checked, and only if all
|
|
|
|
- of them are unwanted are they dropped.
|
2014-01-20 17:31:03 +00:00
|
|
|
-
|
|
|
|
- The runner is used to run commands, and so can be either callCommand
|
|
|
|
- or commandAction.
|
2014-01-19 21:35:36 +00:00
|
|
|
-}
|
2015-10-08 20:55:11 +00:00
|
|
|
handleDropsFrom :: [UUID] -> [Remote] -> Reason -> Bool -> Key -> AssociatedFile -> [VerifiedCopy] -> (CommandStart -> CommandCleanup) -> Annex ()
|
|
|
|
handleDropsFrom locs rs reason fromhere key afile preverified runner = do
|
2015-12-26 19:09:53 +00:00
|
|
|
l <- ifM isDirect
|
|
|
|
( associatedFilesRelative key
|
2016-01-26 18:28:43 +00:00
|
|
|
, do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
map (`fromTopFilePath` g) <$> Database.Keys.getAssociatedFiles key
|
2014-01-19 21:35:36 +00:00
|
|
|
)
|
2016-01-26 18:48:19 +00:00
|
|
|
let fs = case afile of
|
2017-03-10 17:12:24 +00:00
|
|
|
AssociatedFile (Just f) -> nub (f : l)
|
|
|
|
AssociatedFile Nothing -> l
|
2014-01-19 21:35:36 +00:00
|
|
|
n <- getcopies fs
|
2015-10-14 16:33:02 +00:00
|
|
|
void $ if fromhere && checkcopies n Nothing
|
2018-12-18 17:58:12 +00:00
|
|
|
then go fs rs n >>= dropl fs
|
|
|
|
else go fs rs n
|
2014-01-19 21:35:36 +00:00
|
|
|
where
|
|
|
|
getcopies fs = do
|
|
|
|
(untrusted, have) <- trustPartition UnTrusted locs
|
2014-01-23 20:37:08 +00:00
|
|
|
numcopies <- if null fs
|
|
|
|
then getNumCopies
|
|
|
|
else maximum <$> mapM getFileNumCopies fs
|
2014-01-21 20:08:19 +00:00
|
|
|
return (NumCopies (length have), numcopies, S.fromList untrusted)
|
2014-01-19 21:35:36 +00:00
|
|
|
|
|
|
|
{- Check that we have enough copies still to drop the content.
|
|
|
|
- When the remote being dropped from is untrusted, it was not
|
|
|
|
- counted as a copy, so having only numcopies suffices. Otherwise,
|
|
|
|
- we need more than numcopies to safely drop. -}
|
|
|
|
checkcopies (have, numcopies, _untrusted) Nothing = have > numcopies
|
|
|
|
checkcopies (have, numcopies, untrusted) (Just u)
|
|
|
|
| S.member u untrusted = have >= numcopies
|
|
|
|
| otherwise = have > numcopies
|
|
|
|
|
|
|
|
decrcopies (have, numcopies, untrusted) Nothing =
|
2014-01-21 20:08:19 +00:00
|
|
|
(NumCopies (fromNumCopies have - 1), numcopies, untrusted)
|
2014-01-19 21:35:36 +00:00
|
|
|
decrcopies v@(_have, _numcopies, untrusted) (Just u)
|
|
|
|
| S.member u untrusted = v
|
|
|
|
| otherwise = decrcopies v Nothing
|
|
|
|
|
2015-10-14 16:33:02 +00:00
|
|
|
go _ [] n = pure n
|
2014-01-19 21:35:36 +00:00
|
|
|
go fs (r:rest) n
|
|
|
|
| uuid r `S.notMember` slocs = go fs rest n
|
2018-12-18 17:58:12 +00:00
|
|
|
| appendonly r = go fs rest n
|
|
|
|
| exportTree (config r) = go fs rest n
|
2014-01-19 21:35:36 +00:00
|
|
|
| checkcopies n (Just $ Remote.uuid r) =
|
|
|
|
dropr fs r n >>= go fs rest
|
2015-10-14 16:33:02 +00:00
|
|
|
| otherwise = pure n
|
2014-01-19 21:35:36 +00:00
|
|
|
|
2014-01-23 20:37:08 +00:00
|
|
|
checkdrop fs n u a
|
|
|
|
| null fs = check $ -- no associated files; unused content
|
2017-03-10 17:12:24 +00:00
|
|
|
wantDrop True u (Just key) (AssociatedFile Nothing)
|
2014-01-23 20:37:08 +00:00
|
|
|
| otherwise = check $
|
2017-03-10 17:12:24 +00:00
|
|
|
allM (wantDrop True u (Just key) . AssociatedFile . Just) fs
|
2014-01-23 20:37:08 +00:00
|
|
|
where
|
|
|
|
check c = ifM c
|
|
|
|
( dodrop n u a
|
2014-01-19 21:35:36 +00:00
|
|
|
, return n
|
|
|
|
)
|
2014-01-23 20:37:08 +00:00
|
|
|
|
|
|
|
dodrop n@(have, numcopies, _untrusted) u a =
|
|
|
|
ifM (safely $ runner $ a numcopies)
|
|
|
|
( do
|
|
|
|
liftIO $ debugM "drop" $ unwords
|
|
|
|
[ "dropped"
|
2017-03-10 17:12:24 +00:00
|
|
|
, case afile of
|
2019-01-14 17:03:35 +00:00
|
|
|
AssociatedFile Nothing -> serializeKey key
|
2017-03-10 17:12:24 +00:00
|
|
|
AssociatedFile (Just af) -> af
|
2014-01-23 20:37:08 +00:00
|
|
|
, "(from " ++ maybe "here" show u ++ ")"
|
|
|
|
, "(copies now " ++ show (fromNumCopies have - 1) ++ ")"
|
|
|
|
, ": " ++ reason
|
|
|
|
]
|
|
|
|
return $ decrcopies n u
|
2014-01-19 21:35:36 +00:00
|
|
|
, return n
|
|
|
|
)
|
|
|
|
|
|
|
|
dropl fs n = checkdrop fs n Nothing $ \numcopies ->
|
2019-06-06 16:53:24 +00:00
|
|
|
Command.Drop.startLocal afile ai numcopies key preverified
|
2014-01-19 21:35:36 +00:00
|
|
|
|
|
|
|
dropr fs r n = checkdrop fs n (Just $ Remote.uuid r) $ \numcopies ->
|
2019-06-06 16:53:24 +00:00
|
|
|
Command.Drop.startRemote afile ai numcopies key r
|
|
|
|
|
|
|
|
ai = mkActionItem (key, afile)
|
2014-01-19 21:35:36 +00:00
|
|
|
|
2014-01-20 17:31:03 +00:00
|
|
|
slocs = S.fromList locs
|
|
|
|
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
safely a = either (const False) id <$> tryNonAsync a
|
2014-01-19 21:35:36 +00:00
|
|
|
|