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"
5+ #include " nix/store/dummy-store.hh"
36
47namespace nix {
58
6- struct DummyStoreConfig : public std ::enable_shared_from_this<DummyStoreConfig>, virtual StoreConfig
9+ std::string DummyStoreConfig::doc ()
710{
8- using StoreConfig::StoreConfig;
9-
10- DummyStoreConfig (std::string_view scheme, std::string_view authority, const Params & params)
11- : StoreConfig(params)
12- {
13- if (!authority.empty ())
14- throw UsageError (" `%s` store URIs must not contain an authority part %s" , scheme, authority);
15- }
16-
17- static const std::string name ()
18- {
19- return " Dummy Store" ;
20- }
21-
22- static std::string doc ()
23- {
24- return
11+ return
2512#include " dummy-store.md"
26- ;
27- }
28-
29- static StringSet uriSchemes ()
30- {
31- return {" dummy" };
32- }
33-
34- ref<Store> openStore () const override ;
35-
36- StoreReference getReference () const override
37- {
38- return {
39- .variant =
40- StoreReference::Specified{
41- .scheme = *uriSchemes ().begin (),
42- },
43- };
44- }
45- };
13+ ;
14+ }
4615
4716struct DummyStore : virtual Store
4817{
4918 using Config = DummyStoreConfig;
5019
5120 ref<const Config> config;
5221
22+ ref<MemorySourceAccessor> contents;
23+
5324 DummyStore (ref<const Config> config)
5425 : Store{*config}
5526 , config(config)
27+ , contents(make_ref<MemorySourceAccessor>())
5628 {
5729 }
5830
@@ -80,16 +52,54 @@ struct DummyStore : virtual Store
8052 unsupported (" addToStore" );
8153 }
8254
83- virtual StorePath addToStoreFromDump (
84- Source & dump ,
55+ StorePath addToStoreFromDump (
56+ Source & source ,
8557 std::string_view name,
8658 FileSerialisationMethod dumpMethod = FileSerialisationMethod::NixArchive,
8759 ContentAddressMethod hashMethod = FileIngestionMethod::NixArchive,
8860 HashAlgorithm hashAlgo = HashAlgorithm::SHA256,
8961 const StorePathSet & references = StorePathSet(),
9062 RepairFlag repair = NoRepair) override
9163 {
92- 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;
93103 }
94104
95105 void narFromPath (const StorePath & path, Sink & sink) override
@@ -105,7 +115,7 @@ struct DummyStore : virtual Store
105115
106116 virtual ref<SourceAccessor> getFSAccessor (bool requireValidPath) override
107117 {
108- return makeEmptySourceAccessor () ;
118+ return this -> contents ;
109119 }
110120};
111121
0 commit comments