feat: portability, correctness, and quality improvements for v0.2

Portability (Linux distro-agnostic):
- Remove hardcoded Clang compiler enforcement; GCC now builds
- Add find_package(Threads REQUIRED) for older glibc
- Add cmake install() target
- FHS 3.0 default root: /kappa -> /usr/local/kappa
- fs::path operator/ for all init/bootloader paths (fixes prefix fragility)
- Multi-distro zoneinfo search (FHS, NixOS, Guix, alt)
- Portable tar extraction (drop GNU-only --no-same-permissions)
- Runit enable/disable commands now prefix-aware
- --root CLI flag before/after subcommand, lazy directory creation
- Shebang constants de-duplicated to types.hpp

Correctness (race conditions, UB, corruption):
- Fix CWD race in scheduler: per-child chdir() instead of process-global
- Fix UB const_cast in exec_cmd/exec_capture: mutable argv buffers
- Fix non-atomic installed DB writes: tmp+rename pattern
- Fix read_file() no longer calls exit(1), throws instead
- Fix silent catch(...) parse errors now print diagnostics
- Fix rebuild false positives with config_hash change detection
- Fix s6 disable_cmd copy-paste bug (was identical to enable)
- Fix runit enable_cmd incomplete, disable_cmd wrong target
- Fix dinit env vars: functional env-file + companion .env

Quality:
- Add -Wall -Wextra -Wpedantic to CMake, fix 2 pre-existing warnings
- Move parse_int from error.hpp to parse_util.hpp
- Fix hash verification guard checks all three hash types
- Check patch return code in fetch.cpp
- Add explicit system_dir creation in ensure_directories()
- Add resolve to needs_dirs for build_registry() consistency
- Update stale /kappa path references in examples
- Remove inaccurate -Werror claim in CONTRIBUTING.md
- Add build-gcc/ and agent dirs to .gitignore
- Suppress clang-tidy portability-avoid-pragma-once
- Fix .gitignore /kappa pattern (was matching include/kappa/)
- Delete stale vcpkg_installed/ directory

54/54 tests pass. Builds on Clang and GCC with 0 warnings.
This commit is contained in:
2026-07-31 08:28:49 -04:00
parent dd984f96d4
commit 7e93db2d07
25 changed files with 353 additions and 133 deletions
+82 -45
View File
@@ -16,9 +16,11 @@
#include <cstdlib>
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string_view>
using namespace std::string_view_literals;
@@ -49,6 +51,7 @@ Subcommands:
Options:
-h, --help Show this help message
-V, --version Show version information
--root <path> Set kappa root directory (default: /usr/local/kappa)
)";
}
@@ -59,8 +62,7 @@ static bool is_flag(std::string_view arg) {
static std::string read_file(const char* path) {
std::ifstream in(path);
if (!in) {
std::cerr << "error: cannot open '" << path << "'\n";
std::exit(1);
throw std::runtime_error(std::format("cannot open '{}'", path));
}
std::ostringstream buf;
buf << in.rdbuf();
@@ -114,8 +116,10 @@ static resolve::Registry build_registry(const dsl::SystemConfig& cfg) {
registry[pkg.name] = std::move(pkg);
found = true;
break;
} catch (const std::exception& e) {
std::cerr << "warning: parse error in " << sp << ": " << e.what() << "\n";
} catch (...) {
continue;
std::cerr << "warning: unknown parse error in " << sp << "\n";
}
}
@@ -131,7 +135,11 @@ static resolve::Registry build_registry(const dsl::SystemConfig& cfg) {
auto pkg = dsl::parse(buf.str());
registry[pkg.name] = std::move(pkg);
found = true;
} catch (...) {}
} catch (const std::exception& e) {
std::cerr << "warning: parse error in remote recipe " << pref.name << ": " << e.what() << "\n";
} catch (...) {
std::cerr << "warning: unknown parse error in remote recipe " << pref.name << "\n";
}
}
}
}
@@ -146,15 +154,20 @@ static resolve::Registry build_registry(const dsl::SystemConfig& cfg) {
}
int main(int argc, char* argv[]) {
paths::ensure_directories();
if (argc < 2) {
std::cerr << "kappa: missing subcommand\n\n";
print_usage();
return 1;
}
auto subcommand = std::string_view(argv[1]);
// Parse leading --root before subcommand
int arg_start = 1;
while (arg_start < argc && std::string_view(argv[arg_start]) == "--root" && arg_start + 1 < argc) {
paths::set_root(argv[arg_start + 1]);
arg_start += 2;
}
auto subcommand = std::string_view(argv[arg_start]);
if (subcommand == "-h" || subcommand == "--help") {
print_usage();
@@ -185,7 +198,7 @@ int main(int argc, char* argv[]) {
}
const char* file_arg = nullptr;
for (int i = 2; i < argc; ++i) {
for (int i = arg_start + 1; i < argc; ++i) {
if (std::string_view(argv[i]) == "-h"
|| std::string_view(argv[i]) == "--help") {
print_usage();
@@ -249,6 +262,59 @@ int main(int argc, char* argv[]) {
return 0;
}
if (subcommand == "fetch-package") {
if (file_arg == nullptr) {
std::cerr << "error: no package name specified\n";
return 1;
}
try {
std::vector<std::string> remotes;
auto config_path = std::filesystem::path(paths::system_dir()) / "config.kap";
if (std::filesystem::exists(config_path)) {
auto cfg_src = read_file(config_path.c_str());
try {
auto cfg = dsl::parse_system_config(cfg_src);
remotes = cfg.remotes;
} catch (const std::exception& e) {
std::cerr << "warning: config parse error: " << e.what() << "\n";
} catch (...) {
std::cerr << "warning: unknown config parse error\n";
}
}
auto result = fetch::fetch_recipe(file_arg, remotes);
if (result.ok) {
if (result.updated) {
std::cout << "fetched " << file_arg << " " << result.version
<< " → " << result.path << "\n";
} else {
std::cout << file_arg << " " << result.version
<< " (cached, up to date)\n";
}
return 0;
}
std::cerr << "fetch failed: " << result.error << "\n";
return 1;
} catch (const std::exception& e) {
std::cerr << "fetch error: " << e.what() << "\n";
return 1;
}
}
// Subcommands that MUST have writable directories to function.
bool needs_write = (subcommand == "build"
|| subcommand == "fetch" || subcommand == "fetch-package");
// rebuild benefits from cache dirs for registry lookups but
// degrades gracefully — it only reports, never writes.
bool needs_dirs = needs_write || subcommand == "rebuild" || subcommand == "resolve";
if (needs_dirs && !paths::directories_exist()) {
if (!paths::ensure_directories()) {
if (needs_write) {
std::cerr << "error: cannot create kappa directories (check permissions)\n";
return 1;
}
}
}
if (file_arg == nullptr
&& subcommand != "list"
&& subcommand != "rollback") {
@@ -256,7 +322,8 @@ int main(int argc, char* argv[]) {
return 1;
}
auto source = file_arg ? read_file(file_arg) : "";
try {
auto source = file_arg ? read_file(file_arg) : "";
if (subcommand == "parse-package") {
try {
@@ -345,7 +412,7 @@ int main(int argc, char* argv[]) {
auto pkg = dsl::parse(source);
int jobs = 1;
for (int i = 2; i < argc; ++i) {
for (int i = arg_start + 1; i < argc; ++i) {
auto arg = std::string_view(argv[i]);
if ((arg == "-j" || arg == "--jobs") && i + 1 < argc) {
jobs = std::stoi(argv[++i]);
@@ -493,41 +560,6 @@ int main(int argc, char* argv[]) {
}
}
if (subcommand == "fetch-package") {
if (file_arg == nullptr) {
std::cerr << "error: no package name specified\n";
return 1;
}
try {
std::vector<std::string> remotes;
auto config_path = std::filesystem::path(paths::system_dir()) / "config.kap";
if (std::filesystem::exists(config_path)) {
auto cfg_src = read_file(config_path.c_str());
try {
auto cfg = dsl::parse_system_config(cfg_src);
remotes = cfg.remotes;
} catch (...) {
}
}
auto result = fetch::fetch_recipe(file_arg, remotes);
if (result.ok) {
if (result.updated) {
std::cout << "fetched " << file_arg << " " << result.version
<< " → " << result.path << "\n";
} else {
std::cout << file_arg << " " << result.version
<< " (cached, up to date)\n";
}
return 0;
}
std::cerr << "fetch failed: " << result.error << "\n";
return 1;
} catch (const std::exception& e) {
std::cerr << "fetch error: " << e.what() << "\n";
return 1;
}
}
if (subcommand == "rebuild") {
try {
auto cfg = dsl::parse_system_config(source);
@@ -599,5 +631,10 @@ int main(int argc, char* argv[]) {
}
}
} catch (const std::exception& e) {
std::cerr << "error: " << e.what() << "\n";
return 1;
}
return 1; // unreachable
}