mirror of
https://github.com/NixOS/nix.git
synced 2025-11-12 13:36:02 +01:00
libutil: Improve handling of non-directory root in MemorySourceAccessor
This commit is contained in:
parent
4df60e639b
commit
02c9ac445f
2 changed files with 21 additions and 2 deletions
|
|
@ -58,7 +58,7 @@ struct MemorySourceAccessor : virtual SourceAccessor
|
||||||
Stat lstat() const;
|
Stat lstat() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
File root{File::Directory{}};
|
std::optional<File> root;
|
||||||
|
|
||||||
bool operator==(const MemorySourceAccessor &) const noexcept = default;
|
bool operator==(const MemorySourceAccessor &) const noexcept = default;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,22 @@ namespace nix {
|
||||||
|
|
||||||
MemorySourceAccessor::File * MemorySourceAccessor::open(const CanonPath & path, std::optional<File> create)
|
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;
|
bool newF = false;
|
||||||
|
|
||||||
|
|
@ -112,6 +127,10 @@ std::string MemorySourceAccessor::readLink(const CanonPath & path)
|
||||||
|
|
||||||
SourcePath MemorySourceAccessor::addFile(CanonPath path, std::string && contents)
|
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{}});
|
auto * f = open(path, File{File::Regular{}});
|
||||||
if (!f)
|
if (!f)
|
||||||
throw Error("file '%s' cannot be made because some parent file is not a directory", path);
|
throw Error("file '%s' cannot be made because some parent file is not a directory", path);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue