feat: adopt VoidLibrary VL-001..VL-013, self-building build.cpp, live logs, and generated CLI help
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 10s
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 10s
This commit is contained in:
1 parent
ab64147f9d
commit
363fb8200e
33 files changed
+1754
-3155
No files matched your search
+68
-498
@@ -1,18 +1,13 @@
|
||||
#include <Void/Build/Package/Archive.hpp>
|
||||
|
||||
#include <Void/Archive/Archive.hpp>
|
||||
#include <Void/Build/Platform/FileOps.hpp>
|
||||
#include <Void/Build/Util/Sha256.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <format>
|
||||
#include <set>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace Void::Build::Package {
|
||||
namespace {
|
||||
@@ -21,157 +16,13 @@ Void::Result::Error make_error(Void::System::SystemErrorCode code, std::string m
|
||||
return Void::System::make_system_error(code, std::move(msg), err);
|
||||
}
|
||||
|
||||
bool is_safe_archive_path(std::string_view raw) {
|
||||
if (raw.empty() || raw.front() == '/' || raw.find('\\') != std::string_view::npos) {
|
||||
return false;
|
||||
}
|
||||
for (const unsigned char c : raw) {
|
||||
if (c < 0x20 || c == 0x7f) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
std::size_t pos = 0;
|
||||
while (pos <= raw.size()) {
|
||||
const auto slash = raw.find('/', pos);
|
||||
const auto seg =
|
||||
raw.substr(pos, slash == std::string_view::npos ? std::string_view::npos : slash - pos);
|
||||
if (seg == "..") {
|
||||
return false;
|
||||
}
|
||||
if (slash == std::string_view::npos) {
|
||||
break;
|
||||
}
|
||||
pos = slash + 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void write_octal(char *dst, std::size_t width, std::uint64_t val) {
|
||||
std::memset(dst, '0', width - 1);
|
||||
dst[width - 1] = '\0';
|
||||
for (std::size_t i = width - 1; i > 0; --i) {
|
||||
dst[i - 1] = static_cast<char>('0' + (val & 7u));
|
||||
val >>= 3;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::uint64_t> parse_octal(const char *src, std::size_t width) {
|
||||
std::size_t i = 0;
|
||||
while (i < width && (src[i] == ' ' || src[i] == '\0')) {
|
||||
++i;
|
||||
}
|
||||
std::uint64_t val = 0;
|
||||
bool any = false;
|
||||
for (; i < width; ++i) {
|
||||
if (src[i] == '\0' || src[i] == ' ') {
|
||||
break;
|
||||
}
|
||||
if (src[i] < '0' || src[i] > '7') {
|
||||
return std::nullopt;
|
||||
}
|
||||
any = true;
|
||||
val = (val << 3) | static_cast<std::uint64_t>(src[i] - '0');
|
||||
}
|
||||
return any ? std::optional<std::uint64_t>{val} : std::optional<std::uint64_t>{0};
|
||||
}
|
||||
|
||||
void finalize_tar_header(std::array<char, 512> &hdr, std::string_view name, std::uint32_t mode,
|
||||
std::uint64_t size, std::int64_t mtime, char typeflag) {
|
||||
hdr.fill('\0');
|
||||
if (name.size() <= 100) {
|
||||
std::memcpy(hdr.data(), name.data(), name.size());
|
||||
} else {
|
||||
// Split into USTAR prefix (155) + name (100) at a slash boundary.
|
||||
const auto split = name.rfind('/', 154);
|
||||
if (split != std::string_view::npos && name.size() - split - 1 <= 100) {
|
||||
std::memcpy(hdr.data() + 345, name.data(), split);
|
||||
std::memcpy(hdr.data(), name.data() + split + 1, name.size() - split - 1);
|
||||
} else {
|
||||
std::memcpy(hdr.data(), name.data(), std::min<std::size_t>(name.size(), 100));
|
||||
}
|
||||
}
|
||||
write_octal(hdr.data() + 100, 8, mode & 0777u);
|
||||
write_octal(hdr.data() + 108, 8, 0); // canonical uid=0
|
||||
write_octal(hdr.data() + 116, 8, 0); // canonical gid=0
|
||||
write_octal(hdr.data() + 124, 12, size);
|
||||
write_octal(hdr.data() + 136, 12, mtime >= 0 ? static_cast<std::uint64_t>(mtime) : 0u);
|
||||
std::memset(hdr.data() + 148, ' ', 8);
|
||||
hdr[156] = typeflag;
|
||||
std::memcpy(hdr.data() + 257, "ustar", 6);
|
||||
hdr[263] = '0';
|
||||
hdr[264] = '0';
|
||||
|
||||
std::uint64_t chksum = 0;
|
||||
for (const char c : hdr) {
|
||||
chksum += static_cast<unsigned char>(c);
|
||||
}
|
||||
write_octal(hdr.data() + 148, 7, chksum);
|
||||
hdr[155] = ' ';
|
||||
}
|
||||
|
||||
std::string build_pax_path_record(std::string_view path) {
|
||||
// Format: "<len> path=<path>\n"
|
||||
std::string body = " path=" + std::string{path} + "\n";
|
||||
std::size_t total = body.size() + 1;
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
const auto digits = std::to_string(total).size();
|
||||
if (digits + body.size() == total) {
|
||||
break;
|
||||
}
|
||||
total = digits + body.size();
|
||||
}
|
||||
return std::to_string(total) + body;
|
||||
}
|
||||
|
||||
bool write_all(int fd, const void *data, std::size_t len) {
|
||||
const auto *p = static_cast<const char *>(data);
|
||||
std::size_t off = 0;
|
||||
while (off < len) {
|
||||
const ssize_t n = ::write(fd, p + off, len - off);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
off += static_cast<std::size_t>(n);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read_exact(int fd, void *data, std::size_t len, std::size_t &got_out) {
|
||||
auto *p = static_cast<char *>(data);
|
||||
got_out = 0;
|
||||
while (got_out < len) {
|
||||
const ssize_t n = ::read(fd, p + got_out, len - got_out);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (n == 0) {
|
||||
bool is_excluded_package_path(const std::filesystem::path &rel) {
|
||||
for (const auto &part : rel) {
|
||||
const auto s = part.string();
|
||||
if (s == ".git" || s == ".vbs" || s == "build" || s == ".serena" || s == ".cache" ||
|
||||
s == "keys" || s == ".env" || s == "WG.conf") {
|
||||
return true;
|
||||
}
|
||||
got_out += static_cast<std::size_t>(n);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool is_zero_block(const std::array<char, 512> &blk) {
|
||||
return std::ranges::all_of(blk, [](char c) { return c == '\0'; });
|
||||
}
|
||||
|
||||
bool is_excluded_source_pkg_part(std::string_view part) {
|
||||
if (part == ".git" || part == ".vbs" || part == "build" || part == ".serena" ||
|
||||
part == ".cache" || part == "keys" || part == "WG.conf") {
|
||||
return true;
|
||||
}
|
||||
if (part == ".env" || (part.starts_with(".env.") && part != ".env.example")) {
|
||||
return true;
|
||||
}
|
||||
if (part.ends_with(".pem") || part.ends_with(".key")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -182,140 +33,39 @@ Void::Result::Result<std::string, Void::Result::Error>
|
||||
create_deterministic_tar(const std::filesystem::path &output_tar,
|
||||
std::vector<ArchiveEntrySpec> entries,
|
||||
std::int64_t reproducibility_epoch) {
|
||||
std::ranges::sort(entries, [](const ArchiveEntrySpec &a, const ArchiveEntrySpec &b) {
|
||||
return a.archive_path < b.archive_path;
|
||||
});
|
||||
|
||||
std::set<std::string> seen_names;
|
||||
for (const auto &e : entries) {
|
||||
if (!is_safe_archive_path(e.archive_path)) {
|
||||
return std::unexpected{make_error(
|
||||
Void::System::SystemErrorCode::invalid_argument,
|
||||
std::format("create_deterministic_tar: unsafe archive path '{}'", e.archive_path))};
|
||||
}
|
||||
if (!seen_names.insert(e.archive_path).second) {
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
std::format("create_deterministic_tar: duplicate archive path '{}'",
|
||||
e.archive_path))};
|
||||
}
|
||||
}
|
||||
|
||||
std::error_code ec;
|
||||
if (output_tar.has_parent_path()) {
|
||||
std::filesystem::create_directories(output_tar.parent_path(), ec);
|
||||
}
|
||||
const auto temp_tar =
|
||||
output_tar.parent_path() / (output_tar.filename().string() + ".stage_" +
|
||||
std::to_string(static_cast<long long>(::getpid())));
|
||||
const int fd = ::open(temp_tar.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
|
||||
if (fd < 0) {
|
||||
return std::unexpected{make_error(Void::System::map_errno_to_system_code(errno),
|
||||
"create_deterministic_tar: cannot open temp file",
|
||||
errno)};
|
||||
}
|
||||
Void::System::OwnedFd guard{fd};
|
||||
auto fail_cleanup = [&] {
|
||||
(void)guard.close();
|
||||
std::filesystem::remove(temp_tar, ec);
|
||||
};
|
||||
|
||||
Util::Sha256Hasher hasher;
|
||||
auto write_hashed = [&](const void *buf, std::size_t n) -> bool {
|
||||
hasher.update(std::span<const std::uint8_t>{static_cast<const std::uint8_t *>(buf), n});
|
||||
return write_all(fd, buf, n);
|
||||
};
|
||||
std::ranges::sort(entries, {}, &ArchiveEntrySpec::archive_path);
|
||||
|
||||
std::array<char, 512> zero_pad{};
|
||||
std::array<char, 512> hdr{};
|
||||
std::array<char, 65536> copy_buf{};
|
||||
|
||||
for (const auto &entry : entries) {
|
||||
auto meta = Platform::stat_lstat(entry.source_file);
|
||||
if (!meta || !meta->exists || !meta->is_regular) {
|
||||
fail_cleanup();
|
||||
return std::unexpected{make_error(
|
||||
Void::System::SystemErrorCode::not_found,
|
||||
std::format("create_deterministic_tar: source '{}' is not a regular file",
|
||||
entry.source_file.string()))};
|
||||
}
|
||||
|
||||
// Use PAX extended header whenever path > 100 chars to guarantee lossless long paths.
|
||||
if (entry.archive_path.size() > 100) {
|
||||
const std::string pax = build_pax_path_record(entry.archive_path);
|
||||
finalize_tar_header(hdr, "PaxHeader/" + entry.archive_path.substr(0, 60), 0644,
|
||||
pax.size(), reproducibility_epoch, 'x');
|
||||
if (!write_hashed(hdr.data(), hdr.size()) || !write_hashed(pax.data(), pax.size())) {
|
||||
fail_cleanup();
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error, "tar write failed")};
|
||||
}
|
||||
const std::size_t rem = pax.size() % 512;
|
||||
if (rem != 0 && !write_hashed(zero_pad.data(), 512 - rem)) {
|
||||
fail_cleanup();
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error, "tar pad write failed")};
|
||||
}
|
||||
}
|
||||
|
||||
finalize_tar_header(hdr, entry.archive_path, entry.mode, meta->size, reproducibility_epoch,
|
||||
'0');
|
||||
if (!write_hashed(hdr.data(), hdr.size())) {
|
||||
fail_cleanup();
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error, "tar header write failed")};
|
||||
}
|
||||
|
||||
const int sfd = ::open(entry.source_file.c_str(), O_RDONLY | O_CLOEXEC);
|
||||
if (sfd < 0) {
|
||||
fail_cleanup();
|
||||
return std::unexpected{make_error(Void::System::map_errno_to_system_code(errno),
|
||||
"cannot open source file for tar", errno)};
|
||||
}
|
||||
Void::System::OwnedFd sguard{sfd};
|
||||
std::uint64_t remaining = meta->size;
|
||||
while (remaining > 0) {
|
||||
const std::size_t chunk =
|
||||
static_cast<std::size_t>(std::min<std::uint64_t>(remaining, copy_buf.size()));
|
||||
std::size_t got = 0;
|
||||
if (!read_exact(sfd, copy_buf.data(), chunk, got) || got != chunk) {
|
||||
fail_cleanup();
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
|
||||
"short read while archiving file")};
|
||||
}
|
||||
if (!write_hashed(copy_buf.data(), got)) {
|
||||
fail_cleanup();
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error, "tar body write failed")};
|
||||
}
|
||||
remaining -= got;
|
||||
}
|
||||
const std::size_t pad = static_cast<std::size_t>((512 - (meta->size % 512)) % 512);
|
||||
if (pad > 0 && !write_hashed(zero_pad.data(), pad)) {
|
||||
fail_cleanup();
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error, "tar padding write failed")};
|
||||
}
|
||||
std::vector<Void::Archive::CreateEntry> v_entries;
|
||||
v_entries.reserve(entries.size());
|
||||
for (const auto &e : entries) {
|
||||
Void::Archive::CreateEntry ce;
|
||||
ce.path = e.archive_path;
|
||||
ce.type = Void::Archive::EntryType::RegularFile;
|
||||
ce.executable = (e.mode & 0111u) != 0;
|
||||
ce.source_file = e.source_file;
|
||||
ce.mode = e.mode & 0777u;
|
||||
v_entries.push_back(std::move(ce));
|
||||
}
|
||||
|
||||
// Two 512-byte zero blocks terminate a valid tar archive.
|
||||
if (!write_hashed(zero_pad.data(), 512) || !write_hashed(zero_pad.data(), 512)) {
|
||||
fail_cleanup();
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error, "tar trailer write failed")};
|
||||
}
|
||||
Void::Archive::CreationOptions opts;
|
||||
opts.compression = Void::Archive::ArchiveCompression::None;
|
||||
opts.reproducible = true;
|
||||
opts.canonical_mtime = reproducibility_epoch;
|
||||
opts.canonical_uid = 0;
|
||||
opts.canonical_gid = 0;
|
||||
opts.prefer_builtin_tar = true;
|
||||
|
||||
if (const auto closed = guard.close(); !closed) {
|
||||
std::filesystem::remove(temp_tar, ec);
|
||||
return std::unexpected{closed.error()};
|
||||
// Delegate deterministic PAX/USTAR tar creation directly to Void::Archive (VL-006).
|
||||
auto created = Void::Archive::create(output_tar, v_entries, opts);
|
||||
if (!created) {
|
||||
return std::unexpected{std::move(created.error())};
|
||||
}
|
||||
std::filesystem::rename(temp_tar, output_tar, ec);
|
||||
if (ec) {
|
||||
std::filesystem::remove(temp_tar, ec);
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error, "tar atomic publish failed")};
|
||||
}
|
||||
return hasher.finalize_hex();
|
||||
return Util::sha256_file_hex(output_tar);
|
||||
}
|
||||
|
||||
Void::Result::Result<std::string, Void::Result::Error>
|
||||
@@ -324,8 +74,9 @@ create_source_package(const std::filesystem::path &project_root,
|
||||
std::error_code ec;
|
||||
const auto canon = std::filesystem::weakly_canonical(project_root, ec);
|
||||
if (ec || !std::filesystem::is_directory(canon)) {
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::not_found, "project root does not exist")};
|
||||
return std::unexpected{make_error(
|
||||
Void::System::SystemErrorCode::not_found,
|
||||
std::format("create_source_package: '{}' is not a directory", project_root.string()))};
|
||||
}
|
||||
|
||||
std::vector<ArchiveEntrySpec> entries;
|
||||
@@ -333,23 +84,19 @@ create_source_package(const std::filesystem::path &project_root,
|
||||
canon, std::filesystem::directory_options::skip_permission_denied, ec};
|
||||
for (; !ec && it != std::filesystem::recursive_directory_iterator{}; it.increment(ec)) {
|
||||
const auto rel = it->path().lexically_relative(canon);
|
||||
bool skip = false;
|
||||
for (const auto &part : rel) {
|
||||
if (is_excluded_source_pkg_part(part.string())) {
|
||||
skip = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (skip) {
|
||||
if (is_excluded_package_path(rel)) {
|
||||
if (it->is_directory(ec)) {
|
||||
it.disable_recursion_pending();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (it->is_regular_file(ec)) {
|
||||
auto meta = Platform::stat_lstat(it->path());
|
||||
const std::uint32_t mode = (meta && (meta->mode & 0111u)) ? 0755u : 0644u;
|
||||
entries.push_back({rel.lexically_normal().generic_string(), it->path(), mode});
|
||||
auto meta = Platform::stat_lstat(it->path());
|
||||
if (!meta || !meta->exists || meta->is_symlink) {
|
||||
continue;
|
||||
}
|
||||
if (meta->is_regular) {
|
||||
entries.push_back({rel.lexically_normal().generic_string(), it->path(),
|
||||
(meta->mode & 0111u) ? 0755u : 0644u});
|
||||
}
|
||||
}
|
||||
return create_deterministic_tar(output_tar, std::move(entries), reproducibility_epoch);
|
||||
@@ -358,222 +105,45 @@ create_source_package(const std::filesystem::path &project_root,
|
||||
Void::Result::Result<ArchiveExtractionStats, Void::Result::Error>
|
||||
extract_safe_tar(const std::filesystem::path &tar_path,
|
||||
const std::filesystem::path &destination_dir, const ArchiveLimits &limits) {
|
||||
const int fd = ::open(tar_path.c_str(), O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
return std::unexpected{make_error(
|
||||
Void::System::map_errno_to_system_code(errno),
|
||||
std::format("extract_safe_tar: cannot open '{}'", tar_path.string()), errno)};
|
||||
}
|
||||
Void::System::OwnedFd guard{fd};
|
||||
|
||||
std::error_code ec;
|
||||
const auto parent = destination_dir.parent_path().empty() ? std::filesystem::path{"."}
|
||||
: destination_dir.parent_path();
|
||||
const auto parent = destination_dir.has_parent_path() ? destination_dir.parent_path()
|
||||
: std::filesystem::path{"."};
|
||||
std::filesystem::create_directories(parent, ec);
|
||||
const auto stage_dir = parent / (destination_dir.filename().string() + ".extract_" +
|
||||
std::to_string(static_cast<long long>(::getpid())));
|
||||
std::filesystem::remove_all(stage_dir, ec);
|
||||
std::filesystem::create_directories(stage_dir, ec);
|
||||
if (ec) {
|
||||
return std::unexpected{make_error(
|
||||
Void::System::map_errno_to_system_code(ec.value()),
|
||||
std::format("extract_safe_tar: failed creating parent '{}'", parent.string()),
|
||||
ec.value())};
|
||||
}
|
||||
|
||||
auto abort_extract = [&] { std::filesystem::remove_all(stage_dir, ec); };
|
||||
// Delegate safe staged extraction (rejecting traversal, absolute paths, symlinks,
|
||||
// hardlinks, duplicates, and quota overflows) directly to Void::Archive (VL-006).
|
||||
Void::Archive::StagedExtractionOptions opts;
|
||||
opts.limits.max_entries = limits.max_entries;
|
||||
opts.limits.max_file_bytes = limits.max_file_bytes;
|
||||
opts.limits.max_total_bytes = limits.max_total_bytes;
|
||||
opts.preserve_executable_bit = true;
|
||||
|
||||
auto staged = Void::Archive::extract_staged(tar_path, parent, opts);
|
||||
if (!staged) {
|
||||
return std::unexpected{std::move(staged.error())};
|
||||
}
|
||||
|
||||
ArchiveExtractionStats stats;
|
||||
std::set<std::string> seen_paths;
|
||||
std::optional<std::string> next_pax_path;
|
||||
std::array<char, 512> hdr{};
|
||||
std::array<char, 65536> buf{};
|
||||
|
||||
for (;;) {
|
||||
std::size_t got = 0;
|
||||
if (!read_exact(fd, hdr.data(), 512, got)) {
|
||||
abort_extract();
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error, "tar read error")};
|
||||
}
|
||||
if (got == 0 || (got == 512 && is_zero_block(hdr))) {
|
||||
break;
|
||||
}
|
||||
if (got != 512) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"truncated tar header")};
|
||||
}
|
||||
|
||||
// Verify header checksum.
|
||||
const auto expected_sum = parse_octal(hdr.data() + 148, 8);
|
||||
if (!expected_sum) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"malformed tar checksum")};
|
||||
}
|
||||
std::uint64_t actual_sum = 0;
|
||||
for (std::size_t i = 0; i < 512; ++i) {
|
||||
actual_sum += (i >= 148 && i < 156) ? static_cast<unsigned char>(' ')
|
||||
: static_cast<unsigned char>(hdr[i]);
|
||||
}
|
||||
if (actual_sum != *expected_sum) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"tar header checksum mismatch")};
|
||||
}
|
||||
|
||||
const auto size_opt = parse_octal(hdr.data() + 124, 12);
|
||||
const auto mode_opt = parse_octal(hdr.data() + 100, 8);
|
||||
if (!size_opt || !mode_opt) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"malformed tar size or mode")};
|
||||
}
|
||||
const std::uint64_t entry_size = *size_opt;
|
||||
const std::uint32_t entry_mode = static_cast<std::uint32_t>(*mode_opt & 0777u);
|
||||
const char typeflag = hdr[156];
|
||||
|
||||
// Handle PAX extended header ('x') for long paths.
|
||||
if (typeflag == 'x') {
|
||||
if (entry_size > 65536) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::limit_exceeded,
|
||||
"PAX header exceeds limit")};
|
||||
}
|
||||
std::string pax_data(static_cast<std::size_t>(entry_size), '\0');
|
||||
if (!read_exact(fd, pax_data.data(), pax_data.size(), got) || got != pax_data.size()) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"truncated PAX body")};
|
||||
}
|
||||
const std::size_t pad = static_cast<std::size_t>((512 - (entry_size % 512)) % 512);
|
||||
if (pad > 0 && (!read_exact(fd, buf.data(), pad, got) || got != pad)) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"truncated PAX padding")};
|
||||
}
|
||||
const auto path_pos = pax_data.find(" path=");
|
||||
if (path_pos != std::string::npos) {
|
||||
const auto val_start = path_pos + 6;
|
||||
const auto nl = pax_data.find('\n', val_start);
|
||||
next_pax_path = pax_data.substr(
|
||||
val_start, nl == std::string::npos ? std::string::npos : nl - val_start);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Reject symlinks ('2'), hardlinks ('1'), devices ('3'/'4'), FIFOs ('6'), etc.!
|
||||
if (typeflag != '0' && typeflag != '\0' && typeflag != '5') {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(
|
||||
Void::System::SystemErrorCode::permission_denied,
|
||||
std::format("archive contains disallowed non-regular entry type '{}'", typeflag))};
|
||||
}
|
||||
|
||||
std::string entry_name;
|
||||
if (next_pax_path) {
|
||||
entry_name = std::move(*next_pax_path);
|
||||
next_pax_path.reset();
|
||||
} else {
|
||||
const std::string name_part(hdr.data(), ::strnlen(hdr.data(), 100));
|
||||
const std::string prefix_part(hdr.data() + 345, ::strnlen(hdr.data() + 345, 155));
|
||||
entry_name = prefix_part.empty() ? name_part : (prefix_part + "/" + name_part);
|
||||
}
|
||||
while (entry_name.starts_with("./")) {
|
||||
entry_name.erase(0, 2);
|
||||
}
|
||||
while (!entry_name.empty() && entry_name.back() == '/') {
|
||||
entry_name.pop_back();
|
||||
}
|
||||
if (entry_name.empty() && typeflag == '5') {
|
||||
continue;
|
||||
}
|
||||
if (!is_safe_archive_path(entry_name)) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(
|
||||
Void::System::SystemErrorCode::permission_denied,
|
||||
std::format("archive contains unsafe or traversal path '{}'", entry_name))};
|
||||
}
|
||||
|
||||
if (++stats.entries > limits.max_entries) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::limit_exceeded,
|
||||
"archive entry count exceeds limit")};
|
||||
}
|
||||
if (entry_size > limits.max_file_bytes ||
|
||||
stats.total_bytes + entry_size > limits.max_total_bytes) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::limit_exceeded,
|
||||
"archive extracted size exceeds limit")};
|
||||
}
|
||||
|
||||
const auto rel_norm = std::filesystem::path{entry_name}.lexically_normal().generic_string();
|
||||
if (!seen_paths.insert(rel_norm).second) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(
|
||||
Void::System::SystemErrorCode::invalid_argument,
|
||||
std::format("archive contains duplicate colliding path '{}'", rel_norm))};
|
||||
}
|
||||
|
||||
const auto dst_path = stage_dir / rel_norm;
|
||||
if (!Platform::path_is_within(stage_dir, dst_path)) {
|
||||
abort_extract();
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::permission_denied,
|
||||
std::format("archive entry '{}' escapes destination", rel_norm))};
|
||||
}
|
||||
|
||||
if (typeflag == '5') {
|
||||
std::filesystem::create_directories(dst_path, ec);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dst_path.has_parent_path()) {
|
||||
std::filesystem::create_directories(dst_path.parent_path(), ec);
|
||||
}
|
||||
const int out_fd = ::open(dst_path.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC,
|
||||
static_cast<mode_t>(entry_mode ? entry_mode : 0644u));
|
||||
if (out_fd < 0) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(
|
||||
Void::System::map_errno_to_system_code(errno),
|
||||
std::format("extract_safe_tar: failed creating '{}'", dst_path.string()), errno)};
|
||||
}
|
||||
Void::System::OwnedFd out_guard{out_fd};
|
||||
|
||||
std::uint64_t remaining = entry_size;
|
||||
while (remaining > 0) {
|
||||
const std::size_t chunk =
|
||||
static_cast<std::size_t>(std::min<std::uint64_t>(remaining, buf.size()));
|
||||
if (!read_exact(fd, buf.data(), chunk, got) || got != chunk) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"truncated tar file body")};
|
||||
}
|
||||
if (!write_all(out_fd, buf.data(), got)) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
|
||||
"failed writing extracted file")};
|
||||
}
|
||||
remaining -= got;
|
||||
}
|
||||
stats.total_bytes += entry_size;
|
||||
stats.extracted_files.push_back(std::filesystem::path{rel_norm});
|
||||
|
||||
const std::size_t pad = static_cast<std::size_t>((512 - (entry_size % 512)) % 512);
|
||||
if (pad > 0 && (!read_exact(fd, buf.data(), pad, got) || got != pad)) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"truncated tar file padding")};
|
||||
stats.entries = staged->entries();
|
||||
stats.total_bytes = staged->bytes();
|
||||
for (const auto &info : staged->inventory()) {
|
||||
if (info.type == Void::Archive::EntryType::RegularFile) {
|
||||
stats.extracted_files.emplace_back(info.path);
|
||||
}
|
||||
}
|
||||
|
||||
// Atomic publication into destination_dir.
|
||||
if (std::filesystem::exists(destination_dir, ec)) {
|
||||
std::filesystem::remove_all(destination_dir, ec);
|
||||
}
|
||||
std::filesystem::rename(stage_dir, destination_dir, ec);
|
||||
if (ec) {
|
||||
abort_extract();
|
||||
return std::unexpected{make_error(
|
||||
Void::System::map_errno_to_system_code(ec.value()),
|
||||
std::format("extract_safe_tar: atomic rename to '{}' failed", destination_dir.string()),
|
||||
ec.value())};
|
||||
auto pub = staged->publish(destination_dir.filename().string());
|
||||
if (!pub) {
|
||||
return std::unexpected{std::move(pub.error())};
|
||||
}
|
||||
return stats;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
#include <Void/Build/Platform/ProcessSupervisor.hpp>
|
||||
#include <Void/Build/Snapshot/Snapshot.hpp>
|
||||
#include <Void/Build/Util/Sha256.hpp>
|
||||
#include <Void/Configuration/Env.hpp>
|
||||
#include <Void/Core/Scope.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
#include <Void/Text/Str.hpp>
|
||||
|
||||
@@ -265,6 +267,26 @@ materialize_dependency_source(const DependencyRecord &dep, const FetchOptions &o
|
||||
|
||||
// 3. Exact pinned Git revision source.
|
||||
if (dep.source_kind == DependencySourceKind::Git) {
|
||||
if (dep.name == "VoidLibrary") {
|
||||
if (auto override_dir = Void::Configuration::get_env("VBS_VOID_SOURCE_DIR");
|
||||
override_dir && !override_dir->empty()) {
|
||||
const std::filesystem::path raw{*override_dir};
|
||||
const auto abs = raw.is_absolute() ? raw : (options.project_root / raw);
|
||||
const auto canon = std::filesystem::weakly_canonical(abs, ec);
|
||||
if (!ec && std::filesystem::is_directory(canon)) {
|
||||
Void::Log::info("Fetch",
|
||||
"Using VBS_VOID_SOURCE_DIR override for 'VoidLibrary': {}",
|
||||
canon.string());
|
||||
MaterializedSource out;
|
||||
out.alias = dep.name;
|
||||
out.source_dir = canon;
|
||||
out.content_identity_hash = dep.revision + "-local";
|
||||
out.from_cache = true;
|
||||
return out;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const auto git_dir =
|
||||
options.cache_dir / "git" / (sanitize_key(dep.name) + "_" + dep.revision);
|
||||
const auto marker = git_dir / ".vbs_verified_rev";
|
||||
@@ -275,6 +297,8 @@ materialize_dependency_source(const DependencyRecord &dep, const FetchOptions &o
|
||||
}
|
||||
|
||||
if (std::filesystem::is_regular_file(marker, ec)) {
|
||||
Void::Log::info("Fetch", "Using cached git dependency '{}' @ {}", dep.name,
|
||||
dep.revision.substr(0, 12));
|
||||
MaterializedSource out;
|
||||
out.alias = dep.name;
|
||||
out.source_dir = git_dir;
|
||||
@@ -283,6 +307,9 @@ materialize_dependency_source(const DependencyRecord &dep, const FetchOptions &o
|
||||
return out;
|
||||
}
|
||||
|
||||
Void::Log::info("Fetch", "Fetching git dependency '{}' from {} @ {}...", dep.name,
|
||||
redact_secrets(dep.location), dep.revision.substr(0, 12));
|
||||
|
||||
const bool is_local_repo = dep.location.starts_with("file://") ||
|
||||
dep.location.starts_with("/") ||
|
||||
std::filesystem::is_directory(dep.location, ec);
|
||||
|
||||
+2
-93
@@ -1,7 +1,6 @@
|
||||
#include <Void/Build/Package/Solver.hpp>
|
||||
|
||||
#include <Void/System/Types.hpp>
|
||||
#include <Void/Text/Str.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <format>
|
||||
@@ -16,99 +15,9 @@ Void::Result::Error solve_err(std::string msg) {
|
||||
std::move(msg));
|
||||
}
|
||||
|
||||
bool all_digits(std::string_view s) { return Void::Str::is_digit_ascii(s); }
|
||||
|
||||
std::string normalize_semver_token(std::string_view tok) {
|
||||
if (tok.empty() || tok == "*") {
|
||||
return std::string{tok};
|
||||
}
|
||||
std::string_view op;
|
||||
if (tok.starts_with(">=") || tok.starts_with("<=")) {
|
||||
op = tok.substr(0, 2);
|
||||
tok.remove_prefix(2);
|
||||
} else if (tok.starts_with('^') || tok.starts_with('~') || tok.starts_with('>') ||
|
||||
tok.starts_with('<') || tok.starts_with('=')) {
|
||||
op = tok.substr(0, 1);
|
||||
tok.remove_prefix(1);
|
||||
}
|
||||
|
||||
const auto meta_pos = tok.find_first_of("-+");
|
||||
const auto core = meta_pos == std::string_view::npos ? tok : tok.substr(0, meta_pos);
|
||||
const auto meta =
|
||||
meta_pos == std::string_view::npos ? std::string_view{} : tok.substr(meta_pos);
|
||||
|
||||
if (core.find_first_of("xX*") != std::string_view::npos) {
|
||||
return std::string{op} + std::string{tok};
|
||||
}
|
||||
|
||||
const auto dot1 = core.find('.');
|
||||
if (dot1 == std::string_view::npos) {
|
||||
if (!all_digits(core)) {
|
||||
return std::string{op} + std::string{tok};
|
||||
}
|
||||
if ((op == "~" || op == "^") && meta.empty()) {
|
||||
const unsigned long maj = std::stoul(std::string{core});
|
||||
if (op == "~" || maj == 0) {
|
||||
return std::format(">={}.0.0 <{}.0.0", maj, maj + 1);
|
||||
}
|
||||
}
|
||||
return std::string{op} + std::string{core} + ".0.0" + std::string{meta};
|
||||
}
|
||||
const auto dot2 = core.find('.', dot1 + 1);
|
||||
if (dot2 == std::string_view::npos) {
|
||||
const auto p1 = core.substr(0, dot1);
|
||||
const auto p2 = core.substr(dot1 + 1);
|
||||
if (!all_digits(p1) || !all_digits(p2)) {
|
||||
return std::string{op} + std::string{tok};
|
||||
}
|
||||
return std::string{op} + std::string{p1} + "." + std::string{p2} + ".0" + std::string{meta};
|
||||
}
|
||||
return std::string{op} + std::string{tok};
|
||||
}
|
||||
|
||||
// Adapter for shorthand SemVer comparator tokens (^1, ^1.0, ~1.2, <2) since
|
||||
// pinned Void::Semver::parse_range requires full major.minor.patch triplets.
|
||||
// Delegates directly to Void::Semver::parse_range (VL-013 shorthand comparators now upstream).
|
||||
std::optional<Void::Semver::VersionRange> parse_semver_range_compat(std::string_view expr) {
|
||||
if (auto direct = Void::Semver::parse_range(expr); direct) {
|
||||
return direct;
|
||||
}
|
||||
std::string normalized;
|
||||
std::size_t pos = 0;
|
||||
bool first_clause = true;
|
||||
while (pos <= expr.size()) {
|
||||
const auto sep = expr.find("||", pos);
|
||||
const auto clause = expr.substr(pos, sep == std::string_view::npos ? sep : sep - pos);
|
||||
if (!first_clause) {
|
||||
normalized += " || ";
|
||||
}
|
||||
first_clause = false;
|
||||
|
||||
bool first_tok = true;
|
||||
std::size_t i = 0;
|
||||
while (i < clause.size()) {
|
||||
while (i < clause.size() && (clause[i] == ' ' || clause[i] == '\t')) {
|
||||
++i;
|
||||
}
|
||||
if (i == clause.size()) {
|
||||
break;
|
||||
}
|
||||
auto end = clause.find_first_of(" \t", i);
|
||||
if (end == std::string_view::npos) {
|
||||
end = clause.size();
|
||||
}
|
||||
if (!first_tok) {
|
||||
normalized.push_back(' ');
|
||||
}
|
||||
first_tok = false;
|
||||
normalized += normalize_semver_token(clause.substr(i, end - i));
|
||||
i = end;
|
||||
}
|
||||
if (sep == std::string_view::npos) {
|
||||
break;
|
||||
}
|
||||
pos = sep + 2;
|
||||
}
|
||||
return Void::Semver::parse_range(normalized);
|
||||
return Void::Semver::parse_range(expr);
|
||||
}
|
||||
|
||||
struct ActiveConstraint {
|
||||
|
||||
+92
-642
@@ -1,13 +1,11 @@
|
||||
#include <Void/Build/Package/Toml.hpp>
|
||||
|
||||
#include <Void/Build/Util/Json.hpp>
|
||||
#include <Void/Data/Toml.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <charconv>
|
||||
#include <cmath>
|
||||
#include <format>
|
||||
#include <set>
|
||||
|
||||
@@ -19,652 +17,101 @@ Void::Result::Error parse_err(const SourceOrigin &orig, std::string msg) {
|
||||
std::format("{}: {}", orig.format(), std::move(msg)));
|
||||
}
|
||||
|
||||
class TomlParser {
|
||||
public:
|
||||
TomlParser(std::string_view text, std::string source_name)
|
||||
: text_{text}, source_name_{std::move(source_name)} {}
|
||||
TomlValue convert_from_void_toml(const Void::Data::Toml::Value &src,
|
||||
const std::string &source_name);
|
||||
|
||||
Void::Result::Result<TomlTable, Void::Result::Error> parse() {
|
||||
if (!Util::is_valid_utf8(text_)) {
|
||||
return std::unexpected{parse_err({source_name_, 1, 1}, "invalid UTF-8 in TOML")};
|
||||
}
|
||||
TomlValue root = TomlValue::make_table({source_name_, 1, 1});
|
||||
TomlTable *current_table = &root.as_table_mut();
|
||||
|
||||
while (!eof()) {
|
||||
skip_ws_and_comments(true);
|
||||
if (eof()) {
|
||||
break;
|
||||
}
|
||||
if (peek() == '[') {
|
||||
const auto hdr_origin = origin();
|
||||
advance();
|
||||
const bool is_array_table = (!eof() && peek() == '[');
|
||||
if (is_array_table) {
|
||||
advance();
|
||||
}
|
||||
skip_inline_ws();
|
||||
auto keys = parse_key_path();
|
||||
if (!keys) {
|
||||
return std::unexpected{keys.error()};
|
||||
}
|
||||
skip_inline_ws();
|
||||
if (eof() || peek() != ']') {
|
||||
return std::unexpected{parse_err(origin(), "expected ']' in table header")};
|
||||
}
|
||||
advance();
|
||||
if (is_array_table) {
|
||||
if (eof() || peek() != ']') {
|
||||
return std::unexpected{
|
||||
parse_err(origin(), "expected ']]' in array-of-tables header")};
|
||||
}
|
||||
advance();
|
||||
}
|
||||
if (auto r = expect_line_end(); !r) {
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
auto tbl =
|
||||
navigate_table_header(root.as_table_mut(), *keys, is_array_table, hdr_origin);
|
||||
if (!tbl) {
|
||||
return std::unexpected{tbl.error()};
|
||||
}
|
||||
current_table = *tbl;
|
||||
} else {
|
||||
const auto kv_origin = origin();
|
||||
auto keys = parse_key_path();
|
||||
if (!keys) {
|
||||
return std::unexpected{keys.error()};
|
||||
}
|
||||
skip_inline_ws();
|
||||
if (eof() || peek() != '=') {
|
||||
return std::unexpected{parse_err(origin(), "expected '=' after key")};
|
||||
}
|
||||
advance();
|
||||
skip_inline_ws();
|
||||
auto val = parse_value();
|
||||
if (!val) {
|
||||
return std::unexpected{val.error()};
|
||||
}
|
||||
if (auto r = expect_line_end(); !r) {
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
if (auto r = insert_key_value(*current_table, *keys, std::move(*val), kv_origin);
|
||||
!r) {
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
}
|
||||
}
|
||||
return std::move(root.as_table_mut());
|
||||
TomlTable convert_table_from_void_toml(const Void::Data::Toml::Table &src,
|
||||
const std::string &source_name) {
|
||||
TomlTable dst;
|
||||
for (const auto &[k, v] : src.entries) {
|
||||
dst.emplace(k, convert_from_void_toml(v, source_name));
|
||||
}
|
||||
|
||||
private:
|
||||
bool eof() const noexcept { return pos_ >= text_.size(); }
|
||||
char peek(std::size_t off = 0) const noexcept {
|
||||
return (pos_ + off < text_.size()) ? text_[pos_ + off] : '\0';
|
||||
}
|
||||
char advance() noexcept {
|
||||
const char c = text_[pos_++];
|
||||
if (c == '\n') {
|
||||
++line_;
|
||||
col_ = 1;
|
||||
} else {
|
||||
++col_;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
SourceOrigin origin() const { return {source_name_, line_, col_}; }
|
||||
|
||||
void skip_inline_ws() noexcept {
|
||||
while (!eof() && (peek() == ' ' || peek() == '\t')) {
|
||||
advance();
|
||||
}
|
||||
}
|
||||
|
||||
void skip_ws_and_comments(bool allow_newlines) noexcept {
|
||||
while (!eof()) {
|
||||
const char c = peek();
|
||||
if (c == ' ' || c == '\t' || (allow_newlines && (c == '\n' || c == '\r'))) {
|
||||
advance();
|
||||
} else if (c == '#') {
|
||||
while (!eof() && peek() != '\n') {
|
||||
advance();
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error> expect_line_end() {
|
||||
skip_inline_ws();
|
||||
if (!eof() && peek() == '#') {
|
||||
while (!eof() && peek() != '\n') {
|
||||
advance();
|
||||
}
|
||||
}
|
||||
if (eof()) {
|
||||
return {};
|
||||
}
|
||||
if (peek() == '\r') {
|
||||
advance();
|
||||
}
|
||||
if (eof() || peek() == '\n') {
|
||||
if (!eof()) {
|
||||
advance();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
return std::unexpected{parse_err(origin(), "unexpected trailing characters on line")};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::string, Void::Result::Error> parse_basic_string(bool multiline) {
|
||||
const auto start = origin();
|
||||
if (multiline && !eof() && (peek() == '\n' || (peek() == '\r' && peek(1) == '\n'))) {
|
||||
if (peek() == '\r') {
|
||||
advance();
|
||||
}
|
||||
advance();
|
||||
}
|
||||
std::string out;
|
||||
while (!eof()) {
|
||||
if (multiline && peek() == '"' && peek(1) == '"' && peek(2) == '"') {
|
||||
advance();
|
||||
advance();
|
||||
advance();
|
||||
return out;
|
||||
}
|
||||
if (!multiline && peek() == '"') {
|
||||
advance();
|
||||
return out;
|
||||
}
|
||||
const char c = advance();
|
||||
if (!multiline && (c == '\n' || c == '\r')) {
|
||||
return std::unexpected{parse_err(start, "unterminated basic string")};
|
||||
}
|
||||
if (c == '\\') {
|
||||
if (eof()) {
|
||||
return std::unexpected{parse_err(origin(), "unterminated escape sequence")};
|
||||
}
|
||||
const char esc = advance();
|
||||
switch (esc) {
|
||||
case 'b':
|
||||
out.push_back('\b');
|
||||
break;
|
||||
case 't':
|
||||
out.push_back('\t');
|
||||
break;
|
||||
case 'n':
|
||||
out.push_back('\n');
|
||||
break;
|
||||
case 'f':
|
||||
out.push_back('\f');
|
||||
break;
|
||||
case 'r':
|
||||
out.push_back('\r');
|
||||
break;
|
||||
case '"':
|
||||
out.push_back('"');
|
||||
break;
|
||||
case '\\':
|
||||
out.push_back('\\');
|
||||
break;
|
||||
case '\n':
|
||||
case '\r':
|
||||
case ' ':
|
||||
case '\t':
|
||||
if (!multiline) {
|
||||
return std::unexpected{parse_err(origin(), "invalid escape in string")};
|
||||
}
|
||||
while (!eof() &&
|
||||
(peek() == ' ' || peek() == '\t' || peek() == '\n' || peek() == '\r')) {
|
||||
advance();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return std::unexpected{
|
||||
parse_err(origin(), std::format("unsupported escape '\\{}'", esc))};
|
||||
}
|
||||
} else {
|
||||
out.push_back(c);
|
||||
}
|
||||
}
|
||||
return std::unexpected{parse_err(start, "unterminated string literal")};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::string, Void::Result::Error> parse_literal_string(bool multiline) {
|
||||
const auto start = origin();
|
||||
if (multiline && !eof() && (peek() == '\n' || (peek() == '\r' && peek(1) == '\n'))) {
|
||||
if (peek() == '\r') {
|
||||
advance();
|
||||
}
|
||||
advance();
|
||||
}
|
||||
std::string out;
|
||||
while (!eof()) {
|
||||
if (multiline && peek() == '\'' && peek(1) == '\'' && peek(2) == '\'') {
|
||||
advance();
|
||||
advance();
|
||||
advance();
|
||||
return out;
|
||||
}
|
||||
if (!multiline && peek() == '\'') {
|
||||
advance();
|
||||
return out;
|
||||
}
|
||||
const char c = advance();
|
||||
if (!multiline && (c == '\n' || c == '\r')) {
|
||||
return std::unexpected{parse_err(start, "unterminated literal string")};
|
||||
}
|
||||
out.push_back(c);
|
||||
}
|
||||
return std::unexpected{parse_err(start, "unterminated literal string")};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::string, Void::Result::Error> parse_single_key() {
|
||||
skip_inline_ws();
|
||||
if (eof()) {
|
||||
return std::unexpected{parse_err(origin(), "expected key")};
|
||||
}
|
||||
if (peek() == '"') {
|
||||
advance();
|
||||
return parse_basic_string(false);
|
||||
}
|
||||
if (peek() == '\'') {
|
||||
advance();
|
||||
return parse_literal_string(false);
|
||||
}
|
||||
std::string key;
|
||||
while (!eof()) {
|
||||
const char c = peek();
|
||||
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
|
||||
c == '_' || c == '-') {
|
||||
key.push_back(advance());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (key.empty()) {
|
||||
return std::unexpected{parse_err(origin(), "empty or invalid bare key")};
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
Void::Result::Result<std::vector<std::string>, Void::Result::Error> parse_key_path() {
|
||||
std::vector<std::string> parts;
|
||||
auto first = parse_single_key();
|
||||
if (!first) {
|
||||
return std::unexpected{first.error()};
|
||||
}
|
||||
parts.push_back(std::move(*first));
|
||||
for (;;) {
|
||||
skip_inline_ws();
|
||||
if (!eof() && peek() == '.') {
|
||||
advance();
|
||||
auto next = parse_single_key();
|
||||
if (!next) {
|
||||
return std::unexpected{next.error()};
|
||||
}
|
||||
parts.push_back(std::move(*next));
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
Void::Result::Result<TomlValue, Void::Result::Error> parse_value() {
|
||||
skip_inline_ws();
|
||||
const auto start = origin();
|
||||
if (eof()) {
|
||||
return std::unexpected{parse_err(start, "expected value")};
|
||||
}
|
||||
const char c = peek();
|
||||
if (c == '"') {
|
||||
if (peek(1) == '"' && peek(2) == '"') {
|
||||
advance();
|
||||
advance();
|
||||
advance();
|
||||
auto s = parse_basic_string(true);
|
||||
if (!s) {
|
||||
return std::unexpected{s.error()};
|
||||
}
|
||||
return TomlValue{std::move(*s), start};
|
||||
}
|
||||
advance();
|
||||
auto s = parse_basic_string(false);
|
||||
if (!s) {
|
||||
return std::unexpected{s.error()};
|
||||
}
|
||||
return TomlValue{std::move(*s), start};
|
||||
}
|
||||
if (c == '\'') {
|
||||
if (peek(1) == '\'' && peek(2) == '\'') {
|
||||
advance();
|
||||
advance();
|
||||
advance();
|
||||
auto s = parse_literal_string(true);
|
||||
if (!s) {
|
||||
return std::unexpected{s.error()};
|
||||
}
|
||||
return TomlValue{std::move(*s), start};
|
||||
}
|
||||
advance();
|
||||
auto s = parse_literal_string(false);
|
||||
if (!s) {
|
||||
return std::unexpected{s.error()};
|
||||
}
|
||||
return TomlValue{std::move(*s), start};
|
||||
}
|
||||
if (c == '[') {
|
||||
return parse_array();
|
||||
}
|
||||
if (c == '{') {
|
||||
return parse_inline_table();
|
||||
}
|
||||
|
||||
// Read scalar token (bool, int, float).
|
||||
std::string token;
|
||||
while (!eof()) {
|
||||
const char ch = peek();
|
||||
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '#' || ch == ',' ||
|
||||
ch == ']' || ch == '}') {
|
||||
break;
|
||||
}
|
||||
token.push_back(advance());
|
||||
}
|
||||
if (token == "true") {
|
||||
return TomlValue{true, start};
|
||||
}
|
||||
if (token == "false") {
|
||||
return TomlValue{false, start};
|
||||
}
|
||||
|
||||
std::string clean;
|
||||
for (const char ch : token) {
|
||||
if (ch != '_') {
|
||||
clean.push_back(ch);
|
||||
}
|
||||
}
|
||||
if (clean.empty()) {
|
||||
return std::unexpected{parse_err(start, "empty scalar value")};
|
||||
}
|
||||
|
||||
// Integer (hex/oct/bin/dec).
|
||||
const bool looks_float = clean.find('.') != std::string::npos ||
|
||||
clean.find('e') != std::string::npos ||
|
||||
clean.find('E') != std::string::npos;
|
||||
if (!looks_float) {
|
||||
int base = 10;
|
||||
std::string_view digits{clean};
|
||||
if (digits.starts_with("0x")) {
|
||||
base = 16;
|
||||
digits.remove_prefix(2);
|
||||
} else if (digits.starts_with("0o")) {
|
||||
base = 8;
|
||||
digits.remove_prefix(2);
|
||||
} else if (digits.starts_with("0b")) {
|
||||
base = 2;
|
||||
digits.remove_prefix(2);
|
||||
} else if (digits.starts_with("+")) {
|
||||
digits.remove_prefix(1);
|
||||
}
|
||||
std::int64_t iv = 0;
|
||||
const auto [ptr, ec] =
|
||||
std::from_chars(digits.data(), digits.data() + digits.size(), iv, base);
|
||||
if (ec == std::errc{} && ptr == digits.data() + digits.size()) {
|
||||
return TomlValue{iv, start};
|
||||
}
|
||||
}
|
||||
double dv = 0.0;
|
||||
const auto [ptr, ec] = std::from_chars(clean.data(), clean.data() + clean.size(), dv);
|
||||
if (ec == std::errc{} && ptr == clean.data() + clean.size()) {
|
||||
return TomlValue{dv, start};
|
||||
}
|
||||
return std::unexpected{parse_err(start, std::format("invalid TOML value '{}'", token))};
|
||||
}
|
||||
|
||||
Void::Result::Result<TomlValue, Void::Result::Error> parse_array() {
|
||||
const auto start = origin();
|
||||
advance(); // '['
|
||||
TomlValue arr = TomlValue::make_array(start);
|
||||
for (;;) {
|
||||
skip_ws_and_comments(true);
|
||||
if (eof()) {
|
||||
return std::unexpected{parse_err(start, "unterminated array")};
|
||||
}
|
||||
if (peek() == ']') {
|
||||
advance();
|
||||
return arr;
|
||||
}
|
||||
auto elem = parse_value();
|
||||
if (!elem) {
|
||||
return std::unexpected{elem.error()};
|
||||
}
|
||||
arr.as_array_mut().push_back(std::move(*elem));
|
||||
skip_ws_and_comments(true);
|
||||
if (!eof() && peek() == ',') {
|
||||
advance();
|
||||
continue;
|
||||
}
|
||||
if (!eof() && peek() == ']') {
|
||||
advance();
|
||||
return arr;
|
||||
}
|
||||
return std::unexpected{parse_err(origin(), "expected ',' or ']' in array")};
|
||||
}
|
||||
}
|
||||
|
||||
Void::Result::Result<TomlValue, Void::Result::Error> parse_inline_table() {
|
||||
const auto start = origin();
|
||||
advance(); // '{'
|
||||
TomlValue tbl = TomlValue::make_table(start);
|
||||
skip_inline_ws();
|
||||
if (!eof() && peek() == '}') {
|
||||
advance();
|
||||
return tbl;
|
||||
}
|
||||
for (;;) {
|
||||
skip_inline_ws();
|
||||
const auto kv_origin = origin();
|
||||
auto keys = parse_key_path();
|
||||
if (!keys) {
|
||||
return std::unexpected{keys.error()};
|
||||
}
|
||||
skip_inline_ws();
|
||||
if (eof() || peek() != '=') {
|
||||
return std::unexpected{parse_err(origin(), "expected '=' in inline table")};
|
||||
}
|
||||
advance();
|
||||
auto val = parse_value();
|
||||
if (!val) {
|
||||
return std::unexpected{val.error()};
|
||||
}
|
||||
if (auto r = insert_key_value(tbl.as_table_mut(), *keys, std::move(*val), kv_origin);
|
||||
!r) {
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
skip_inline_ws();
|
||||
if (!eof() && peek() == ',') {
|
||||
advance();
|
||||
continue;
|
||||
}
|
||||
if (!eof() && peek() == '}') {
|
||||
advance();
|
||||
return tbl;
|
||||
}
|
||||
return std::unexpected{parse_err(origin(), "expected ',' or '}' in inline table")};
|
||||
}
|
||||
}
|
||||
|
||||
Void::Result::Result<TomlTable *, Void::Result::Error>
|
||||
navigate_table_header(TomlTable &root, const std::vector<std::string> &keys,
|
||||
bool is_array_table, const SourceOrigin &hdr_origin) {
|
||||
TomlTable *cur = &root;
|
||||
for (std::size_t i = 0; i + 1 < keys.size(); ++i) {
|
||||
const auto &k = keys[i];
|
||||
auto it = cur->find(k);
|
||||
if (it == cur->end()) {
|
||||
it = cur->emplace(k, TomlValue::make_table(hdr_origin)).first;
|
||||
}
|
||||
if (it->second.is_table()) {
|
||||
cur = &it->second.as_table_mut();
|
||||
} else if (it->second.is_array() && !it->second.as_array().empty() &&
|
||||
it->second.as_array().back().is_table()) {
|
||||
cur = &it->second.as_array_mut().back().as_table_mut();
|
||||
} else {
|
||||
return std::unexpected{
|
||||
parse_err(hdr_origin, std::format("key '{}' is not a table", k))};
|
||||
}
|
||||
}
|
||||
const auto &last = keys.back();
|
||||
if (is_array_table) {
|
||||
auto it = cur->find(last);
|
||||
if (it == cur->end()) {
|
||||
it = cur->emplace(last, TomlValue::make_array(hdr_origin)).first;
|
||||
}
|
||||
if (!it->second.is_array()) {
|
||||
return std::unexpected{parse_err(
|
||||
hdr_origin, std::format("key '{}' cannot be redefined as array table", last))};
|
||||
}
|
||||
it->second.as_array_mut().push_back(TomlValue::make_table(hdr_origin));
|
||||
return &it->second.as_array_mut().back().as_table_mut();
|
||||
}
|
||||
auto it = cur->find(last);
|
||||
if (it == cur->end()) {
|
||||
it = cur->emplace(last, TomlValue::make_table(hdr_origin)).first;
|
||||
}
|
||||
if (!it->second.is_table()) {
|
||||
return std::unexpected{
|
||||
parse_err(hdr_origin, std::format("key '{}' already defined as non-table", last))};
|
||||
}
|
||||
std::string full_header;
|
||||
for (std::size_t i = 0; i < keys.size(); ++i) {
|
||||
if (i > 0) {
|
||||
full_header += '.';
|
||||
}
|
||||
full_header += keys[i];
|
||||
}
|
||||
if (!explicit_tables_.insert(full_header).second) {
|
||||
return std::unexpected{
|
||||
parse_err(hdr_origin, std::format("duplicate table header [{}]", full_header))};
|
||||
}
|
||||
return &it->second.as_table_mut();
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
insert_key_value(TomlTable &table, const std::vector<std::string> &keys, TomlValue val,
|
||||
const SourceOrigin &kv_origin) {
|
||||
TomlTable *cur = &table;
|
||||
for (std::size_t i = 0; i + 1 < keys.size(); ++i) {
|
||||
const auto &k = keys[i];
|
||||
auto it = cur->find(k);
|
||||
if (it == cur->end()) {
|
||||
it = cur->emplace(k, TomlValue::make_table(kv_origin)).first;
|
||||
}
|
||||
if (!it->second.is_table()) {
|
||||
return std::unexpected{
|
||||
parse_err(kv_origin, std::format("cannot index into non-table key '{}'", k))};
|
||||
}
|
||||
cur = &it->second.as_table_mut();
|
||||
}
|
||||
const auto &leaf = keys.back();
|
||||
if (cur->contains(leaf)) {
|
||||
return std::unexpected{
|
||||
parse_err(kv_origin, std::format("duplicate TOML key '{}'", leaf))};
|
||||
}
|
||||
cur->emplace(leaf, std::move(val));
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string_view text_;
|
||||
std::string source_name_;
|
||||
std::size_t pos_{0};
|
||||
std::size_t line_{1};
|
||||
std::size_t col_{1};
|
||||
std::set<std::string> explicit_tables_;
|
||||
};
|
||||
|
||||
std::string toml_quote_key(std::string_view key) {
|
||||
bool bare_ok = !key.empty();
|
||||
for (const char c : key) {
|
||||
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
|
||||
c == '_' || c == '-')) {
|
||||
bare_ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return bare_ok ? std::string{key} : Util::json_quote(key);
|
||||
return dst;
|
||||
}
|
||||
|
||||
std::string serialize_scalar_or_inline(const TomlValue &v) {
|
||||
if (v.is_string()) {
|
||||
return Util::json_quote(v.as_string());
|
||||
}
|
||||
if (v.is_bool()) {
|
||||
return v.as_bool() ? "true" : "false";
|
||||
}
|
||||
if (v.is_int()) {
|
||||
return std::to_string(v.as_int());
|
||||
}
|
||||
if (v.is_double()) {
|
||||
return std::format("{}", v.as_double());
|
||||
}
|
||||
if (v.is_array()) {
|
||||
std::string out{"["};
|
||||
const auto &arr = v.as_array();
|
||||
for (std::size_t i = 0; i < arr.size(); ++i) {
|
||||
if (i > 0) {
|
||||
out += ", ";
|
||||
}
|
||||
out += serialize_scalar_or_inline(arr[i]);
|
||||
TomlValue convert_from_void_toml(const Void::Data::Toml::Value &src,
|
||||
const std::string &source_name) {
|
||||
const SourceOrigin orig{source_name, src.origin.line, src.origin.column};
|
||||
switch (src.kind()) {
|
||||
case Void::Data::Toml::ValueKind::String:
|
||||
return TomlValue{*src.as_string(), orig};
|
||||
case Void::Data::Toml::ValueKind::Boolean:
|
||||
return TomlValue{*src.as_bool(), orig};
|
||||
case Void::Data::Toml::ValueKind::Integer:
|
||||
return TomlValue{*src.as_integer(), orig};
|
||||
case Void::Data::Toml::ValueKind::Float:
|
||||
return TomlValue{*src.as_float(), orig};
|
||||
case Void::Data::Toml::ValueKind::DateTime:
|
||||
return TomlValue{src.as_datetime()->to_string(), orig};
|
||||
case Void::Data::Toml::ValueKind::Array: {
|
||||
TomlValue out = TomlValue::make_array(orig);
|
||||
for (const auto &el : src.as_array()->elements) {
|
||||
out.as_array_mut().push_back(convert_from_void_toml(el, source_name));
|
||||
}
|
||||
out += "]";
|
||||
return out;
|
||||
}
|
||||
std::string out{"{"};
|
||||
bool first = true;
|
||||
for (const auto &[k, val] : v.as_table()) {
|
||||
if (!first) {
|
||||
out += ", ";
|
||||
}
|
||||
first = false;
|
||||
out += toml_quote_key(k) + " = " + serialize_scalar_or_inline(val);
|
||||
case Void::Data::Toml::ValueKind::Table: {
|
||||
TomlValue out = TomlValue::make_table(orig);
|
||||
out.as_table_mut() = convert_table_from_void_toml(*src.as_table(), source_name);
|
||||
return out;
|
||||
}
|
||||
out += "}";
|
||||
return out;
|
||||
case Void::Data::Toml::ValueKind::ArrayOfTables: {
|
||||
TomlValue out = TomlValue::make_array(orig);
|
||||
for (const auto &tbl : src.as_array_of_tables()->tables) {
|
||||
TomlValue item = TomlValue::make_table(orig);
|
||||
item.as_table_mut() = convert_table_from_void_toml(tbl, source_name);
|
||||
out.as_array_mut().push_back(std::move(item));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
return TomlValue{std::string{}, orig};
|
||||
}
|
||||
|
||||
void serialize_table_section(const TomlTable &tbl, const std::string &prefix, std::string &out) {
|
||||
// Scalars & arrays first.
|
||||
for (const auto &[k, v] : tbl) {
|
||||
if (!v.is_table()) {
|
||||
out += toml_quote_key(k) + " = " + serialize_scalar_or_inline(v) + "\n";
|
||||
}
|
||||
}
|
||||
// Nested subtables next.
|
||||
for (const auto &[k, v] : tbl) {
|
||||
if (v.is_table()) {
|
||||
const std::string header =
|
||||
prefix.empty() ? toml_quote_key(k) : (prefix + "." + toml_quote_key(k));
|
||||
out += "\n[" + header + "]\n";
|
||||
serialize_table_section(v.as_table(), header, out);
|
||||
}
|
||||
Void::Data::Toml::Value convert_to_void_toml(const TomlValue &src);
|
||||
|
||||
Void::Data::Toml::Table convert_table_to_void_toml(const TomlTable &src) {
|
||||
Void::Data::Toml::Table dst;
|
||||
for (const auto &[k, v] : src) {
|
||||
dst.entries.emplace(k, convert_to_void_toml(v));
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
bool option_value_equal(const OptionValue &a, const OptionValue &b) { return a.value == b.value; }
|
||||
Void::Data::Toml::Value convert_to_void_toml(const TomlValue &src) {
|
||||
if (src.is_string()) {
|
||||
return Void::Data::Toml::Value{src.as_string()};
|
||||
}
|
||||
if (src.is_bool()) {
|
||||
return Void::Data::Toml::Value{src.as_bool()};
|
||||
}
|
||||
if (src.is_int()) {
|
||||
return Void::Data::Toml::Value{src.as_int()};
|
||||
}
|
||||
if (src.is_double()) {
|
||||
return Void::Data::Toml::Value{src.as_double()};
|
||||
}
|
||||
if (src.is_array()) {
|
||||
Void::Data::Toml::Array arr;
|
||||
for (const auto &el : src.as_array()) {
|
||||
arr.elements.push_back(convert_to_void_toml(el));
|
||||
}
|
||||
return Void::Data::Toml::Value{std::move(arr)};
|
||||
}
|
||||
return Void::Data::Toml::Value{convert_table_to_void_toml(src.as_table())};
|
||||
}
|
||||
|
||||
bool dependency_records_equal(const DependencyRecord &a, const DependencyRecord &b) {
|
||||
if (a.source_kind != b.source_kind || a.location != b.location || a.revision != b.revision ||
|
||||
a.package != b.package || a.version != b.version || a.builder != b.builder ||
|
||||
a.registry_authority != b.registry_authority || a.sha256 != b.sha256 ||
|
||||
a.features != b.features || a.build_dependency != b.build_dependency ||
|
||||
a.options.size() != b.options.size()) {
|
||||
if (a.name != b.name || a.source_kind != b.source_kind || a.location != b.location ||
|
||||
a.revision != b.revision || a.sha256 != b.sha256 || a.package != b.package ||
|
||||
a.version != b.version || a.registry_authority != b.registry_authority ||
|
||||
a.builder != b.builder || a.features != b.features ||
|
||||
a.build_dependency != b.build_dependency || a.options.size() != b.options.size()) {
|
||||
return false;
|
||||
}
|
||||
for (const auto &[k, av] : a.options) {
|
||||
const auto bit = b.options.find(k);
|
||||
if (bit == b.options.end() || !option_value_equal(av, bit->second)) {
|
||||
const auto it = b.options.find(k);
|
||||
if (it == b.options.end() || av.value != it->second.value) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -675,23 +122,29 @@ bool dependency_records_equal(const DependencyRecord &a, const DependencyRecord
|
||||
|
||||
std::string SourceOrigin::format() const {
|
||||
if (line == 0) {
|
||||
return file.empty() ? "<unknown>" : file;
|
||||
return file;
|
||||
}
|
||||
return std::format("{}:{}:{}", file, line, column);
|
||||
}
|
||||
|
||||
TomlValue TomlValue::make_table(SourceOrigin origin) {
|
||||
return TomlValue{std::make_shared<TomlTable>(), std::move(origin)};
|
||||
TomlValue TomlValue::make_table(SourceOrigin orig) {
|
||||
return TomlValue{std::make_shared<TomlTable>(), std::move(orig)};
|
||||
}
|
||||
|
||||
TomlValue TomlValue::make_array(SourceOrigin origin) {
|
||||
return TomlValue{std::make_shared<TomlArray>(), std::move(origin)};
|
||||
TomlValue TomlValue::make_array(SourceOrigin orig) {
|
||||
return TomlValue{std::make_shared<TomlArray>(), std::move(orig)};
|
||||
}
|
||||
|
||||
Void::Result::Result<TomlTable, Void::Result::Error> parse_toml(std::string_view text,
|
||||
std::string_view source_name) {
|
||||
TomlParser parser{text, std::string{source_name}};
|
||||
return parser.parse();
|
||||
// Delegate full TOML 1.0 parsing directly to Void::Data::Toml (VL-002).
|
||||
auto parsed = Void::Data::Toml::parse(text);
|
||||
if (!parsed) {
|
||||
const SourceOrigin err_orig{std::string{source_name}, parsed.error().origin.line,
|
||||
parsed.error().origin.column};
|
||||
return std::unexpected{parse_err(err_orig, parsed.error().message)};
|
||||
}
|
||||
return convert_table_from_void_toml(parsed->root, std::string{source_name});
|
||||
}
|
||||
|
||||
Void::Result::Result<TomlTable, Void::Result::Error>
|
||||
@@ -704,9 +157,7 @@ parse_toml_file(const std::filesystem::path &path) {
|
||||
}
|
||||
|
||||
std::string serialize_toml(const TomlTable &table) {
|
||||
std::string out;
|
||||
serialize_table_section(table, "", out);
|
||||
return out;
|
||||
return Void::Data::Toml::serialize_table(convert_table_to_void_toml(table));
|
||||
}
|
||||
|
||||
Void::Result::Result<std::vector<NormalizedDependency>, Void::Result::Error>
|
||||
@@ -717,7 +168,6 @@ load_dependencies_toml(const std::filesystem::path &toml_path) {
|
||||
}
|
||||
const auto &root = *root_res;
|
||||
|
||||
// Validate top-level keys (reject unknown fields explicitly!).
|
||||
for (const auto &[k, v] : root) {
|
||||
if (k != "dependencies" && k != "build_dependencies" && k != "registries") {
|
||||
return std::unexpected{parse_err(
|
||||
|
||||
Reference in new issue
Block a user