1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-12 05:26:02 +01:00

libutil: Improve handling of non-directory root in MemorySourceAccessor

This commit is contained in:
Sergei Zimmerman 2025-09-20 11:25:23 +03:00
parent 4df60e639b
commit 02c9ac445f
No known key found for this signature in database
2 changed files with 21 additions and 2 deletions

View file

@ -58,7 +58,7 @@ struct MemorySourceAccessor : virtual SourceAccessor
Stat lstat() const;
};
File root{File::Directory{}};
std::optional<File> root;
bool operator==(const MemorySourceAccessor &) const noexcept = default;

View file

@ -4,7 +4,22 @@ namespace nix {
MemorySourceAccessor::File * MemorySourceAccessor::open(const CanonPath & path, std::optional<File> create)
{
File * cur = &root;
bool hasRoot = root.has_value();
// Special handling of root directory.
if (path.isRoot() && !hasRoot) {
if (create) {
root = std::move(*create);
return &root.value();
}
return nullptr;
}
// Root does not exist.
if (!hasRoot)
return nullptr;
File * cur = &root.value();
bool newF = false;
@ -112,6 +127,10 @@ std::string MemorySourceAccessor::readLink(const CanonPath & path)
SourcePath MemorySourceAccessor::addFile(CanonPath path, std::string && contents)
{
// Create root directory automatically if necessary as a convenience.
if (!root && !path.isRoot())
open(CanonPath::root, File::Directory{});
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);