2011-03-05 21:23:55 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2016-01-20 16:46:00 +00:00
|
|
|
- Copyright 2010-2016 Joey Hess <id@joeyh.name>
|
2011-03-05 21:23:55 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-03-05 21:23:55 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Whereis where
|
|
|
|
|
|
|
|
import Command
|
2011-07-01 19:24:07 +00:00
|
|
|
import Remote
|
2011-10-15 20:21:08 +00:00
|
|
|
import Logs.Trust
|
2014-12-08 23:14:24 +00:00
|
|
|
import Logs.Web
|
2015-08-17 15:35:34 +00:00
|
|
|
import Remote.Web (getWebUrls)
|
2016-01-15 18:16:48 +00:00
|
|
|
import Annex.UUID
|
2011-03-05 21:23:55 +00:00
|
|
|
|
2016-01-06 16:33:32 +00:00
|
|
|
import qualified Data.Map as M
|
Fix mangling of --json output of utf-8 characters when not running in a utf-8 locale
As long as all code imports Utility.Aeson rather than Data.Aeson,
and no Strings that may contain utf-8 characters are used for eg, object
keys via T.pack, this is guaranteed to fix the problem everywhere that
git-annex generates json.
It's kind of annoying to need to wrap ToJSON with a ToJSON', especially
since every data type that has a ToJSON instance has to be ported over.
However, that only took 50 lines of code, which is worth it to ensure full
coverage. I initially tried an alternative approach of a newtype FileEncoded,
which had to be used everywhere a String was fed into aeson, and chasing
down all the sites would have been far too hard. Did consider creating an
intentionally overlapping instance ToJSON String, and letting ghc fail
to build anything that passed in a String, but am not sure that wouldn't
pollute some library that git-annex depends on that happens to use ToJSON
String internally.
This commit was supported by the NSF-funded DataLad project.
2018-04-16 19:42:45 +00:00
|
|
|
import qualified Data.Vector as V
|
2016-01-06 16:33:32 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2018-02-19 18:28:17 +00:00
|
|
|
cmd = noCommit $ withGlobalOptions [jsonOptions, annexedMatchingOptions] $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "whereis" SectionQuery
|
2015-07-08 16:33:27 +00:00
|
|
|
"lists repositories that have file content"
|
2015-07-10 20:32:33 +00:00
|
|
|
paramPaths (seek <$$> optParser)
|
2011-03-05 21:23:55 +00:00
|
|
|
|
2015-07-09 23:03:21 +00:00
|
|
|
data WhereisOptions = WhereisOptions
|
|
|
|
{ whereisFiles :: CmdParams
|
|
|
|
, keyOptions :: Maybe KeyOptions
|
2016-01-20 16:46:00 +00:00
|
|
|
, batchOption :: BatchMode
|
2015-07-09 23:03:21 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 20:32:33 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser WhereisOptions
|
|
|
|
optParser desc = WhereisOptions
|
|
|
|
<$> cmdParams desc
|
2016-08-03 16:37:12 +00:00
|
|
|
<*> optional parseKeyOptions
|
2016-01-20 16:46:00 +00:00
|
|
|
<*> parseBatchOption
|
2015-07-10 20:32:33 +00:00
|
|
|
|
|
|
|
seek :: WhereisOptions -> CommandSeek
|
|
|
|
seek o = do
|
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
|
|
|
m <- remoteMap id
|
2016-01-20 16:46:00 +00:00
|
|
|
let go = whenAnnexed $ start m
|
|
|
|
case batchOption o of
|
2019-12-04 17:15:34 +00:00
|
|
|
Batch fmt -> batchFilesMatching fmt (go . toRawFilePath)
|
2016-01-20 16:46:00 +00:00
|
|
|
NoBatch ->
|
|
|
|
withKeyOptions (keyOptions o) False
|
2018-10-01 18:12:06 +00:00
|
|
|
(commandAction . startKeys m)
|
|
|
|
(withFilesInGit (commandAction . go))
|
2017-10-16 18:10:03 +00:00
|
|
|
=<< workTreeItems (whereisFiles o)
|
2011-03-05 21:23:55 +00:00
|
|
|
|
2019-12-04 17:15:34 +00:00
|
|
|
start :: M.Map UUID Remote -> RawFilePath -> Key -> CommandStart
|
2019-06-06 16:53:24 +00:00
|
|
|
start remotemap file key = startKeys remotemap (key, mkActionItem (key, afile))
|
2016-07-20 19:22:55 +00:00
|
|
|
where
|
2017-03-10 17:12:24 +00:00
|
|
|
afile = AssociatedFile (Just file)
|
2014-01-26 18:26:32 +00:00
|
|
|
|
2018-10-01 18:12:06 +00:00
|
|
|
startKeys :: M.Map UUID Remote -> (Key, ActionItem) -> CommandStart
|
make CommandStart return a StartMessage
The goal is to be able to run CommandStart in the main thread when -J is
used, rather than unncessarily passing it off to a worker thread, which
incurs overhead that is signficant when the CommandStart is going to
quickly decide to stop.
To do that, the message it displays needs to be displayed in the worker
thread, after the CommandStart has run.
Also, the change will mean that CommandStart will no longer necessarily
run with the same Annex state as CommandPerform. While its docs already
said it should avoid modifying Annex state, I audited all the
CommandStart code as part of the conversion. (Note that CommandSeek
already sometimes runs with a different Annex state, and that has not been
a source of any problems, so I am not too worried that this change will
lead to breakage going forward.)
The only modification of Annex state I found was it calling
allowMessages in some Commands that default to noMessages. Dealt with
that by adding a startCustomOutput and a startingUsualMessages.
This lets a command start with noMessages and then select the output it
wants for each CommandStart.
One bit of breakage: onlyActionOn has been removed from commands that used it.
The plan is that, since a StartMessage contains an ActionItem,
when a Key can be extracted from that, the parallel job runner can
run onlyActionOn' automatically. Then commands won't need to worry about
this detail. Future work.
Otherwise, this was a fairly straightforward process of making each
CommandStart compile again. Hopefully other behavior changes were mostly
avoided.
In a few cases, a command had a CommandStart that called a CommandPerform
that then called showStart multiple times. I have collapsed those
down to a single start action. The main command to perhaps suffer from it
is Command.Direct, which used to show a start for each file, and no
longer does.
Another minor behavior change is that some commands used showStart
before, but had an associated file and a Key available, so were changed
to ShowStart with an ActionItemAssociatedFile. That will not change the
normal output or behavior, but --json output will now include the key.
This should not break it for anyone using a real json parser.
2019-06-06 19:42:30 +00:00
|
|
|
startKeys remotemap (key, ai) = starting "whereis" ai $ perform remotemap key
|
2011-03-05 21:23:55 +00:00
|
|
|
|
2012-02-16 04:41:30 +00:00
|
|
|
perform :: M.Map UUID Remote -> Key -> CommandPerform
|
2012-02-14 07:49:48 +00:00
|
|
|
perform remotemap key = do
|
|
|
|
locations <- keyLocations key
|
2016-01-15 18:16:48 +00:00
|
|
|
urls <- getUUIDUrls key locations remotemap
|
2012-02-14 07:49:48 +00:00
|
|
|
(untrustedlocations, safelocations) <- trustPartition UnTrusted locations
|
2011-09-06 20:59:53 +00:00
|
|
|
let num = length safelocations
|
2011-03-05 21:41:36 +00:00
|
|
|
showNote $ show num ++ " " ++ copiesplural num
|
2016-01-15 18:16:48 +00:00
|
|
|
pp <- ppwhereis "whereis" safelocations urls
|
2011-10-31 20:46:51 +00:00
|
|
|
unless (null safelocations) $ showLongNote pp
|
2016-01-15 18:16:48 +00:00
|
|
|
pp' <- ppwhereis "untrusted" untrustedlocations urls
|
2011-10-31 20:46:51 +00:00
|
|
|
unless (null untrustedlocations) $ showLongNote $ untrustedheader ++ pp'
|
2015-08-17 15:35:34 +00:00
|
|
|
|
2016-01-15 18:16:48 +00:00
|
|
|
mapM_ (showRemoteUrls remotemap) urls
|
|
|
|
|
2011-09-06 20:59:53 +00:00
|
|
|
if null safelocations then stop else next $ return True
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
copiesplural 1 = "copy"
|
|
|
|
copiesplural _ = "copies"
|
|
|
|
untrustedheader = "The following untrusted locations may also have copies:\n"
|
2016-01-15 18:16:48 +00:00
|
|
|
ppwhereis h ls urls = do
|
|
|
|
descm <- uuidDescriptions
|
Fix mangling of --json output of utf-8 characters when not running in a utf-8 locale
As long as all code imports Utility.Aeson rather than Data.Aeson,
and no Strings that may contain utf-8 characters are used for eg, object
keys via T.pack, this is guaranteed to fix the problem everywhere that
git-annex generates json.
It's kind of annoying to need to wrap ToJSON with a ToJSON', especially
since every data type that has a ToJSON instance has to be ported over.
However, that only took 50 lines of code, which is worth it to ensure full
coverage. I initially tried an alternative approach of a newtype FileEncoded,
which had to be used everywhere a String was fed into aeson, and chasing
down all the sites would have been far too hard. Did consider creating an
intentionally overlapping instance ToJSON String, and letting ghc fail
to build anything that passed in a String, but am not sure that wouldn't
pollute some library that git-annex depends on that happens to use ToJSON
String internally.
This commit was supported by the NSF-funded DataLad project.
2018-04-16 19:42:45 +00:00
|
|
|
let urlvals = map (\(u, us) -> (u, Just (V.fromList us))) $
|
2016-01-15 18:16:48 +00:00
|
|
|
filter (\(u,_) -> u `elem` ls) urls
|
|
|
|
prettyPrintUUIDsWith (Just "urls") h descm (const Nothing) urlvals
|
|
|
|
|
|
|
|
getUUIDUrls :: Key -> [UUID] -> M.Map UUID Remote -> Annex [(UUID, [URLString])]
|
|
|
|
getUUIDUrls key uuids remotemap = forM uuids $ \uu -> (,)
|
|
|
|
<$> pure uu
|
|
|
|
<*> maybe (pure []) (getRemoteUrls key) (M.lookup uu remotemap)
|
2012-02-14 07:49:48 +00:00
|
|
|
|
2016-01-15 18:16:48 +00:00
|
|
|
getRemoteUrls :: Key -> Remote -> Annex [URLString]
|
|
|
|
getRemoteUrls key remote
|
|
|
|
| uuid remote == webUUID = getWebUrls key
|
|
|
|
| otherwise = (++)
|
2014-12-08 23:14:24 +00:00
|
|
|
<$> askremote
|
|
|
|
<*> claimedurls
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2020-02-27 18:09:18 +00:00
|
|
|
askremote = case whereisKey remote of
|
|
|
|
Nothing -> pure []
|
|
|
|
Just w -> tryNonAsync (w key) >>= \case
|
|
|
|
Right l -> pure l
|
|
|
|
Left e -> do
|
|
|
|
warning $ unwords
|
|
|
|
[ "unable to query remote"
|
|
|
|
, name remote
|
|
|
|
, "for urls:"
|
|
|
|
, show e
|
|
|
|
]
|
|
|
|
return []
|
2014-12-08 23:14:24 +00:00
|
|
|
claimedurls = do
|
|
|
|
us <- map fst
|
|
|
|
. filter (\(_, d) -> d == OtherDownloader)
|
|
|
|
. map getDownloader
|
|
|
|
<$> getUrls key
|
2014-12-11 18:09:57 +00:00
|
|
|
filterM (\u -> (==) <$> pure remote <*> claimingUrl u) us
|
2015-08-17 15:35:34 +00:00
|
|
|
|
2016-01-15 18:16:48 +00:00
|
|
|
showRemoteUrls :: M.Map UUID Remote -> (UUID, [URLString]) -> Annex ()
|
|
|
|
showRemoteUrls remotemap (uu, us)
|
|
|
|
| null us = noop
|
|
|
|
| otherwise = case M.lookup uu remotemap of
|
2016-09-09 19:49:44 +00:00
|
|
|
Just r -> showLongNote $
|
|
|
|
unlines $ map (\u -> name r ++ ": " ++ u) us
|
2016-01-15 18:16:48 +00:00
|
|
|
Nothing -> noop
|