2023-12-18 23:14:42 +02:00
|
|
|
#pragma once
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* @brief SourcePath
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ref.hh"
|
|
|
|
#include "canon-path.hh"
|
2024-05-03 13:14:01 +03:00
|
|
|
#include "source-accessor.hh"
|
2023-12-18 23:14:42 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An abstraction for accessing source files during
|
|
|
|
* evaluation. Currently, it's just a wrapper around `CanonPath` that
|
|
|
|
* accesses files in the regular filesystem, but in the future it will
|
|
|
|
* support fetching files in other ways.
|
|
|
|
*/
|
|
|
|
struct SourcePath
|
|
|
|
{
|
2024-05-03 13:14:01 +03:00
|
|
|
ref<SourceAccessor> accessor;
|
2023-12-18 23:14:42 +02:00
|
|
|
CanonPath path;
|
|
|
|
|
2024-05-03 13:14:01 +03:00
|
|
|
SourcePath(ref<SourceAccessor> accessor, CanonPath path = CanonPath::root)
|
2023-12-18 23:14:42 +02:00
|
|
|
: accessor(std::move(accessor))
|
|
|
|
, path(std::move(path))
|
|
|
|
{ }
|
|
|
|
|
|
|
|
std::string_view baseName() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct the parent of this `SourcePath`. Aborts if `this`
|
|
|
|
* denotes the root.
|
|
|
|
*/
|
|
|
|
SourcePath parent() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If this `SourcePath` denotes a regular file (not a symlink),
|
|
|
|
* return its contents; otherwise throw an error.
|
|
|
|
*/
|
|
|
|
std::string readFile() const;
|
|
|
|
|
2024-05-06 20:05:42 +03:00
|
|
|
void readFile(
|
|
|
|
Sink & sink,
|
|
|
|
std::function<void(uint64_t)> sizeCallback = [](uint64_t size){}) const
|
|
|
|
{ return accessor->readFile(path, sink, sizeCallback); }
|
|
|
|
|
2023-12-18 23:14:42 +02:00
|
|
|
/**
|
|
|
|
* Return whether this `SourcePath` denotes a file (of any type)
|
|
|
|
* that exists
|
|
|
|
*/
|
|
|
|
bool pathExists() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return stats about this `SourcePath`, or throw an exception if
|
|
|
|
* it doesn't exist.
|
|
|
|
*/
|
2024-05-03 13:14:01 +03:00
|
|
|
SourceAccessor::Stat lstat() const;
|
2023-12-18 23:14:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return stats about this `SourcePath`, or std::nullopt if it
|
|
|
|
* doesn't exist.
|
|
|
|
*/
|
2024-05-03 13:14:01 +03:00
|
|
|
std::optional<SourceAccessor::Stat> maybeLstat() const;
|
2023-12-18 23:14:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If this `SourcePath` denotes a directory (not a symlink),
|
|
|
|
* return its directory entries; otherwise throw an error.
|
|
|
|
*/
|
2024-05-03 13:14:01 +03:00
|
|
|
SourceAccessor::DirEntries readDirectory() const;
|
2023-12-18 23:14:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If this `SourcePath` denotes a symlink, return its target;
|
|
|
|
* otherwise throw an error.
|
|
|
|
*/
|
|
|
|
std::string readLink() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dump this `SourcePath` to `sink` as a NAR archive.
|
|
|
|
*/
|
|
|
|
void dumpPath(
|
|
|
|
Sink & sink,
|
|
|
|
PathFilter & filter = defaultPathFilter) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the location of this path in the "real" filesystem, if
|
|
|
|
* it has a physical location.
|
|
|
|
*/
|
2024-02-06 23:23:58 +02:00
|
|
|
std::optional<std::filesystem::path> getPhysicalPath() const;
|
2023-12-18 23:14:42 +02:00
|
|
|
|
|
|
|
std::string to_string() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append a `CanonPath` to this path.
|
|
|
|
*/
|
2024-02-05 16:13:11 +02:00
|
|
|
SourcePath operator / (const CanonPath & x) const;
|
2023-12-18 23:14:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Append a single component `c` to this path. `c` must not
|
|
|
|
* contain a slash. A slash is implicitly added between this path
|
|
|
|
* and `c`.
|
|
|
|
*/
|
2024-02-05 16:13:11 +02:00
|
|
|
SourcePath operator / (std::string_view c) const;
|
|
|
|
|
2023-12-18 23:14:42 +02:00
|
|
|
bool operator==(const SourcePath & x) const;
|
|
|
|
bool operator!=(const SourcePath & x) const;
|
|
|
|
bool operator<(const SourcePath & x) const;
|
|
|
|
|
|
|
|
/**
|
2024-04-11 00:46:19 +03:00
|
|
|
* Convenience wrapper around `SourceAccessor::resolveSymlinks()`.
|
2023-12-18 23:14:42 +02:00
|
|
|
*/
|
2024-02-10 21:56:54 +02:00
|
|
|
SourcePath resolveSymlinks(
|
2024-04-11 00:46:19 +03:00
|
|
|
SymlinkResolution mode = SymlinkResolution::Full) const
|
|
|
|
{
|
|
|
|
return {accessor, accessor->resolveSymlinks(path, mode)};
|
|
|
|
}
|
2023-12-18 23:14:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream & operator << (std::ostream & str, const SourcePath & path);
|
|
|
|
|
|
|
|
}
|