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]
|
2012-12-29 18:28:19 +00:00
|
|
|
def = [notDirect $ withOptions [fromOption] $ command "drop" paramPaths seek
|
2010-12-30 19:06:26 +00:00
|
|
|
"indicate content of files not currently wanted"]
|
|
|
|
|
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]
|
2012-02-14 03:42:44 +00:00
|
|
|
seek = [withField fromOption Remote.byName $ \from ->
|
|
|
|
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
|
2012-10-08 20:06:56 +00:00
|
|
|
start from file (key, _) = autoCopiesWith 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
|
2011-12-09 16:23:45 +00:00
|
|
|
whenM (inAnnex key) $ removeAnnex key
|
|
|
|
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
|
2011-10-28 21:26:38 +00:00
|
|
|
-- better safe than sorry: assume the remote dropped the key
|
|
|
|
-- even if it seemed to fail; the failure could have occurred
|
|
|
|
-- after it really dropped it
|
2012-01-19 19:24:05 +00:00
|
|
|
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
|
2011-10-28 21:26:38 +00:00
|
|
|
findCopies key need skip = helper []
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
helper bad have []
|
|
|
|
| length have >= need = return True
|
|
|
|
| otherwise = notEnoughCopies key need have skip bad
|
|
|
|
helper bad have (r:rs)
|
|
|
|
| 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
|
|
|
|
(False, Right True) -> helper bad (u:have) rs
|
|
|
|
(False, Left _) -> helper (r:bad) have rs
|
|
|
|
_ -> helper bad 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)
|
|
|
|
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.)"
|