mirror of
https://github.com/NixOS/nix
synced 2025-06-25 06:31:14 +02:00
MemoryInputAccessor is an in-memory virtual filesystem that returns files like <nix/fetchurl.nix>. This removes the need for special hacks to handle those files.
54 lines
1.3 KiB
C++
54 lines
1.3 KiB
C++
#include "memory-input-accessor.hh"
|
|
|
|
namespace nix {
|
|
|
|
struct MemoryInputAccessorImpl : MemoryInputAccessor
|
|
{
|
|
std::map<CanonPath, std::string> files;
|
|
|
|
std::string readFile(const CanonPath & path) override
|
|
{
|
|
auto i = files.find(path);
|
|
if (i == files.end())
|
|
throw Error("file '%s' does not exist", path);
|
|
return i->second;
|
|
}
|
|
|
|
bool pathExists(const CanonPath & path) override
|
|
{
|
|
auto i = files.find(path);
|
|
return i != files.end();
|
|
}
|
|
|
|
Stat lstat(const CanonPath & path) override
|
|
{
|
|
auto i = files.find(path);
|
|
if (i != files.end())
|
|
return Stat { .type = tRegular, .isExecutable = false };
|
|
throw Error("file '%s' does not exist", path);
|
|
}
|
|
|
|
DirEntries readDirectory(const CanonPath & path) override
|
|
{
|
|
return {};
|
|
}
|
|
|
|
std::string readLink(const CanonPath & path) override
|
|
{
|
|
throw UnimplementedError("MemoryInputAccessor::readLink");
|
|
}
|
|
|
|
SourcePath addFile(CanonPath path, std::string && contents) override
|
|
{
|
|
files.emplace(path, std::move(contents));
|
|
|
|
return {ref(shared_from_this()), std::move(path)};
|
|
}
|
|
};
|
|
|
|
ref<MemoryInputAccessor> makeMemoryInputAccessor()
|
|
{
|
|
return make_ref<MemoryInputAccessorImpl>();
|
|
}
|
|
|
|
}
|