|
4 | 4 | #include "nix/util/source-path.hh" |
5 | 5 | #include "nix/util/fs-sink.hh" |
6 | 6 | #include "nix/util/variant-wrapper.hh" |
| 7 | +#include "nix/util/json-impls.hh" |
7 | 8 |
|
8 | 9 | namespace nix { |
9 | 10 |
|
10 | 11 | /** |
11 | | - * An source accessor for an in-memory file system. |
| 12 | + * File System Object definitions |
| 13 | + * |
| 14 | + * @see https://nix.dev/manual/nix/latest/store/file-system-object.html |
12 | 15 | */ |
13 | | -struct MemorySourceAccessor : virtual SourceAccessor |
| 16 | +namespace fso { |
| 17 | + |
| 18 | +template<typename RegularContents> |
| 19 | +struct Regular |
| 20 | +{ |
| 21 | + bool executable = false; |
| 22 | + RegularContents contents; |
| 23 | + |
| 24 | + auto operator<=>(const Regular &) const = default; |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * Child parameter because sometimes we want "shallow" directories without |
| 29 | + * full file children. |
| 30 | + */ |
| 31 | +template<typename Child> |
| 32 | +struct DirectoryT |
| 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 | + |
| 42 | +struct Symlink |
| 43 | +{ |
| 44 | + std::string target; |
| 45 | + |
| 46 | + auto operator<=>(const Symlink &) const = default; |
| 47 | +}; |
| 48 | + |
| 49 | +/** |
| 50 | + * For when we know there is child, but don't know anything about it. |
| 51 | + * |
| 52 | + * This is not part of the core File System Object data model --- this |
| 53 | + * represents not knowing, not an additional type of file. |
| 54 | + */ |
| 55 | +struct Opaque |
| 56 | +{ |
| 57 | + auto operator<=>(const Opaque &) const = default; |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * `File<std::string>` nicely defining what a "file system object" |
| 62 | + * is in Nix. |
| 63 | + * |
| 64 | + * With a different type arugment, it is also can be a "skeletal" |
| 65 | + * version is that abstract syntax for a "NAR listing". |
| 66 | + */ |
| 67 | +template<typename RegularContents, bool recur> |
| 68 | +struct VariantT |
14 | 69 | { |
| 70 | + bool operator==(const VariantT &) const noexcept; |
| 71 | + std::strong_ordering operator<=>(const VariantT &) const noexcept; |
| 72 | + |
| 73 | + using Regular = nix::fso::Regular<RegularContents>; |
| 74 | + |
15 | 75 | /** |
16 | | - * In addition to being part of the implementation of |
17 | | - * `MemorySourceAccessor`, this has a side benefit of nicely |
18 | | - * defining what a "file system object" is in Nix. |
| 76 | + * In the default case, we do want full file children for our directory. |
19 | 77 | */ |
20 | | - struct File |
21 | | - { |
22 | | - bool operator==(const File &) const noexcept; |
23 | | - std::strong_ordering operator<=>(const File &) const noexcept; |
| 78 | + using Directory = nix::fso::DirectoryT<std::conditional_t<recur, VariantT, Opaque>>; |
24 | 79 |
|
25 | | - struct Regular |
26 | | - { |
27 | | - bool executable = false; |
28 | | - std::string contents; |
| 80 | + using Symlink = nix::fso::Symlink; |
29 | 81 |
|
30 | | - bool operator==(const Regular &) const = default; |
31 | | - auto operator<=>(const Regular &) const = default; |
32 | | - }; |
| 82 | + using Raw = std::variant<Regular, Directory, Symlink>; |
| 83 | + Raw raw; |
33 | 84 |
|
34 | | - struct Directory |
35 | | - { |
36 | | - using Name = std::string; |
| 85 | + MAKE_WRAPPER_CONSTRUCTOR(VariantT); |
37 | 86 |
|
38 | | - std::map<Name, File, std::less<>> contents; |
| 87 | + SourceAccessor::Stat lstat() const; |
| 88 | +}; |
39 | 89 |
|
40 | | - bool operator==(const Directory &) const noexcept; |
41 | | - // TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet. |
42 | | - bool operator<(const Directory &) const noexcept; |
43 | | - }; |
| 90 | +template<typename Child> |
| 91 | +inline bool DirectoryT<Child>::operator==(const DirectoryT &) const noexcept = default; |
44 | 92 |
|
45 | | - struct Symlink |
46 | | - { |
47 | | - std::string target; |
| 93 | +template<typename Child> |
| 94 | +inline std::strong_ordering DirectoryT<Child>::operator<=>(const DirectoryT &) const noexcept = default; |
48 | 95 |
|
49 | | - bool operator==(const Symlink &) const = default; |
50 | | - auto operator<=>(const Symlink &) const = default; |
51 | | - }; |
| 96 | +template<typename RegularContents, bool recur> |
| 97 | +inline bool |
| 98 | +VariantT<RegularContents, recur>::operator==(const VariantT<RegularContents, recur> &) const noexcept = default; |
52 | 99 |
|
53 | | - using Raw = std::variant<Regular, Directory, Symlink>; |
54 | | - Raw raw; |
| 100 | +template<typename RegularContents, bool recur> |
| 101 | +inline std::strong_ordering |
| 102 | +VariantT<RegularContents, recur>::operator<=>(const VariantT<RegularContents, recur> &) const noexcept = default; |
55 | 103 |
|
56 | | - MAKE_WRAPPER_CONSTRUCTOR(File); |
| 104 | +} // namespace fso |
57 | 105 |
|
58 | | - Stat lstat() const; |
59 | | - }; |
| 106 | +/** |
| 107 | + * An source accessor for an in-memory file system. |
| 108 | + */ |
| 109 | +struct MemorySourceAccessor : virtual SourceAccessor |
| 110 | +{ |
| 111 | + using File = fso::VariantT<std::string, true>; |
60 | 112 |
|
61 | 113 | std::optional<File> root; |
62 | 114 |
|
@@ -89,19 +141,6 @@ struct MemorySourceAccessor : virtual SourceAccessor |
89 | 141 | SourcePath addFile(CanonPath path, std::string && contents); |
90 | 142 | }; |
91 | 143 |
|
92 | | -inline bool MemorySourceAccessor::File::Directory::operator==( |
93 | | - const MemorySourceAccessor::File::Directory &) const noexcept = default; |
94 | | - |
95 | | -inline bool |
96 | | -MemorySourceAccessor::File::Directory::operator<(const MemorySourceAccessor::File::Directory & other) const noexcept |
97 | | -{ |
98 | | - return contents < other.contents; |
99 | | -} |
100 | | - |
101 | | -inline bool MemorySourceAccessor::File::operator==(const MemorySourceAccessor::File &) const noexcept = default; |
102 | | -inline std::strong_ordering |
103 | | -MemorySourceAccessor::File::operator<=>(const MemorySourceAccessor::File &) const noexcept = default; |
104 | | - |
105 | 144 | /** |
106 | 145 | * Write to a `MemorySourceAccessor` at the given path |
107 | 146 | */ |
|
0 commit comments