2012-10-18 19:22:28 +00:00
|
|
|
{- git-annex assistant dropping of unwanted content
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.Drop where
|
|
|
|
|
|
|
|
import Assistant.Common
|
2012-10-18 19:37:57 +00:00
|
|
|
import Assistant.DaemonStatus
|
2012-10-18 19:22:28 +00:00
|
|
|
import Logs.Location
|
|
|
|
import Logs.Trust
|
2013-07-04 06:36:02 +00:00
|
|
|
import Types.Remote (uuid)
|
2012-10-18 19:22:28 +00:00
|
|
|
import qualified Remote
|
|
|
|
import qualified Command.Drop
|
|
|
|
import Command
|
|
|
|
import Annex.Wanted
|
2012-12-05 16:28:50 +00:00
|
|
|
import Annex.Exception
|
2012-10-18 19:22:28 +00:00
|
|
|
import Config
|
2013-06-15 18:44:43 +00:00
|
|
|
import Annex.Content.Direct
|
2012-10-18 19:22:28 +00:00
|
|
|
|
2012-12-05 16:44:08 +00:00
|
|
|
import qualified Data.Set as S
|
|
|
|
|
2013-03-01 19:58:44 +00:00
|
|
|
type Reason = String
|
|
|
|
|
2012-10-18 20:05:43 +00:00
|
|
|
{- Drop from local and/or remote when allowed by the preferred content and
|
2012-12-05 16:32:26 +00:00
|
|
|
- numcopies settings. -}
|
2013-03-01 19:58:44 +00:00
|
|
|
handleDrops :: Reason -> Bool -> Key -> AssociatedFile -> Maybe Remote -> Assistant ()
|
|
|
|
handleDrops _ _ _ Nothing _ = noop
|
|
|
|
handleDrops reason fromhere key f knownpresentremote = do
|
2012-11-11 20:23:16 +00:00
|
|
|
syncrs <- syncDataRemotes <$> getDaemonStatus
|
2013-03-01 19:58:44 +00:00
|
|
|
locs <- liftAnnex $ loggedLocations key
|
|
|
|
handleDropsFrom locs syncrs reason fromhere key f knownpresentremote
|
2012-10-18 19:22:28 +00:00
|
|
|
|
2012-12-05 16:44:08 +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.
|
2013-06-15 18:44:43 +00:00
|
|
|
- If allowed to drop fromhere, that drop will be tried first.
|
|
|
|
-
|
|
|
|
- In direct mode, all associated files are checked, and only if all
|
|
|
|
- of them are unwanted are they dropped.
|
|
|
|
-}
|
2013-03-01 19:58:44 +00:00
|
|
|
handleDropsFrom :: [UUID] -> [Remote] -> Reason -> Bool -> Key -> AssociatedFile -> Maybe Remote -> Assistant ()
|
|
|
|
handleDropsFrom _ _ _ _ _ Nothing _ = noop
|
2013-06-15 18:44:43 +00:00
|
|
|
handleDropsFrom locs rs reason fromhere key (Just afile) knownpresentremote = do
|
|
|
|
fs <- liftAnnex $ ifM isDirect
|
2013-06-24 16:44:51 +00:00
|
|
|
( do
|
|
|
|
l <- associatedFilesRelative key
|
|
|
|
if null l
|
|
|
|
then return [afile]
|
|
|
|
else return l
|
2013-06-15 18:44:43 +00:00
|
|
|
, return [afile]
|
|
|
|
)
|
|
|
|
n <- getcopies fs
|
|
|
|
if fromhere && checkcopies n Nothing
|
|
|
|
then go fs rs =<< dropl fs n
|
|
|
|
else go fs rs n
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
2013-06-15 18:44:43 +00:00
|
|
|
getcopies fs = liftAnnex $ do
|
2013-03-31 22:39:49 +00:00
|
|
|
(untrusted, have) <- trustPartition UnTrusted locs
|
2013-06-15 18:44:43 +00:00
|
|
|
numcopies <- maximum <$> mapM (getNumCopies <=< numCopies) fs
|
2013-03-31 22:39:49 +00:00
|
|
|
return (length have, numcopies, S.fromList untrusted)
|
|
|
|
|
|
|
|
{- 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 =
|
|
|
|
(have - 1, numcopies, untrusted)
|
|
|
|
decrcopies v@(_have, _numcopies, untrusted) (Just u)
|
|
|
|
| S.member u untrusted = v
|
|
|
|
| otherwise = decrcopies v Nothing
|
2012-10-18 19:22:28 +00:00
|
|
|
|
2013-06-15 18:44:43 +00:00
|
|
|
go _ [] _ = noop
|
|
|
|
go fs (r:rest) n
|
|
|
|
| uuid r `S.notMember` slocs = go fs rest n
|
2013-03-31 22:39:49 +00:00
|
|
|
| checkcopies n (Just $ Remote.uuid r) =
|
2013-06-15 18:44:43 +00:00
|
|
|
dropr fs r n >>= go fs rest
|
2012-10-31 06:34:03 +00:00
|
|
|
| otherwise = noop
|
2012-10-18 19:22:28 +00:00
|
|
|
|
2013-06-15 18:44:43 +00:00
|
|
|
checkdrop fs n@(have, numcopies, _untrusted) u a =
|
|
|
|
ifM (liftAnnex $ allM (wantDrop True u . Just) fs)
|
2013-03-01 19:58:44 +00:00
|
|
|
( ifM (liftAnnex $ safely $ doCommand $ a (Just numcopies))
|
|
|
|
( do
|
|
|
|
debug
|
|
|
|
[ "dropped"
|
2013-06-15 18:44:43 +00:00
|
|
|
, afile
|
2013-03-01 20:58:19 +00:00
|
|
|
, "(from " ++ maybe "here" show u ++ ")"
|
2013-03-01 19:58:44 +00:00
|
|
|
, "(copies now " ++ show (have - 1) ++ ")"
|
|
|
|
, ": " ++ reason
|
|
|
|
]
|
2013-03-31 22:39:49 +00:00
|
|
|
return $ decrcopies n u
|
2013-03-01 19:58:44 +00:00
|
|
|
, return n
|
|
|
|
)
|
2012-10-31 06:34:03 +00:00
|
|
|
, return n
|
|
|
|
)
|
2012-10-18 19:22:28 +00:00
|
|
|
|
2013-06-15 18:44:43 +00:00
|
|
|
dropl fs n = checkdrop fs n Nothing $ \numcopies ->
|
|
|
|
Command.Drop.startLocal afile numcopies key knownpresentremote
|
2012-10-18 19:22:28 +00:00
|
|
|
|
2013-06-15 18:44:43 +00:00
|
|
|
dropr fs r n = checkdrop fs n (Just $ Remote.uuid r) $ \numcopies ->
|
|
|
|
Command.Drop.startRemote afile numcopies key r
|
2012-12-05 16:28:50 +00:00
|
|
|
|
|
|
|
safely a = either (const False) id <$> tryAnnex a
|
2012-12-05 16:44:08 +00:00
|
|
|
|
|
|
|
slocs = S.fromList locs
|