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
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2011-10-29 19:19:05 +00:00
|
|
|
def :: [Command]
|
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
|
|
|
def = [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-07-10 17:53:45 +00:00
|
|
|
start from file (key, _) = autoCopiesWith file key (>) $ \numcopies ->
|
2011-11-11 03:35:08 +00:00
|
|
|
case from of
|
|
|
|
Nothing -> startLocal file numcopies key
|
2012-01-06 08:02:35 +00:00
|
|
|
Just remote -> do
|
2011-11-11 03:35:08 +00:00
|
|
|
u <- getUUID
|
|
|
|
if Remote.uuid remote == u
|
|
|
|
then startLocal file numcopies key
|
|
|
|
else startRemote file numcopies key remote
|
2011-10-28 21:26:38 +00:00
|
|
|
|
|
|
|
startLocal :: FilePath -> Maybe Int -> Key -> CommandStart
|
2011-12-09 16:23:45 +00:00
|
|
|
startLocal file numcopies key = stopUnless (inAnnex key) $ do
|
|
|
|
showStart "drop" file
|
|
|
|
next $ performLocal key numcopies
|
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
|
|
|
|
showStart "drop" file
|
|
|
|
next $ performRemote key numcopies remote
|
|
|
|
|
|
|
|
performLocal :: Key -> Maybe Int -> CommandPerform
|
2011-11-09 22:33:15 +00:00
|
|
|
performLocal key numcopies = lockContent key $ do
|
2011-10-28 21:26:38 +00:00
|
|
|
(remotes, trusteduuids) <- Remote.keyPossibilitiesTrusted key
|
|
|
|
untrusteduuids <- trustGet UnTrusted
|
|
|
|
let tocheck = Remote.remotesWithoutUUID remotes (trusteduuids++untrusteduuids)
|
2011-12-09 16:23:45 +00:00
|
|
|
stopUnless (canDropKey key numcopies trusteduuids tocheck []) $ do
|
|
|
|
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
|
2011-10-28 21:26:38 +00:00
|
|
|
where
|
|
|
|
uuid = Remote.uuid remote
|
|
|
|
|
|
|
|
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 []
|
2011-07-05 22:31:46 +00:00
|
|
|
where
|
2011-10-28 21:26:38 +00:00
|
|
|
helper bad have []
|
2011-07-05 22:31:46 +00:00
|
|
|
| length have >= need = return True
|
2011-10-28 21:26:38 +00:00
|
|
|
| otherwise = notEnoughCopies key need have skip bad
|
|
|
|
helper bad have (r:rs)
|
2011-07-05 22:31:46 +00:00
|
|
|
| length have >= need = return True
|
|
|
|
| otherwise = do
|
|
|
|
let u = Remote.uuid r
|
2011-10-04 02:24:57 +00:00
|
|
|
let duplicate = u `elem` have
|
2011-07-05 22:31:46 +00:00
|
|
|
haskey <- Remote.hasKey r key
|
2011-10-04 02:24:57 +00:00
|
|
|
case (duplicate, haskey) of
|
2011-10-28 21:26:38 +00:00
|
|
|
(False, Right True) -> helper bad (u:have) rs
|
|
|
|
(False, Left _) -> helper (r:bad) have rs
|
|
|
|
_ -> helper bad have rs
|
|
|
|
|
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
|
|
|
|
where
|
2011-07-05 22:31:46 +00:00
|
|
|
unsafe = showNote "unsafe"
|
|
|
|
hint = showLongNote "(Use --force to override this check, or adjust annex.numcopies.)"
|