2010-11-02 23:04:24 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2010 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- 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
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2011-07-05 22:31:46 +00:00
|
|
|
import Config
|
2012-01-06 14:14:37 +00:00
|
|
|
import qualified Option
|
2012-10-08 20:06:56 +00:00
|
|
|
import Annex.Wanted
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2011-10-29 19:19:05 +00:00
|
|
|
def :: [Command]
|
2013-01-05 21:16:58 +00:00
|
|
|
def = [withOptions [fromOption] $ 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
|
|
|
|
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
|
|
|
fromOption :: Option
|
2012-01-06 14:14:37 +00:00
|
|
|
fromOption = Option.field ['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
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
seek :: [CommandSeek]
|
2013-03-05 19:39:42 +00:00
|
|
|
seek = [withField fromOption Remote.byNameWithUUID $ \from ->
|
2012-02-14 03:42:44 +00:00
|
|
|
withFilesInGit $ whenAnnexed $ start from]
|
2011-11-11 03:35:08 +00:00
|
|
|
|
2012-02-14 03:42:44 +00:00
|
|
|
start :: Maybe Remote -> FilePath -> (Key, Backend) -> CommandStart
|
2013-04-01 19:20:42 +00:00
|
|
|
start from file (key, _) = checkDropAuto from file key $ \numcopies ->
|
2012-12-06 17:22:16 +00:00
|
|
|
stopUnless (checkAuto $ wantDrop False (Remote.uuid <$> from) (Just file)) $
|
2012-10-08 20:06:56 +00:00
|
|
|
case from of
|
2012-11-24 20:30:15 +00:00
|
|
|
Nothing -> startLocal file numcopies key Nothing
|
2012-10-08 20:06:56 +00:00
|
|
|
Just remote -> do
|
|
|
|
u <- getUUID
|
|
|
|
if Remote.uuid remote == u
|
2012-11-24 20:30:15 +00:00
|
|
|
then startLocal file numcopies key Nothing
|
2012-10-08 20:06:56 +00:00
|
|
|
else startRemote file numcopies key remote
|
2011-10-28 21:26:38 +00:00
|
|
|
|
2012-11-24 20:30:15 +00:00
|
|
|
startLocal :: FilePath -> Maybe Int -> Key -> Maybe Remote -> CommandStart
|
|
|
|
startLocal file numcopies key knownpresentremote = stopUnless (inAnnex key) $ do
|
2011-12-09 16:23:45 +00:00
|
|
|
showStart "drop" file
|
2012-11-24 20:30:15 +00:00
|
|
|
next $ performLocal key numcopies knownpresentremote
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
startRemote :: FilePath -> Maybe Int -> Key -> Remote -> CommandStart
|
2011-10-28 21:26:38 +00:00
|
|
|
startRemote file numcopies key remote = do
|
2012-10-18 18:55:59 +00:00
|
|
|
showStart ("drop " ++ Remote.name remote) file
|
2011-10-28 21:26:38 +00:00
|
|
|
next $ performRemote key numcopies remote
|
|
|
|
|
2012-11-24 20:30:15 +00:00
|
|
|
performLocal :: Key -> Maybe Int -> Maybe Remote -> CommandPerform
|
|
|
|
performLocal key numcopies knownpresentremote = lockContent key $ 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
|
|
|
|
Just r -> nub (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)
|
|
|
|
stopUnless (canDropKey key numcopies trusteduuids' tocheck []) $ do
|
2013-02-15 21:58:49 +00:00
|
|
|
removeAnnex key
|
2011-12-09 16:23:45 +00:00
|
|
|
next $ cleanupLocal key
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
performRemote :: Key -> Maybe Int -> Remote -> CommandPerform
|
2011-11-09 22:33:15 +00:00
|
|
|
performRemote key numcopies remote = lockContent key $ 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.
|
|
|
|
-- When the local repo has the key, that's one additional copy.
|
|
|
|
(remotes, trusteduuids) <- Remote.keyPossibilitiesTrusted key
|
|
|
|
present <- inAnnex key
|
|
|
|
u <- getUUID
|
|
|
|
let have = filter (/= uuid) $
|
|
|
|
if present then u:trusteduuids else trusteduuids
|
|
|
|
untrusteduuids <- trustGet UnTrusted
|
|
|
|
let tocheck = filter (/= remote) $
|
|
|
|
Remote.remotesWithoutUUID remotes (have++untrusteduuids)
|
2011-12-09 16:23:45 +00:00
|
|
|
stopUnless (canDropKey key numcopies have tocheck [uuid]) $ do
|
|
|
|
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
|
|
|
|
- some locations where the key is known/assumed to be present. -}
|
2011-12-31 08:11:39 +00:00
|
|
|
canDropKey :: Key -> Maybe Int -> [UUID] -> [Remote] -> [UUID] -> Annex Bool
|
2011-10-28 21:26:38 +00:00
|
|
|
canDropKey key numcopiesM have check skip = do
|
2011-07-05 22:31:46 +00:00
|
|
|
force <- Annex.getState Annex.force
|
|
|
|
if force || numcopiesM == Just 0
|
|
|
|
then return True
|
|
|
|
else do
|
2011-10-28 21:26:38 +00:00
|
|
|
need <- getNumCopies numcopiesM
|
|
|
|
findCopies key need skip have check
|
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
findCopies :: Key -> Int -> [UUID] -> [UUID] -> [Remote] -> Annex Bool
|
2013-01-17 01:44:42 +00:00
|
|
|
findCopies key need skip = helper [] []
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2013-01-17 01:44:42 +00:00
|
|
|
helper bad missing have []
|
2012-11-12 05:05:04 +00:00
|
|
|
| length have >= need = return True
|
2013-01-17 01:44:42 +00:00
|
|
|
| otherwise = notEnoughCopies key need have (skip++missing) bad
|
|
|
|
helper bad missing have (r:rs)
|
2012-11-12 05:05:04 +00:00
|
|
|
| length have >= need = return True
|
|
|
|
| otherwise = do
|
|
|
|
let u = Remote.uuid r
|
|
|
|
let duplicate = u `elem` have
|
|
|
|
haskey <- Remote.hasKey r key
|
|
|
|
case (duplicate, haskey) of
|
2013-01-17 01:44:42 +00:00
|
|
|
(False, Right True) -> helper bad missing (u:have) rs
|
|
|
|
(False, Left _) -> helper (r:bad) missing have rs
|
|
|
|
(False, Right False) -> helper bad (u:missing) have rs
|
|
|
|
_ -> helper bad missing have rs
|
2011-10-28 21:26:38 +00:00
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
notEnoughCopies :: Key -> Int -> [UUID] -> [UUID] -> [Remote] -> Annex Bool
|
2011-10-28 21:26:38 +00:00
|
|
|
notEnoughCopies key need have skip bad = do
|
|
|
|
unsafe
|
|
|
|
showLongNote $
|
|
|
|
"Could only verify the existence of " ++
|
|
|
|
show (length have) ++ " out of " ++ show need ++
|
|
|
|
" necessary copies"
|
|
|
|
Remote.showTriedRemotes bad
|
|
|
|
Remote.showLocations key (have++skip)
|
2013-01-09 22:53:59 +00:00
|
|
|
"Rather than dropping this file, try using: git annex move"
|
2011-10-28 21:26:38 +00:00
|
|
|
hint
|
|
|
|
return False
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
unsafe = showNote "unsafe"
|
|
|
|
hint = showLongNote "(Use --force to override this check, or adjust annex.numcopies.)"
|
2013-04-01 19:20:42 +00:00
|
|
|
|
|
|
|
{- In auto mode, only runs the action if there are enough copies
|
|
|
|
- copies on other semitrusted repositories.
|
|
|
|
-
|
|
|
|
- Passes any numcopies attribute of the file on to the action as an
|
|
|
|
- optimisation. -}
|
|
|
|
checkDropAuto :: Maybe Remote -> FilePath -> Key -> (Maybe Int -> CommandStart) -> CommandStart
|
|
|
|
checkDropAuto mremote file key a = do
|
|
|
|
numcopiesattr <- numCopies file
|
|
|
|
Annex.getState Annex.auto >>= auto numcopiesattr
|
|
|
|
where
|
|
|
|
auto numcopiesattr False = a numcopiesattr
|
|
|
|
auto numcopiesattr True = do
|
|
|
|
needed <- getNumCopies numcopiesattr
|
|
|
|
locs <- Remote.keyLocations key
|
|
|
|
uuid <- getUUID
|
|
|
|
let remoteuuid = fromMaybe uuid $ Remote.uuid <$> mremote
|
|
|
|
locs' <- trustExclude UnTrusted $ filter (/= remoteuuid) locs
|
|
|
|
if length locs' >= needed
|
|
|
|
then a numcopiesattr
|
|
|
|
else stop
|