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') diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 75456ab8c..604f0dac8 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 76a2f5e9c..3d8b4fbbb 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 v1.2.3