From a91954f0c658e90b08f7f6e371305281e4d7d329 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Fri, 2 Sep 2016 06:35:48 -0400 Subject: Merge openStore and openStoreAt with default arguments --- src/libstore/store-api.cc | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'src/libstore/store-api.cc') diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 5dd56f905d57..75456ab8c8af 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -506,7 +506,7 @@ namespace nix { RegisterStoreImplementation::Implementations * RegisterStoreImplementation::implementations = 0; -ref openStoreAt(const std::string & uri_) +ref openStore(const std::string & uri_) { auto uri(uri_); Store::Params params; @@ -529,12 +529,6 @@ ref openStoreAt(const std::string & uri_) } -ref openStore() -{ - return openStoreAt(getEnv("NIX_REMOTE")); -} - - static RegisterStoreImplementation regStore([]( const std::string & uri, const Store::Params & params) -> std::shared_ptr @@ -579,7 +573,7 @@ std::list> getDefaultSubstituters() auto addStore = [&](const std::string & uri) { if (done.count(uri)) return; done.insert(uri); - state->stores.push_back(openStoreAt(uri)); + state->stores.push_back(openStore(uri)); }; for (auto uri : settings.get("substituters", Strings())) -- cgit 1.4.1 From 53b27ddce22869430e2ab0932c32d8e3c3844564 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Fri, 2 Sep 2016 06:39:29 -0400 Subject: Factor a function to get the store type from a URI out of the main RegisterStoreImplementation --- src/libstore/store-api.cc | 43 +++++++++++++++++++++++++------------------ src/libstore/store-api.hh | 10 ++++++++++ 2 files changed, 35 insertions(+), 18 deletions(-) (limited to 'src/libstore/store-api.cc') diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 75456ab8c8af..604f0dac8989 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -529,30 +529,37 @@ ref openStore(const std::string & uri_) } -static RegisterStoreImplementation regStore([]( - const std::string & uri, const Store::Params & params) - -> std::shared_ptr +StoreType getStoreType(const std::string & uri, const std::string & stateDir) { - enum { mDaemon, mLocal, mAuto } mode; - - if (uri == "daemon") mode = mDaemon; - else if (uri == "local") mode = mLocal; - else if (uri == "") mode = mAuto; - else return 0; - - if (mode == mAuto) { - auto stateDir = get(params, "state", settings.nixStateDir); + if (uri == "daemon") { + return tDaemon; + } else if (uri == "local") { + return tLocal; + } else if (uri == "") { if (access(stateDir.c_str(), R_OK | W_OK) == 0) - mode = mLocal; + return tLocal; else if (pathExists(settings.nixDaemonSocketFile)) - mode = mDaemon; + return tDaemon; else - mode = mLocal; + return tLocal; + } else { + return tOther; } +} - return mode == mDaemon - ? std::shared_ptr(std::make_shared(params)) - : std::shared_ptr(std::make_shared(params)); + +static RegisterStoreImplementation regStore([]( + const std::string & uri, const Store::Params & params) + -> std::shared_ptr +{ + switch (getStoreType(uri, get(params, "state", settings.nixStateDir))) { + case tDaemon: + return std::shared_ptr(std::make_shared(params)); + case tLocal: + return std::shared_ptr(std::make_shared(params)); + default: + return nullptr; + } }); diff --git a/src/libstore/store-api.hh b/src/libstore/store-api.hh index 76a2f5e9c791..3d8b4fbbb0cc 100644 --- a/src/libstore/store-api.hh +++ b/src/libstore/store-api.hh @@ -5,6 +5,7 @@ #include "crypto.hh" #include "lru-cache.hh" #include "sync.hh" +#include "globals.hh" #include #include @@ -590,6 +591,15 @@ void removeTempRoots(); ref openStore(const std::string & uri = getEnv("NIX_REMOTE")); +enum StoreType { + tDaemon, + tLocal, + tOther +}; + + +StoreType getStoreType(const std::string & uri = getEnv("NIX_REMOTE"), const std::string & stateDir = settings.nixStateDir); + /* Return the default substituter stores, defined by the ‘substituters’ option and various legacy options like ‘binary-caches’. */ -- cgit 1.4.1 From 0f3963329018d9cf930e2a0e2b0ec2f4e26b40b3 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Fri, 2 Sep 2016 14:15:04 -0400 Subject: Factor out the unix domain socket-specific code from RemoteStore --- src/libstore/local-store.cc | 3 +- src/libstore/remote-store.cc | 65 ++++++++++++++++++++++++++------------------ src/libstore/remote-store.hh | 33 +++++++++++++++++----- src/libstore/store-api.cc | 2 +- src/libstore/store-api.hh | 2 +- 5 files changed, 69 insertions(+), 36 deletions(-) (limited to 'src/libstore/store-api.cc') diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index 10056f2f1fd8..272d48741114 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -37,7 +37,8 @@ namespace nix { LocalStore::LocalStore(const Params & params) - : LocalFSStore(params) + : Store(params) + , LocalFSStore(params) , realStoreDir(get(params, "real", rootDir != "" ? rootDir + "/nix/store" : storeDir)) , dbDir(stateDir + "/db") , linksDir(realStoreDir + "/.links") diff --git a/src/libstore/remote-store.cc b/src/libstore/remote-store.cc index 94075f3b9b39..232f62e7ab6d 100644 --- a/src/libstore/remote-store.cc +++ b/src/libstore/remote-store.cc @@ -38,9 +38,9 @@ template T readStorePaths(Store & store, Source & from) template PathSet readStorePaths(Store & store, Source & from); - +/* TODO: Separate these store impls into different files, give them better names */ RemoteStore::RemoteStore(const Params & params, size_t maxConnections) - : LocalFSStore(params) + : Store(params) , connections(make_ref>( maxConnections, [this]() { return openConnection(); }, @@ -50,13 +50,21 @@ RemoteStore::RemoteStore(const Params & params, size_t maxConnections) } -std::string RemoteStore::getUri() +UDSRemoteStore::UDSRemoteStore(const Params & params, size_t maxConnections) + : Store(params) + , LocalFSStore(params) + , RemoteStore(params, maxConnections) +{ +} + + +std::string UDSRemoteStore::getUri() { return "daemon"; } -ref RemoteStore::openConnection() +ref UDSRemoteStore::openConnection() { auto conn = make_ref(); @@ -84,46 +92,52 @@ ref RemoteStore::openConnection() conn->from.fd = conn->fd.get(); conn->to.fd = conn->fd.get(); + initConnection(*conn); + + return conn; +} + + +void RemoteStore::initConnection(Connection & conn) +{ /* Send the magic greeting, check for the reply. */ try { - conn->to << WORKER_MAGIC_1; - conn->to.flush(); - unsigned int magic = readInt(conn->from); + conn.to << WORKER_MAGIC_1; + conn.to.flush(); + unsigned int magic = readInt(conn.from); if (magic != WORKER_MAGIC_2) throw Error("protocol mismatch"); - conn->daemonVersion = readInt(conn->from); - if (GET_PROTOCOL_MAJOR(conn->daemonVersion) != GET_PROTOCOL_MAJOR(PROTOCOL_VERSION)) + conn.daemonVersion = readInt(conn.from); + if (GET_PROTOCOL_MAJOR(conn.daemonVersion) != GET_PROTOCOL_MAJOR(PROTOCOL_VERSION)) throw Error("Nix daemon protocol version not supported"); - if (GET_PROTOCOL_MINOR(conn->daemonVersion) < 10) + if (GET_PROTOCOL_MINOR(conn.daemonVersion) < 10) throw Error("the Nix daemon version is too old"); - conn->to << PROTOCOL_VERSION; + conn.to << PROTOCOL_VERSION; - if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 14) { + if (GET_PROTOCOL_MINOR(conn.daemonVersion) >= 14) { int cpu = settings.lockCPU ? lockToCurrentCPU() : -1; if (cpu != -1) - conn->to << 1 << cpu; + conn.to << 1 << cpu; else - conn->to << 0; + conn.to << 0; } - if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 11) - conn->to << false; + if (GET_PROTOCOL_MINOR(conn.daemonVersion) >= 11) + conn.to << false; - conn->processStderr(); + conn.processStderr(); } catch (Error & e) { throw Error(format("cannot start daemon worker: %1%") % e.msg()); } setOptions(conn); - - return conn; } -void RemoteStore::setOptions(ref conn) +void RemoteStore::setOptions(Connection & conn) { - conn->to << wopSetOptions + conn.to << wopSetOptions << settings.keepFailed << settings.keepGoing << settings.tryFallback @@ -137,16 +151,16 @@ void RemoteStore::setOptions(ref conn) << settings.buildCores << settings.useSubstitutes; - if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 12) { + if (GET_PROTOCOL_MINOR(conn.daemonVersion) >= 12) { Settings::SettingsMap overrides = settings.getOverrides(); if (overrides["ssh-auth-sock"] == "") overrides["ssh-auth-sock"] = getEnv("SSH_AUTH_SOCK"); - conn->to << overrides.size(); + conn.to << overrides.size(); for (auto & i : overrides) - conn->to << i.first << i.second; + conn.to << i.first << i.second; } - conn->processStderr(); + conn.processStderr(); } @@ -528,7 +542,6 @@ RemoteStore::Connection::~Connection() { try { to.flush(); - fd = -1; } catch (...) { ignoreException(); } diff --git a/src/libstore/remote-store.hh b/src/libstore/remote-store.hh index e756805ea05b..a69a4f2a3b8b 100644 --- a/src/libstore/remote-store.hh +++ b/src/libstore/remote-store.hh @@ -18,7 +18,7 @@ template class Pool; /* FIXME: RemoteStore is a misnomer - should be something like DaemonStore. */ -class RemoteStore : public LocalFSStore +class RemoteStore : public virtual Store { public: @@ -26,8 +26,6 @@ public: /* Implementations of abstract store API methods. */ - std::string getUri() override; - bool isValidPathUncached(const Path & path) override; PathSet queryValidPaths(const PathSet & paths) override; @@ -84,11 +82,10 @@ public: void addSignatures(const Path & storePath, const StringSet & sigs) override; -private: +protected: struct Connection { - AutoCloseFD fd; FdSink to; FdSource from; unsigned int daemonVersion; @@ -98,11 +95,33 @@ private: void processStderr(Sink * sink = 0, Source * source = 0); }; + virtual ref openConnection() = 0; + + void setOptions(Connection & conn); + + void initConnection(Connection & conn); + +private: + ref> connections; +}; - ref openConnection(); +class UDSRemoteStore : public LocalFSStore, public RemoteStore +{ +public: + + UDSRemoteStore(const Params & params, size_t maxConnections = std::numeric_limits::max()); + + std::string getUri() override; + +private: + + struct Connection : RemoteStore::Connection + { + AutoCloseFD fd; + }; - void setOptions(ref conn); + ref openConnection() override; }; diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 604f0dac8989..1ce483ca991a 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -554,7 +554,7 @@ static RegisterStoreImplementation regStore([]( { switch (getStoreType(uri, get(params, "state", settings.nixStateDir))) { case tDaemon: - return std::shared_ptr(std::make_shared(params)); + return std::shared_ptr(std::make_shared(params)); case tLocal: return std::shared_ptr(std::make_shared(params)); default: diff --git a/src/libstore/store-api.hh b/src/libstore/store-api.hh index 3d8b4fbbb0cc..ae1d51016e99 100644 --- a/src/libstore/store-api.hh +++ b/src/libstore/store-api.hh @@ -526,7 +526,7 @@ protected: }; -class LocalFSStore : public Store +class LocalFSStore : public virtual Store { public: const Path rootDir; -- cgit 1.4.1