2023-10-19 14:45:26 +02:00
|
|
|
#pragma once
|
|
|
|
|
2024-02-06 16:23:58 -05:00
|
|
|
#include <filesystem>
|
|
|
|
|
2023-10-19 14:45:26 +02:00
|
|
|
#include "canon-path.hh"
|
|
|
|
#include "hash.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2023-10-20 16:36:41 +02:00
|
|
|
struct Sink;
|
|
|
|
|
2024-04-10 23:46:19 +02:00
|
|
|
/**
|
|
|
|
* Note there is a decent chance this type soon goes away because the problem is solved another way.
|
|
|
|
* See the discussion in https://github.com/NixOS/nix/pull/9985.
|
|
|
|
*/
|
|
|
|
enum class SymlinkResolution {
|
|
|
|
/**
|
|
|
|
* Resolve symlinks in the ancestors only.
|
|
|
|
*
|
|
|
|
* Only the last component of the result is possibly a symlink.
|
|
|
|
*/
|
|
|
|
Ancestors,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve symlinks fully, realpath(3)-style.
|
|
|
|
*
|
|
|
|
* No component of the result will be a symlink.
|
|
|
|
*/
|
|
|
|
Full,
|
|
|
|
};
|
|
|
|
|
2023-10-19 14:45:26 +02:00
|
|
|
/**
|
|
|
|
* A read-only filesystem abstraction. This is used by the Nix
|
|
|
|
* evaluator and elsewhere for accessing sources in various
|
|
|
|
* filesystem-like entities (such as the real filesystem, tarballs or
|
|
|
|
* Git repositories).
|
|
|
|
*/
|
2024-05-03 12:14:01 +02:00
|
|
|
struct SourceAccessor : std::enable_shared_from_this<SourceAccessor>
|
2023-10-19 14:45:26 +02:00
|
|
|
{
|
|
|
|
const size_t number;
|
|
|
|
|
2023-11-20 18:54:36 +01:00
|
|
|
std::string displayPrefix, displaySuffix;
|
|
|
|
|
2023-10-19 14:45:26 +02:00
|
|
|
SourceAccessor();
|
|
|
|
|
|
|
|
virtual ~SourceAccessor()
|
|
|
|
{ }
|
|
|
|
|
2023-10-20 16:36:41 +02:00
|
|
|
/**
|
|
|
|
* Return the contents of a file as a string.
|
2023-11-06 09:04:50 -05:00
|
|
|
*
|
|
|
|
* @note Unlike Unix, this method should *not* follow symlinks. Nix
|
|
|
|
* by default wants to manipulate symlinks explicitly, and not
|
|
|
|
* implictly follow them, as they are frequently untrusted user data
|
|
|
|
* and thus may point to arbitrary locations. Acting on the targets
|
|
|
|
* targets of symlinks should only occasionally be done, and only
|
|
|
|
* with care.
|
2023-10-20 16:36:41 +02:00
|
|
|
*/
|
|
|
|
virtual std::string readFile(const CanonPath & path);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write the contents of a file as a sink. `sizeCallback` must be
|
|
|
|
* called with the size of the file before any data is written to
|
|
|
|
* the sink.
|
|
|
|
*
|
2023-11-06 09:04:50 -05:00
|
|
|
* @note Like the other `readFile`, this method should *not* follow
|
|
|
|
* symlinks.
|
|
|
|
*
|
|
|
|
* @note subclasses of `SourceAccessor` need to implement at least
|
2023-10-20 16:36:41 +02:00
|
|
|
* one of the `readFile()` variants.
|
|
|
|
*/
|
|
|
|
virtual void readFile(
|
|
|
|
const CanonPath & path,
|
|
|
|
Sink & sink,
|
|
|
|
std::function<void(uint64_t)> sizeCallback = [](uint64_t size){});
|
2023-10-19 14:45:26 +02:00
|
|
|
|
2023-11-01 17:09:28 +01:00
|
|
|
virtual bool pathExists(const CanonPath & path);
|
2023-10-19 14:45:26 +02:00
|
|
|
|
|
|
|
enum Type {
|
|
|
|
tRegular, tSymlink, tDirectory,
|
|
|
|
/**
|
|
|
|
Any other node types that may be encountered on the file system, such as device nodes, sockets, named pipe, and possibly even more exotic things.
|
|
|
|
|
|
|
|
Responsible for `"unknown"` from `builtins.readFileType "/dev/null"`.
|
|
|
|
|
|
|
|
Unlike `DT_UNKNOWN`, this must not be used for deferring the lookup of types.
|
|
|
|
*/
|
|
|
|
tMisc
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Stat
|
|
|
|
{
|
|
|
|
Type type = tMisc;
|
2023-11-01 17:09:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For regular files only: the size of the file. Not all
|
|
|
|
* accessors return this since it may be too expensive to
|
|
|
|
* compute.
|
|
|
|
*/
|
|
|
|
std::optional<uint64_t> fileSize;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For regular files only: whether this is an executable.
|
|
|
|
*/
|
|
|
|
bool isExecutable = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For regular files only: the position of the contents of this
|
|
|
|
* file in the NAR. Only returned by NAR accessors.
|
|
|
|
*/
|
|
|
|
std::optional<uint64_t> narOffset;
|
2023-10-19 14:45:26 +02:00
|
|
|
};
|
|
|
|
|
2023-11-01 15:26:07 +01:00
|
|
|
Stat lstat(const CanonPath & path);
|
2023-10-19 14:45:26 +02:00
|
|
|
|
2023-11-01 15:26:07 +01:00
|
|
|
virtual std::optional<Stat> maybeLstat(const CanonPath & path) = 0;
|
2023-10-19 14:45:26 +02:00
|
|
|
|
|
|
|
typedef std::optional<Type> DirEntry;
|
|
|
|
|
|
|
|
typedef std::map<std::string, DirEntry> DirEntries;
|
|
|
|
|
2023-11-06 09:04:50 -05:00
|
|
|
/**
|
|
|
|
* @note Like `readFile`, this method should *not* follow symlinks.
|
|
|
|
*/
|
2023-10-19 14:45:26 +02:00
|
|
|
virtual DirEntries readDirectory(const CanonPath & path) = 0;
|
|
|
|
|
|
|
|
virtual std::string readLink(const CanonPath & path) = 0;
|
|
|
|
|
|
|
|
virtual void dumpPath(
|
|
|
|
const CanonPath & path,
|
|
|
|
Sink & sink,
|
|
|
|
PathFilter & filter = defaultPathFilter);
|
|
|
|
|
|
|
|
Hash hashPath(
|
2024-04-10 23:46:19 +02:00
|
|
|
const CanonPath & path,
|
|
|
|
PathFilter & filter = defaultPathFilter,
|
|
|
|
HashAlgorithm ha = HashAlgorithm::SHA256);
|
2023-10-19 14:45:26 +02:00
|
|
|
|
2023-10-20 12:36:18 +02:00
|
|
|
/**
|
|
|
|
* Return a corresponding path in the root filesystem, if
|
|
|
|
* possible. This is only possible for filesystems that are
|
|
|
|
* materialized in the root filesystem.
|
|
|
|
*/
|
2024-02-06 16:23:58 -05:00
|
|
|
virtual std::optional<std::filesystem::path> getPhysicalPath(const CanonPath & path)
|
2023-10-19 14:45:26 +02:00
|
|
|
{ return std::nullopt; }
|
|
|
|
|
|
|
|
bool operator == (const SourceAccessor & x) const
|
|
|
|
{
|
|
|
|
return number == x.number;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator < (const SourceAccessor & x) const
|
|
|
|
{
|
|
|
|
return number < x.number;
|
|
|
|
}
|
|
|
|
|
2023-11-20 18:54:36 +01:00
|
|
|
void setPathDisplay(std::string displayPrefix, std::string displaySuffix = "");
|
|
|
|
|
2023-10-19 14:45:26 +02:00
|
|
|
virtual std::string showPath(const CanonPath & path);
|
2024-04-10 23:46:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve any symlinks in `path` according to the given
|
|
|
|
* resolution mode.
|
|
|
|
*
|
|
|
|
* @param mode might only be a temporary solution for this.
|
|
|
|
* See the discussion in https://github.com/NixOS/nix/pull/9985.
|
|
|
|
*/
|
|
|
|
CanonPath resolveSymlinks(
|
|
|
|
const CanonPath & path,
|
|
|
|
SymlinkResolution mode = SymlinkResolution::Full);
|
2024-05-03 12:14:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A string that uniquely represents the contents of this
|
|
|
|
* accessor. This is used for caching lookups (see `fetchToStore()`).
|
|
|
|
*/
|
|
|
|
std::optional<std::string> fingerprint;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the maximum last-modified time of the files in this
|
|
|
|
* tree, if available.
|
|
|
|
*/
|
|
|
|
virtual std::optional<time_t> getLastModified()
|
|
|
|
{ return std::nullopt; }
|
2023-10-19 14:45:26 +02:00
|
|
|
};
|
|
|
|
|
2024-05-03 12:14:01 +02:00
|
|
|
/**
|
|
|
|
* Return a source accessor that contains only an empty root directory.
|
|
|
|
*/
|
|
|
|
ref<SourceAccessor> makeEmptySourceAccessor();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exception thrown when accessing a filtered path (see
|
2024-05-06 17:29:03 +02:00
|
|
|
* `FilteringSourceAccessor`).
|
2024-05-03 12:14:01 +02:00
|
|
|
*/
|
|
|
|
MakeError(RestrictedPathError, Error);
|
|
|
|
|
2024-05-03 12:30:28 +02:00
|
|
|
/**
|
|
|
|
* Return an accessor for the root filesystem.
|
|
|
|
*/
|
|
|
|
ref<SourceAccessor> makeFSSourceAccessor();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an accessor for the filesystem rooted at `root`.
|
|
|
|
*/
|
|
|
|
ref<SourceAccessor> makeFSSourceAccessor(std::filesystem::path root);
|
|
|
|
|
2023-10-19 14:45:26 +02:00
|
|
|
}
|