Skip to content

Commit cf7084a

Browse files
authored
Merge pull request #13739 from obsidiansystems/getUri-not-string
Rewrite `StoreConfig::getUri` in terms of new `StoreConfig::getReference`
2 parents dfcbe70 + 3e7879e commit cf7084a

22 files changed

+181
-60
lines changed

src/libstore-tests/http-binary-cache-store.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ TEST(HttpBinaryCacheStore, constructConfig)
88
{
99
HttpBinaryCacheStoreConfig config{"http", "foo.bar.baz", {}};
1010

11-
EXPECT_EQ(config.cacheUri, "http://foo.bar.baz");
11+
EXPECT_EQ(config.cacheUri.to_string(), "http://foo.bar.baz");
1212
}
1313

1414
TEST(HttpBinaryCacheStore, constructConfigNoTrailingSlash)
1515
{
1616
HttpBinaryCacheStoreConfig config{"https", "foo.bar.baz/a/b/", {}};
1717

18-
EXPECT_EQ(config.cacheUri, "https://foo.bar.baz/a/b");
18+
EXPECT_EQ(config.cacheUri.to_string(), "https://foo.bar.baz/a/b");
1919
}
2020

2121
} // namespace nix

src/libstore-tests/nix_api_store.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ TEST_F(nix_api_store_test, nix_store_get_uri)
2424
std::string str;
2525
auto ret = nix_store_get_uri(ctx, store, OBSERVE_STRING(str));
2626
ASSERT_EQ(NIX_OK, ret);
27-
auto expectedStoreURI = "local?"
27+
auto expectedStoreURI = "local://?"
2828
+ nix::encodeQuery({
2929
{"log", nixLogDir},
3030
{"state", nixStateDir},
@@ -104,7 +104,7 @@ TEST_F(nix_api_util_context, nix_store_open_dummy)
104104
nix_libstore_init(ctx);
105105
Store * store = nix_store_open(ctx, "dummy://", nullptr);
106106
ASSERT_EQ(NIX_OK, ctx->last_err_code);
107-
ASSERT_STREQ("dummy", store->ptr->config.getUri().c_str());
107+
ASSERT_STREQ("dummy://", store->ptr->config.getUri().c_str());
108108

109109
std::string str;
110110
nix_store_get_version(ctx, store, OBSERVE_STRING(str));

src/libstore/dummy-store.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ struct DummyStoreConfig : public std::enable_shared_from_this<DummyStoreConfig>,
3333

3434
ref<Store> openStore() const override;
3535

36-
std::string getUri() const override
36+
StoreReference getReference() const override
3737
{
38-
return *uriSchemes().begin();
38+
return {
39+
.variant =
40+
StoreReference::Specified{
41+
.scheme = *uriSchemes().begin(),
42+
},
43+
};
3944
}
4045
};
4146

src/libstore/http-binary-cache-store.cc

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,25 @@ HttpBinaryCacheStoreConfig::HttpBinaryCacheStoreConfig(
2222
std::string_view scheme, std::string_view _cacheUri, const Params & params)
2323
: StoreConfig(params)
2424
, BinaryCacheStoreConfig(params)
25-
, cacheUri(
25+
, cacheUri(parseURL(
2626
std::string{scheme} + "://"
2727
+ (!_cacheUri.empty() ? _cacheUri
28-
: throw UsageError("`%s` Store requires a non-empty authority in Store URL", scheme)))
28+
: throw UsageError("`%s` Store requires a non-empty authority in Store URL", scheme))))
2929
{
30-
while (!cacheUri.empty() && cacheUri.back() == '/')
31-
cacheUri.pop_back();
30+
while (!cacheUri.path.empty() && cacheUri.path.back() == '/')
31+
cacheUri.path.pop_back();
32+
}
33+
34+
StoreReference HttpBinaryCacheStoreConfig::getReference() const
35+
{
36+
return {
37+
.variant =
38+
StoreReference::Specified{
39+
.scheme = cacheUri.scheme,
40+
.authority = (cacheUri.authority ? cacheUri.authority->to_string() : "") + cacheUri.path,
41+
},
42+
.params = cacheUri.query,
43+
};
3244
}
3345

3446
std::string HttpBinaryCacheStoreConfig::doc()
@@ -65,16 +77,17 @@ class HttpBinaryCacheStore : public virtual BinaryCacheStore
6577
void init() override
6678
{
6779
// FIXME: do this lazily?
68-
if (auto cacheInfo = diskCache->upToDateCacheExists(config->cacheUri)) {
80+
if (auto cacheInfo = diskCache->upToDateCacheExists(config->cacheUri.to_string())) {
6981
config->wantMassQuery.setDefault(cacheInfo->wantMassQuery);
7082
config->priority.setDefault(cacheInfo->priority);
7183
} else {
7284
try {
7385
BinaryCacheStore::init();
7486
} catch (UploadToHTTP &) {
75-
throw Error("'%s' does not appear to be a binary cache", config->cacheUri);
87+
throw Error("'%s' does not appear to be a binary cache", config->cacheUri.to_string());
7688
}
77-
diskCache->createCache(config->cacheUri, config->storeDir, config->wantMassQuery, config->priority);
89+
diskCache->createCache(
90+
config->cacheUri.to_string(), config->storeDir, config->wantMassQuery, config->priority);
7891
}
7992
}
8093

@@ -134,16 +147,29 @@ class HttpBinaryCacheStore : public virtual BinaryCacheStore
134147
try {
135148
getFileTransfer()->upload(req);
136149
} catch (FileTransferError & e) {
137-
throw UploadToHTTP("while uploading to HTTP binary cache at '%s': %s", config->cacheUri, e.msg());
150+
throw UploadToHTTP(
151+
"while uploading to HTTP binary cache at '%s': %s", config->cacheUri.to_string(), e.msg());
138152
}
139153
}
140154

141155
FileTransferRequest makeRequest(const std::string & path)
142156
{
157+
/* FIXME path is not a path, but a full relative or absolute
158+
URL, e.g. we've seen in the wild NARINFO files have a URL
159+
field which is
160+
`nar/15f99rdaf26k39knmzry4xd0d97wp6yfpnfk1z9avakis7ipb9yg.nar?hash=zphkqn2wg8mnvbkixnl2aadkbn0rcnfj`
161+
(note the query param) and that gets passed here.
162+
163+
What should actually happen is that we have two parsed URLs
164+
(if we support relative URLs), and then we combined them with
165+
a URL `operator/` which would be like
166+
`std::filesystem::path`'s equivalent operator, which properly
167+
combines the the URLs, whether the right is relative or
168+
absolute. */
143169
return FileTransferRequest(
144170
hasPrefix(path, "https://") || hasPrefix(path, "http://") || hasPrefix(path, "file://")
145171
? path
146-
: config->cacheUri + "/" + path);
172+
: config->cacheUri.to_string() + "/" + path);
147173
}
148174

149175
void getFile(const std::string & path, Sink & sink) override

src/libstore/include/nix/store/http-binary-cache-store.hh

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "nix/util/url.hh"
12
#include "nix/store/binary-cache-store.hh"
23

34
namespace nix {
@@ -11,7 +12,7 @@ struct HttpBinaryCacheStoreConfig : std::enable_shared_from_this<HttpBinaryCache
1112
HttpBinaryCacheStoreConfig(
1213
std::string_view scheme, std::string_view cacheUri, const Store::Config::Params & params);
1314

14-
Path cacheUri;
15+
ParsedURL cacheUri;
1516

1617
static const std::string name()
1718
{
@@ -24,10 +25,7 @@ struct HttpBinaryCacheStoreConfig : std::enable_shared_from_this<HttpBinaryCache
2425

2526
ref<Store> openStore() const override;
2627

27-
std::string getUri() const override
28-
{
29-
return cacheUri;
30-
}
28+
StoreReference getReference() const override;
3129
};
3230

3331
} // namespace nix

src/libstore/include/nix/store/legacy-ssh-store.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct LegacySSHStoreConfig : std::enable_shared_from_this<LegacySSHStoreConfig>
5454

5555
ref<Store> openStore() const override;
5656

57-
std::string getUri() const override;
57+
StoreReference getReference() const override;
5858
};
5959

6060
struct LegacySSHStore : public virtual Store

src/libstore/include/nix/store/local-binary-cache-store.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct LocalBinaryCacheStoreConfig : std::enable_shared_from_this<LocalBinaryCac
2727

2828
ref<Store> openStore() const override;
2929

30-
std::string getUri() const override;
30+
StoreReference getReference() const override;
3131
};
3232

3333
} // namespace nix

src/libstore/include/nix/store/local-overlay-store.hh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,7 @@ struct LocalOverlayStoreConfig : virtual LocalStoreConfig
8888

8989
ref<Store> openStore() const override;
9090

91-
std::string getUri() const override
92-
{
93-
return "local-overlay://";
94-
}
91+
StoreReference getReference() const override;
9592

9693
protected:
9794
/**

src/libstore/include/nix/store/local-store.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct LocalStoreConfig : std::enable_shared_from_this<LocalStoreConfig>,
112112

113113
ref<Store> openStore() const override;
114114

115-
std::string getUri() const override;
115+
StoreReference getReference() const override;
116116
};
117117

118118
class LocalStore : public virtual IndirectRootStore, public virtual GcStore

src/libstore/include/nix/store/s3-binary-cache-store.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public:
107107

108108
ref<Store> openStore() const override;
109109

110-
std::string getUri() const override;
110+
StoreReference getReference() const override;
111111
};
112112

113113
struct S3BinaryCacheStore : virtual BinaryCacheStore

0 commit comments

Comments
 (0)