From 7db734afad313b8669623138130797fad797daa7 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Wed, 5 Aug 2020 22:46:21 -0400 Subject: feat(tvix): Implement AddToStoreNar Implement both the client and server sides of AddToStoreNar, using a templated generalization of the sources and sinks we were using for AddToStore on both ends. Change-Id: I73d0ed34118c711b125851dff99a7518ced4af35 Reviewed-on: https://cl.tvl.fyi/c/depot/+/1686 Tested-by: BuildkiteCI Reviewed-by: kanepyork --- third_party/nix/src/nix-daemon/nix-daemon-proto.cc | 86 ++++++++++++++++++++-- 1 file changed, 79 insertions(+), 7 deletions(-) (limited to 'third_party/nix/src/nix-daemon') diff --git a/third_party/nix/src/nix-daemon/nix-daemon-proto.cc b/third_party/nix/src/nix-daemon/nix-daemon-proto.cc index b310cc92c43c..33e440dfde92 100644 --- a/third_party/nix/src/nix-daemon/nix-daemon-proto.cc +++ b/third_party/nix/src/nix-daemon/nix-daemon-proto.cc @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -10,6 +11,7 @@ #include #include #include +#include #include "libmain/shared.hh" #include "libproto/worker.grpc.pb.h" @@ -24,6 +26,7 @@ namespace nix::daemon { +using ::google::protobuf::util::TimeUtil; using ::grpc::Status; using ::nix::proto::BuildStatus; using ::nix::proto::PathInfo; @@ -31,20 +34,20 @@ using ::nix::proto::StorePath; using ::nix::proto::StorePaths; using ::nix::proto::WorkerService; -class AddToStoreRequestSource final : public Source { - using Reader = grpc::ServerReader; - +template +class RPCSource final : public Source { public: - explicit AddToStoreRequestSource(Reader* reader) : reader_(reader) {} + using Reader = grpc::ServerReader; + explicit RPCSource(Reader* reader) : reader_(reader) {} size_t read(unsigned char* data, size_t len) override { auto got = buffer_.sgetn(reinterpret_cast(data), len); if (got < len) { - proto::AddToStoreRequest msg; + Request msg; if (!reader_->Read(&msg)) { return got; } - if (msg.add_oneof_case() != proto::AddToStoreRequest::kData) { + if (msg.add_oneof_case() != Request::kData) { // TODO(grfn): Make Source::read return a StatusOr and get rid of this // throw throw Error( @@ -152,7 +155,7 @@ class WorkerServiceImpl final : public WorkerService::Service { } auto meta = metadata_request.meta(); - AddToStoreRequestSource source(reader); + RPCSource source(reader); auto opt_hash_type = hash_type_from(meta.hash_type()); if (!opt_hash_type) { return Status(grpc::StatusCode::INVALID_ARGUMENT, @@ -194,6 +197,62 @@ class WorkerServiceImpl final : public WorkerService::Service { __FUNCTION__); } + Status AddToStoreNar( + grpc::ServerContext* context, + grpc::ServerReader* reader, + google::protobuf::Empty*) override { + return HandleExceptions( + [&]() -> Status { + proto::AddToStoreNarRequest path_info_request; + auto has_path_info = reader->Read(&path_info_request); + if (!has_path_info || !path_info_request.has_path_info()) { + return Status(grpc::StatusCode::INVALID_ARGUMENT, + "Path info must be set before sending nar content"); + } + + auto path_info = path_info_request.path_info(); + + ValidPathInfo info; + info.path = path_info.path().path(); + info.deriver = path_info.deriver().path(); + + if (!info.deriver.empty()) { + ASSERT_INPUT_STORE_PATH(info.deriver); + } + + auto nar_hash = Hash::deserialize(path_info.nar_hash(), htSHA256); + + if (!nar_hash.ok()) { + return Status(grpc::StatusCode::INVALID_ARGUMENT, + std::string(nar_hash.status().message())); + } + + info.narHash = nar_hash.ConsumeValueOrDie(); + for (const auto& ref : path_info.references()) { + info.references.insert(ref); + } + info.registrationTime = + TimeUtil::TimestampToTimeT(path_info.registration_time()); + info.narSize = path_info.nar_size(); + info.ultimate = path_info.ultimate(); + for (const auto& sig : path_info.sigs()) { + info.sigs.insert(sig); + } + info.ca = path_info.ca(); + + auto repair = path_info.repair(); + auto check_sigs = path_info.check_sigs(); + + std::string saved; + RPCSource source(reader); + store_->addToStore(info, source, repair ? Repair : NoRepair, + check_sigs ? CheckSigs : NoCheckSigs, nullptr); + + return Status::OK; + }, + __FUNCTION__); + } + Status AddTextToStore(grpc::ServerContext*, const nix::proto::AddTextToStoreRequest* request, nix::proto::StorePath* response) override { @@ -228,6 +287,19 @@ class WorkerServiceImpl final : public WorkerService::Service { return Status::OK; } + Status AddTempRoot(grpc::ServerContext*, const nix::proto::StorePath* request, + google::protobuf::Empty*) override { + auto path = request->path(); + ASSERT_INPUT_STORE_PATH(path); + + return HandleExceptions( + [&]() -> Status { + store_->addTempRoot(path); + return Status::OK; + }, + __FUNCTION__); + } + Status AddIndirectRoot(grpc::ServerContext*, const nix::proto::StorePath* request, google::protobuf::Empty*) override { -- cgit 1.4.1