Merge pull request #10781 from obsidiansystems/build-mode-parse

Worker proto use proper serialiser for `BuildMode`
This commit is contained in:
John Ericson 2024-05-27 16:07:54 -04:00 committed by GitHub
commit 17964441d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 46 additions and 4 deletions

View file

@ -531,7 +531,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
auto drvs = WorkerProto::Serialise<DerivedPaths>::read(*store, rconn); auto drvs = WorkerProto::Serialise<DerivedPaths>::read(*store, rconn);
BuildMode mode = bmNormal; BuildMode mode = bmNormal;
if (GET_PROTOCOL_MINOR(clientVersion) >= 15) { if (GET_PROTOCOL_MINOR(clientVersion) >= 15) {
mode = (BuildMode) readInt(from); mode = WorkerProto::Serialise<BuildMode>::read(*store, rconn);
/* Repairing is not atomic, so disallowed for "untrusted" /* Repairing is not atomic, so disallowed for "untrusted"
clients. clients.
@ -555,7 +555,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
case WorkerProto::Op::BuildPathsWithResults: { case WorkerProto::Op::BuildPathsWithResults: {
auto drvs = WorkerProto::Serialise<DerivedPaths>::read(*store, rconn); auto drvs = WorkerProto::Serialise<DerivedPaths>::read(*store, rconn);
BuildMode mode = bmNormal; BuildMode mode = bmNormal;
mode = (BuildMode) readInt(from); mode = WorkerProto::Serialise<BuildMode>::read(*store, rconn);
/* Repairing is not atomic, so disallowed for "untrusted" /* Repairing is not atomic, so disallowed for "untrusted"
clients. clients.
@ -586,7 +586,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
* correctly. * correctly.
*/ */
readDerivation(from, *store, drv, Derivation::nameFromPath(drvPath)); readDerivation(from, *store, drv, Derivation::nameFromPath(drvPath));
BuildMode buildMode = (BuildMode) readInt(from); auto buildMode = WorkerProto::Serialise<BuildMode>::read(*store, rconn);
logger->startWork(); logger->startWork();
auto drvType = drv.type(); auto drvType = drv.type();

View file

@ -92,7 +92,7 @@ enum SubstituteFlag : bool { NoSubstitute = false, Substitute = true };
const uint32_t exportMagic = 0x4558494e; const uint32_t exportMagic = 0x4558494e;
enum BuildMode { bmNormal, bmRepair, bmCheck }; enum BuildMode : uint8_t { bmNormal, bmRepair, bmCheck };
enum TrustedFlag : bool { NotTrusted = false, Trusted = true }; enum TrustedFlag : bool { NotTrusted = false, Trusted = true };
struct BuildResult; struct BuildResult;

View file

@ -14,6 +14,34 @@ namespace nix {
/* protocol-specific definitions */ /* protocol-specific definitions */
BuildMode WorkerProto::Serialise<BuildMode>::read(const StoreDirConfig & store, WorkerProto::ReadConn conn)
{
auto temp = readNum<uint8_t>(conn.from);
switch (temp) {
case 0: return bmNormal;
case 1: return bmRepair;
case 2: return bmCheck;
default: throw Error("Invalid build mode");
}
}
void WorkerProto::Serialise<BuildMode>::write(const StoreDirConfig & store, WorkerProto::WriteConn conn, const BuildMode & buildMode)
{
switch (buildMode) {
case bmNormal:
conn.to << uint8_t{0};
break;
case bmRepair:
conn.to << uint8_t{1};
break;
case bmCheck:
conn.to << uint8_t{2};
break;
default:
assert(false);
};
}
std::optional<TrustedFlag> WorkerProto::Serialise<std::optional<TrustedFlag>>::read(const StoreDirConfig & store, WorkerProto::ReadConn conn) std::optional<TrustedFlag> WorkerProto::Serialise<std::optional<TrustedFlag>>::read(const StoreDirConfig & store, WorkerProto::ReadConn conn)
{ {
auto temp = readNum<uint8_t>(conn.from); auto temp = readNum<uint8_t>(conn.from);

View file

@ -35,6 +35,7 @@ struct BuildResult;
struct KeyedBuildResult; struct KeyedBuildResult;
struct ValidPathInfo; struct ValidPathInfo;
struct UnkeyedValidPathInfo; struct UnkeyedValidPathInfo;
enum BuildMode : uint8_t;
enum TrustedFlag : bool; enum TrustedFlag : bool;
@ -215,6 +216,8 @@ DECLARE_WORKER_SERIALISER(ValidPathInfo);
template<> template<>
DECLARE_WORKER_SERIALISER(UnkeyedValidPathInfo); DECLARE_WORKER_SERIALISER(UnkeyedValidPathInfo);
template<> template<>
DECLARE_WORKER_SERIALISER(BuildMode);
template<>
DECLARE_WORKER_SERIALISER(std::optional<TrustedFlag>); DECLARE_WORKER_SERIALISER(std::optional<TrustedFlag>);
template<> template<>
DECLARE_WORKER_SERIALISER(std::optional<std::chrono::microseconds>); DECLARE_WORKER_SERIALISER(std::optional<std::chrono::microseconds>);

View file

@ -529,6 +529,17 @@ VERSIONED_CHARACTERIZATION_TEST(
}), }),
})) }))
VERSIONED_CHARACTERIZATION_TEST(
WorkerProtoTest,
buildMode,
"build-mode",
defaultVersion,
(std::tuple<BuildMode, BuildMode, BuildMode> {
bmNormal,
bmRepair,
bmCheck,
}))
VERSIONED_CHARACTERIZATION_TEST( VERSIONED_CHARACTERIZATION_TEST(
WorkerProtoTest, WorkerProtoTest,
optionalTrustedFlag, optionalTrustedFlag,