Likewise namespace and enum struct-ify ServeCommand

The motivation is exactly the same as for the last commit. In addition,
this anticipates us formally defining separate serialisers for the serve
protocol.
This commit is contained in:
John Ericson 2023-05-26 11:22:24 -04:00
parent 95eae0c002
commit 4e8b495ad7
3 changed files with 66 additions and 30 deletions

View file

@ -134,7 +134,7 @@ struct LegacySSHStore : public virtual LegacySSHStoreConfig, public virtual Stor
debug("querying remote host '%s' for info on '%s'", host, printStorePath(path)); debug("querying remote host '%s' for info on '%s'", host, printStorePath(path));
conn->to << cmdQueryPathInfos << PathSet{printStorePath(path)}; conn->to << ServeProto::Command::QueryPathInfos << PathSet{printStorePath(path)};
conn->to.flush(); conn->to.flush();
auto p = readString(conn->from); auto p = readString(conn->from);
@ -177,7 +177,7 @@ struct LegacySSHStore : public virtual LegacySSHStoreConfig, public virtual Stor
if (GET_PROTOCOL_MINOR(conn->remoteVersion) >= 5) { if (GET_PROTOCOL_MINOR(conn->remoteVersion) >= 5) {
conn->to conn->to
<< cmdAddToStoreNar << ServeProto::Command::AddToStoreNar
<< printStorePath(info.path) << printStorePath(info.path)
<< (info.deriver ? printStorePath(*info.deriver) : "") << (info.deriver ? printStorePath(*info.deriver) : "")
<< info.narHash.to_string(Base16, false); << info.narHash.to_string(Base16, false);
@ -199,7 +199,7 @@ struct LegacySSHStore : public virtual LegacySSHStoreConfig, public virtual Stor
} else { } else {
conn->to conn->to
<< cmdImportPaths << ServeProto::Command::ImportPaths
<< 1; << 1;
try { try {
copyNAR(source, conn->to); copyNAR(source, conn->to);
@ -227,7 +227,7 @@ struct LegacySSHStore : public virtual LegacySSHStoreConfig, public virtual Stor
{ {
auto conn(connections->get()); auto conn(connections->get());
conn->to << cmdDumpStorePath << printStorePath(path); conn->to << ServeProto::Command::DumpStorePath << printStorePath(path);
conn->to.flush(); conn->to.flush();
copyNAR(conn->from, sink); copyNAR(conn->from, sink);
} }
@ -280,7 +280,7 @@ public:
auto conn(connections->get()); auto conn(connections->get());
conn->to conn->to
<< cmdBuildDerivation << ServeProto::Command::BuildDerivation
<< printStorePath(drvPath); << printStorePath(drvPath);
writeDerivation(conn->to, *this, drv); writeDerivation(conn->to, *this, drv);
@ -311,7 +311,7 @@ public:
auto conn(connections->get()); auto conn(connections->get());
conn->to << cmdBuildPaths; conn->to << ServeProto::Command::BuildPaths;
Strings ss; Strings ss;
for (auto & p : drvPaths) { for (auto & p : drvPaths) {
auto sOrDrvPath = StorePathWithOutputs::tryFromDerivedPath(p); auto sOrDrvPath = StorePathWithOutputs::tryFromDerivedPath(p);
@ -368,7 +368,7 @@ public:
auto conn(connections->get()); auto conn(connections->get());
conn->to conn->to
<< cmdQueryClosure << ServeProto::Command::QueryClosure
<< includeOutputs; << includeOutputs;
WorkerProto::write(*this, conn->to, paths); WorkerProto::write(*this, conn->to, paths);
conn->to.flush(); conn->to.flush();
@ -383,7 +383,7 @@ public:
auto conn(connections->get()); auto conn(connections->get());
conn->to conn->to
<< cmdQueryValidPaths << ServeProto::Command::QueryValidPaths
<< false // lock << false // lock
<< maybeSubstitute; << maybeSubstitute;
WorkerProto::write(*this, conn->to, paths); WorkerProto::write(*this, conn->to, paths);

View file

@ -10,16 +10,52 @@ namespace nix {
#define GET_PROTOCOL_MAJOR(x) ((x) & 0xff00) #define GET_PROTOCOL_MAJOR(x) ((x) & 0xff00)
#define GET_PROTOCOL_MINOR(x) ((x) & 0x00ff) #define GET_PROTOCOL_MINOR(x) ((x) & 0x00ff)
typedef enum { /**
cmdQueryValidPaths = 1, * The "serve protocol", used by ssh:// stores.
cmdQueryPathInfos = 2, *
cmdDumpStorePath = 3, * This `struct` is basically just a `namespace`; We use a type rather
cmdImportPaths = 4, * than a namespace just so we can use it as a template argument.
cmdExportPaths = 5, */
cmdBuildPaths = 6, struct ServeProto
cmdQueryClosure = 7, {
cmdBuildDerivation = 8, /**
cmdAddToStoreNar = 9, * Enumeration of all the request types for the protocol.
} ServeCommand; */
enum struct Command : uint64_t;
};
enum struct ServeProto::Command : uint64_t
{
QueryValidPaths = 1,
QueryPathInfos = 2,
DumpStorePath = 3,
ImportPaths = 4,
ExportPaths = 5,
BuildPaths = 6,
QueryClosure = 7,
BuildDerivation = 8,
AddToStoreNar = 9,
};
/**
* Convenience for sending operation codes.
*
* @todo Switch to using `ServeProto::Serialize` instead probably. But
* this was not done at this time so there would be less churn.
*/
inline Sink & operator << (Sink & sink, ServeProto::Command op)
{
return sink << (uint64_t) op;
}
/**
* Convenience for debugging.
*
* @todo Perhaps render known opcodes more nicely.
*/
inline std::ostream & operator << (std::ostream & s, ServeProto::Command op)
{
return s << (uint64_t) op;
}
} }

View file

@ -838,16 +838,16 @@ static void opServe(Strings opFlags, Strings opArgs)
}; };
while (true) { while (true) {
ServeCommand cmd; ServeProto::Command cmd;
try { try {
cmd = (ServeCommand) readInt(in); cmd = (ServeProto::Command) readInt(in);
} catch (EndOfFile & e) { } catch (EndOfFile & e) {
break; break;
} }
switch (cmd) { switch (cmd) {
case cmdQueryValidPaths: { case ServeProto::Command::QueryValidPaths: {
bool lock = readInt(in); bool lock = readInt(in);
bool substitute = readInt(in); bool substitute = readInt(in);
auto paths = WorkerProto::Serialise<StorePathSet>::read(*store, in); auto paths = WorkerProto::Serialise<StorePathSet>::read(*store, in);
@ -863,7 +863,7 @@ static void opServe(Strings opFlags, Strings opArgs)
break; break;
} }
case cmdQueryPathInfos: { case ServeProto::Command::QueryPathInfos: {
auto paths = WorkerProto::Serialise<StorePathSet>::read(*store, in); auto paths = WorkerProto::Serialise<StorePathSet>::read(*store, in);
// !!! Maybe we want a queryPathInfos? // !!! Maybe we want a queryPathInfos?
for (auto & i : paths) { for (auto & i : paths) {
@ -886,24 +886,24 @@ static void opServe(Strings opFlags, Strings opArgs)
break; break;
} }
case cmdDumpStorePath: case ServeProto::Command::DumpStorePath:
store->narFromPath(store->parseStorePath(readString(in)), out); store->narFromPath(store->parseStorePath(readString(in)), out);
break; break;
case cmdImportPaths: { case ServeProto::Command::ImportPaths: {
if (!writeAllowed) throw Error("importing paths is not allowed"); if (!writeAllowed) throw Error("importing paths is not allowed");
store->importPaths(in, NoCheckSigs); // FIXME: should we skip sig checking? store->importPaths(in, NoCheckSigs); // FIXME: should we skip sig checking?
out << 1; // indicate success out << 1; // indicate success
break; break;
} }
case cmdExportPaths: { case ServeProto::Command::ExportPaths: {
readInt(in); // obsolete readInt(in); // obsolete
store->exportPaths(WorkerProto::Serialise<StorePathSet>::read(*store, in), out); store->exportPaths(WorkerProto::Serialise<StorePathSet>::read(*store, in), out);
break; break;
} }
case cmdBuildPaths: { case ServeProto::Command::BuildPaths: {
if (!writeAllowed) throw Error("building paths is not allowed"); if (!writeAllowed) throw Error("building paths is not allowed");
@ -924,7 +924,7 @@ static void opServe(Strings opFlags, Strings opArgs)
break; break;
} }
case cmdBuildDerivation: { /* Used by hydra-queue-runner. */ case ServeProto::Command::BuildDerivation: { /* Used by hydra-queue-runner. */
if (!writeAllowed) throw Error("building paths is not allowed"); if (!writeAllowed) throw Error("building paths is not allowed");
@ -951,7 +951,7 @@ static void opServe(Strings opFlags, Strings opArgs)
break; break;
} }
case cmdQueryClosure: { case ServeProto::Command::QueryClosure: {
bool includeOutputs = readInt(in); bool includeOutputs = readInt(in);
StorePathSet closure; StorePathSet closure;
store->computeFSClosure(WorkerProto::Serialise<StorePathSet>::read(*store, in), store->computeFSClosure(WorkerProto::Serialise<StorePathSet>::read(*store, in),
@ -960,7 +960,7 @@ static void opServe(Strings opFlags, Strings opArgs)
break; break;
} }
case cmdAddToStoreNar: { case ServeProto::Command::AddToStoreNar: {
if (!writeAllowed) throw Error("importing paths is not allowed"); if (!writeAllowed) throw Error("importing paths is not allowed");
auto path = readString(in); auto path = readString(in);