2018-03-07 19:15:23 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2024-06-10 22:01:36 +00:00
|
|
|
- Copyright 2018-2024 Joey Hess <id@joeyh.name>
|
2018-03-07 19:15:23 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2018-03-07 19:15:23 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.P2PStdIO where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import P2P.IO
|
|
|
|
import P2P.Annex
|
2024-06-10 22:01:36 +00:00
|
|
|
import P2P.Proxy
|
2018-03-07 19:15:23 +00:00
|
|
|
import qualified P2P.Protocol as P2P
|
|
|
|
import qualified Annex
|
|
|
|
import Annex.UUID
|
|
|
|
import qualified CmdLine.GitAnnexShell.Checks as Checks
|
2024-06-12 14:10:11 +00:00
|
|
|
import Remote.Helper.Ssh (openP2PShellConnection', closeP2PShellConnection)
|
2024-06-12 15:37:14 +00:00
|
|
|
import Logs.Location
|
|
|
|
import qualified Remote
|
2018-03-07 19:15:23 +00:00
|
|
|
|
2018-09-25 20:49:59 +00:00
|
|
|
import System.IO.Error
|
|
|
|
|
2018-03-07 19:15:23 +00:00
|
|
|
cmd :: Command
|
|
|
|
cmd = noMessages $ command "p2pstdio" SectionPlumbing
|
|
|
|
"communicate in P2P protocol over stdio"
|
2018-03-08 20:21:16 +00:00
|
|
|
paramUUID (withParams seek)
|
2018-03-07 19:15:23 +00:00
|
|
|
|
|
|
|
seek :: CmdParams -> CommandSeek
|
2018-03-08 20:21:16 +00:00
|
|
|
seek [u] = commandAction $ start $ toUUID u
|
|
|
|
seek _ = giveup "missing UUID parameter"
|
2018-03-07 19:15:23 +00:00
|
|
|
|
2018-03-08 20:21:16 +00:00
|
|
|
start :: UUID -> CommandStart
|
finish CommandStart transition
The hoped for optimisation of CommandStart with -J did not materialize.
In fact, not runnign CommandStart in parallel is slower than -J3.
So, CommandStart are still run in parallel.
(The actual bad performance I've been seeing with -J in my big repo
has to do with building the remoteList.)
But, this is still progress toward making -J faster, because it gets rid
of the onlyActionOn roadblock in the way of making CommandCleanup jobs
run separate from CommandPerform jobs.
Added OnlyActionOn constructor for ActionItem which fixes the
onlyActionOn breakage in the last commit.
Made CustomOutput include an ActionItem, so even things using it can
specify OnlyActionOn.
In Command.Move and Command.Sync, there were CommandStarts that used
includeCommandAction, so output messages, which is no longer allowed.
Fixed by using startingCustomOutput, but that's still not quite right,
since it prevents message display for the includeCommandAction run
inside it too.
2019-06-12 13:23:26 +00:00
|
|
|
start theiruuid = startingCustomOutput (ActionItemOther Nothing) $ do
|
2018-05-25 17:17:56 +00:00
|
|
|
servermode <- liftIO $ do
|
|
|
|
ro <- Checks.checkEnvSet Checks.readOnlyEnv
|
|
|
|
ao <- Checks.checkEnvSet Checks.appendOnlyEnv
|
|
|
|
return $ case (ro, ao) of
|
|
|
|
(True, _) -> P2P.ServeReadOnly
|
|
|
|
(False, True) -> P2P.ServeAppendOnly
|
|
|
|
(False, False) -> P2P.ServeReadWrite
|
2024-06-10 22:01:36 +00:00
|
|
|
Annex.getState Annex.proxyremote >>= \case
|
|
|
|
Nothing -> performLocal theiruuid servermode
|
|
|
|
Just r -> performProxy theiruuid servermode r
|
|
|
|
|
|
|
|
performLocal :: UUID -> P2P.ServerMode -> CommandPerform
|
|
|
|
performLocal theiruuid servermode = do
|
2018-03-07 19:15:23 +00:00
|
|
|
myuuid <- getUUID
|
git-annex-shell: block relay requests
connRepo is only used when relaying git upload-pack and receive-pack.
That's only supposed to be used when git-annex-remotedaemon is serving
git-remote-tor-annex connections over tor. But, it was always set, and
so could be used in other places possibly.
Fixed by making connRepo optional in the P2P protocol interface.
In Command.EnableTor, it's not needed, because it only speaks the
protocol in order to check that it's able to connect back to itself via
the hidden service. So changed that to pass Nothing rather than the git
repo.
In Remote.Helper.Ssh, it's connecting to git-annex-shell p2pstdio,
so is making the requests, so will never need connRepo.
In git-annex-shell p2pstdio, it was accepting git upload-pack and
receive-pack requests over the P2P protocol, even though nothing sent
them. This is arguably a security hole, particularly if the user has
set environment variables like GIT_ANNEX_SHELL_LIMITED to prevent
git push/pull via git-annex-shell.
2024-06-10 17:53:28 +00:00
|
|
|
let conn = stdioP2PConnection Nothing
|
2018-03-08 18:02:18 +00:00
|
|
|
let server = do
|
|
|
|
P2P.net $ P2P.sendMessage (P2P.AUTH_SUCCESS myuuid)
|
|
|
|
P2P.serveAuthed servermode myuuid
|
2018-03-12 17:43:19 +00:00
|
|
|
runst <- liftIO $ mkRunState $ Serving theiruuid Nothing
|
|
|
|
runFullProto runst conn server >>= \case
|
2018-09-25 20:49:59 +00:00
|
|
|
Right () -> done
|
|
|
|
-- Avoid displaying an error when the client hung up on us.
|
|
|
|
Left (ProtoFailureIOError e) | isEOFError e -> done
|
|
|
|
Left e -> giveup (describeProtoFailure e)
|
|
|
|
where
|
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
|
|
|
done = next $ return True
|
2024-06-10 22:01:36 +00:00
|
|
|
|
|
|
|
performProxy :: UUID -> P2P.ServerMode -> Remote -> CommandPerform
|
|
|
|
performProxy clientuuid servermode remote = do
|
|
|
|
clientside <- ClientSide
|
|
|
|
<$> liftIO (mkRunState $ Serving clientuuid Nothing)
|
|
|
|
<*> pure (stdioP2PConnection Nothing)
|
2024-06-11 14:20:11 +00:00
|
|
|
getClientProtocolVersion remote clientside
|
|
|
|
(withclientversion clientside)
|
2024-06-11 16:05:44 +00:00
|
|
|
protoerrhandler
|
2024-06-10 22:01:36 +00:00
|
|
|
where
|
2024-06-11 14:20:11 +00:00
|
|
|
withclientversion clientside (Just (clientmaxversion, othermsg)) =
|
|
|
|
connectremote clientmaxversion $ \remoteside ->
|
2024-06-12 15:37:14 +00:00
|
|
|
proxy done proxymethods servermode clientside remoteside
|
2024-06-11 16:05:44 +00:00
|
|
|
othermsg protoerrhandler
|
2024-06-11 14:20:11 +00:00
|
|
|
withclientversion _ Nothing = done
|
|
|
|
|
2024-06-12 15:37:14 +00:00
|
|
|
proxymethods = ProxyMethods
|
|
|
|
{ removedContent = \u k -> logChange k u InfoMissing
|
|
|
|
, addedContent = \u k -> logChange k u InfoPresent
|
|
|
|
}
|
|
|
|
|
2024-06-12 14:10:11 +00:00
|
|
|
-- FIXME: Support special remotes.
|
2024-06-10 22:01:36 +00:00
|
|
|
connectremote clientmaxversion cont =
|
2024-06-12 14:10:11 +00:00
|
|
|
openP2PShellConnection' remote clientmaxversion >>= \case
|
2024-06-10 22:01:36 +00:00
|
|
|
Just conn@(P2P.IO.OpenConnection (remoterunst, remoteconn, _)) ->
|
2024-06-12 15:37:14 +00:00
|
|
|
cont (RemoteSide remoterunst remoteconn (Remote.uuid remote))
|
2024-06-12 14:10:11 +00:00
|
|
|
`finally` liftIO (closeP2PShellConnection conn)
|
2024-06-10 22:01:36 +00:00
|
|
|
_ -> giveup "Unable to connect to remote."
|
|
|
|
|
2024-06-11 16:05:44 +00:00
|
|
|
protoerrhandler cont a = a >>= \case
|
2024-06-10 22:01:36 +00:00
|
|
|
-- Avoid displaying an error when the client hung up on us.
|
|
|
|
Left (ProtoFailureIOError e) | isEOFError e -> done
|
|
|
|
Left e -> giveup (describeProtoFailure e)
|
|
|
|
Right v -> cont v
|
|
|
|
|
|
|
|
done = next $ return True
|