2023-12-18 23:14:42 +02:00
|
|
|
#include "source-path.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
std::string_view SourcePath::baseName() const
|
|
|
|
{ return path.baseName().value_or("source"); }
|
|
|
|
|
|
|
|
SourcePath SourcePath::parent() const
|
|
|
|
{
|
|
|
|
auto p = path.parent();
|
|
|
|
assert(p);
|
|
|
|
return {accessor, std::move(*p)};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string SourcePath::readFile() const
|
|
|
|
{ return accessor->readFile(path); }
|
|
|
|
|
|
|
|
bool SourcePath::pathExists() const
|
|
|
|
{ return accessor->pathExists(path); }
|
|
|
|
|
|
|
|
InputAccessor::Stat SourcePath::lstat() const
|
|
|
|
{ return accessor->lstat(path); }
|
|
|
|
|
|
|
|
std::optional<InputAccessor::Stat> SourcePath::maybeLstat() const
|
|
|
|
{ return accessor->maybeLstat(path); }
|
|
|
|
|
|
|
|
InputAccessor::DirEntries SourcePath::readDirectory() const
|
|
|
|
{ return accessor->readDirectory(path); }
|
|
|
|
|
|
|
|
std::string SourcePath::readLink() const
|
|
|
|
{ return accessor->readLink(path); }
|
|
|
|
|
|
|
|
void SourcePath::dumpPath(
|
|
|
|
Sink & sink,
|
|
|
|
PathFilter & filter) const
|
|
|
|
{ return accessor->dumpPath(path, sink, filter); }
|
|
|
|
|
2024-02-06 23:23:58 +02:00
|
|
|
std::optional<std::filesystem::path> SourcePath::getPhysicalPath() const
|
2023-12-18 23:14:42 +02:00
|
|
|
{ return accessor->getPhysicalPath(path); }
|
|
|
|
|
|
|
|
std::string SourcePath::to_string() const
|
|
|
|
{ return accessor->showPath(path); }
|
|
|
|
|
2024-02-05 16:13:11 +02:00
|
|
|
SourcePath SourcePath::operator / (const CanonPath & x) const
|
|
|
|
{ return {accessor, path / x}; }
|
2023-12-18 23:14:42 +02:00
|
|
|
|
2024-02-05 16:13:11 +02:00
|
|
|
SourcePath SourcePath::operator / (std::string_view c) const
|
|
|
|
{ return {accessor, path / c}; }
|
2023-12-18 23:14:42 +02:00
|
|
|
|
|
|
|
bool SourcePath::operator==(const SourcePath & x) const
|
|
|
|
{
|
|
|
|
return std::tie(*accessor, path) == std::tie(*x.accessor, x.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SourcePath::operator!=(const SourcePath & x) const
|
|
|
|
{
|
|
|
|
return std::tie(*accessor, path) != std::tie(*x.accessor, x.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SourcePath::operator<(const SourcePath & x) const
|
|
|
|
{
|
|
|
|
return std::tie(*accessor, path) < std::tie(*x.accessor, x.path);
|
|
|
|
}
|
|
|
|
|
2024-02-10 21:56:54 +02:00
|
|
|
SourcePath SourcePath::resolveSymlinks(SymlinkResolution mode) const
|
2023-12-18 23:14:42 +02:00
|
|
|
{
|
|
|
|
auto res = SourcePath(accessor);
|
|
|
|
|
|
|
|
int linksAllowed = 1024;
|
|
|
|
|
|
|
|
std::list<std::string> todo;
|
|
|
|
for (auto & c : path)
|
|
|
|
todo.push_back(std::string(c));
|
|
|
|
|
2024-02-10 21:56:54 +02:00
|
|
|
bool resolve_last = mode == SymlinkResolution::Full;
|
|
|
|
|
2023-12-18 23:14:42 +02:00
|
|
|
while (!todo.empty()) {
|
|
|
|
auto c = *todo.begin();
|
|
|
|
todo.pop_front();
|
|
|
|
if (c == "" || c == ".")
|
|
|
|
;
|
|
|
|
else if (c == "..")
|
|
|
|
res.path.pop();
|
|
|
|
else {
|
|
|
|
res.path.push(c);
|
2024-02-10 21:56:54 +02:00
|
|
|
if (resolve_last || !todo.empty()) {
|
|
|
|
if (auto st = res.maybeLstat(); st && st->type == InputAccessor::tSymlink) {
|
|
|
|
if (!linksAllowed--)
|
|
|
|
throw Error("infinite symlink recursion in path '%s'", path);
|
|
|
|
auto target = res.readLink();
|
|
|
|
res.path.pop();
|
|
|
|
if (hasPrefix(target, "/"))
|
|
|
|
res.path = CanonPath::root;
|
|
|
|
todo.splice(todo.begin(), tokenizeString<std::list<std::string>>(target, "/"));
|
|
|
|
}
|
2023-12-18 23:14:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream & operator<<(std::ostream & str, const SourcePath & path)
|
|
|
|
{
|
|
|
|
str << path.to_string();
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|