2023-10-23 12:05:50 +03:00
|
|
|
#include "posix-source-accessor.hh"
|
2024-05-06 20:05:42 +03:00
|
|
|
#include "source-path.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());
|
2023-09-03 00:35:16 +03:00
|
|
|
displayPrefix = root.string();
|
2024-02-06 23:23:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PosixSourceAccessor::PosixSourceAccessor()
|
|
|
|
: PosixSourceAccessor(std::filesystem::path {})
|
|
|
|
{ }
|
|
|
|
|
2024-05-06 20:05:42 +03:00
|
|
|
SourcePath PosixSourceAccessor::createAtRoot(const std::filesystem::path & path)
|
2024-02-06 23:23:58 +02:00
|
|
|
{
|
2023-09-03 00:35:16 +03:00
|
|
|
std::filesystem::path path2 = absPath(path.string());
|
2024-02-06 23:23:58 +02:00
|
|
|
return {
|
2024-05-06 20:05:42 +03:00
|
|
|
make_ref<PosixSourceAccessor>(path2.root_path()),
|
2023-09-03 00:35:16 +03:00
|
|
|
CanonPath { path2.relative_path().string() },
|
2024-02-06 23:23:58 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2023-09-03 00:35:16 +03:00
|
|
|
AutoCloseFD fd = toDescriptor(open(ap.string().c_str(), O_RDONLY
|
|
|
|
#ifndef _WIN32
|
|
|
|
| O_NOFOLLOW | O_CLOEXEC
|
|
|
|
#endif
|
|
|
|
));
|
2023-10-23 12:05:50 +03:00
|
|
|
if (!fd)
|
2023-09-03 00:35:16 +03:00
|
|
|
throw SysError("opening file '%1%'", ap.string());
|
2023-10-23 12:05:50 +03:00
|
|
|
|
|
|
|
struct stat st;
|
2023-09-03 00:35:16 +03:00
|
|
|
if (fstat(fromDescriptorReadOnly(fd.get()), &st) == -1)
|
2023-10-23 12:05:50 +03:00
|
|
|
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();
|
2023-09-03 00:35:16 +03:00
|
|
|
ssize_t rd = read(fromDescriptorReadOnly(fd.get()), buf.data(), (size_t) std::min(left, (off_t) buf.size()));
|
2023-10-23 12:05:50 +03:00
|
|
|
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);
|
2023-09-03 00:35:16 +03:00
|
|
|
return nix::pathExists(makeAbsPath(path).string());
|
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)
|
|
|
|
{
|
2024-03-04 23:24:12 +02:00
|
|
|
static Sync<std::unordered_map<Path, std::optional<struct stat>>> _cache;
|
2024-03-04 22:54:35 +02:00
|
|
|
|
2024-03-04 23:24:12 +02:00
|
|
|
// Note: we convert std::filesystem::path to Path because the
|
|
|
|
// former is not hashable on libc++.
|
2023-09-03 00:35:16 +03:00
|
|
|
Path absPath = makeAbsPath(path).string();
|
2023-12-06 14:55:07 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
auto cache(_cache.lock());
|
2024-03-04 22:54:35 +02:00
|
|
|
auto i = cache->find(absPath);
|
2023-12-06 14:55:07 +02:00
|
|
|
if (i != cache->end()) return i->second;
|
|
|
|
}
|
|
|
|
|
2024-03-29 21:21:10 +02:00
|
|
|
auto st = nix::maybeLstat(absPath.c_str());
|
2023-12-06 14:55:07 +02:00
|
|
|
|
|
|
|
auto cache(_cache.lock());
|
|
|
|
if (cache->size() >= 16384) cache->clear();
|
2024-03-04 22:54:35 +02:00
|
|
|
cache->emplace(absPath, st);
|
2023-12-06 14:55:07 +02:00
|
|
|
|
|
|
|
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-05-12 15:12:18 +03:00
|
|
|
for (auto & entry : std::filesystem::directory_iterator{makeAbsPath(path)}) {
|
2024-05-07 20:57:39 +03:00
|
|
|
auto type = [&]() -> std::optional<Type> {
|
|
|
|
std::filesystem::file_type nativeType;
|
|
|
|
try {
|
|
|
|
nativeType = entry.symlink_status().type();
|
|
|
|
} catch (std::filesystem::filesystem_error & e) {
|
|
|
|
// We cannot always stat the child. (Ideally there is no
|
|
|
|
// stat because the native directory entry has the type
|
|
|
|
// already, but this isn't always the case.)
|
|
|
|
if (e.code() == std::errc::permission_denied || e.code() == std::errc::operation_not_permitted)
|
|
|
|
return std::nullopt;
|
|
|
|
else throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cannot exhaustively enumerate because implementation-specific
|
|
|
|
// additional file types are allowed.
|
2024-05-07 07:14:49 +03:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wswitch-enum"
|
2024-05-07 20:57:39 +03:00
|
|
|
switch (nativeType) {
|
|
|
|
case std::filesystem::file_type::regular: return Type::tRegular; break;
|
|
|
|
case std::filesystem::file_type::symlink: return Type::tSymlink; break;
|
|
|
|
case std::filesystem::file_type::directory: return Type::tDirectory; break;
|
|
|
|
default: return tMisc;
|
|
|
|
}
|
2024-05-07 07:14:49 +03:00
|
|
|
#pragma GCC diagnostic pop
|
2024-05-07 20:57:39 +03:00
|
|
|
}();
|
2024-05-07 18:29:33 +03:00
|
|
|
res.emplace(entry.path().filename().string(), type);
|
2023-10-23 12:05:50 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PosixSourceAccessor::readLink(const CanonPath & path)
|
|
|
|
{
|
2023-12-06 00:02:59 +02:00
|
|
|
if (auto parent = path.parent()) assertNoSymlinks(*parent);
|
2023-09-03 00:35:16 +03:00
|
|
|
return nix::readLink(makeAbsPath(path).string());
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-06 20:16:52 +03:00
|
|
|
ref<SourceAccessor> getFSSourceAccessor()
|
2024-05-03 13:30:28 +03:00
|
|
|
{
|
|
|
|
static auto rootFS = make_ref<PosixSourceAccessor>();
|
|
|
|
return rootFS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref<SourceAccessor> makeFSSourceAccessor(std::filesystem::path root)
|
|
|
|
{
|
|
|
|
return make_ref<PosixSourceAccessor>(std::move(root));
|
|
|
|
}
|
2023-10-23 12:05:50 +03:00
|
|
|
}
|