• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IJHack / QtPass / 43760214630

15 Nov 2026 08:49PM UTC coverage: 96.341% (-0.04%) from 96.384%
43760214630

push

github

web-flow
WSL git: keep white space that ends a path (#2204)

* WSL git: keep white space that ends a path

Executor::translatePathForWsl() cleaned wslpath's answer with
QString::trimmed(), which also stripped a trailing space, tab or
no-break space belonging to the folder name. With git run through WSL,
deleting or renaming "team " acted on its neighbour "team" (committed
under the name the user picked), and a folder named " " became the
store itself. Only the line end wslpath prints is dropped now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Anne Jan Brouwer <brouwer@annejan.com>

* CHANGELOG: no space-ending code span, 'whitespace' (lint)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Anne Jan Brouwer <brouwer@annejan.com>

---------

Signed-off-by: Anne Jan Brouwer <brouwer@annejan.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

3 of 4 new or added lines in 1 file covered. (75.0%)

3 existing lines in 1 file now uncovered.

8769 of 9102 relevant lines covered (96.34%)

710.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.2
/src/executor.cpp
1
// SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
#include "executor.h"
4
#include <QCoreApplication>
5
#include <QDeadlineTimer>
6
#include <QDir>
7
#include <QFileInfo>
8
#include <QStringDecoder>
9
#include <algorithm>
10
#include <climits>
11
#include <utility>
12

13
#include "qtpasslogging.h"
14

15
#ifdef Q_OS_UNIX
16
#include <sys/stat.h>
17
#endif
18

19
namespace {
20
/// Executor::setChildUmask(); negative for none.
21
std::atomic_int g_childUmask{-1};
22
/// How often a cancellable blocking run re-checks its cancel flag.
23
constexpr int kBlockingCancelPollMs = 100;
24
/// Grace between terminate() and kill() once a blocking run is cancelled;
25
/// gpg and git exit on SIGTERM well within this.
26
constexpr int kBlockingKillGraceMs = 1000;
27
} // namespace
28

29
Executor::Executor(QObject *parent) : QObject(parent) {
395 ✔
30
  connect(&m_process, &QProcess::finished, this, &Executor::onProcessFinished);
395 ✔
31
  connect(&m_process, &QProcess::started, this, &Executor::starting);
395 ✔
32
}
395 ✔
33

34
void Executor::detachFromTerminal(QProcess &process) {
1,726 ✔
35
#if defined(Q_OS_UNIX) && QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
36
  // A child inherits QtPass's controlling terminal when QtPass was started
37
  // from one. git (HTTPS credentials) and ssh (key passphrase, host key) then
38
  // prompt on that /dev/tty, and as the queue runs one command at a time,
39
  // every decrypt and save waits behind a prompt nobody sees: QtPass looks
40
  // frozen. In a session of its own the child has no terminal, so git and
41
  // ssh use SSH_ASKPASS/GIT_ASKPASS when set and otherwise fail with an
42
  // error QtPass shows. GIT_TERMINAL_PROMPT=0 is not set on top: without a
43
  // tty git has nothing to prompt on, and some credential helpers read it as
44
  // "no interaction at all" and would skip their own dialogs.
45
  //
46
  // gpg is unaffected: pinentry is started by gpg-agent, which finds the
47
  // terminal through GPG_TTY (set by the user's shell) or the tty on gpg's
48
  // stdin, here always a pipe. pass is a shell script running that gpg and
49
  // git, so the same holds for it.
50
  //
51
  // Signal dispositions are left alone (no ResetSignalHandlers): exec()
52
  // already resets caught signals, and QtPass sets no SIG_IGN of its own, so
53
  // that flag would only undo what QtPass's own parent chose (nohup, say).
54
  // Terminal signals (Ctrl+C, the SIGHUP of a closed terminal) now reach
55
  // QtPass but not the child; the children are short-lived and, with no
56
  // terminal, cannot sit waiting for input.
57
  QProcess::UnixProcessParameters params = process.unixProcessParameters();
1,726 ✔
58
  params.flags |= QProcess::UnixProcessFlag::CreateNewSession;
59
  process.setUnixProcessParameters(params);
1,726 ✔
60
#else
61
  Q_UNUSED(process);
62
#endif
63
}
1,726 ✔
64

65
void Executor::setChildUmask(int mask) { g_childUmask.store(mask); }
5 ✔
66

67
auto Executor::childUmask() -> int { return g_childUmask.load(); }
31 ✔
68

69
void Executor::startProcess(QProcess &process, const QString &app,
1,721 ✔
70
                            const QStringList &args) {
71
  detachFromTerminal(process);
1,721 ✔
72
#ifdef Q_OS_UNIX
73
  // In the child, between fork and exec: umask() is async-signal-safe.
74
  if (const int mask = g_childUmask.load(); mask >= 0) {
1,721 ✔
75
    process.setChildProcessModifier(
1 ✔
76
        [mask] { ::umask(static_cast<mode_t>(mask)); });
1 ✔
77
  }
78
#endif
79
  if (const auto wsl = parseWslCommand(app)) {
1,721 ✔
80
    process.start(wsl->launcher, wsl->argv(args));
18 ✔
81
  } else {
82
    process.start(resolveExecutable(app), args);
3,424 ✔
83
  }
84
}
1,721 ✔
85

86
auto Executor::resolveExecutable(const QString &app) -> QString {
1,718 ✔
87
  if (app.isEmpty() || QDir::isAbsolutePath(app)) {
3,435 ✔
88
    return app;
89
  }
90
  const QDir appDir(QCoreApplication::applicationDirPath());
65 ✔
91
  QStringList candidates{QDir::cleanPath(appDir.absoluteFilePath(app))};
260 ✔
92
#ifdef Q_OS_WIN
93
  candidates << QDir::cleanPath(
94
      appDir.absoluteFilePath(app + QStringLiteral(".exe")));
95
#endif
96
  for (const QString &candidate : std::as_const(candidates)) {
127 ✔
97
    // A stray non-executable file of that name must not mask PATH.
98
    const QFileInfo info(candidate);
65 ✔
99
    if (info.isFile() && info.isExecutable()) {
65 ✔
100
      return candidate;
101
    }
102
  }
65 ✔
103
  return app;
104
}
130 ✔
105

106
auto Executor::WslCommand::argv(const QStringList &args) const -> QStringList {
35 ✔
107
  return options + QStringList{QStringLiteral("--exec"), command} + args;
140 ✔
108
}
109

110
auto Executor::WslCommand::with(const QString &program) const -> WslCommand {
12 ✔
111
  return {launcher, options, program};
12 ✔
112
}
113

114
auto Executor::parseWslCommand(const QString &app)
3,372 ✔
115
    -> std::optional<WslCommand> {
116
  const QStringList parts = QProcess::splitCommand(app);
3,372 ✔
117
  if (parts.size() < 2) {
3,372 ✔
118
    return std::nullopt;
3,327 ✔
119
  }
120
  // The file name after the last separator of either kind: a Windows path
121
  // has to be recognised as such wherever the parser runs.
122
  const QString &launcher = parts.first();
123
  const qsizetype separator = std::max(launcher.lastIndexOf(QLatin1Char('/')),
45 ✔
124
                                       launcher.lastIndexOf(QLatin1Char('\\')));
45 ✔
125
  const QString launcherName = launcher.mid(separator + 1);
45 ✔
126
  const bool isWsl =
127
      launcherName.compare(QLatin1String("wsl"), Qt::CaseInsensitive) == 0 ||
58 ✔
128
      launcherName.compare(QLatin1String("wsl.exe"), Qt::CaseInsensitive) == 0;
13 ✔
129
  if (!isWsl) {
130
    return std::nullopt;
3 ✔
131
  }
132
  WslCommand wsl;
42 ✔
133
#ifdef Q_OS_WIN
134
  // A bare wsl or wsl.exe in any case is looked up on PATH as `wsl`; a path
135
  // stays as is.
136
  wsl.launcher = separator < 0 ? QStringLiteral("wsl") : launcher;
137
#else
138
  // QtPass running inside WSL reaches wsl.exe through interop under exactly
139
  // that name; nothing says `wsl` resolves too, so start what was written.
140
  wsl.launcher = launcher;
42 ✔
141
#endif
142
  // wsl.exe options that take a value; anything else starting with `-` is a
143
  // flag. A user-written -e/--exec is dropped, argv() always adds one.
144
  static const QStringList valued{
145
      QStringLiteral("-d"),   QStringLiteral("--distribution"),
2 ✔
146
      QStringLiteral("-u"),   QStringLiteral("--user"),
2 ✔
147
      QStringLiteral("--cd"), QStringLiteral("--shell-type")};
56 ✔
148
  qsizetype i = 1;
149
  for (; i < parts.size() && parts.at(i).startsWith(QLatin1Char('-')); ++i) {
59 ✔
150
    const QString &option = parts.at(i);
151
    if (option == QLatin1String("-e") || option == QLatin1String("--exec")) {
18 ✔
152
      continue;
7 ✔
153
    }
154
    if (valued.contains(option)) {
11 ✔
155
      if (i + 1 >= parts.size()) {
11 ✔
156
        return std::nullopt;
1 ✔
157
      }
158
      wsl.options << option << parts.at(++i);
159
    } else {
160
      wsl.options << option;
161
    }
162
  }
163
  // Exactly one program. More is a shell command line (`sh -c "..."`),
164
  // which is what --exec exists to keep out.
165
  if (i != parts.size() - 1) {
41 ✔
166
    return std::nullopt;
6 ✔
167
  }
168
  wsl.command = parts.at(i);
35 ✔
169
  return wsl;
170
}
42 ✔
171

172
auto Executor::wslExecArgs(const QString &command, const QStringList &args)
2 ✔
173
    -> QStringList {
174
  return WslCommand{QStringLiteral("wsl"), {}, command}.argv(args);
2 ✔
175
}
176

177
auto Executor::translatePathForWsl(const QString &path, const QString &exe)
1,239 ✔
178
    -> QString {
179
  QString normalizedPath = QDir::cleanPath(path);
1,239 ✔
180
  const auto wsl = parseWslCommand(exe);
1,239 ✔
181
  if (!wsl) {
1,239 ✔
182
    return normalizedPath;
1,235 ✔
183
  }
184
  QString wslPath;
4 ✔
185
  const auto wslpath = wsl->with(QStringLiteral("wslpath"));
8 ✔
186
  const int rc = executeBlocking(wslpath.launcher,
4 ✔
187
                                 wslpath.argv({normalizedPath}), &wslPath);
12 ✔
188
  // Only the line end wslpath prints: white space at the end of the path is
189
  // the name's own ("team " beside "team", a folder named " "), and trimmed
190
  // it made git remove or move the neighbour, or the whole store.
191
  QString translated = wslPath;
192
  if (translated.endsWith(QLatin1Char('\n'))) {
4 ✔
193
    translated.chop(1);
4 ✔
194
  }
195
  if (translated.endsWith(QLatin1Char('\r'))) {
4 ✔
NEW
196
    translated.chop(1);
×
197
  }
198
  return (rc == 0 && !translated.isEmpty()) ? translated : normalizedPath;
4 ✔
199
}
4 ✔
200

201
void Executor::executeNext() {
591 ✔
202
  if (running || m_execQueue.isEmpty()) {
591 ✔
203
    return;
309 ✔
204
  }
205
  // A copy, and marked running, before anyone hears of it: a slot on
206
  // dequeuing() that queues more cannot invalidate it or start it a second
207
  // time, and one that calls cancelNext() cannot drop it.
208
  const ExecQueueItem i = m_execQueue.head();
292 ✔
209
  running = true;
292 ✔
210
  emit dequeuing(i.id, true);
292 ✔
211

212
  // An empty executable never emits finished(); dropping it wedged the queue
213
  // (#1682). Fail it like a failed-to-start process so the queue drains.
214
  if (i.app.isEmpty()) {
292 ✔
215
    qCDebug(lcQtPass) << "No executable set for:" << i.id;
5 ✔
216
    const int failedId = i.id;
5 ✔
217
    running = false;
5 ✔
218
    m_execQueue.dequeue();
5 ✔
219
    ++m_pendingErrors;
5 ✔
220
    QMetaObject::invokeMethod(
5 ✔
221
        this,
222
        [this, failedId]() {
5 ✔
223
          emit error(failedId, -1, QString(),
10 ✔
224
                     tr("No executable configured for this command"));
5 ✔
225
          --m_pendingErrors;
5 ✔
226
          postDrainedIfIdle();
5 ✔
227
        },
5 ✔
228
        Qt::QueuedConnection);
229
    executeNext();
5 ✔
230
    return;
231
  }
232

233
  // Always set it: an item without a directory runs in the application's
234
  // cwd, not wherever the previous item happened to run.
235
  m_process.setWorkingDirectory(i.workingDir);
287 ✔
236
  startProcess(m_process, i.app, i.args);
287 ✔
237

238
  // A failed start never emits finished(), so `running` would stall the queue
239
  // and waiters (the keygen dialog) would hang. The emit is queued so it does
240
  // not re-enter KeygenDialog::done(), still on the stack; -1 goes through
241
  // Pass::finished's non-zero error gate.
242
  if (!m_process.waitForStarted(-1)) {
287 ✔
243
    qCDebug(lcQtPass) << "Process failed to start:" << i.id << " " << i.app;
5 ✔
244
    const int failedId = i.id;
5 ✔
245
    const QString failedApp = i.app;
246
    m_process.closeWriteChannel();
5 ✔
247
    running = false;
5 ✔
248
    m_execQueue.dequeue();
5 ✔
249
    ++m_pendingErrors;
5 ✔
250
    QMetaObject::invokeMethod(
5 ✔
251
        this,
252
        [this, failedId, failedApp]() {
10 ✔
253
          emit error(failedId, -1, QString(),
15 ✔
254
                     tr("Failed to start %1").arg(failedApp));
10 ✔
255
          --m_pendingErrors;
5 ✔
256
          postDrainedIfIdle();
5 ✔
257
        },
5 ✔
258
        Qt::QueuedConnection);
259
    executeNext();
5 ✔
260
    return;
261
  }
262

263
  if (!i.input.isEmpty()) {
282 ✔
264
    QByteArray data = i.input.toUtf8();
265
    if (m_process.write(data) != data.length()) {
78 ✔
266
      qCDebug(lcQtPass) << "Not all data written to process:" << i.id << " "
×
267
                        << i.app;
×
268
    }
269
  }
270
  m_process.closeWriteChannel();
282 ✔
271
}
292 ✔
272

273
void Executor::execute(int id, const QString &app, const QStringList &args,
83 ✔
274
                       bool readStdout, bool readStderr) {
275
  execute(id, QString(), app, args, QString(), readStdout, readStderr);
166 ✔
276
}
83 ✔
277

278
void Executor::execute(int id, const QString &workDir, const QString &app,
2 ✔
279
                       const QStringList &args, bool readStdout,
280
                       bool readStderr) {
281
  execute(id, workDir, app, args, QString(), readStdout, readStderr);
2 ✔
282
}
2 ✔
283

284
void Executor::execute(int id, const QString &app, const QStringList &args,
1 ✔
285
                       QString input, bool readStdout, bool readStderr) {
286
  execute(id, QString(), app, args, std::move(input), readStdout, readStderr);
2 ✔
287
}
1 ✔
288

289
void Executor::execute(int id, const QString &workDir, const QString &app,
300 ✔
290
                       const QStringList &args, QString input, bool readStdout,
291
                       bool readStderr) {
292
  // Queue even an empty executable: executeNext() reports it (#1682).
293
  // startProcess resolves the executable, as on the blocking path.
294
  m_execQueue.push_back(
300 ✔
295
      {id, app, args, std::move(input), readStdout, readStderr, workDir});
296
  executeNext();
300 ✔
297
}
900 ✔
298

299
// UTF-8 first; on a decoding error fall back to the system encoding.
300
// lossless, when given, says whether the text encodes back to exactly these
301
// bytes: what writing it to gpg or pass (toUtf8(), as every writer here
302
// does) would store. Not for bytes that are not UTF-8 (each became U+FFFD,
303
// or on Windows a code-page character), nor for a leading byte order mark,
304
// which the decoder drops without an error. A literal U+FFFD, as older
305
// QtPass versions left in such entries, does encode back.
306
static auto decodeAssumingUtf8(const QByteArray &in, bool *lossless = nullptr)
1,773 ✔
307
    -> QString {
308
  // Stateless: a truncated trailing byte becomes a replacement character
309
  // instead of being held back for a continuation that never comes.
310
  auto converter =
1,773 ✔
311
      QStringDecoder(QStringDecoder::Utf8, QStringDecoder::Flag::Stateless);
312
  QString out = converter(in);
1,773 ✔
313
  if (converter.hasError()) {
1,773 ✔
314
    auto fallback =
4 ✔
315
        QStringDecoder(QStringDecoder::System, QStringDecoder::Flag::Stateless);
316
    out = fallback(in);
8 ✔
317
  }
318
  if (lossless != nullptr) {
1,773 ✔
319
    *lossless = out.toUtf8() == in;
230 ✔
320
  }
321
  return out;
1,773 ✔
322
}
323

324
// Returns an error code rather than throwing, matching QtPass's error
325
// handling elsewhere.
326
namespace {
327

328
/**
329
 * @brief Stop a running @p process from the thread that owns it:
330
 * terminate(), then kill() if it is still running after a grace period,
331
 * then at most one more grace period for it to exit.
332
 * A console gpg ignores terminate() on Windows (WM_CLOSE), hence the kill.
333
 */
334
void stopBlocking(QProcess &process) {
14 ✔
335
  process.terminate();
14 ✔
336
  if (process.waitForFinished(kBlockingKillGraceMs)) {
14 ✔
337
    return;
338
  }
339
  process.kill();
3 ✔
340
  // SIGKILL cannot be ignored, but a child in uninterruptible sleep (a hung
341
  // NFS or FUSE mount) dies only when the kernel lets it. Wait one more grace
342
  // period, not forever: the run is reported as failed either way, and a
343
  // blocking run then lasts at most its timeout plus twice the grace.
344
  if (!process.waitForFinished(kBlockingKillGraceMs)) {
3 ✔
345
    qCWarning(lcQtPass) << "Process still running after kill():"
×
346
                        << process.program();
×
347
  }
348
}
349

350
/**
351
 * @brief What is left of @p deadline, as QProcess's wait functions take it:
352
 * -1 for none.
353
 */
354
auto msecsLeft(const QDeadlineTimer &deadline) -> int {
2,413 ✔
355
  if (deadline.isForever()) {
2,413 ✔
356
    return -1;
357
  }
358
  return static_cast<int>(
359
      std::clamp<qint64>(deadline.remainingTime(), 0, INT_MAX));
56 ✔
360
}
361

362
/**
363
 * @brief Wait for @p process to finish, stopping it when @p cancel is set
364
 * or @p deadline passes.
365
 *
366
 * Polls the flag; every QProcess call, terminate()/kill() included, stays on
367
 * this thread, so another thread can never act on an exited process (or a
368
 * pid the OS has since reused).
369
 * @return false when it was cancelled or ran out of time.
370
 */
371
auto waitOrCancel(QProcess &process, const std::atomic_bool *cancel,
1,378 ✔
372
                  const QDeadlineTimer &deadline) -> bool {
373
  if (cancel == nullptr) {
1,378 ✔
374
    // waitForFinished() can also fail (a poll error) with the child still
375
    // running; that is not completion either. Stop it rather than report
376
    // it done.
377
    if (!process.waitForFinished(msecsLeft(deadline)) &&
979 ✔
378
        process.state() != QProcess::NotRunning) {
3 ✔
379
      stopBlocking(process);
3 ✔
380
      return false;
3 ✔
381
    }
382
    return true;
973 ✔
383
  }
384
  while (!process.waitForFinished(
848 ✔
385
      deadline.isForever()
386
          ? kBlockingCancelPollMs
387
          : qMin(kBlockingCancelPollMs, msecsLeft(deadline)))) {
3 ✔
388
    if (process.state() == QProcess::NotRunning) {
33 ✔
389
      return true;
390
    }
391
    if (cancel->load() || deadline.hasExpired()) {
44 ✔
392
      stopBlocking(process);
11 ✔
393
      return false;
11 ✔
394
    }
395
  }
396
  return true;
397
}
398

399
} // namespace
400

401
auto Executor::runBlocking(QProcess &process, const QString &app,
1,239 ✔
402
                           const QStringList &args, const QString &input,
403
                           QString *process_out, QString *process_err,
404
                           const std::atomic_bool *cancel, int timeoutMs)
405
    -> int {
406
  QByteArray out;
1,239 ✔
407
  const int rc = runBlockingRaw(process, app, args, input.toUtf8(),
2,478 ✔
408
                                process_out != nullptr ? &out : nullptr,
409
                                process_err, cancel, timeoutMs);
410
  if (process_out != nullptr) {
1,239 ✔
411
    *process_out = decodeAssumingUtf8(out);
2,260 ✔
412
  }
413
  return rc;
1,239 ✔
414
}
415

416
auto Executor::runBlockingRaw(QProcess &process, const QString &app,
1,442 ✔
417
                              const QStringList &args, const QByteArray &input,
418
                              QByteArray *process_out, QString *process_err,
419
                              const std::atomic_bool *cancel, int timeoutMs)
420
    -> int {
421
  if (cancel != nullptr && cancel->load())
1,442 ✔
422
    return -1;
423
  const QDeadlineTimer deadline = timeoutMs < 0
424
                                      ? QDeadlineTimer(QDeadlineTimer::Forever)
1,434 ✔
425
                                      : QDeadlineTimer(timeoutMs);
1,434 ✔
426
  startProcess(process, app, args);
1,434 ✔
427
  if (!process.waitForStarted(msecsLeft(deadline))) {
1,434 ✔
428
    qCDebug(lcQtPass) << "Process failed to start:" << app;
56 ✔
429
    if (process.state() != QProcess::NotRunning) {
56 ✔
430
      stopBlocking(process);
×
431
    }
432
    return -1;
56 ✔
433
  }
434
  if (!input.isEmpty()) {
1,378 ✔
435
    if (process.write(input) != input.length()) {
200 ✔
436
      qCDebug(lcQtPass) << "Not all input written:" << app;
×
437
    }
438
  }
439
  // Always close stdin so a child blocking on EOF doesn't hang.
440
  process.closeWriteChannel();
1,378 ✔
441
  if (!waitOrCancel(process, cancel, deadline)) {
1,378 ✔
442
    return -1;
443
  }
444
  // Read before judging the exit: a process that crashed may have said why,
445
  // as the queued path (onProcessFinished) keeps it too.
446
  if (process_out != nullptr) {
1,364 ✔
447
    *process_out = process.readAllStandardOutput();
2,414 ✔
448
  }
449
  if (process_err != nullptr) {
1,364 ✔
450
    *process_err = decodeAssumingUtf8(process.readAllStandardError());
402 ✔
451
  }
452
  return process.exitStatus() == QProcess::NormalExit ? process.exitCode() : -1;
1,364 ✔
453
}
454

455
auto Executor::executeBlockingRaw(QProcess &process, const QString &app,
203 ✔
456
                                  const QStringList &args,
457
                                  const QByteArray &input,
458
                                  QByteArray *process_out, QString *process_err,
459
                                  const std::atomic_bool *cancel, int timeoutMs)
460
    -> int {
461
  return runBlockingRaw(process, app, args, input, process_out, process_err,
203 ✔
462
                        cancel, timeoutMs);
203 ✔
463
}
464

465
auto Executor::executeBlocking(const QString &app, const QStringList &args,
245 ✔
466
                               const QString &input, QString *process_out,
467
                               QString *process_err) -> int {
468
  QProcess internal;
245 ✔
469
  return runBlocking(internal, app, args, input, process_out, process_err);
490 ✔
470
}
245 ✔
471

472
auto Executor::executeBlocking(QProcess &process, const QString &app,
958 ✔
473
                               const QStringList &args, const QString &input,
474
                               QString *process_out, QString *process_err,
475
                               const std::atomic_bool *cancel, int timeoutMs)
476
    -> int {
477
  return runBlocking(process, app, args, input, process_out, process_err,
958 ✔
478
                     cancel, timeoutMs);
958 ✔
479
}
480

481
auto Executor::executeBlocking(const QString &app, const QStringList &args,
209 ✔
482
                               QString *process_out, QString *process_err)
483
    -> int {
484
  return executeBlocking(app, args, QString(), process_out, process_err);
418 ✔
485
}
486

487
auto Executor::executeBlocking(const QProcessEnvironment &env,
36 ✔
488
                               const QString &app, const QStringList &args,
489
                               QString *process_out, QString *process_err)
490
    -> int {
491
  QProcess process;
36 ✔
492
  process.setProcessEnvironment(env);
36 ✔
493
  return runBlocking(process, app, args, QString(), process_out, process_err);
72 ✔
494
}
36 ✔
495

496
void Executor::setEnvironment(const QProcessEnvironment &env) {
577 ✔
497
  m_process.setProcessEnvironment(env);
577 ✔
498
}
577 ✔
499

500
auto Executor::environment() const -> QProcessEnvironment {
95 ✔
501
  return m_process.processEnvironment();
95 ✔
502
}
503

504
// Also -1 while the head is already running: that one is not cancelled.
505
auto Executor::cancelNext() -> int {
12 ✔
506
  if (running || m_execQueue.isEmpty()) {
12 ✔
507
    return -1;
508
  }
509
  const int id = m_execQueue.dequeue().id;
8 ✔
510
  // As in executeNext(): while the signal is out, a slot's cancelNext()
511
  // drops nothing and its execute() only queues. Nothing is started here
512
  // afterwards: the caller (ImitatePass dropping the rest of a failed
513
  // transaction from its completion handler) may cancel again, and the
514
  // queue moves on when that handler returns (onProcessFinished()).
515
  running = true;
8 ✔
516
  emit dequeuing(id, false);
8 ✔
517
  running = false;
8 ✔
518
  return id;
8 ✔
519
}
520

521
void Executor::onProcessFinished(int exitCode,
281 ✔
522
                                 QProcess::ExitStatus exitStatus) {
523
  ExecQueueItem i = m_execQueue.dequeue();
524
  running = false;
281 ✔
525
  auto [output, err] = collectOutput(i, exitCode);
281 ✔
526
  if (exitStatus == QProcess::NormalExit) {
281 ✔
527
    if (exitCode != 0) {
278 ✔
528
      qCDebug(lcQtPass) << i.app << "exited with" << exitCode << err;
40 ✔
529
    }
530
    emit finished(i.id, exitCode, output, err);
278 ✔
531
    m_outputLossless = true;
278 ✔
532
  } else {
533
    // A signal-killed process usually leaves stderr empty; without this the
534
    // password pane would show nothing at all for the failure.
535
    if (err.trimmed().isEmpty()) {
3 ✔
536
      err = tr("%1 crashed or was killed").arg(i.app);
6 ✔
537
    }
538
    // Qt leaves exitCode undefined after a CrashExit (the signal number on
539
    // Unix), and Pass::finished gates on it: 0 would pass as success and 1
540
    // as grep's "no matches". Report -1, like the failed-to-start path.
541
    emit error(i.id, -1, output, err);
3 ✔
542
    m_outputLossless = true;
3 ✔
543
  }
544
  executeNext();
281 ✔
545
  postDrainedIfIdle();
281 ✔
546
}
281 ✔
547

548
void Executor::postDrainedIfIdle() {
291 ✔
549
  // One check at a time, run after what is emitted now; it fires only once
550
  // nothing runs or waits and every posted error() of a failed start is
551
  // out (the last of those posts the check again).
552
  if (m_drainCheckPosted) {
291 ✔
553
    return;
554
  }
555
  m_drainCheckPosted = true;
287 ✔
556
  QMetaObject::invokeMethod(
287 ✔
557
      this,
558
      [this]() {
287 ✔
559
        m_drainCheckPosted = false;
231 ✔
560
        if (isIdle() && m_pendingErrors == 0) {
120 ✔
561
          emit drained();
120 ✔
562
        }
563
      },
231 ✔
564
      Qt::QueuedConnection);
565
}
566

567
auto Executor::collectOutput(const ExecQueueItem &item, int exitCode)
281 ✔
568
    -> std::pair<QString, QString> {
569
  QString output;
281 ✔
570
  QString err;
281 ✔
571
  m_outputLossless = true;
281 ✔
572
  if (item.readStdout) {
281 ✔
573
    output = decodeAssumingUtf8(m_process.readAllStandardOutput(),
460 ✔
574
                                &m_outputLossless);
575
  }
576
  if (item.readStderr || exitCode != 0) {
281 ✔
577
    err = decodeAssumingUtf8(m_process.readAllStandardError());
424 ✔
578
  }
579
  return {output, err};
281 ✔
580
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc