Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
da2e93c405
commit
28b94883fd
156 files changed
+278
-220
No files matched your search
@@ -0,0 +1,253 @@
|
||||
#include <Void/System/Process.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
|
||||
#include <format>
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
namespace Void::Process
|
||||
{
|
||||
Void::Result::Result<Output, Void::Result::Error>
|
||||
run(const std::filesystem::path&,
|
||||
std::vector<std::string>,
|
||||
std::string_view,
|
||||
std::chrono::milliseconds)
|
||||
{
|
||||
return Void::Result::fail(
|
||||
"Process::run: not implemented on Windows");
|
||||
}
|
||||
|
||||
Void::Result::Result<Output, Void::Result::Error>
|
||||
shell(std::string_view, std::chrono::milliseconds)
|
||||
{
|
||||
return Void::Result::fail(
|
||||
"Process::shell: not implemented on Windows");
|
||||
}
|
||||
} // namespace Void::Process
|
||||
|
||||
#else // POSIX
|
||||
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <csignal>
|
||||
#include <cstring>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace Void::Process
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
// Read into buf until EAGAIN (no more data right now) or EOF.
|
||||
// Returns true on EOF, false on EAGAIN.
|
||||
bool drain(int fd, std::string& buf)
|
||||
{
|
||||
char tmp[4096];
|
||||
for (;;) {
|
||||
ssize_t n = ::read(fd, tmp, sizeof(tmp));
|
||||
if (n > 0) {
|
||||
buf.append(tmp, static_cast<std::size_t>(n));
|
||||
} else if (n == 0) {
|
||||
return true; // EOF
|
||||
} else {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
// EAGAIN / EWOULDBLOCK: no data right now
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void set_nonblock(int fd)
|
||||
{
|
||||
int flags = ::fcntl(fd, F_GETFL, 0);
|
||||
if (flags >= 0) {
|
||||
::fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
}
|
||||
}
|
||||
|
||||
// Close an fd if it is >= 0, then set it to -1.
|
||||
void safe_close(int& fd)
|
||||
{
|
||||
if (fd >= 0) {
|
||||
::close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<Output, Void::Result::Error>
|
||||
run(const std::filesystem::path& executable,
|
||||
std::vector<std::string> args,
|
||||
std::string_view stdin_data,
|
||||
std::chrono::milliseconds timeout)
|
||||
{
|
||||
// [0] = read end, [1] = write end
|
||||
int stdin_pipe[2] = {-1, -1};
|
||||
int stdout_pipe[2] = {-1, -1};
|
||||
int stderr_pipe[2] = {-1, -1};
|
||||
|
||||
if (::pipe(stdin_pipe) != 0 || ::pipe(stdout_pipe) != 0 ||
|
||||
::pipe(stderr_pipe) != 0) {
|
||||
safe_close(stdin_pipe[0]);
|
||||
safe_close(stdin_pipe[1]);
|
||||
safe_close(stdout_pipe[0]);
|
||||
safe_close(stdout_pipe[1]);
|
||||
safe_close(stderr_pipe[0]);
|
||||
safe_close(stderr_pipe[1]);
|
||||
return Void::Result::fail(std::format("Process::run: pipe() failed: {}",
|
||||
std::strerror(errno)));
|
||||
}
|
||||
|
||||
Void::Log::log(Void::Log::Level::Debug, "Process",
|
||||
std::format("run: '{}'", executable.string()));
|
||||
|
||||
std::vector<const char*> argv;
|
||||
argv.push_back(executable.c_str());
|
||||
for (const auto& a : args) {
|
||||
argv.push_back(a.c_str());
|
||||
}
|
||||
argv.push_back(nullptr);
|
||||
|
||||
pid_t pid = ::fork();
|
||||
if (pid < 0) {
|
||||
safe_close(stdin_pipe[0]);
|
||||
safe_close(stdin_pipe[1]);
|
||||
safe_close(stdout_pipe[0]);
|
||||
safe_close(stdout_pipe[1]);
|
||||
safe_close(stderr_pipe[0]);
|
||||
safe_close(stderr_pipe[1]);
|
||||
return Void::Result::fail(std::format("Process::run: fork() failed: {}",
|
||||
std::strerror(errno)));
|
||||
}
|
||||
|
||||
if (pid == 0) {
|
||||
// Child process
|
||||
::dup2(stdin_pipe[0], STDIN_FILENO);
|
||||
::dup2(stdout_pipe[1], STDOUT_FILENO);
|
||||
::dup2(stderr_pipe[1], STDERR_FILENO);
|
||||
::close(stdin_pipe[0]);
|
||||
::close(stdin_pipe[1]);
|
||||
::close(stdout_pipe[0]);
|
||||
::close(stdout_pipe[1]);
|
||||
::close(stderr_pipe[0]);
|
||||
::close(stderr_pipe[1]);
|
||||
::execvp(executable.c_str(),
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
|
||||
const_cast<char* const*>(argv.data()));
|
||||
// execvp only returns on error; 127 is the POSIX convention
|
||||
::_exit(127);
|
||||
}
|
||||
|
||||
// Parent: close child-side ends
|
||||
safe_close(stdin_pipe[0]);
|
||||
safe_close(stdout_pipe[1]);
|
||||
safe_close(stderr_pipe[1]);
|
||||
|
||||
// Write stdin and close immediately so the child sees EOF
|
||||
if (!stdin_data.empty()) {
|
||||
const char* p = stdin_data.data();
|
||||
std::size_t remaining = stdin_data.size();
|
||||
while (remaining > 0) {
|
||||
ssize_t n = ::write(stdin_pipe[1], p, remaining);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
break; // child likely closed stdin; ignore
|
||||
}
|
||||
p += static_cast<std::size_t>(n);
|
||||
remaining -= static_cast<std::size_t>(n);
|
||||
}
|
||||
}
|
||||
safe_close(stdin_pipe[1]);
|
||||
|
||||
set_nonblock(stdout_pipe[0]);
|
||||
set_nonblock(stderr_pipe[0]);
|
||||
|
||||
Output output;
|
||||
bool out_done = false;
|
||||
bool err_done = false;
|
||||
|
||||
const bool has_timeout = timeout.count() > 0;
|
||||
const auto deadline =
|
||||
std::chrono::steady_clock::now() +
|
||||
(has_timeout ? timeout : std::chrono::milliseconds{30'000});
|
||||
|
||||
while (!out_done || !err_done) {
|
||||
struct pollfd pfds[2];
|
||||
int nfds = 0;
|
||||
if (!out_done) {
|
||||
pfds[nfds++] = {stdout_pipe[0], POLLIN, 0};
|
||||
}
|
||||
if (!err_done) {
|
||||
pfds[nfds++] = {stderr_pipe[0], POLLIN, 0};
|
||||
}
|
||||
|
||||
auto left_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
deadline - std::chrono::steady_clock::now())
|
||||
.count();
|
||||
if (left_ms <= 0) {
|
||||
::kill(pid, SIGTERM);
|
||||
break;
|
||||
}
|
||||
|
||||
int ret = ::poll(pfds, static_cast<nfds_t>(nfds),
|
||||
static_cast<int>(left_ms));
|
||||
if (ret < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (ret == 0) {
|
||||
// Wall-clock timeout expired
|
||||
::kill(pid, SIGTERM);
|
||||
break;
|
||||
}
|
||||
|
||||
for (int i = 0; i < nfds; ++i) {
|
||||
if ((pfds[i].revents & (POLLIN | POLLHUP | POLLERR)) == 0) {
|
||||
continue;
|
||||
}
|
||||
bool& done =
|
||||
(pfds[i].fd == stdout_pipe[0]) ? out_done : err_done;
|
||||
std::string& buf =
|
||||
(pfds[i].fd == stdout_pipe[0]) ? output.out : output.err;
|
||||
bool eof = drain(pfds[i].fd, buf);
|
||||
// POLLHUP means the write end was closed; no more data will arrive
|
||||
if (eof || (pfds[i].revents & (POLLHUP | POLLERR))) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
safe_close(stdout_pipe[0]);
|
||||
safe_close(stderr_pipe[0]);
|
||||
|
||||
int status = 0;
|
||||
::waitpid(pid, &status, 0);
|
||||
output.exit_code = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
|
||||
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Debug, "Process",
|
||||
std::format("run: '{}' exited {}", executable.string(),
|
||||
output.exit_code));
|
||||
|
||||
return Void::Result::ok(std::move(output));
|
||||
}
|
||||
|
||||
Void::Result::Result<Output, Void::Result::Error>
|
||||
shell(std::string_view command, std::chrono::milliseconds timeout)
|
||||
{
|
||||
return run("/bin/sh", {"-c", std::string{command}}, {}, timeout);
|
||||
}
|
||||
|
||||
} // namespace Void::Process
|
||||
|
||||
#endif // _WIN32
|
||||
Reference in new issue
Block a user