2010-11-02 23:04:24 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2010 Joey Hess <id@joeyh.name>
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Drop where
|
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2010-11-02 23:04:24 +00:00
|
|
|
import Command
|
2011-07-05 22:31:46 +00:00
|
|
|
import qualified Remote
|
|
|
|
import qualified Annex
|
2011-10-28 21:26:38 +00:00
|
|
|
import Annex.UUID
|
2011-10-15 20:21:08 +00:00
|
|
|
import Logs.Location
|
|
|
|
import Logs.Trust
|
2014-03-29 19:20:55 +00:00
|
|
|
import Logs.PreferredContent
|
2015-04-30 18:02:56 +00:00
|
|
|
import Annex.NumCopies
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2012-10-08 20:06:56 +00:00
|
|
|
import Annex.Wanted
|
2014-03-22 19:01:48 +00:00
|
|
|
import Annex.Notification
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2014-03-29 19:20:55 +00:00
|
|
|
import qualified Data.Set as S
|
|
|
|
|
2014-10-14 18:20:10 +00:00
|
|
|
cmd :: [Command]
|
2015-02-06 21:08:14 +00:00
|
|
|
cmd = [withOptions (dropOptions) $ command "drop" paramPaths seek
|
2013-03-24 22:28:21 +00:00
|
|
|
SectionCommon "indicate content of files not currently wanted"]
|
2010-12-30 19:06:26 +00:00
|
|
|
|
2015-02-06 21:08:14 +00:00
|
|
|
dropOptions :: [Option]
|
2015-05-12 17:00:06 +00:00
|
|
|
dropOptions = dropFromOption : annexedMatchingOptions ++ [autoOption] ++ keyOptions
|
2015-02-06 21:08:14 +00:00
|
|
|
|
2014-01-26 20:25:55 +00:00
|
|
|
dropFromOption :: Option
|
|
|
|
dropFromOption = fieldOption ['f'] "from" paramRemote "drop content from a remote"
|
more command-specific options
Made --from and --to command-specific options.
Added generic storage for values of command-specific options,
which allows removing some of the special case fields in AnnexState.
(Also added generic storage for command-specific flags, although there are
not yet any.)
Note that this storage uses a Map, so repeatedly looking up the same value
is slightly more expensive than looking up an AnnexState field. But, the
value can be looked up once in the seek stage, transformed as necessary,
and passed in a closure to the start stage, and this avoids that overhead.
Still, I'm hesitant to use this for things like force or fast flags.
It's probably best to reserve it for flags that are only used by a few
commands, or options like --from and --to that it's important only be
allowed to be used with commands that implement them, to avoid user
confusion.
2012-01-06 07:06:25 +00:00
|
|
|
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
seek :: CommandSeek
|
|
|
|
seek ps = do
|
2014-01-26 20:25:55 +00:00
|
|
|
from <- getOptionField dropFromOption Remote.byNameWithUUID
|
2015-03-25 21:06:14 +00:00
|
|
|
auto <- getOptionFlag autoOption
|
2015-05-12 17:00:06 +00:00
|
|
|
withKeyOptions auto
|
|
|
|
(startKeys auto from)
|
|
|
|
(withFilesInGit $ whenAnnexed $ start auto from)
|
|
|
|
ps
|
2011-11-11 03:35:08 +00:00
|
|
|
|
2015-03-25 21:06:14 +00:00
|
|
|
start :: Bool -> Maybe Remote -> FilePath -> Key -> CommandStart
|
2015-05-12 17:00:06 +00:00
|
|
|
start auto from file key = start' auto from key (Just file)
|
|
|
|
|
|
|
|
start' :: Bool -> Maybe Remote -> Key -> AssociatedFile -> CommandStart
|
|
|
|
start' auto from key afile = checkDropAuto auto from afile key $ \numcopies ->
|
2015-03-25 21:06:14 +00:00
|
|
|
stopUnless want $
|
2012-10-08 20:06:56 +00:00
|
|
|
case from of
|
2015-05-12 17:00:06 +00:00
|
|
|
Nothing -> startLocal afile numcopies key Nothing
|
2012-10-08 20:06:56 +00:00
|
|
|
Just remote -> do
|
|
|
|
u <- getUUID
|
|
|
|
if Remote.uuid remote == u
|
2015-05-12 17:00:06 +00:00
|
|
|
then startLocal afile numcopies key Nothing
|
|
|
|
else startRemote afile numcopies key remote
|
2015-03-25 21:06:14 +00:00
|
|
|
where
|
|
|
|
want
|
2015-05-12 17:00:06 +00:00
|
|
|
| auto = wantDrop False (Remote.uuid <$> from) (Just key) afile
|
2015-03-25 21:06:14 +00:00
|
|
|
| otherwise = return True
|
2011-10-28 21:26:38 +00:00
|
|
|
|
2015-05-12 17:00:06 +00:00
|
|
|
startKeys :: Bool -> Maybe Remote -> Key -> CommandStart
|
|
|
|
startKeys auto from key = start' auto from key Nothing
|
|
|
|
|
2014-01-21 21:08:49 +00:00
|
|
|
startLocal :: AssociatedFile -> NumCopies -> Key -> Maybe Remote -> CommandStart
|
2014-01-01 21:39:33 +00:00
|
|
|
startLocal afile numcopies key knownpresentremote = stopUnless (inAnnex key) $ do
|
2014-01-26 19:53:01 +00:00
|
|
|
showStart' "drop" key afile
|
2014-03-22 19:01:48 +00:00
|
|
|
next $ performLocal key afile numcopies knownpresentremote
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2014-01-21 21:08:49 +00:00
|
|
|
startRemote :: AssociatedFile -> NumCopies -> Key -> Remote -> CommandStart
|
2014-01-01 21:39:33 +00:00
|
|
|
startRemote afile numcopies key remote = do
|
2014-01-26 19:53:01 +00:00
|
|
|
showStart' ("drop " ++ Remote.name remote) key afile
|
2014-03-29 19:20:55 +00:00
|
|
|
next $ performRemote key afile numcopies remote
|
2011-10-28 21:26:38 +00:00
|
|
|
|
2014-08-21 00:08:45 +00:00
|
|
|
-- Note that lockContent is called before checking if the key is present
|
|
|
|
-- on enough remotes to allow removal. This avoids a scenario where two
|
|
|
|
-- or more remotes are trying to remove a key at the same time, and each
|
|
|
|
-- see the key is present on the other.
|
2014-03-22 19:01:48 +00:00
|
|
|
performLocal :: Key -> AssociatedFile -> NumCopies -> Maybe Remote -> CommandPerform
|
2014-08-21 00:08:45 +00:00
|
|
|
performLocal key afile numcopies knownpresentremote = lockContent key $ \contentlock -> do
|
2011-10-28 21:26:38 +00:00
|
|
|
(remotes, trusteduuids) <- Remote.keyPossibilitiesTrusted key
|
2012-11-24 20:30:15 +00:00
|
|
|
let trusteduuids' = case knownpresentremote of
|
|
|
|
Nothing -> trusteduuids
|
2015-04-30 18:11:29 +00:00
|
|
|
Just r -> Remote.uuid r:trusteduuids
|
2011-10-28 21:26:38 +00:00
|
|
|
untrusteduuids <- trustGet UnTrusted
|
2012-11-24 20:30:15 +00:00
|
|
|
let tocheck = Remote.remotesWithoutUUID remotes (trusteduuids'++untrusteduuids)
|
2014-03-29 19:20:55 +00:00
|
|
|
u <- getUUID
|
|
|
|
ifM (canDrop u key afile numcopies trusteduuids' tocheck [])
|
2014-03-22 19:01:48 +00:00
|
|
|
( do
|
2014-08-21 00:08:45 +00:00
|
|
|
removeAnnex contentlock
|
2014-03-22 19:01:48 +00:00
|
|
|
notifyDrop afile True
|
|
|
|
next $ cleanupLocal key
|
|
|
|
, do
|
|
|
|
notifyDrop afile False
|
|
|
|
stop
|
|
|
|
)
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2014-03-29 19:20:55 +00:00
|
|
|
performRemote :: Key -> AssociatedFile -> NumCopies -> Remote -> CommandPerform
|
2014-08-21 00:08:45 +00:00
|
|
|
performRemote key afile numcopies remote = do
|
2011-10-28 21:26:38 +00:00
|
|
|
-- Filter the remote it's being dropped from out of the lists of
|
|
|
|
-- places assumed to have the key, and places to check.
|
2014-04-17 17:31:39 +00:00
|
|
|
-- When the local repo has the key, that's one additional copy,
|
2015-04-30 18:02:56 +00:00
|
|
|
-- as long as the local repo is not untrusted.
|
|
|
|
(remotes, trusteduuids) <- knownCopies key
|
|
|
|
let have = filter (/= uuid) trusteduuids
|
2011-10-28 21:26:38 +00:00
|
|
|
untrusteduuids <- trustGet UnTrusted
|
|
|
|
let tocheck = filter (/= remote) $
|
|
|
|
Remote.remotesWithoutUUID remotes (have++untrusteduuids)
|
2014-03-29 19:20:55 +00:00
|
|
|
stopUnless (canDrop uuid key afile numcopies have tocheck [uuid]) $ do
|
2011-12-09 16:23:45 +00:00
|
|
|
ok <- Remote.removeKey remote key
|
|
|
|
next $ cleanupRemote key remote ok
|
2012-12-13 04:45:27 +00:00
|
|
|
where
|
2012-11-12 05:05:04 +00:00
|
|
|
uuid = Remote.uuid remote
|
2011-10-28 21:26:38 +00:00
|
|
|
|
|
|
|
cleanupLocal :: Key -> CommandCleanup
|
|
|
|
cleanupLocal key = do
|
2011-07-01 19:24:07 +00:00
|
|
|
logStatus key InfoMissing
|
2010-11-08 23:26:37 +00:00
|
|
|
return True
|
2011-07-05 22:31:46 +00:00
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
cleanupRemote :: Key -> Remote -> Bool -> CommandCleanup
|
2011-11-09 20:54:18 +00:00
|
|
|
cleanupRemote key remote ok = do
|
bugfix: drop --from an unavailable remote no longer updates the location log, incorrectly, to say the remote does not have the key.
The comments correctly noted that the remote could drop the key and
yet False be returned due to some problem that occurred afterwards.
For example, if it's a network remote, it could drop the key just
as the network goes down, and so things timeout and a nonzero exit
from ssh is propigated through and False returned.
However... Most of the time, this scenario will not have happened.
False will mean the remote was not available or could not drop the key
at all.
So, instead of assuming the worst, just trust the status we have.
If we get it wrong, and the scenario above happened, our location
log will think the remote has the key. But the remote's location
log (assuming it has one) will know it dropped it, and the next sync
will regain consistency.
For a special remote, with no location log, our location log will be wrong,
but this is no different than the situation where someone else dropped
the key from the remote and we've not synced with them. The standard
paranoia about not trusting the location log to be the last word about
whether a remote has a key will save us from these situations. Ie,
if we try to drop the file, we'll actively check the remote,
and determine the inconsistency then.
2013-03-10 23:15:53 +00:00
|
|
|
when ok $
|
|
|
|
Remote.logStatus remote key InfoMissing
|
2011-10-28 21:26:38 +00:00
|
|
|
return ok
|
|
|
|
|
|
|
|
{- Checks specified remotes to verify that enough copies of a key exist to
|
|
|
|
- allow it to be safely removed (with no data loss). Can be provided with
|
2014-03-29 19:20:55 +00:00
|
|
|
- some locations where the key is known/assumed to be present.
|
|
|
|
-
|
|
|
|
- Also checks if it's required content, and refuses to drop if so.
|
|
|
|
-
|
|
|
|
- --force overrides and always allows dropping.
|
|
|
|
-}
|
|
|
|
canDrop :: UUID -> Key -> AssociatedFile -> NumCopies -> [UUID] -> [Remote] -> [UUID] -> Annex Bool
|
2015-04-30 18:02:56 +00:00
|
|
|
canDrop dropfrom key afile numcopies have check skip =
|
|
|
|
ifM (Annex.getState Annex.force)
|
|
|
|
( return True
|
|
|
|
, ifM (checkRequiredContent dropfrom key afile
|
|
|
|
<&&> verifyEnoughCopies nolocmsg key numcopies skip have check
|
|
|
|
)
|
|
|
|
( return True
|
|
|
|
, do
|
|
|
|
hint
|
|
|
|
return False
|
|
|
|
)
|
|
|
|
)
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2015-04-30 18:02:56 +00:00
|
|
|
nolocmsg = "Rather than dropping this file, try using: git annex move"
|
2014-01-20 20:47:56 +00:00
|
|
|
hint = showLongNote "(Use --force to override this check, or adjust numcopies.)"
|
2013-04-01 19:20:42 +00:00
|
|
|
|
2014-03-29 19:20:55 +00:00
|
|
|
checkRequiredContent :: UUID -> Key -> AssociatedFile -> Annex Bool
|
|
|
|
checkRequiredContent u k afile =
|
|
|
|
ifM (isRequiredContent (Just u) S.empty (Just k) afile False)
|
|
|
|
( requiredContent
|
|
|
|
, return True
|
|
|
|
)
|
|
|
|
|
|
|
|
requiredContent :: Annex Bool
|
|
|
|
requiredContent = do
|
|
|
|
showLongNote "That file is required content, it cannot be dropped!"
|
|
|
|
showLongNote "(Use --force to override this check, or adjust required content configuration.)"
|
|
|
|
return False
|
|
|
|
|
2013-10-28 18:50:17 +00:00
|
|
|
{- In auto mode, only runs the action if there are enough
|
2014-01-21 21:08:49 +00:00
|
|
|
- copies on other semitrusted repositories. -}
|
2015-05-12 17:00:06 +00:00
|
|
|
checkDropAuto :: Bool -> Maybe Remote -> AssociatedFile -> Key -> (NumCopies -> CommandStart) -> CommandStart
|
|
|
|
checkDropAuto auto mremote afile key a = go =<< maybe getNumCopies getFileNumCopies afile
|
2013-04-01 19:20:42 +00:00
|
|
|
where
|
2015-03-25 21:06:14 +00:00
|
|
|
go numcopies
|
|
|
|
| auto = do
|
|
|
|
locs <- Remote.keyLocations key
|
|
|
|
uuid <- getUUID
|
|
|
|
let remoteuuid = fromMaybe uuid $ Remote.uuid <$> mremote
|
|
|
|
locs' <- trustExclude UnTrusted $ filter (/= remoteuuid) locs
|
|
|
|
if NumCopies (length locs') >= numcopies
|
|
|
|
then a numcopies
|
|
|
|
else stop
|
|
|
|
| otherwise = a numcopies
|