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
|
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-10 17:18:46 +00:00
|
|
|
cmd = withGlobalOptions annexedMatchingOptions $
|
|
|
|
command "drop" SectionCommon
|
|
|
|
"remove content of files from repository"
|
|
|
|
paramPaths (seek <$$> optParser)
|
2015-07-08 21:59:06 +00:00
|
|
|
|
|
|
|
data DropOptions = DropOptions
|
|
|
|
{ dropFiles :: CmdParams
|
2015-07-09 19:23:14 +00:00
|
|
|
, dropFrom :: Maybe (DeferredParse Remote)
|
2015-07-08 21:59:06 +00:00
|
|
|
, autoMode :: Bool
|
2015-07-09 19:23:14 +00:00
|
|
|
, keyOptions :: Maybe KeyOptions
|
2015-07-08 21:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
optParser :: CmdParamsDesc -> Parser DropOptions
|
|
|
|
optParser desc = DropOptions
|
|
|
|
<$> cmdParams desc
|
2015-07-09 19:23:14 +00:00
|
|
|
<*> optional parseDropFromOption
|
2015-07-08 21:59:06 +00:00
|
|
|
<*> parseAutoOption
|
2015-07-09 19:23:14 +00:00
|
|
|
<*> optional (parseKeyOptions False)
|
2015-07-08 21:59:06 +00:00
|
|
|
|
2015-07-09 19:23:14 +00:00
|
|
|
parseDropFromOption :: Parser (DeferredParse Remote)
|
|
|
|
parseDropFromOption = parseRemoteOption $ strOption
|
2015-07-09 14:41:17 +00:00
|
|
|
( long "from" <> short 'f' <> metavar paramRemote
|
|
|
|
<> help "drop content from a remote"
|
2015-09-14 17:19:04 +00:00
|
|
|
<> completeRemotes
|
2015-07-08 21:59:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
seek :: DropOptions -> CommandSeek
|
|
|
|
seek o = withKeyOptions (keyOptions o) (autoMode o)
|
|
|
|
(startKeys o)
|
|
|
|
(withFilesInGit $ whenAnnexed $ start o)
|
|
|
|
(dropFiles o)
|
|
|
|
|
|
|
|
start :: DropOptions -> FilePath -> Key -> CommandStart
|
|
|
|
start o file key = start' o key (Just file)
|
|
|
|
|
|
|
|
start' :: DropOptions -> Key -> AssociatedFile -> CommandStart
|
|
|
|
start' o key afile = do
|
2015-07-09 19:23:14 +00:00
|
|
|
from <- maybe (pure Nothing) (Just <$$> getParsed) (dropFrom o)
|
2015-07-08 21:59:06 +00:00
|
|
|
checkDropAuto (autoMode o) from afile key $ \numcopies ->
|
|
|
|
stopUnless (want from) $
|
|
|
|
case from of
|
|
|
|
Nothing -> startLocal afile numcopies key Nothing
|
|
|
|
Just remote -> do
|
|
|
|
u <- getUUID
|
|
|
|
if Remote.uuid remote == u
|
|
|
|
then startLocal afile numcopies key Nothing
|
|
|
|
else startRemote afile numcopies key remote
|
|
|
|
where
|
|
|
|
want from
|
|
|
|
| autoMode o = wantDrop False (Remote.uuid <$> from) (Just key) afile
|
|
|
|
| otherwise = return True
|
|
|
|
|
|
|
|
startKeys :: DropOptions -> Key -> CommandStart
|
|
|
|
startKeys o key = start' o key Nothing
|
2015-05-12 17:00:06 +00:00
|
|
|
|
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
|
|
|
|
2015-10-08 18:27:37 +00:00
|
|
|
-- Note that lockContentExclusive is called before checking if the key is
|
|
|
|
-- present on enough remotes to allow removal. This avoids a scenario where two
|
2014-08-21 00:08:45 +00:00
|
|
|
-- or more remotes are trying to remove a key at the same time, and each
|
2015-10-08 19:01:38 +00:00
|
|
|
-- sees the key is present on the other.
|
2014-03-22 19:01:48 +00:00
|
|
|
performLocal :: Key -> AssociatedFile -> NumCopies -> Maybe Remote -> CommandPerform
|
2015-10-08 18:27:37 +00:00
|
|
|
performLocal key afile numcopies knownpresentremote = lockContentExclusive 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
|
2015-07-08 21:59:06 +00:00
|
|
|
checkDropAuto automode 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
|
2015-07-08 21:59:06 +00:00
|
|
|
| automode = do
|
2015-03-25 21:06:14 +00:00
|
|
|
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
|