Allow parameters in store URIs
This is to allow store-specific configuration, e.g. s3://my-cache?compression=bzip2&secret-key=/path/to/key.
This commit is contained in:
		
							parent
							
								
									aa3bc3d5dc
								
							
						
					
					
						commit
						95d20dfde9
					
				
					 9 changed files with 56 additions and 8 deletions
				
			
		|  | @ -85,7 +85,10 @@ protected: | ||||||
| 
 | 
 | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| static RegisterStoreImplementation regStore([](const std::string & uri) -> std::shared_ptr<Store> { | static RegisterStoreImplementation regStore([]( | ||||||
|  |     const std::string & uri, const StoreParams & params) | ||||||
|  |     -> std::shared_ptr<Store> | ||||||
|  | { | ||||||
|     if (std::string(uri, 0, 7) != "http://" && |     if (std::string(uri, 0, 7) != "http://" && | ||||||
|         std::string(uri, 0, 8) != "https://") return 0; |         std::string(uri, 0, 8) != "https://") return 0; | ||||||
|     auto store = std::make_shared<HttpBinaryCacheStore>(std::shared_ptr<Store>(0), |     auto store = std::make_shared<HttpBinaryCacheStore>(std::shared_ptr<Store>(0), | ||||||
|  |  | ||||||
|  | @ -16,6 +16,11 @@ public: | ||||||
| 
 | 
 | ||||||
|     void init() override; |     void init() override; | ||||||
| 
 | 
 | ||||||
|  |     std::string getUri() override | ||||||
|  |     { | ||||||
|  |         return "file://" + binaryCacheDir; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
| protected: | protected: | ||||||
| 
 | 
 | ||||||
|     bool fileExists(const std::string & path) override; |     bool fileExists(const std::string & path) override; | ||||||
|  | @ -78,7 +83,10 @@ ref<Store> openLocalBinaryCacheStore(std::shared_ptr<Store> localStore, | ||||||
|     return store; |     return store; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static RegisterStoreImplementation regStore([](const std::string & uri) -> std::shared_ptr<Store> { | static RegisterStoreImplementation regStore([]( | ||||||
|  |     const std::string & uri, const StoreParams & params) | ||||||
|  |     -> std::shared_ptr<Store> | ||||||
|  | { | ||||||
|     if (std::string(uri, 0, 7) != "file://") return 0; |     if (std::string(uri, 0, 7) != "file://") return 0; | ||||||
|     return openLocalBinaryCacheStore(std::shared_ptr<Store>(0), |     return openLocalBinaryCacheStore(std::shared_ptr<Store>(0), | ||||||
|         settings.get("binary-cache-secret-key-file", string("")), |         settings.get("binary-cache-secret-key-file", string("")), | ||||||
|  |  | ||||||
|  | @ -229,6 +229,12 @@ LocalStore::~LocalStore() | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | std::string LocalStore::getUri() | ||||||
|  | { | ||||||
|  |     return "local"; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| int LocalStore::getSchema() | int LocalStore::getSchema() | ||||||
| { | { | ||||||
|     int curSchema = 0; |     int curSchema = 0; | ||||||
|  |  | ||||||
|  | @ -87,6 +87,8 @@ public: | ||||||
| 
 | 
 | ||||||
|     /* Implementations of abstract store API methods. */ |     /* Implementations of abstract store API methods. */ | ||||||
| 
 | 
 | ||||||
|  |     std::string getUri() override; | ||||||
|  | 
 | ||||||
|     bool isValidPathUncached(const Path & path) override; |     bool isValidPathUncached(const Path & path) override; | ||||||
| 
 | 
 | ||||||
|     PathSet queryValidPaths(const PathSet & paths) override; |     PathSet queryValidPaths(const PathSet & paths) override; | ||||||
|  |  | ||||||
|  | @ -49,6 +49,12 @@ RemoteStore::RemoteStore(size_t maxConnections) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | std::string RemoteStore::getUri() | ||||||
|  | { | ||||||
|  |     return "daemon"; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| ref<RemoteStore::Connection> RemoteStore::openConnection() | ref<RemoteStore::Connection> RemoteStore::openConnection() | ||||||
| { | { | ||||||
|     auto conn = make_ref<Connection>(); |     auto conn = make_ref<Connection>(); | ||||||
|  |  | ||||||
|  | @ -26,6 +26,8 @@ public: | ||||||
| 
 | 
 | ||||||
|     /* Implementations of abstract store API methods. */ |     /* Implementations of abstract store API methods. */ | ||||||
| 
 | 
 | ||||||
|  |     std::string getUri() override; | ||||||
|  | 
 | ||||||
|     bool isValidPathUncached(const Path & path) override; |     bool isValidPathUncached(const Path & path) override; | ||||||
| 
 | 
 | ||||||
|     PathSet queryValidPaths(const PathSet & paths) override; |     PathSet queryValidPaths(const PathSet & paths) override; | ||||||
|  |  | ||||||
|  | @ -239,7 +239,10 @@ struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore | ||||||
| 
 | 
 | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| static RegisterStoreImplementation regStore([](const std::string & uri) -> std::shared_ptr<Store> { | static RegisterStoreImplementation regStore([]( | ||||||
|  |     const std::string & uri, const StoreParams & params) | ||||||
|  |     -> std::shared_ptr<Store> | ||||||
|  | { | ||||||
|     if (std::string(uri, 0, 5) != "s3://") return 0; |     if (std::string(uri, 0, 5) != "s3://") return 0; | ||||||
|     auto store = std::make_shared<S3BinaryCacheStoreImpl>(std::shared_ptr<Store>(0), |     auto store = std::make_shared<S3BinaryCacheStoreImpl>(std::shared_ptr<Store>(0), | ||||||
|         settings.get("binary-cache-secret-key-file", string("")), |         settings.get("binary-cache-secret-key-file", string("")), | ||||||
|  |  | ||||||
|  | @ -461,10 +461,22 @@ namespace nix { | ||||||
| RegisterStoreImplementation::Implementations * RegisterStoreImplementation::implementations = 0; | RegisterStoreImplementation::Implementations * RegisterStoreImplementation::implementations = 0; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| ref<Store> openStoreAt(const std::string & uri) | ref<Store> openStoreAt(const std::string & uri_) | ||||||
| { | { | ||||||
|  |     auto uri(uri_); | ||||||
|  |     StoreParams params; | ||||||
|  |     auto q = uri.find('?'); | ||||||
|  |     if (q != std::string::npos) { | ||||||
|  |         for (auto s : tokenizeString<Strings>(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) { |     for (auto fun : *RegisterStoreImplementation::implementations) { | ||||||
|         auto store = fun(uri); |         auto store = fun(uri, params); | ||||||
|         if (store) return ref<Store>(store); |         if (store) return ref<Store>(store); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -478,7 +490,10 @@ ref<Store> openStore() | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| static RegisterStoreImplementation regStore([](const std::string & uri) -> std::shared_ptr<Store> { | static RegisterStoreImplementation regStore([]( | ||||||
|  |     const std::string & uri, const StoreParams & params) | ||||||
|  |     -> std::shared_ptr<Store> | ||||||
|  | { | ||||||
|     enum { mDaemon, mLocal, mAuto } mode; |     enum { mDaemon, mLocal, mAuto } mode; | ||||||
| 
 | 
 | ||||||
|     if (uri == "daemon") mode = mDaemon; |     if (uri == "daemon") mode = mDaemon; | ||||||
|  |  | ||||||
|  | @ -192,7 +192,7 @@ public: | ||||||
| 
 | 
 | ||||||
|     virtual ~Store() { } |     virtual ~Store() { } | ||||||
| 
 | 
 | ||||||
|     virtual std::string getUri(); |     virtual std::string getUri() = 0; | ||||||
| 
 | 
 | ||||||
|     /* Check whether a path is valid. */ |     /* Check whether a path is valid. */ | ||||||
|     bool isValidPath(const Path & path); |     bool isValidPath(const Path & path); | ||||||
|  | @ -540,7 +540,10 @@ std::list<ref<Store>> getDefaultSubstituters(); | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| /* Store implementation registration. */ | /* Store implementation registration. */ | ||||||
| typedef std::function<std::shared_ptr<Store>(const std::string & uri)> OpenStore; | typedef std::map<std::string, std::string> StoreParams; | ||||||
|  | 
 | ||||||
|  | typedef std::function<std::shared_ptr<Store>( | ||||||
|  |     const std::string & uri, const StoreParams & params)> OpenStore; | ||||||
| 
 | 
 | ||||||
| struct RegisterStoreImplementation | struct RegisterStoreImplementation | ||||||
| { | { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue