mirror of
https://github.com/NixOS/nix.git
synced 2024-11-22 06:42:28 +00:00
LegacySSHStore::openConnection
move more logic inside catch block
Broader error handling logic is more robust.
This commit is contained in:
parent
ce2f714e6d
commit
4580bed3e4
@ -61,28 +61,27 @@ ref<LegacySSHStore::Connection> LegacySSHStore::openConnection()
|
|||||||
conn->to = FdSink(conn->sshConn->in.get());
|
conn->to = FdSink(conn->sshConn->in.get());
|
||||||
conn->from = FdSource(conn->sshConn->out.get());
|
conn->from = FdSource(conn->sshConn->out.get());
|
||||||
|
|
||||||
|
StringSink saved;
|
||||||
|
TeeSource tee(conn->from, saved);
|
||||||
try {
|
try {
|
||||||
conn->to << SERVE_MAGIC_1 << SERVE_PROTOCOL_VERSION;
|
conn->to << SERVE_MAGIC_1 << SERVE_PROTOCOL_VERSION;
|
||||||
conn->to.flush();
|
conn->to.flush();
|
||||||
|
|
||||||
StringSink saved;
|
unsigned int magic = readInt(conn->from);
|
||||||
try {
|
if (magic != SERVE_MAGIC_2)
|
||||||
TeeSource tee(conn->from, saved);
|
throw Error("'nix-store --serve' protocol mismatch from '%s'", host);
|
||||||
unsigned int magic = readInt(tee);
|
|
||||||
if (magic != SERVE_MAGIC_2)
|
|
||||||
throw Error("'nix-store --serve' protocol mismatch from '%s'", host);
|
|
||||||
} catch (SerialisationError & e) {
|
|
||||||
/* In case the other side is waiting for our input,
|
|
||||||
close it. */
|
|
||||||
conn->sshConn->in.close();
|
|
||||||
auto msg = conn->from.drain();
|
|
||||||
throw Error("'nix-store --serve' protocol mismatch from '%s', got '%s'",
|
|
||||||
host, chomp(saved.s + msg));
|
|
||||||
}
|
|
||||||
conn->remoteVersion = readInt(conn->from);
|
conn->remoteVersion = readInt(conn->from);
|
||||||
if (GET_PROTOCOL_MAJOR(conn->remoteVersion) != 0x200)
|
if (GET_PROTOCOL_MAJOR(conn->remoteVersion) != 0x200)
|
||||||
throw Error("unsupported 'nix-store --serve' protocol version on '%s'", host);
|
throw Error("unsupported 'nix-store --serve' protocol version on '%s'", host);
|
||||||
|
} catch (SerialisationError & e) {
|
||||||
|
// in.close(): Don't let the remote block on us not writing.
|
||||||
|
conn->sshConn->in.close();
|
||||||
|
{
|
||||||
|
NullSink nullSink;
|
||||||
|
conn->from.drainInto(nullSink);
|
||||||
|
}
|
||||||
|
throw Error("'nix-store --serve' protocol mismatch from '%s', got '%s'",
|
||||||
|
host, chomp(saved.s));
|
||||||
} catch (EndOfFile & e) {
|
} catch (EndOfFile & e) {
|
||||||
throw Error("cannot connect to '%1%'", host);
|
throw Error("cannot connect to '%1%'", host);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user