chore(3p/nix/daemon): replace all assertStorePath with a macro
Two exceptions: IsValidPath needs to return success for invalid paths, and QueryAllValidPaths shouldn't need to check the paths it gets from itself. Change-Id: I4d9d4125d34e8de42f30070aec607f8a902eded7 Reviewed-on: https://cl.tvl.fyi/c/depot/+/1598 Tested-by: BuildkiteCI Reviewed-by: glittershark <grfn@gws.fyi>
This commit is contained in:
		
							parent
							
								
									1f12544179
								
							
						
					
					
						commit
						661353f200
					
				
					 1 changed files with 20 additions and 14 deletions
				
			
		| 
						 | 
				
			
			@ -2,6 +2,7 @@
 | 
			
		|||
 | 
			
		||||
#include <sstream>
 | 
			
		||||
 | 
			
		||||
#include <absl/strings/str_format.h>
 | 
			
		||||
#include <google/protobuf/empty.pb.h>
 | 
			
		||||
#include <google/protobuf/util/time_util.h>
 | 
			
		||||
#include <grpcpp/impl/codegen/server_context.h>
 | 
			
		||||
| 
						 | 
				
			
			@ -83,6 +84,12 @@ struct RetrieveRegularNARSink : ParseSink {
 | 
			
		|||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#define ASSERT_INPUT_STORE_PATH(path)                                          \
 | 
			
		||||
  if (!store_->isStorePath(path)) {                                            \
 | 
			
		||||
    return Status(grpc::StatusCode::INVALID_ARGUMENT,                          \
 | 
			
		||||
                  absl::StrFormat("path '%s' is not in the Nix store", path)); \
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		||||
 public:
 | 
			
		||||
  WorkerServiceImpl(nix::Store& store) : store_(&store) {}
 | 
			
		||||
| 
						 | 
				
			
			@ -90,7 +97,6 @@ class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		|||
  Status IsValidPath(grpc::ServerContext* context, const StorePath* request,
 | 
			
		||||
                     nix::proto::IsValidPathResponse* response) override {
 | 
			
		||||
    const auto& path = request->path();
 | 
			
		||||
    store_->assertStorePath(path);
 | 
			
		||||
    response->set_is_valid(store_->isValidPath(path));
 | 
			
		||||
 | 
			
		||||
    return Status::OK;
 | 
			
		||||
| 
						 | 
				
			
			@ -99,7 +105,7 @@ class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		|||
  Status HasSubstitutes(grpc::ServerContext* context, const StorePath* request,
 | 
			
		||||
                        nix::proto::HasSubstitutesResponse* response) override {
 | 
			
		||||
    const auto& path = request->path();
 | 
			
		||||
    store_->assertStorePath(path);
 | 
			
		||||
    ASSERT_INPUT_STORE_PATH(path);
 | 
			
		||||
    PathSet res = store_->querySubstitutablePaths({path});
 | 
			
		||||
    response->set_has_substitutes(res.find(path) != res.end());
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -109,7 +115,7 @@ class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		|||
  Status QueryReferrers(grpc::ServerContext* context, const StorePath* request,
 | 
			
		||||
                        StorePaths* response) override {
 | 
			
		||||
    const auto& path = request->path();
 | 
			
		||||
    store_->assertStorePath(path);
 | 
			
		||||
    ASSERT_INPUT_STORE_PATH(path);
 | 
			
		||||
 | 
			
		||||
    PathSet paths;
 | 
			
		||||
    store_->queryReferrers(path, paths);
 | 
			
		||||
| 
						 | 
				
			
			@ -233,7 +239,7 @@ class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		|||
                            const StorePath* request,
 | 
			
		||||
                            StorePaths* response) override {
 | 
			
		||||
    const auto& path = request->path();
 | 
			
		||||
    store_->assertStorePath(path);
 | 
			
		||||
    ASSERT_INPUT_STORE_PATH(path);
 | 
			
		||||
 | 
			
		||||
    PathSet paths = store_->queryValidDerivers(path);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -248,7 +254,7 @@ class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		|||
                                const StorePath* request,
 | 
			
		||||
                                StorePaths* response) override {
 | 
			
		||||
    const auto& path = request->path();
 | 
			
		||||
    store_->assertStorePath(path);
 | 
			
		||||
    ASSERT_INPUT_STORE_PATH(path);
 | 
			
		||||
 | 
			
		||||
    PathSet paths = store_->queryDerivationOutputs(path);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -264,7 +270,6 @@ class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		|||
                            StorePaths* response) override {
 | 
			
		||||
    const auto paths = store_->queryAllValidPaths();
 | 
			
		||||
    for (const auto& path : paths) {
 | 
			
		||||
      store_->assertStorePath(path);
 | 
			
		||||
      response->add_paths(path);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -274,7 +279,8 @@ class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		|||
  Status QueryPathInfo(grpc::ServerContext* context, const StorePath* request,
 | 
			
		||||
                       PathInfo* response) override {
 | 
			
		||||
    auto path = request->path();
 | 
			
		||||
    store_->assertStorePath(path);
 | 
			
		||||
    ASSERT_INPUT_STORE_PATH(path);
 | 
			
		||||
 | 
			
		||||
    response->mutable_path()->set_path(path);
 | 
			
		||||
    try {
 | 
			
		||||
      auto info = store_->queryPathInfo(path);
 | 
			
		||||
| 
						 | 
				
			
			@ -310,7 +316,7 @@ class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		|||
      grpc::ServerContext* context, const StorePath* request,
 | 
			
		||||
      nix::proto::DerivationOutputNames* response) override {
 | 
			
		||||
    auto path = request->path();
 | 
			
		||||
    store_->assertStorePath(path);
 | 
			
		||||
    ASSERT_INPUT_STORE_PATH(path);
 | 
			
		||||
    auto names = store_->queryDerivationOutputNames(path);
 | 
			
		||||
    for (const auto& name : names) {
 | 
			
		||||
      response->add_names(name);
 | 
			
		||||
| 
						 | 
				
			
			@ -324,7 +330,7 @@ class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		|||
                               StorePath* response) override {
 | 
			
		||||
    auto hash_part = request->hash_part();
 | 
			
		||||
    auto path = store_->queryPathFromHashPart(hash_part);
 | 
			
		||||
    store_->assertStorePath(path);
 | 
			
		||||
    ASSERT_INPUT_STORE_PATH(path);
 | 
			
		||||
    response->set_path(path);
 | 
			
		||||
    return Status::OK;
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			@ -334,7 +340,7 @@ class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		|||
                         StorePaths* response) override {
 | 
			
		||||
    std::set<Path> paths;
 | 
			
		||||
    for (const auto& path : request->paths()) {
 | 
			
		||||
      store_->assertStorePath(path);
 | 
			
		||||
      ASSERT_INPUT_STORE_PATH(path);
 | 
			
		||||
      paths.insert(path);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -352,7 +358,7 @@ class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		|||
                                 StorePaths* response) override {
 | 
			
		||||
    std::set<Path> paths;
 | 
			
		||||
    for (const auto& path : request->paths()) {
 | 
			
		||||
      store_->assertStorePath(path);
 | 
			
		||||
      ASSERT_INPUT_STORE_PATH(path);
 | 
			
		||||
      paths.insert(path);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -388,7 +394,7 @@ class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		|||
      const nix::proto::BuildDerivationRequest* request,
 | 
			
		||||
      nix::proto::BuildDerivationResponse* response) override {
 | 
			
		||||
    auto drv_path = request->drv_path().path();
 | 
			
		||||
    store_->assertStorePath(drv_path);
 | 
			
		||||
    ASSERT_INPUT_STORE_PATH(drv_path);
 | 
			
		||||
    auto drv = BasicDerivation::from_proto(&request->derivation(), *store_);
 | 
			
		||||
 | 
			
		||||
    auto build_mode = nix::BuildModeFrom(request->build_mode());
 | 
			
		||||
| 
						 | 
				
			
			@ -408,7 +414,7 @@ class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		|||
                       const nix::proto::AddSignaturesRequest* request,
 | 
			
		||||
                       google::protobuf::Empty* response) override {
 | 
			
		||||
    auto path = request->path().path();
 | 
			
		||||
    store_->assertStorePath(path);
 | 
			
		||||
    ASSERT_INPUT_STORE_PATH(path);
 | 
			
		||||
 | 
			
		||||
    StringSet sigs;
 | 
			
		||||
    sigs.insert(request->sigs().sigs().begin(), request->sigs().sigs().end());
 | 
			
		||||
| 
						 | 
				
			
			@ -422,7 +428,7 @@ class WorkerServiceImpl final : public WorkerService::Service {
 | 
			
		|||
                      nix::proto::QueryMissingResponse* response) override {
 | 
			
		||||
    std::set<Path> targets;
 | 
			
		||||
    for (auto& path : request->paths()) {
 | 
			
		||||
      store_->assertStorePath(path);
 | 
			
		||||
      ASSERT_INPUT_STORE_PATH(path);
 | 
			
		||||
      targets.insert(path);
 | 
			
		||||
    }
 | 
			
		||||
    PathSet will_build;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue