2023-10-23 12:05:50 +03:00
|
|
|
#include "posix-source-accessor.hh"
|
2023-10-25 07:43:36 +03:00
|
|
|
#include "signals.hh"
|
2023-12-06 14:55:07 +02:00
|
|
|
#include "sync.hh"
|
|
|
|
|
|
|
|
#include <unordered_map>
|
2023-10-23 12:05:50 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2024-02-06 23:23:58 +02:00
|
|
|
PosixSourceAccessor::PosixSourceAccessor(std::filesystem::path && root)
|
|
|
|
: root(std::move(root))
|
|
|
|
{
|
|
|
|
assert(root.empty() || root.is_absolute());
|
|
|
|
displayPrefix = root;
|
|
|
|
}
|
|
|
|
|
|
|
|
PosixSourceAccessor::PosixSourceAccessor()
|
|
|
|
: PosixSourceAccessor(std::filesystem::path {})
|
|
|
|
{ }
|
|
|
|
|
|
|
|
std::pair<PosixSourceAccessor, CanonPath> PosixSourceAccessor::createAtRoot(const std::filesystem::path & path)
|
|
|
|
{
|
|
|
|
std::filesystem::path path2 = absPath(path.native());
|
|
|
|
return {
|
|
|
|
PosixSourceAccessor { path2.root_path() },
|
|
|
|
CanonPath { static_cast<std::string>(path2.relative_path()) },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::filesystem::path PosixSourceAccessor::makeAbsPath(const CanonPath & path)
|
|
|
|
{
|
|
|
|
return root.empty()
|
|
|
|
? (std::filesystem::path { path.abs() })
|
2024-02-19 14:54:40 +02:00
|
|
|
: path.isRoot()
|
|
|
|
? /* Don't append a slash for the root of the accessor, since
|
|
|
|
it can be a non-directory (e.g. in the case of `fetchTree
|
|
|
|
{ type = "file" }`). */
|
|
|
|
root
|
2024-02-06 23:23:58 +02:00
|
|
|
: root / path.rel();
|
|
|
|
}
|
|
|
|
|
2023-10-23 12:05:50 +03:00
|
|
|
void PosixSourceAccessor::readFile(
|
|
|
|
const CanonPath & path,
|
|
|
|
Sink & sink,
|
|
|
|
std::function<void(uint64_t)> sizeCallback)
|
|
|
|
{
|
2023-12-06 00:02:59 +02:00
|
|
|
assertNoSymlinks(path);
|
|
|
|
|
2024-02-06 23:23:58 +02:00
|
|
|
auto ap = makeAbsPath(path);
|
|
|
|
|
|
|
|
AutoCloseFD fd = open(ap.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
|
2023-10-23 12:05:50 +03:00
|
|
|
if (!fd)
|
2024-02-06 23:23:58 +02:00
|
|
|
throw SysError("opening file '%1%'", ap.native());
|
2023-10-23 12:05:50 +03:00
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(fd.get(), &st) == -1)
|
|
|
|
throw SysError("statting file");
|
|
|
|
|
|
|
|
sizeCallback(st.st_size);
|
|
|
|
|
|
|
|
off_t left = st.st_size;
|
|
|
|
|
2023-12-10 11:34:55 +02:00
|
|
|
std::array<unsigned char, 64 * 1024> buf;
|
2023-10-23 12:05:50 +03:00
|
|
|
while (left) {
|
|
|
|
checkInterrupt();
|
|
|
|
ssize_t rd = read(fd.get(), buf.data(), (size_t) std::min(left, (off_t) buf.size()));
|
|
|
|
if (rd == -1) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
throw SysError("reading from file '%s'", showPath(path));
|
|
|
|
}
|
|
|
|
else if (rd == 0)
|
|
|
|
throw SysError("unexpected end-of-file reading '%s'", showPath(path));
|
|
|
|
else {
|
|
|
|
assert(rd <= left);
|
|
|
|
sink({(char *) buf.data(), (size_t) rd});
|
|
|
|
left -= rd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PosixSourceAccessor::pathExists(const CanonPath & path)
|
|
|
|
{
|
2023-12-06 00:02:59 +02:00
|
|
|
if (auto parent = path.parent()) assertNoSymlinks(*parent);
|
2024-02-06 23:23:58 +02:00
|
|
|
return nix::pathExists(makeAbsPath(path));
|
2023-10-23 12:05:50 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 14:55:07 +02:00
|
|
|
std::optional<struct stat> PosixSourceAccessor::cachedLstat(const CanonPath & path)
|
|
|
|
{
|
|
|
|
static Sync<std::unordered_map<CanonPath, std::optional<struct stat>>> _cache;
|
|
|
|
|
|
|
|
{
|
|
|
|
auto cache(_cache.lock());
|
|
|
|
auto i = cache->find(path);
|
|
|
|
if (i != cache->end()) return i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<struct stat> st{std::in_place};
|
2024-02-06 23:23:58 +02:00
|
|
|
if (::lstat(makeAbsPath(path).c_str(), &*st)) {
|
2023-12-06 14:55:07 +02:00
|
|
|
if (errno == ENOENT || errno == ENOTDIR)
|
|
|
|
st.reset();
|
|
|
|
else
|
|
|
|
throw SysError("getting status of '%s'", showPath(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto cache(_cache.lock());
|
|
|
|
if (cache->size() >= 16384) cache->clear();
|
|
|
|
cache->emplace(path, st);
|
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2023-11-01 16:26:07 +02:00
|
|
|
std::optional<SourceAccessor::Stat> PosixSourceAccessor::maybeLstat(const CanonPath & path)
|
2023-10-23 12:05:50 +03:00
|
|
|
{
|
2023-12-06 00:02:59 +02:00
|
|
|
if (auto parent = path.parent()) assertNoSymlinks(*parent);
|
2023-12-06 14:55:07 +02:00
|
|
|
auto st = cachedLstat(path);
|
|
|
|
if (!st) return std::nullopt;
|
|
|
|
mtime = std::max(mtime, st->st_mtime);
|
2023-10-23 12:05:50 +03:00
|
|
|
return Stat {
|
|
|
|
.type =
|
2023-12-06 14:55:07 +02:00
|
|
|
S_ISREG(st->st_mode) ? tRegular :
|
|
|
|
S_ISDIR(st->st_mode) ? tDirectory :
|
|
|
|
S_ISLNK(st->st_mode) ? tSymlink :
|
2023-10-23 12:05:50 +03:00
|
|
|
tMisc,
|
2023-12-06 14:55:07 +02:00
|
|
|
.fileSize = S_ISREG(st->st_mode) ? std::optional<uint64_t>(st->st_size) : std::nullopt,
|
|
|
|
.isExecutable = S_ISREG(st->st_mode) && st->st_mode & S_IXUSR,
|
2023-10-23 12:05:50 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceAccessor::DirEntries PosixSourceAccessor::readDirectory(const CanonPath & path)
|
|
|
|
{
|
2023-12-06 00:02:59 +02:00
|
|
|
assertNoSymlinks(path);
|
2023-10-23 12:05:50 +03:00
|
|
|
DirEntries res;
|
2024-02-06 23:23:58 +02:00
|
|
|
for (auto & entry : nix::readDirectory(makeAbsPath(path))) {
|
2023-10-23 12:05:50 +03:00
|
|
|
std::optional<Type> type;
|
|
|
|
switch (entry.type) {
|
|
|
|
case DT_REG: type = Type::tRegular; break;
|
|
|
|
case DT_LNK: type = Type::tSymlink; break;
|
|
|
|
case DT_DIR: type = Type::tDirectory; break;
|
|
|
|
}
|
|
|
|
res.emplace(entry.name, type);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PosixSourceAccessor::readLink(const CanonPath & path)
|
|
|
|
{
|
2023-12-06 00:02:59 +02:00
|
|
|
if (auto parent = path.parent()) assertNoSymlinks(*parent);
|
2024-02-06 23:23:58 +02:00
|
|
|
return nix::readLink(makeAbsPath(path));
|
2023-10-23 12:05:50 +03:00
|
|
|
}
|
|
|
|
|
2024-02-06 23:23:58 +02:00
|
|
|
std::optional<std::filesystem::path> PosixSourceAccessor::getPhysicalPath(const CanonPath & path)
|
2023-10-23 12:05:50 +03:00
|
|
|
{
|
2024-02-06 23:23:58 +02:00
|
|
|
return makeAbsPath(path);
|
2023-10-23 12:05:50 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 00:02:59 +02:00
|
|
|
void PosixSourceAccessor::assertNoSymlinks(CanonPath path)
|
|
|
|
{
|
|
|
|
while (!path.isRoot()) {
|
2023-12-06 14:55:07 +02:00
|
|
|
auto st = cachedLstat(path);
|
|
|
|
if (st && S_ISLNK(st->st_mode))
|
2023-12-06 00:02:59 +02:00
|
|
|
throw Error("path '%s' is a symlink", showPath(path));
|
|
|
|
path.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 12:05:50 +03:00
|
|
|
}
|