mirror of
https://github.com/NixOS/nix.git
synced 2025-11-16 23:42:43 +01:00
Merge pull request #13739 from obsidiansystems/getUri-not-string
Rewrite `StoreConfig::getUri` in terms of new `StoreConfig::getReference`
This commit is contained in:
commit
cf7084a67c
22 changed files with 181 additions and 60 deletions
|
|
@ -33,9 +33,14 @@ struct DummyStoreConfig : public std::enable_shared_from_this<DummyStoreConfig>,
|
|||
|
||||
ref<Store> openStore() const override;
|
||||
|
||||
std::string getUri() const override
|
||||
StoreReference getReference() const override
|
||||
{
|
||||
return *uriSchemes().begin();
|
||||
return {
|
||||
.variant =
|
||||
StoreReference::Specified{
|
||||
.scheme = *uriSchemes().begin(),
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -22,13 +22,25 @@ HttpBinaryCacheStoreConfig::HttpBinaryCacheStoreConfig(
|
|||
std::string_view scheme, std::string_view _cacheUri, const Params & params)
|
||||
: StoreConfig(params)
|
||||
, BinaryCacheStoreConfig(params)
|
||||
, cacheUri(
|
||||
, cacheUri(parseURL(
|
||||
std::string{scheme} + "://"
|
||||
+ (!_cacheUri.empty() ? _cacheUri
|
||||
: throw UsageError("`%s` Store requires a non-empty authority in Store URL", scheme)))
|
||||
: throw UsageError("`%s` Store requires a non-empty authority in Store URL", scheme))))
|
||||
{
|
||||
while (!cacheUri.empty() && cacheUri.back() == '/')
|
||||
cacheUri.pop_back();
|
||||
while (!cacheUri.path.empty() && cacheUri.path.back() == '/')
|
||||
cacheUri.path.pop_back();
|
||||
}
|
||||
|
||||
StoreReference HttpBinaryCacheStoreConfig::getReference() const
|
||||
{
|
||||
return {
|
||||
.variant =
|
||||
StoreReference::Specified{
|
||||
.scheme = cacheUri.scheme,
|
||||
.authority = (cacheUri.authority ? cacheUri.authority->to_string() : "") + cacheUri.path,
|
||||
},
|
||||
.params = cacheUri.query,
|
||||
};
|
||||
}
|
||||
|
||||
std::string HttpBinaryCacheStoreConfig::doc()
|
||||
|
|
@ -65,16 +77,17 @@ public:
|
|||
void init() override
|
||||
{
|
||||
// FIXME: do this lazily?
|
||||
if (auto cacheInfo = diskCache->upToDateCacheExists(config->cacheUri)) {
|
||||
if (auto cacheInfo = diskCache->upToDateCacheExists(config->cacheUri.to_string())) {
|
||||
config->wantMassQuery.setDefault(cacheInfo->wantMassQuery);
|
||||
config->priority.setDefault(cacheInfo->priority);
|
||||
} else {
|
||||
try {
|
||||
BinaryCacheStore::init();
|
||||
} catch (UploadToHTTP &) {
|
||||
throw Error("'%s' does not appear to be a binary cache", config->cacheUri);
|
||||
throw Error("'%s' does not appear to be a binary cache", config->cacheUri.to_string());
|
||||
}
|
||||
diskCache->createCache(config->cacheUri, config->storeDir, config->wantMassQuery, config->priority);
|
||||
diskCache->createCache(
|
||||
config->cacheUri.to_string(), config->storeDir, config->wantMassQuery, config->priority);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -134,16 +147,29 @@ protected:
|
|||
try {
|
||||
getFileTransfer()->upload(req);
|
||||
} catch (FileTransferError & e) {
|
||||
throw UploadToHTTP("while uploading to HTTP binary cache at '%s': %s", config->cacheUri, e.msg());
|
||||
throw UploadToHTTP(
|
||||
"while uploading to HTTP binary cache at '%s': %s", config->cacheUri.to_string(), e.msg());
|
||||
}
|
||||
}
|
||||
|
||||
FileTransferRequest makeRequest(const std::string & path)
|
||||
{
|
||||
/* FIXME path is not a path, but a full relative or absolute
|
||||
URL, e.g. we've seen in the wild NARINFO files have a URL
|
||||
field which is
|
||||
`nar/15f99rdaf26k39knmzry4xd0d97wp6yfpnfk1z9avakis7ipb9yg.nar?hash=zphkqn2wg8mnvbkixnl2aadkbn0rcnfj`
|
||||
(note the query param) and that gets passed here.
|
||||
|
||||
What should actually happen is that we have two parsed URLs
|
||||
(if we support relative URLs), and then we combined them with
|
||||
a URL `operator/` which would be like
|
||||
`std::filesystem::path`'s equivalent operator, which properly
|
||||
combines the the URLs, whether the right is relative or
|
||||
absolute. */
|
||||
return FileTransferRequest(
|
||||
hasPrefix(path, "https://") || hasPrefix(path, "http://") || hasPrefix(path, "file://")
|
||||
? path
|
||||
: config->cacheUri + "/" + path);
|
||||
: config->cacheUri.to_string() + "/" + path);
|
||||
}
|
||||
|
||||
void getFile(const std::string & path, Sink & sink) override
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
#include "nix/util/url.hh"
|
||||
#include "nix/store/binary-cache-store.hh"
|
||||
|
||||
namespace nix {
|
||||
|
|
@ -11,7 +12,7 @@ struct HttpBinaryCacheStoreConfig : std::enable_shared_from_this<HttpBinaryCache
|
|||
HttpBinaryCacheStoreConfig(
|
||||
std::string_view scheme, std::string_view cacheUri, const Store::Config::Params & params);
|
||||
|
||||
Path cacheUri;
|
||||
ParsedURL cacheUri;
|
||||
|
||||
static const std::string name()
|
||||
{
|
||||
|
|
@ -24,10 +25,7 @@ struct HttpBinaryCacheStoreConfig : std::enable_shared_from_this<HttpBinaryCache
|
|||
|
||||
ref<Store> openStore() const override;
|
||||
|
||||
std::string getUri() const override
|
||||
{
|
||||
return cacheUri;
|
||||
}
|
||||
StoreReference getReference() const override;
|
||||
};
|
||||
|
||||
} // namespace nix
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ struct LegacySSHStoreConfig : std::enable_shared_from_this<LegacySSHStoreConfig>
|
|||
|
||||
ref<Store> openStore() const override;
|
||||
|
||||
std::string getUri() const override;
|
||||
StoreReference getReference() const override;
|
||||
};
|
||||
|
||||
struct LegacySSHStore : public virtual Store
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ struct LocalBinaryCacheStoreConfig : std::enable_shared_from_this<LocalBinaryCac
|
|||
|
||||
ref<Store> openStore() const override;
|
||||
|
||||
std::string getUri() const override;
|
||||
StoreReference getReference() const override;
|
||||
};
|
||||
|
||||
} // namespace nix
|
||||
|
|
|
|||
|
|
@ -88,10 +88,7 @@ struct LocalOverlayStoreConfig : virtual LocalStoreConfig
|
|||
|
||||
ref<Store> openStore() const override;
|
||||
|
||||
std::string getUri() const override
|
||||
{
|
||||
return "local-overlay://";
|
||||
}
|
||||
StoreReference getReference() const override;
|
||||
|
||||
protected:
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -112,7 +112,7 @@ struct LocalStoreConfig : std::enable_shared_from_this<LocalStoreConfig>,
|
|||
|
||||
ref<Store> openStore() const override;
|
||||
|
||||
std::string getUri() const override;
|
||||
StoreReference getReference() const override;
|
||||
};
|
||||
|
||||
class LocalStore : public virtual IndirectRootStore, public virtual GcStore
|
||||
|
|
|
|||
|
|
@ -107,7 +107,7 @@ public:
|
|||
|
||||
ref<Store> openStore() const override;
|
||||
|
||||
std::string getUri() const override;
|
||||
StoreReference getReference() const override;
|
||||
};
|
||||
|
||||
struct S3BinaryCacheStore : virtual BinaryCacheStore
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ struct SSHStoreConfig : std::enable_shared_from_this<SSHStoreConfig>,
|
|||
|
||||
ref<Store> openStore() const override;
|
||||
|
||||
std::string getUri() const override;
|
||||
StoreReference getReference() const override;
|
||||
};
|
||||
|
||||
struct MountedSSHStoreConfig : virtual SSHStoreConfig, virtual LocalFSStoreConfig
|
||||
|
|
|
|||
|
|
@ -199,11 +199,16 @@ struct StoreConfig : public StoreDirConfig
|
|||
virtual ref<Store> openStore() const = 0;
|
||||
|
||||
/**
|
||||
* Render the config back to a "store URL". It should round-trip
|
||||
* Render the config back to a `StoreReference`. It should round-trip
|
||||
* with `resolveStoreConfig` (for stores configs that are
|
||||
* registered).
|
||||
*/
|
||||
virtual std::string getUri() const;
|
||||
virtual StoreReference getReference() const;
|
||||
|
||||
std::string getUri() const
|
||||
{
|
||||
return getReference().render();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ struct UDSRemoteStoreConfig : std::enable_shared_from_this<UDSRemoteStoreConfig>
|
|||
|
||||
ref<Store> openStore() const override;
|
||||
|
||||
std::string getUri() const override;
|
||||
StoreReference getReference() const override;
|
||||
};
|
||||
|
||||
struct UDSRemoteStore : virtual IndirectRootStore, virtual RemoteStore
|
||||
|
|
|
|||
|
|
@ -88,9 +88,16 @@ ref<LegacySSHStore::Connection> LegacySSHStore::openConnection()
|
|||
return conn;
|
||||
};
|
||||
|
||||
std::string LegacySSHStoreConfig::getUri() const
|
||||
StoreReference LegacySSHStoreConfig::getReference() const
|
||||
{
|
||||
return ParsedURL{.scheme = *uriSchemes().begin(), .authority = authority, .query = getQueryParams()}.to_string();
|
||||
return {
|
||||
.variant =
|
||||
StoreReference::Specified{
|
||||
.scheme = *uriSchemes().begin(),
|
||||
.authority = authority.to_string(),
|
||||
},
|
||||
.params = getQueryParams(),
|
||||
};
|
||||
}
|
||||
|
||||
std::map<StorePath, UnkeyedValidPathInfo> LegacySSHStore::queryPathInfosUncached(const StorePathSet & paths)
|
||||
|
|
|
|||
|
|
@ -23,9 +23,15 @@ std::string LocalBinaryCacheStoreConfig::doc()
|
|||
;
|
||||
}
|
||||
|
||||
std::string LocalBinaryCacheStoreConfig::getUri() const
|
||||
StoreReference LocalBinaryCacheStoreConfig::getReference() const
|
||||
{
|
||||
return "file://" + binaryCacheDir;
|
||||
return {
|
||||
.variant =
|
||||
StoreReference::Specified{
|
||||
.scheme = "file",
|
||||
.authority = binaryCacheDir,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
struct LocalBinaryCacheStore : virtual BinaryCacheStore
|
||||
|
|
|
|||
|
|
@ -23,6 +23,16 @@ ref<Store> LocalOverlayStoreConfig::openStore() const
|
|||
ref{std::dynamic_pointer_cast<const LocalOverlayStoreConfig>(shared_from_this())});
|
||||
}
|
||||
|
||||
StoreReference LocalOverlayStoreConfig::getReference() const
|
||||
{
|
||||
return {
|
||||
.variant =
|
||||
StoreReference::Specified{
|
||||
.scheme = *uriSchemes().begin(),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Path LocalOverlayStoreConfig::toUpperPath(const StorePath & path) const
|
||||
{
|
||||
return upperLayer + "/" + path.to_string();
|
||||
|
|
|
|||
|
|
@ -439,15 +439,15 @@ LocalStore::~LocalStore()
|
|||
}
|
||||
}
|
||||
|
||||
std::string LocalStoreConfig::getUri() const
|
||||
StoreReference LocalStoreConfig::getReference() const
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << *uriSchemes().begin();
|
||||
auto queryParams = getQueryParams();
|
||||
if (!queryParams.empty())
|
||||
oss << "?";
|
||||
oss << encodeQuery(queryParams);
|
||||
return std::move(oss).str();
|
||||
return {
|
||||
.variant =
|
||||
StoreReference::Specified{
|
||||
.scheme = *uriSchemes().begin(),
|
||||
},
|
||||
.params = getQueryParams(),
|
||||
};
|
||||
}
|
||||
|
||||
int LocalStore::getSchema()
|
||||
|
|
|
|||
|
|
@ -254,9 +254,15 @@ std::string S3BinaryCacheStoreConfig::doc()
|
|||
;
|
||||
}
|
||||
|
||||
std::string S3BinaryCacheStoreConfig::getUri() const
|
||||
StoreReference S3BinaryCacheStoreConfig::getReference() const
|
||||
{
|
||||
return "s3://" + bucketName;
|
||||
return {
|
||||
.variant =
|
||||
StoreReference::Specified{
|
||||
.scheme = *uriSchemes().begin(),
|
||||
.authority = bucketName,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
struct S3BinaryCacheStoreImpl : virtual S3BinaryCacheStore
|
||||
|
|
|
|||
|
|
@ -25,9 +25,16 @@ std::string SSHStoreConfig::doc()
|
|||
;
|
||||
}
|
||||
|
||||
std::string SSHStoreConfig::getUri() const
|
||||
StoreReference SSHStoreConfig::getReference() const
|
||||
{
|
||||
return ParsedURL{.scheme = *uriSchemes().begin(), .authority = authority, .query = getQueryParams()}.to_string();
|
||||
return {
|
||||
.variant =
|
||||
StoreReference::Specified{
|
||||
.scheme = *uriSchemes().begin(),
|
||||
.authority = authority.to_string(),
|
||||
},
|
||||
.params = getQueryParams(),
|
||||
};
|
||||
}
|
||||
|
||||
struct SSHStore : virtual RemoteStore
|
||||
|
|
|
|||
|
|
@ -300,9 +300,9 @@ Store::Store(const Store::Config & config)
|
|||
assertLibStoreInitialized();
|
||||
}
|
||||
|
||||
std::string StoreConfig::getUri() const
|
||||
StoreReference StoreConfig::getReference() const
|
||||
{
|
||||
return "";
|
||||
return {.variant = StoreReference::Auto{}};
|
||||
}
|
||||
|
||||
bool Store::PathInfoCacheValue::isKnownNow()
|
||||
|
|
|
|||
|
|
@ -54,14 +54,19 @@ UDSRemoteStore::UDSRemoteStore(ref<const Config> config)
|
|||
{
|
||||
}
|
||||
|
||||
std::string UDSRemoteStoreConfig::getUri() const
|
||||
StoreReference UDSRemoteStoreConfig::getReference() const
|
||||
{
|
||||
return path == settings.nixDaemonSocketFile ? // FIXME: Not clear why we return daemon here and not default
|
||||
// to settings.nixDaemonSocketFile
|
||||
//
|
||||
// unix:// with no path also works. Change what we return?
|
||||
"daemon"
|
||||
: std::string(*uriSchemes().begin()) + "://" + path;
|
||||
return {
|
||||
.variant =
|
||||
StoreReference::Specified{
|
||||
.scheme = *uriSchemes().begin(),
|
||||
// We return the empty string when the path looks like the
|
||||
// default path, but we could also just return the path
|
||||
// verbatim always, to be robust to overall config changes
|
||||
// at the cost of some verbosity.
|
||||
.authority = path == settings.nixDaemonSocketFile ? "" : path,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
void UDSRemoteStore::Connection::closeWrite()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue