Skip to content

Commit d5ce8c3

Browse files
Ericson2314haenoeedolstra
committed
Use MemorySourceAccessor in DummyStore
Add `read-only` setting to `dummy://` store for back compat. Test by changing an existing test to use this instead, fixing a TODO. Co-Authored-By: HaeNoe <[email protected]> Co-authored-by: Eelco Dolstra <[email protected]>
1 parent 168c24b commit d5ce8c3

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

src/libfetchers-tests/git.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "nix/store/store-open.hh"
22
#include "nix/store/globals.hh"
3+
#include "nix/store/dummy-store.hh"
34
#include "nix/fetchers/fetch-settings.hh"
45
#include "nix/fetchers/fetchers.hh"
56
#include "nix/fetchers/git-utils.hh"
@@ -179,10 +180,11 @@ TEST_F(GitTest, submodulePeriodSupport)
179180
// 6) Commit the addition in super
180181
commitAll(super.get(), "Add submodule with branch='.'");
181182

182-
// TODO: Use dummy:// store with MemorySourceAccessor.
183-
Path storeTmpDir = createTempDir();
184-
auto storeTmpDirAutoDelete = AutoDelete(storeTmpDir, true);
185-
ref<Store> store = openStore(storeTmpDir);
183+
auto store = [] {
184+
auto cfg = make_ref<DummyStoreConfig>(StoreReference::Params{});
185+
cfg->readOnly = false;
186+
return cfg->openStore();
187+
}();
186188

187189
auto settings = fetchers::Settings{};
188190
auto input = fetchers::Input::fromAttrs(

src/libstore/dummy-store.cc

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "nix/store/store-registration.hh"
2+
#include "nix/util/archive.hh"
23
#include "nix/util/callback.hh"
4+
#include "nix/util/memory-source-accessor.hh"
35
#include "nix/store/dummy-store.hh"
46

57
namespace nix {
@@ -17,9 +19,12 @@ struct DummyStore : virtual Store
1719

1820
ref<const Config> config;
1921

22+
ref<MemorySourceAccessor> contents;
23+
2024
DummyStore(ref<const Config> config)
2125
: Store{*config}
2226
, config(config)
27+
, contents(make_ref<MemorySourceAccessor>())
2328
{
2429
}
2530

@@ -47,16 +52,54 @@ struct DummyStore : virtual Store
4752
unsupported("addToStore");
4853
}
4954

50-
virtual StorePath addToStoreFromDump(
51-
Source & dump,
55+
StorePath addToStoreFromDump(
56+
Source & source,
5257
std::string_view name,
5358
FileSerialisationMethod dumpMethod = FileSerialisationMethod::NixArchive,
5459
ContentAddressMethod hashMethod = FileIngestionMethod::NixArchive,
5560
HashAlgorithm hashAlgo = HashAlgorithm::SHA256,
5661
const StorePathSet & references = StorePathSet(),
5762
RepairFlag repair = NoRepair) override
5863
{
59-
unsupported("addToStore");
64+
if (config->readOnly)
65+
unsupported("addToStoreFromDump");
66+
67+
auto temp = make_ref<MemorySourceAccessor>();
68+
69+
{
70+
MemorySink tempSink{*temp};
71+
72+
// TODO factor this out into `restorePath`, same todo on it.
73+
switch (dumpMethod) {
74+
case FileSerialisationMethod::NixArchive:
75+
parseDump(tempSink, source);
76+
break;
77+
case FileSerialisationMethod::Flat: {
78+
// Replace root dir with file so next part succeeds.
79+
temp->root = MemorySourceAccessor::File::Regular{};
80+
tempSink.createRegularFile(CanonPath::root, [&](auto & sink) { source.drainInto(sink); });
81+
break;
82+
}
83+
}
84+
}
85+
86+
auto hash = hashPath({temp, CanonPath::root}, hashMethod.getFileIngestionMethod(), hashAlgo).first;
87+
88+
auto desc = ContentAddressWithReferences::fromParts(
89+
hashMethod,
90+
hash,
91+
{
92+
.others = references,
93+
// caller is not capable of creating a self-reference, because
94+
// this is content-addressed without modulus
95+
.self = false,
96+
});
97+
98+
auto dstPath = makeFixedOutputPathFromCA(name, desc);
99+
100+
contents->open(CanonPath(printStorePath(dstPath)), std::move(temp->root));
101+
102+
return dstPath;
60103
}
61104

62105
void narFromPath(const StorePath & path, Sink & sink) override
@@ -72,7 +115,7 @@ struct DummyStore : virtual Store
72115

73116
virtual ref<SourceAccessor> getFSAccessor(bool requireValidPath) override
74117
{
75-
return makeEmptySourceAccessor();
118+
return this->contents;
76119
}
77120
};
78121

src/libstore/dummy-store.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ R"(
22

33
**Store URL format**: `dummy://`
44

5-
This store type represents a store that contains no store paths and
6-
cannot be written to. It's useful when you want to use the Nix
7-
evaluator when no actual Nix store exists, e.g.
5+
This store type represents a store in memory.
6+
Store objects can be read and written, but only so long as the store is open.
7+
Once the store is closed, all data will be forgoton.
8+
9+
It's useful when you want to use the Nix evaluator when no actual Nix store exists, e.g.
810

911
```console
1012
# nix eval --store dummy:// --expr '1 + 2'

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ struct DummyStoreConfig : public std::enable_shared_from_this<DummyStoreConfig>,
1313
throw UsageError("`%s` store URIs must not contain an authority part %s", scheme, authority);
1414
}
1515

16+
Setting<bool> readOnly{
17+
this,
18+
true,
19+
"read-only",
20+
R"(
21+
Make any sort of write fail instead of succeeding.
22+
No additional memory will be used, because no information needs to be stored.
23+
)"};
24+
1625
static const std::string name()
1726
{
1827
return "Dummy Store";

0 commit comments

Comments
 (0)