Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
memory-source-accessor.hh
Go to the documentation of this file.
1#pragma once
3
5#include "nix/util/fs-sink.hh"
8
9namespace nix {
10
16namespace fso {
17
18template<typename RegularContents>
19struct Regular
20{
21 bool executable = false;
22 RegularContents contents;
23
24 auto operator<=>(const Regular &) const = default;
25};
26
31template<typename Child>
33{
34 using Name = std::string;
35
36 std::map<Name, Child, std::less<>> entries;
37
38 inline bool operator==(const DirectoryT &) const noexcept;
39 inline std::strong_ordering operator<=>(const DirectoryT &) const noexcept;
40};
41
42struct Symlink
43{
44 std::string target;
45
46 auto operator<=>(const Symlink &) const = default;
47};
48
55struct Opaque
56{
57 auto operator<=>(const Opaque &) const = default;
58};
59
67template<typename RegularContents, bool recur>
69{
70 bool operator==(const VariantT &) const noexcept;
71 std::strong_ordering operator<=>(const VariantT &) const noexcept;
72
74
79
80 using Symlink = nix::fso::Symlink;
81
82 using Raw = std::variant<Regular, Directory, Symlink>;
83 Raw raw;
84
85 MAKE_WRAPPER_CONSTRUCTOR(VariantT);
86
87 SourceAccessor::Stat lstat() const;
88};
89
90template<typename Child>
91inline bool DirectoryT<Child>::operator==(const DirectoryT &) const noexcept = default;
92
93template<typename Child>
94inline std::strong_ordering DirectoryT<Child>::operator<=>(const DirectoryT &) const noexcept = default;
95
96template<typename RegularContents, bool recur>
97inline bool
98VariantT<RegularContents, recur>::operator==(const VariantT<RegularContents, recur> &) const noexcept = default;
99
100template<typename RegularContents, bool recur>
101inline std::strong_ordering
102VariantT<RegularContents, recur>::operator<=>(const VariantT<RegularContents, recur> &) const noexcept = default;
103
104} // namespace fso
105
109struct MemorySourceAccessor : virtual SourceAccessor
110{
111private:
112 void anchor() override;
113
114public:
116
117 std::optional<File> root;
118
119 bool operator==(const MemorySourceAccessor &) const noexcept = default;
120
121 bool operator<(const MemorySourceAccessor & other) const noexcept
122 {
123 return root < other.root;
124 }
125
126 void readFile(const CanonPath & path, Sink & sink, fun<void(uint64_t)> sizeCallback) override;
128
129 bool pathExists(const CanonPath & path) override;
130 std::optional<Stat> maybeLstat(const CanonPath & path) override;
131 DirEntries readDirectory(const CanonPath & path) override;
132 std::string readLink(const CanonPath & path) override;
133
145 File * open(const CanonPath & path, std::optional<File> create);
146
151 SourcePath addFile(CanonPath path, std::string && contents);
152};
153
157struct MemorySink : FileSystemObjectSink
158{
159private:
160 void anchor() override;
161
162public:
164
165 MemorySink(MemorySourceAccessor & dst)
166 : dst(dst)
167 {
168 }
169
170 void createDirectory(const CanonPath & path) override;
171
172 void createRegularFile(const CanonPath & path, fun<void(CreateRegularFileSink &)>) override;
173
174 void createSymlink(const CanonPath & path, const std::string & target) override;
175};
176
177template<>
178struct json_avoids_null<MemorySourceAccessor::File::Regular> : std::true_type
179{};
180
181template<>
182struct json_avoids_null<MemorySourceAccessor::File::Directory> : std::true_type
183{};
184
185template<>
186struct json_avoids_null<MemorySourceAccessor::File::Symlink> : std::true_type
187{};
188
189template<>
190struct json_avoids_null<MemorySourceAccessor::File> : std::true_type
191{};
192
193template<>
194struct json_avoids_null<MemorySourceAccessor> : std::true_type
195{};
196
197} // namespace nix
198
199namespace nlohmann {
200
201#define ARG nix::fso::Regular<RegularContents>
202template<typename RegularContents>
203JSON_IMPL_INNER(ARG);
204#undef ARG
205
206#define ARG nix::fso::DirectoryT<Child>
207template<typename Child>
208JSON_IMPL_INNER(ARG);
209#undef ARG
210
211template<>
212JSON_IMPL_INNER(nix::fso::Symlink);
213
214template<>
215JSON_IMPL_INNER(nix::fso::Opaque);
216
217#define ARG nix::fso::VariantT<RegularContents, recur>
218template<typename RegularContents, bool recur>
219JSON_IMPL_INNER(ARG);
220#undef ARG
221
222} // namespace nlohmann
223
Definition canon-path.hh:50
Definition fun.hh:19
Definition memory-source-accessor.hh:16
SourcePath.
Definition fs-sink.hh:17
Definition fs-sink.hh:39
void createRegularFile(const CanonPath &path, fun< void(CreateRegularFileSink &)>) override
Definition memory-source-accessor.cc:216
Definition memory-source-accessor.hh:110
DirEntries readDirectory(const CanonPath &path) override
Definition memory-source-accessor.cc:127
bool pathExists(const CanonPath &path) override
Check whether a file exists at path.
Definition memory-source-accessor.cc:90
File * open(const CanonPath &path, std::optional< File > create)
Definition memory-source-accessor.cc:11
SourcePath addFile(CanonPath path, std::string &&contents)
Insert a new regular file into with the specified contents.
Definition memory-source-accessor.cc:161
void readFile(const CanonPath &path, Sink &sink, fun< void(uint64_t)> sizeCallback) override
Definition memory-source-accessor.cc:72
Definition serialise.hh:18
Definition source-accessor.hh:119
std::string readFile(const CanonPath &path)
Definition source-accessor.cc:62
Definition source-path.hh:22
Definition memory-source-accessor.hh:33
Definition memory-source-accessor.hh:56
Definition memory-source-accessor.hh:20
Definition memory-source-accessor.hh:69
nix::fso::DirectoryT< std::conditional_t< recur, VariantT, Opaque > > Directory
Definition memory-source-accessor.hh:78