git-annex/Assistant/Drop.hs

66 lines
1.7 KiB
Haskell
Raw Normal View History

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
import Assistant.DaemonStatus
2012-10-18 19:22:28 +00:00
import Logs.Location
import Logs.Trust
import Types.Remote (AssociatedFile)
2012-10-18 19:22:28 +00:00
import qualified Remote
import qualified Command.Drop
import Command
import Annex.Wanted
import Config
2012-10-18 20:05:43 +00:00
{- Drop from local and/or remote when allowed by the preferred content and
2012-10-18 19:22:28 +00:00
- numcopies settings. -}
handleDrops :: Bool -> Key -> AssociatedFile -> Assistant ()
handleDrops _ _ Nothing = noop
handleDrops fromhere key f = do
syncrs <- syncRemotes <$> getDaemonStatus
liftAnnex $ do
locs <- loggedLocations key
handleDrops' locs syncrs fromhere key f
2012-10-18 19:22:28 +00:00
2012-10-18 20:05:43 +00:00
handleDrops' :: [UUID] -> [Remote] -> Bool -> Key -> AssociatedFile -> Annex ()
handleDrops' _ _ _ _ Nothing = noop
handleDrops' locs rs fromhere key (Just f)
| fromhere = do
2012-10-18 19:22:28 +00:00
n <- getcopies
if checkcopies n
then go rs =<< dropl n
else go rs n
| otherwise = go rs =<< getcopies
2012-10-31 06:34:03 +00:00
where
getcopies = do
have <- length . snd <$> trustPartition UnTrusted locs
numcopies <- getNumCopies =<< numCopies f
return (have, numcopies)
checkcopies (have, numcopies) = have > numcopies
decrcopies (have, numcopies) = (have - 1, numcopies)
2012-10-18 19:22:28 +00:00
2012-10-31 06:34:03 +00:00
go [] _ = noop
go (r:rest) n
| checkcopies n = dropr r n >>= go rest
| otherwise = noop
2012-10-18 19:22:28 +00:00
2012-10-31 06:34:03 +00:00
checkdrop n@(_, numcopies) u a = ifM (wantDrop u (Just f))
( ifM (doCommand $ a (Just numcopies))
( return $ decrcopies n
, return n
)
, return n
)
2012-10-18 19:22:28 +00:00
2012-10-31 06:34:03 +00:00
dropl n = checkdrop n Nothing $ \numcopies ->
Command.Drop.startLocal f numcopies key
2012-10-18 19:22:28 +00:00
2012-10-31 06:34:03 +00:00
dropr r n = checkdrop n (Just $ Remote.uuid r) $ \numcopies ->
Command.Drop.startRemote f numcopies key r