mirror of
https://github.com/NixOS/nix
synced 2025-06-30 03:23:16 +02:00
Factor out MemorySourceAccessor
, implement missing features
The new `MemorySourceAccessor` rather than being a slightly lossy flat map is a complete in-memory model of file system objects. Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
This commit is contained in:
parent
8e222fbb12
commit
9b880e3e29
4 changed files with 205 additions and 39 deletions
74
src/libutil/memory-source-accessor.hh
Normal file
74
src/libutil/memory-source-accessor.hh
Normal file
|
@ -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 {
|
||||
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);
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue