Factor a function to get the store type from a URI out of the main RegisterStoreImplementation

This commit is contained in:
Shea Levy 2016-09-02 06:39:29 -04:00
parent a91954f0c6
commit 53b27ddce2
2 changed files with 36 additions and 19 deletions

View file

@ -529,30 +529,37 @@ ref<Store> openStore(const std::string & uri_)
} }
StoreType getStoreType(const std::string & uri, const std::string & stateDir)
{
if (uri == "daemon") {
return tDaemon;
} else if (uri == "local") {
return tLocal;
} else if (uri == "") {
if (access(stateDir.c_str(), R_OK | W_OK) == 0)
return tLocal;
else if (pathExists(settings.nixDaemonSocketFile))
return tDaemon;
else
return tLocal;
} else {
return tOther;
}
}
static RegisterStoreImplementation regStore([]( static RegisterStoreImplementation regStore([](
const std::string & uri, const Store::Params & params) const std::string & uri, const Store::Params & params)
-> std::shared_ptr<Store> -> std::shared_ptr<Store>
{ {
enum { mDaemon, mLocal, mAuto } mode; switch (getStoreType(uri, get(params, "state", settings.nixStateDir))) {
case tDaemon:
if (uri == "daemon") mode = mDaemon; return std::shared_ptr<Store>(std::make_shared<RemoteStore>(params));
else if (uri == "local") mode = mLocal; case tLocal:
else if (uri == "") mode = mAuto; return std::shared_ptr<Store>(std::make_shared<LocalStore>(params));
else return 0; default:
return nullptr;
if (mode == mAuto) {
auto stateDir = get(params, "state", settings.nixStateDir);
if (access(stateDir.c_str(), R_OK | W_OK) == 0)
mode = mLocal;
else if (pathExists(settings.nixDaemonSocketFile))
mode = mDaemon;
else
mode = mLocal;
} }
return mode == mDaemon
? std::shared_ptr<Store>(std::make_shared<RemoteStore>(params))
: std::shared_ptr<Store>(std::make_shared<LocalStore>(params));
}); });

View file

@ -5,6 +5,7 @@
#include "crypto.hh" #include "crypto.hh"
#include "lru-cache.hh" #include "lru-cache.hh"
#include "sync.hh" #include "sync.hh"
#include "globals.hh"
#include <atomic> #include <atomic>
#include <limits> #include <limits>
@ -590,6 +591,15 @@ void removeTempRoots();
ref<Store> openStore(const std::string & uri = getEnv("NIX_REMOTE")); ref<Store> 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 /* Return the default substituter stores, defined by the
substituters option and various legacy options like substituters option and various legacy options like
binary-caches. */ binary-caches. */