libutil: make RunningProgram more useful
make it moveable, make it killable, and add a stdout fd accessor. Change-Id: I2387cbe8ac67b899a322cd6c7d306ef9ea7abcd0
This commit is contained in:
+2
-2
@@ -250,7 +250,7 @@ void runNix(Path program, const Strings & args)
|
||||
.program = settings.nixBinDir+ "/" + program,
|
||||
.args = args,
|
||||
.environment = subprocessEnv,
|
||||
}).wait();
|
||||
}).waitAndCheck();
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -672,7 +672,7 @@ ProcessLineResult NixRepl::processLine(std::string line)
|
||||
|
||||
// runProgram redirects stdout to a StringSink,
|
||||
// using runProgram2 to allow editors to display their UI
|
||||
runProgram2(RunOptions { .program = editor, .searchPath = true, .args = args }).wait();
|
||||
runProgram2(RunOptions { .program = editor, .searchPath = true, .args = args }).waitAndCheck();
|
||||
|
||||
// Reload right after exiting the editor if path is not in store
|
||||
// Store is immutable, so there could be no changes, so there's no need to reload
|
||||
|
||||
@@ -759,7 +759,7 @@ struct GitInputScheme : InputScheme
|
||||
.args = { "-C", repoDir, "--git-dir", gitDir, "archive", input.getRev()->gitRev() },
|
||||
.captureStdout = true,
|
||||
});
|
||||
Finally const _wait([&] { proc.wait(); });
|
||||
Finally const _wait([&] { proc.waitAndCheck(); });
|
||||
|
||||
unpackTarfile(*proc.getStdout(), tmpDir);
|
||||
}
|
||||
|
||||
@@ -990,7 +990,7 @@ void runPostBuildHook(
|
||||
.captureStdout = true,
|
||||
.mergeStderrToStdout = true,
|
||||
});
|
||||
Finally const _wait([&] { proc.wait(); });
|
||||
Finally const _wait([&] { proc.waitAndCheck(); });
|
||||
|
||||
// FIXME just process the data, without a wrapper sink class
|
||||
proc.getStdout()->drainInto(sink);
|
||||
|
||||
@@ -249,7 +249,7 @@ std::pair<int, std::string> runProgram(RunOptions && options)
|
||||
|
||||
try {
|
||||
auto proc = runProgram2(options);
|
||||
Finally const _wait([&] { proc.wait(); });
|
||||
Finally const _wait([&] { proc.waitAndCheck(); });
|
||||
stdout = proc.getStdout()->drain();
|
||||
} catch (ExecError & e) {
|
||||
status = e.status;
|
||||
@@ -277,7 +277,22 @@ RunningProgram::~RunningProgram()
|
||||
}
|
||||
}
|
||||
|
||||
void RunningProgram::wait()
|
||||
std::tuple<pid_t, std::unique_ptr<Source>, int> RunningProgram::release()
|
||||
{
|
||||
return {pid.release(), std::move(stdoutSource), stdout_.release()};
|
||||
}
|
||||
|
||||
int RunningProgram::kill()
|
||||
{
|
||||
return pid.kill();
|
||||
}
|
||||
|
||||
int RunningProgram::wait()
|
||||
{
|
||||
return pid.wait();
|
||||
}
|
||||
|
||||
void RunningProgram::waitAndCheck()
|
||||
{
|
||||
if (std::uncaught_exceptions() == 0) {
|
||||
int status = pid.wait();
|
||||
|
||||
@@ -102,9 +102,23 @@ private:
|
||||
|
||||
public:
|
||||
RunningProgram() = default;
|
||||
RunningProgram(RunningProgram &&) = default;
|
||||
RunningProgram & operator=(RunningProgram &&) = default;
|
||||
~RunningProgram();
|
||||
|
||||
void wait();
|
||||
explicit operator bool() const { return bool(pid); }
|
||||
|
||||
std::tuple<pid_t, std::unique_ptr<Source>, int> release();
|
||||
|
||||
int kill();
|
||||
[[nodiscard]]
|
||||
int wait();
|
||||
void waitAndCheck();
|
||||
|
||||
std::optional<int> getStdoutFD() const
|
||||
{
|
||||
return stdout_ ? std::optional(stdout_.get()) : std::nullopt;
|
||||
}
|
||||
|
||||
Source * getStdout() const { return stdoutSource.get(); };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user