#include "kappa/system/activate.hpp" #include #include #include namespace kappa::system { ActivateResult write_hostname(const std::string& hostname, std::string_view prefix) { if (hostname.empty()) { return {false, "hostname is empty"}; } std::filesystem::path path = std::filesystem::path(prefix) / "etc/hostname"; std::error_code ec; std::filesystem::create_directories(path.parent_path(), ec); if (ec) { return {false, std::format("cannot create {}: {}", path.parent_path().string(), ec.message())}; } std::ofstream out(path); if (!out) { return {false, std::format("cannot write {}", path.string())}; } out << hostname << "\n"; return {true, {}}; } ActivateResult write_timezone(const std::string& timezone, std::string_view prefix) { if (timezone.empty()) { return {false, "timezone is empty"}; } // /etc/localtime is a symlink to /usr/share/zoneinfo/{timezone} std::filesystem::path localtime = std::filesystem::path(prefix) / "etc/localtime"; std::filesystem::path zoneinfo = std::filesystem::path(prefix) / "usr/share/zoneinfo" / timezone; std::error_code ec; if (!std::filesystem::exists(zoneinfo, ec)) { return {false, std::format("timezone data not found: {}", zoneinfo.string())}; } std::filesystem::create_directories(localtime.parent_path(), ec); // Remove existing symlink/file if present std::filesystem::remove(localtime, ec); std::filesystem::create_symlink(zoneinfo, localtime, ec); if (ec) { return {false, std::format("cannot create symlink {}: {}", localtime.string(), ec.message())}; } return {true, {}}; } } // namespace kappa::system