fix: CWD restore, install error checks, fetch EINTR

- build.cpp: save/restore CWD on both success and failure paths
- install.cpp: check error_code after each filesystem op, abort on failure
- install.cpp: check write_installed() return value
- fetch.cpp: EINTR retry loop on both waitpid calls (exec_cmd + exec_capture)
This commit is contained in:
2026-07-30 05:31:59 -04:00
parent 6c993d2d17
commit c276465f3a
2 changed files with 12 additions and 4 deletions
+5 -2
View File
@@ -2,6 +2,7 @@
#include "kappa/paths.hpp"
#include <array>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
@@ -29,7 +30,8 @@ static int exec_cmd(const std::vector<std::string>& argv) {
}
if (pid < 0) { return -1; }
int status = 0;
waitpid(pid, &status, 0);
pid_t w;
do { w = waitpid(pid, &status, 0); } while (w == -1 && errno == EINTR);
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
}
@@ -61,7 +63,8 @@ static std::string exec_capture(const std::vector<std::string>& argv) {
result += buf.data();
}
close(pipefd[0]);
waitpid(pid, nullptr, 0);
pid_t w;
do { w = waitpid(pid, nullptr, 0); } while (w == -1 && errno == EINTR);
if (!result.empty() && result.back() == '\n') { result.pop_back(); }
return result;
+7 -2
View File
@@ -42,21 +42,26 @@ InstallResult install(const resolve::BuildStep& step,
auto src = work_dir / "destdir";
if (fs::exists(src)) {
for (auto& entry : fs::recursive_directory_iterator(src, ec)) {
if (ec) { break; }
auto rel = fs::relative(entry.path(), src);
auto target = dest / rel;
if (entry.is_directory()) {
fs::create_directories(target, ec);
} else {
fs::create_directories(target.parent_path(), ec);
fs::rename(entry.path(), target, ec);
if (!ec) { fs::rename(entry.path(), target, ec); }
}
if (ec) { break; }
}
}
auto entries = read_installed();
entries.push_back({step.name, step.resolved.original.version,
hash, step.resolved.original.provides});
write_installed(entries);
if (!write_installed(entries)) {
result.error = "failed to write installed DB";
return result;
}
std::vector<std::string> hashes;
for (auto& e : entries) { hashes.push_back(e.hash); }