2023-10-19 15:45:26 +03:00
|
|
|
#include "source-accessor.hh"
|
|
|
|
#include "archive.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
static std::atomic<size_t> nextNumber{0};
|
|
|
|
|
|
|
|
SourceAccessor::SourceAccessor()
|
|
|
|
: number(++nextNumber)
|
2023-11-20 19:54:36 +02:00
|
|
|
, displayPrefix{"«unknown»"}
|
2023-10-19 15:45:26 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-11-01 18:09:28 +02:00
|
|
|
bool SourceAccessor::pathExists(const CanonPath & path)
|
|
|
|
{
|
|
|
|
return maybeLstat(path).has_value();
|
|
|
|
}
|
|
|
|
|
2023-10-20 17:36:41 +03:00
|
|
|
std::string SourceAccessor::readFile(const CanonPath & path)
|
|
|
|
{
|
|
|
|
StringSink sink;
|
|
|
|
std::optional<uint64_t> size;
|
|
|
|
readFile(path, sink, [&](uint64_t _size)
|
|
|
|
{
|
|
|
|
size = _size;
|
|
|
|
});
|
|
|
|
assert(size && *size == sink.s.size());
|
|
|
|
return std::move(sink.s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceAccessor::readFile(
|
|
|
|
const CanonPath & path,
|
|
|
|
Sink & sink,
|
|
|
|
std::function<void(uint64_t)> sizeCallback)
|
|
|
|
{
|
|
|
|
auto s = readFile(path);
|
|
|
|
sizeCallback(s.size());
|
|
|
|
sink(s);
|
|
|
|
}
|
|
|
|
|
2023-10-19 15:45:26 +03:00
|
|
|
Hash SourceAccessor::hashPath(
|
2023-11-28 15:20:27 +02:00
|
|
|
const CanonPath & path,
|
|
|
|
PathFilter & filter,
|
|
|
|
HashAlgorithm ha)
|
2023-10-19 15:45:26 +03:00
|
|
|
{
|
2023-11-28 15:20:27 +02:00
|
|
|
HashSink sink(ha);
|
2023-10-19 15:45:26 +03:00
|
|
|
dumpPath(path, sink, filter);
|
|
|
|
return sink.finish().first;
|
|
|
|
}
|
|
|
|
|
2023-11-01 16:26:07 +02:00
|
|
|
SourceAccessor::Stat SourceAccessor::lstat(const CanonPath & path)
|
2023-10-19 15:45:26 +03:00
|
|
|
{
|
2023-11-01 16:26:07 +02:00
|
|
|
if (auto st = maybeLstat(path))
|
|
|
|
return *st;
|
|
|
|
else
|
|
|
|
throw Error("path '%s' does not exist", showPath(path));
|
2023-10-19 15:45:26 +03:00
|
|
|
}
|
|
|
|
|
2023-11-20 19:54:36 +02:00
|
|
|
void SourceAccessor::setPathDisplay(std::string displayPrefix, std::string displaySuffix)
|
|
|
|
{
|
|
|
|
this->displayPrefix = std::move(displayPrefix);
|
|
|
|
this->displaySuffix = std::move(displaySuffix);
|
|
|
|
}
|
|
|
|
|
2023-10-19 15:45:26 +03:00
|
|
|
std::string SourceAccessor::showPath(const CanonPath & path)
|
|
|
|
{
|
2023-11-20 19:54:36 +02:00
|
|
|
return displayPrefix + path.abs() + displaySuffix;
|
2023-10-19 15:45:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|