1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-02 21:51:50 +02:00

Deduplicate the Store downcasting with a template

This commit is contained in:
John Ericson 2022-03-09 15:27:48 +00:00
parent 678d1c2aa0
commit a03b1fd7f6
11 changed files with 39 additions and 41 deletions

View file

@ -3,6 +3,7 @@
#include "worker-protocol.hh"
#include "build-result.hh"
#include "store-api.hh"
#include "store-cast.hh"
#include "gc-store.hh"
#include "log-store.hh"
#include "path-with-outputs.hh"
@ -646,7 +647,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
Path path = absPath(readString(from));
logger->startWork();
auto & gcStore = GcStore::require(*store);
auto & gcStore = require<GcStore>(*store);
gcStore.addIndirectRoot(path);
logger->stopWork();
@ -664,7 +665,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
case wopFindRoots: {
logger->startWork();
auto & gcStore = GcStore::require(*store);
auto & gcStore = require<GcStore>(*store);
Roots roots = gcStore.findRoots(!trusted);
logger->stopWork();
@ -696,7 +697,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
logger->startWork();
if (options.ignoreLiveness)
throw Error("you are not allowed to ignore liveness");
auto & gcStore = GcStore::require(*store);
auto & gcStore = require<GcStore>(*store);
gcStore.collectGarbage(options, results);
logger->stopWork();
@ -954,7 +955,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
logger->startWork();
if (!trusted)
throw Error("you are not privileged to add logs");
auto & logStore = LogStore::require(*store);
auto & logStore = require<LogStore>(*store);
{
FramedSource source(from);
StringSink sink;

View file

@ -1,13 +0,0 @@
#include "gc-store.hh"
namespace nix {
GcStore & GcStore::require(Store & store)
{
auto * gcStore = dynamic_cast<GcStore *>(&store);
if (!gcStore)
throw UsageError("Garbage collection not supported by store '%s'", store.getUri());
return *gcStore;
}
}

View file

@ -61,6 +61,8 @@ struct GCResults
struct GcStore : public virtual Store
{
inline static std::string operationName = "Garbage collection";
/* Add an indirect root, which is merely a symlink to `path' from
/nix/var/nix/gcroots/auto/<hash of `path'>. `path' is supposed
to be a symlink to a store path. The garbage collector will
@ -77,8 +79,6 @@ struct GcStore : public virtual Store
/* Perform a garbage collection. */
virtual void collectGarbage(const GCOptions & options, GCResults & results) = 0;
static GcStore & require(Store & store);
};
}

View file

@ -1,13 +0,0 @@
#include "log-store.hh"
namespace nix {
LogStore & LogStore::require(Store & store)
{
auto * gcStore = dynamic_cast<LogStore *>(&store);
if (!gcStore)
throw UsageError("Build log storage and retrieval not supported by store '%s'", store.getUri());
return *gcStore;
}
}

View file

@ -7,6 +7,8 @@ namespace nix {
struct LogStore : public virtual Store
{
inline static std::string operationName = "Build log storage and retrieval";
/* Return the build log of the specified store path, if available,
or null otherwise. */
virtual std::optional<std::string> getBuildLog(const StorePath & path) = 0;

View file

@ -0,0 +1,16 @@
#pragma once
#include "store-api.hh"
namespace nix {
template<typename T>
T & require(Store & store)
{
auto * castedStore = dynamic_cast<T *>(&store);
if (!castedStore)
throw UsageError("%s not supported by store '%s'", T::operationName, store.getUri());
return *castedStore;
}
}