10#define SRC_EXECUTOR_H_
15#include <QProcessEnvironment>
111 static void startProcess(QProcess &process,
const QString &app,
112 const QStringList &args);
122 -> std::pair<QString, QString>;
145 static auto runBlocking(QProcess &process,
const QString &app,
146 const QStringList &args,
const QString &input,
147 QString *process_out, QString *process_err,
148 const std::atomic_bool *cancel =
nullptr,
149 int timeoutMs = -1) -> int;
166 const QStringList &args,
const QByteArray &input,
167 QByteArray *process_out, QString *process_err,
168 const std::atomic_bool *cancel =
nullptr,
169 int timeoutMs = -1) -> int;
199 void append(
const QByteArray &chunk);
206 auto take() -> QByteArray;
266 explicit Executor(QObject *parent =
nullptr);
286 void execute(
int id,
const QString &app,
const QStringList &args,
287 bool readStdout,
bool readStderr =
true);
298 void execute(
int id,
const QString &workDir,
const QString &app,
299 const QStringList &args,
bool readStdout,
300 bool readStderr =
true);
311 void execute(
int id,
const QString &app,
const QStringList &args,
312 QString input = QString(),
bool readStdout =
false,
313 bool readStderr =
true);
325 void execute(
int id,
const QString &workDir,
const QString &app,
326 const QStringList &args, QString input = QString(),
327 bool readStdout =
false,
bool readStderr =
true);
339 const QStringList &args,
340 const QString &input = QString(),
341 QString *process_out =
nullptr,
342 QString *process_err =
nullptr)
378 QProcess &process,
const QString &app,
const QStringList &args,
379 const QString &input = QString(), QString *process_out =
nullptr,
380 QString *process_err =
nullptr,
const std::atomic_bool *cancel =
nullptr,
381 int timeoutMs = -1) -> int;
401 [[nodiscard]]
static auto
403 const QStringList &args,
const QByteArray &input,
404 QByteArray *process_out, QString *process_err =
nullptr,
405 const std::atomic_bool *cancel =
nullptr,
406 int timeoutMs = -1) -> int;
416 [[nodiscard]]
static auto
418 QString *process_out, QString *process_err =
nullptr) -> int;
429 [[nodiscard]]
static auto
431 const QStringList &args, QString *process_out =
nullptr,
432 QString *process_err =
nullptr) -> int;
477 [[nodiscard]]
static auto childUmask() -> int;
512 auto argv(
const QStringList &args)
const -> QStringList;
536 static auto parseWslCommand(
const QString &app) -> std::optional<WslCommand>;
545 static auto wslExecArgs(
const QString &command,
const QStringList &args)
556 [[nodiscard]]
static auto decodeText(
const QByteArray &bytes) -> QString;
695 void finished(
int id,
int exitCode,
const QString &output,
696 const QString &errout);
727 void error(
int id,
int exitCode,
const QString &output,
728 const QString &errout);
A stream of bytes held as its first and last kMaxStderrBytes / 2 bytes, with its gpg status records c...
void clear()
Drop everything, counts included, for the next command.
QByteArray m_head
The first bytes, up to half the cap.
static constexpr qsizetype kMaxStatusLineBytes
Longest line still looked at for a status keyword.
void countStatus(const QByteArray &line)
Count line if it is a status record.
bool m_lineTooLong
The line not ended yet ran past kMaxStatusLineBytes.
static constexpr int kMaxStatusKeywords
Most distinct status keywords counted.
void append(const QByteArray &chunk)
Add what the process wrote next.
qint64 m_dropped
Bytes that fell out between head and tail.
QHash< QString, qint64 > m_status
Status keyword to count, see statusCounts().
auto take() -> QByteArray
Everything kept, and start over.
auto statusCounts() const -> QHash< QString, qint64 >
How many complete lines of each gpg status keyword came by since the last take(), the left-out ones i...
QByteArray m_line
The line not ended yet; empty once it ran too long.
QByteArray m_tail
The latest bytes, up to half the cap.
auto lastOutputLossless() const -> bool
Whether the stdout of the item whose finished() or error() is being emitted encodes back (toUtf8(),...
void dequeuing(int id, bool completes)
Emitted, synchronously, when the queue's head leaves it: just before it is started,...
auto stopRunning() -> bool
Stop the command that is running, if any: terminate it (its whole process group on Unix),...
QQueue< ExecQueueItem > m_execQueue
Commands waiting to run, head first. The head stays queued while it runs and is dequeued in onProcess...
auto isIdle() const -> bool
Whether no command is running and nothing is queued.
static auto childUmask() -> int
The umask setChildUmask() set.
static constexpr qsizetype kMaxStderrBytes
Most bytes of a command's stderr kept: the first and the last half of this, with a note of how much w...
static auto executeBlocking(const QString &app, const QStringList &args, const QString &input=QString(), QString *process_out=nullptr, QString *process_err=nullptr) -> int
Run a command synchronously and return its exit code.
static auto runBlockingRaw(QProcess &process, const QString &app, const QStringList &args, const QByteArray &input, QByteArray *process_out, QString *process_err, const std::atomic_bool *cancel=nullptr, int timeoutMs=-1) -> int
runBlocking() on bytes: input is written and stdout returned as they are, with no text decoding or en...
static auto translatePathForWsl(const QString &path, const QString &exe) -> QString
Translate a native path for a WSL-routed executable.
auto environment() const -> QProcessEnvironment
Return the environment passed to child processes.
auto runningSerial() const -> quint64
Which run is going: a number no other run of this executor had.
bool m_discardStdout
The running queued command's stdout is not wanted: dropped as it comes.
void onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
Finish the running item when its process exits.
auto lastStatusCounts() const -> QHash< QString, qint64 >
The gpg status records on the stderr of the command whose finished() or error() is being emitted,...
auto collectOutput(const ExecQueueItem &item, int exitCode) -> std::pair< QString, QString >
Read what a finished process produced, according to the queue item's readStdout/readStderr flags; std...
CappedOutput m_stderr
stderr of the running queued command, read as it arrives and capped.
bool m_drainCheckPosted
A drained() check is posted and not run yet.
auto hasQueued(int id) const -> bool
Whether a command with process id id is queued or running.
auto runningId() const -> int
The id of the command that is running.
static void startProcess(QProcess &process, const QString &app, const QStringList &args)
Start process running app, handling the "wsl " prefix once for both the queued and the blocking path.
void execute(int id, const QString &app, const QStringList &args, bool readStdout, bool readStderr=true)
Queue a command without stdin input.
static auto resolveExecutable(const QString &app) -> QString
The one rule for turning a configured executable into what runs.
void drained()
Emitted once the last queued item is done and nothing runs or waits any more (isIdle()),...
void error(int id, int exitCode, const QString &output, const QString &errout)
Emitted instead of finished() when a queued process could not be started, had no executable configure...
static auto wslExecArgs(const QString &command, const QStringList &args) -> QStringList
Build the wsl.exe argument list that runs command directly.
static void setChildUmask(int mask)
The umask every child QtPass starts gets, as pass sets its own.
Executor(QObject *parent=nullptr)
Construct an Executor with an optional parent QObject.
QHash< QString, qint64 > m_lastStatus
lastStatusCounts(): set by collectOutput(), cleared after the emit.
static auto executeBlockingRaw(QProcess &process, const QString &app, const QStringList &args, const QByteArray &input, QByteArray *process_out, QString *process_err=nullptr, const std::atomic_bool *cancel=nullptr, int timeoutMs=-1) -> int
executeBlocking() on bytes, for an entry's plaintext: input is written to stdin and stdout returned e...
bool running
Whether the head of the queue has been started (or is being dequeued, see dequeuing()) and has not fi...
void finished(int id, int exitCode, const QString &output, const QString &errout)
Emitted when a queued process exits normally, whatever its exit code; a non-zero code is the receiver...
void starting()
Emitted just before a queued process is started; the main window shows the busy indicator on it.
static auto decodeText(const QByteArray &bytes) -> QString
Decode process output as executeBlocking() does: UTF-8 first, the system encoding when it is not UTF-...
void setEnvironment(const QProcessEnvironment &env)
Set the environment passed to all child processes.
static auto runBlocking(QProcess &process, const QString &app, const QStringList &args, const QString &input, QString *process_out, QString *process_err, const std::atomic_bool *cancel=nullptr, int timeoutMs=-1) -> int
Shared blocking run: start process, optionally feed input on stdin, wait, and capture stdout/stderr....
static void detachFromTerminal(QProcess &process)
Make process start its child without a controlling terminal.
void executeNext()
Start the head of the queue unless something runs or the queue is empty.
static auto parseWslCommand(const QString &app) -> std::optional< WslCommand >
Parse a configured executable as a WSL command.
auto cancelNext() -> int
Cancel the next queued command without executing it.
QProcess m_process
The one process object; every queued command reuses it.
Execution queue item for non-interactive ordered execution.
bool readStdout
Whether stdout is captured for finished().
QString workingDir
Working directory to start in; empty for the current one.
QString input
Data written to the process's stdin, then closed.
int id
Identifier the caller gave; echoed in finished()/error().
bool readStderr
Whether stderr is captured; it is read regardless when the process exits non-zero.
QStringList args
Arguments for the executable.
QString app
Executable as configured; resolved when started.
A configured executable that runs a Linux program through WSL.
auto argv(const QStringList &args) const -> QStringList
Arguments for launcher that run command with args.
QString command
The Linux program to run, looked up on the WSL PATH.
QString launcher
What to start.
QStringList options
wsl.exe options that go before the program (-d Debian, -u me), without any -e/--exec: argv() always a...
auto with(const QString &program) const -> WslCommand
The same launcher and options running another program.