From 95d20dfde94fc715f39e2ffeadefc5b5bd5e570b Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 29 Apr 2016 16:26:16 +0200 Subject: Allow parameters in store URIs This is to allow store-specific configuration, e.g. s3://my-cache?compression=bzip2&secret-key=/path/to/key. --- src/libstore/http-binary-cache-store.cc | 5 ++++- src/libstore/local-binary-cache-store.cc | 10 +++++++++- src/libstore/local-store.cc | 6 ++++++ src/libstore/local-store.hh | 2 ++ src/libstore/remote-store.cc | 6 ++++++ src/libstore/remote-store.hh | 2 ++ src/libstore/s3-binary-cache-store.cc | 5 ++++- src/libstore/store-api.cc | 21 ++++++++++++++++++--- src/libstore/store-api.hh | 7 +++++-- 9 files changed, 56 insertions(+), 8 deletions(-) (limited to 'src/libstore') diff --git a/src/libstore/http-binary-cache-store.cc b/src/libstore/http-binary-cache-store.cc index 771eb42ee561..392945ca771d 100644 --- a/src/libstore/http-binary-cache-store.cc +++ b/src/libstore/http-binary-cache-store.cc @@ -85,7 +85,10 @@ protected: }; -static RegisterStoreImplementation regStore([](const std::string & uri) -> std::shared_ptr { +static RegisterStoreImplementation regStore([]( + const std::string & uri, const StoreParams & params) + -> std::shared_ptr +{ if (std::string(uri, 0, 7) != "http://" && std::string(uri, 0, 8) != "https://") return 0; auto store = std::make_shared(std::shared_ptr(0), diff --git a/src/libstore/local-binary-cache-store.cc b/src/libstore/local-binary-cache-store.cc index 7968c98b9558..2ec9a0d1045c 100644 --- a/src/libstore/local-binary-cache-store.cc +++ b/src/libstore/local-binary-cache-store.cc @@ -16,6 +16,11 @@ public: void init() override; + std::string getUri() override + { + return "file://" + binaryCacheDir; + } + protected: bool fileExists(const std::string & path) override; @@ -78,7 +83,10 @@ ref openLocalBinaryCacheStore(std::shared_ptr localStore, return store; } -static RegisterStoreImplementation regStore([](const std::string & uri) -> std::shared_ptr { +static RegisterStoreImplementation regStore([]( + const std::string & uri, const StoreParams & params) + -> std::shared_ptr +{ if (std::string(uri, 0, 7) != "file://") return 0; return openLocalBinaryCacheStore(std::shared_ptr(0), settings.get("binary-cache-secret-key-file", string("")), diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index 45ffbad67a58..01a11f11f65d 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -229,6 +229,12 @@ LocalStore::~LocalStore() } +std::string LocalStore::getUri() +{ + return "local"; +} + + int LocalStore::getSchema() { int curSchema = 0; diff --git a/src/libstore/local-store.hh b/src/libstore/local-store.hh index abc4f719be1e..6f2341decfbd 100644 --- a/src/libstore/local-store.hh +++ b/src/libstore/local-store.hh @@ -87,6 +87,8 @@ public: /* Implementations of abstract store API methods. */ + std::string getUri() override; + bool isValidPathUncached(const Path & path) override; PathSet queryValidPaths(const PathSet & paths) override; diff --git a/src/libstore/remote-store.cc b/src/libstore/remote-store.cc index 4663291b9171..5a254a6104f4 100644 --- a/src/libstore/remote-store.cc +++ b/src/libstore/remote-store.cc @@ -49,6 +49,12 @@ RemoteStore::RemoteStore(size_t maxConnections) } +std::string RemoteStore::getUri() +{ + return "daemon"; +} + + ref RemoteStore::openConnection() { auto conn = make_ref(); diff --git a/src/libstore/remote-store.hh b/src/libstore/remote-store.hh index 4ea8e8a5be4d..8e45a7449e2e 100644 --- a/src/libstore/remote-store.hh +++ b/src/libstore/remote-store.hh @@ -26,6 +26,8 @@ public: /* Implementations of abstract store API methods. */ + std::string getUri() override; + bool isValidPathUncached(const Path & path) override; PathSet queryValidPaths(const PathSet & paths) override; diff --git a/src/libstore/s3-binary-cache-store.cc b/src/libstore/s3-binary-cache-store.cc index 6fadf7be28c9..cd88a3271244 100644 --- a/src/libstore/s3-binary-cache-store.cc +++ b/src/libstore/s3-binary-cache-store.cc @@ -239,7 +239,10 @@ struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore }; -static RegisterStoreImplementation regStore([](const std::string & uri) -> std::shared_ptr { +static RegisterStoreImplementation regStore([]( + const std::string & uri, const StoreParams & params) + -> std::shared_ptr +{ if (std::string(uri, 0, 5) != "s3://") return 0; auto store = std::make_shared(std::shared_ptr(0), settings.get("binary-cache-secret-key-file", string("")), diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 2763f5ad4199..463e132e0299 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -461,10 +461,22 @@ namespace nix { RegisterStoreImplementation::Implementations * RegisterStoreImplementation::implementations = 0; -ref openStoreAt(const std::string & uri) +ref openStoreAt(const std::string & uri_) { + auto uri(uri_); + StoreParams params; + auto q = uri.find('?'); + if (q != std::string::npos) { + for (auto s : tokenizeString(uri.substr(q + 1), "&")) { + auto e = s.find('='); + if (e != std::string::npos) + params[s.substr(0, e)] = s.substr(e + 1); + } + uri = uri_.substr(0, q); + } + for (auto fun : *RegisterStoreImplementation::implementations) { - auto store = fun(uri); + auto store = fun(uri, params); if (store) return ref(store); } @@ -478,7 +490,10 @@ ref openStore() } -static RegisterStoreImplementation regStore([](const std::string & uri) -> std::shared_ptr { +static RegisterStoreImplementation regStore([]( + const std::string & uri, const StoreParams & params) + -> std::shared_ptr +{ enum { mDaemon, mLocal, mAuto } mode; if (uri == "daemon") mode = mDaemon; diff --git a/src/libstore/store-api.hh b/src/libstore/store-api.hh index 87b8e884759c..cdde0be7b91c 100644 --- a/src/libstore/store-api.hh +++ b/src/libstore/store-api.hh @@ -192,7 +192,7 @@ public: virtual ~Store() { } - virtual std::string getUri(); + virtual std::string getUri() = 0; /* Check whether a path is valid. */ bool isValidPath(const Path & path); @@ -540,7 +540,10 @@ std::list> getDefaultSubstituters(); /* Store implementation registration. */ -typedef std::function(const std::string & uri)> OpenStore; +typedef std::map StoreParams; + +typedef std::function( + const std::string & uri, const StoreParams & params)> OpenStore; struct RegisterStoreImplementation { -- cgit 1.4.1