-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Factor out MemorySourceAccessor, implement missing features
#9283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| #include "memory-source-accessor.hh" | ||
|
|
||
| namespace nix { | ||
|
|
||
| MemorySourceAccessor::File * | ||
| MemorySourceAccessor::open(const CanonPath & path, std::optional<File> create) | ||
| { | ||
| File * cur = &root; | ||
|
|
||
| bool newF = false; | ||
|
|
||
| for (std::string_view name : path) | ||
| { | ||
| auto * curDirP = std::get_if<File::Directory>(&cur->raw); | ||
| if (!curDirP) | ||
| return nullptr; | ||
| auto & curDir = *curDirP; | ||
|
|
||
| auto i = curDir.contents.find(name); | ||
| if (i == curDir.contents.end()) { | ||
| if (!create) | ||
| return nullptr; | ||
| else { | ||
| newF = true; | ||
| i = curDir.contents.insert(i, { | ||
| std::string { name }, | ||
| File::Directory {}, | ||
| }); | ||
| } | ||
| } | ||
| cur = &i->second; | ||
| } | ||
|
|
||
| if (newF && create) *cur = std::move(*create); | ||
|
|
||
| return cur; | ||
| } | ||
|
|
||
| std::string MemorySourceAccessor::readFile(const CanonPath & path) | ||
| { | ||
| auto * f = open(path, std::nullopt); | ||
| if (!f) | ||
| throw Error("file '%s' does not exist", path); | ||
| if (auto * r = std::get_if<File::Regular>(&f->raw)) | ||
| return r->contents; | ||
| else | ||
| throw Error("file '%s' is not a regular file", path); | ||
| } | ||
|
|
||
| bool MemorySourceAccessor::pathExists(const CanonPath & path) | ||
| { | ||
| return open(path, std::nullopt); | ||
| } | ||
|
|
||
| MemorySourceAccessor::Stat MemorySourceAccessor::File::lstat() const | ||
| { | ||
| return std::visit(overloaded { | ||
| [](const Regular & r) { | ||
| return Stat { | ||
| .type = tRegular, | ||
| .fileSize = r.contents.size(), | ||
| .isExecutable = r.executable, | ||
| }; | ||
| }, | ||
| [](const Directory &) { | ||
| return Stat { | ||
| .type = tDirectory, | ||
| }; | ||
| }, | ||
| [](const Symlink &) { | ||
| return Stat { | ||
| .type = tSymlink, | ||
| }; | ||
| }, | ||
| }, this->raw); | ||
| } | ||
|
|
||
| std::optional<MemorySourceAccessor::Stat> | ||
| MemorySourceAccessor::maybeLstat(const CanonPath & path) | ||
| { | ||
| const auto * f = open(path, std::nullopt); | ||
| return f ? std::optional { f->lstat() } : std::nullopt; | ||
| } | ||
|
|
||
| MemorySourceAccessor::DirEntries MemorySourceAccessor::readDirectory(const CanonPath & path) | ||
| { | ||
| auto * f = open(path, std::nullopt); | ||
| if (!f) | ||
| throw Error("file '%s' does not exist", path); | ||
| if (auto * d = std::get_if<File::Directory>(&f->raw)) { | ||
| DirEntries res; | ||
| for (auto & [name, file] : d->contents) | ||
| res.insert_or_assign(name, file.lstat().type); | ||
| return res; | ||
| } else | ||
| throw Error("file '%s' is not a directory", path); | ||
| return {}; | ||
| } | ||
|
|
||
| std::string MemorySourceAccessor::readLink(const CanonPath & path) | ||
| { | ||
| auto * f = open(path, std::nullopt); | ||
| if (!f) | ||
| throw Error("file '%s' does not exist", path); | ||
| if (auto * s = std::get_if<File::Symlink>(&f->raw)) | ||
| return s->target; | ||
| else | ||
| throw Error("file '%s' is not a symbolic link", path); | ||
| } | ||
|
|
||
| CanonPath MemorySourceAccessor::addFile(CanonPath path, std::string && contents) | ||
| { | ||
| auto * f = open(path, File { File::Regular {} }); | ||
| if (!f) | ||
| throw Error("file '%s' cannot be made because some parent file is not a directory", path); | ||
| if (auto * r = std::get_if<File::Regular>(&f->raw)) | ||
| r->contents = std::move(contents); | ||
| else | ||
| throw Error("file '%s' is not a regular file", path); | ||
|
|
||
| return path; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,74 @@ | ||||||
| #include "source-accessor.hh" | ||||||
| #include "variant-wrapper.hh" | ||||||
|
|
||||||
| namespace nix { | ||||||
|
|
||||||
| /** | ||||||
| * An source accessor for an in-memory file system. | ||||||
| */ | ||||||
| struct MemorySourceAccessor : virtual SourceAccessor | ||||||
| { | ||||||
| /** | ||||||
| * In addition to being part of the implementation of | ||||||
| * `MemorySourceAccessor`, this has a side benefit of nicely | ||||||
| * defining what a "file system object" is in Nix. | ||||||
| */ | ||||||
| struct File { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps:
Suggested change
I don't like the idea that a directory would be a file. |
||||||
| struct Regular { | ||||||
| bool executable = false; | ||||||
| std::string contents; | ||||||
|
|
||||||
| GENERATE_CMP(Regular, me->executable, me->contents); | ||||||
| }; | ||||||
|
|
||||||
| struct Directory { | ||||||
| using Name = std::string; | ||||||
|
|
||||||
| std::map<Name, File, std::less<>> contents; | ||||||
|
|
||||||
| GENERATE_CMP(Directory, me->contents); | ||||||
| }; | ||||||
|
|
||||||
| struct Symlink { | ||||||
| std::string target; | ||||||
|
|
||||||
| GENERATE_CMP(Symlink, me->target); | ||||||
| }; | ||||||
|
|
||||||
| using Raw = std::variant<Regular, Directory, Symlink>; | ||||||
| Raw raw; | ||||||
|
|
||||||
| MAKE_WRAPPER_CONSTRUCTOR(File); | ||||||
|
|
||||||
| GENERATE_CMP(File, me->raw); | ||||||
|
|
||||||
| Stat lstat() const; | ||||||
| }; | ||||||
|
|
||||||
| File root { File::Directory {} }; | ||||||
|
|
||||||
| GENERATE_CMP(MemorySourceAccessor, me->root); | ||||||
|
|
||||||
| std::string readFile(const CanonPath & path) override; | ||||||
| bool pathExists(const CanonPath & path) override; | ||||||
| std::optional<Stat> maybeLstat(const CanonPath & path) override; | ||||||
| DirEntries readDirectory(const CanonPath & path) override; | ||||||
| std::string readLink(const CanonPath & path) override; | ||||||
|
|
||||||
| /** | ||||||
| * @param create If present, create this file and any parent directories | ||||||
| * that are needed. | ||||||
| * | ||||||
| * Return null if | ||||||
| * | ||||||
| * - `create = false`: File does not exist. | ||||||
| * | ||||||
| * - `create = true`: some parent file was not a dir, so couldn't | ||||||
| * look/create inside. | ||||||
| */ | ||||||
| File * open(const CanonPath & path, std::optional<File> create); | ||||||
|
|
||||||
| CanonPath addFile(CanonPath path, std::string && contents); | ||||||
| }; | ||||||
|
|
||||||
| } | ||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No symlink support.
Maybe the source accessor virtual methods should not even be responsible for that behavior. Or at least, symlink resolution should be implemented once, as a mix-in or an adapter.
(Also applies to other methods)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I actually think this is right and its
PosixSourceAccessorthat needs to be fixed.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#9306 partially fixes
PosixSourceAccesorand documents this in the methods.