Skip to content

Commit

Permalink
Merge pull request NixOS#10781 from obsidiansystems/build-mode-parse
Browse files Browse the repository at this point in the history
Worker proto use proper serialiser for `BuildMode`
  • Loading branch information
Ericson2314 authored May 27, 2024
2 parents 263905d + 8ebd99c commit 1796444
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/libstore/daemon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
auto drvs = WorkerProto::Serialise<DerivedPaths>::read(*store, rconn);
BuildMode mode = bmNormal;
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"
clients.
Expand All @@ -555,7 +555,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
case WorkerProto::Op::BuildPathsWithResults: {
auto drvs = WorkerProto::Serialise<DerivedPaths>::read(*store, rconn);
BuildMode mode = bmNormal;
mode = (BuildMode) readInt(from);
mode = WorkerProto::Serialise<BuildMode>::read(*store, rconn);

/* Repairing is not atomic, so disallowed for "untrusted"
clients.
Expand Down Expand Up @@ -586,7 +586,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
* correctly.
*/
readDerivation(from, *store, drv, Derivation::nameFromPath(drvPath));
BuildMode buildMode = (BuildMode) readInt(from);
auto buildMode = WorkerProto::Serialise<BuildMode>::read(*store, rconn);
logger->startWork();

auto drvType = drv.type();
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/store-api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ enum SubstituteFlag : bool { NoSubstitute = false, Substitute = true };
const uint32_t exportMagic = 0x4558494e;


enum BuildMode { bmNormal, bmRepair, bmCheck };
enum BuildMode : uint8_t { bmNormal, bmRepair, bmCheck };
enum TrustedFlag : bool { NotTrusted = false, Trusted = true };

struct BuildResult;
Expand Down
28 changes: 28 additions & 0 deletions src/libstore/worker-protocol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,34 @@ namespace nix {

/* 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)
{
auto temp = readNum<uint8_t>(conn.from);
Expand Down
3 changes: 3 additions & 0 deletions src/libstore/worker-protocol.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct BuildResult;
struct KeyedBuildResult;
struct ValidPathInfo;
struct UnkeyedValidPathInfo;
enum BuildMode : uint8_t;
enum TrustedFlag : bool;


Expand Down Expand Up @@ -215,6 +216,8 @@ DECLARE_WORKER_SERIALISER(ValidPathInfo);
template<>
DECLARE_WORKER_SERIALISER(UnkeyedValidPathInfo);
template<>
DECLARE_WORKER_SERIALISER(BuildMode);
template<>
DECLARE_WORKER_SERIALISER(std::optional<TrustedFlag>);
template<>
DECLARE_WORKER_SERIALISER(std::optional<std::chrono::microseconds>);
Expand Down
Binary file not shown.
11 changes: 11 additions & 0 deletions tests/unit/libstore/worker-protocol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
WorkerProtoTest,
optionalTrustedFlag,
Expand Down

0 comments on commit 1796444

Please sign in to comment.