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

IJHack / QtPass / 23862950363

01 Apr 2026 05:54PM UTC coverage: 20.086% (+0.004%) from 20.082%
23862950363

push

github

web-flow
Merge pull request #890 from IJHack/fix/ai-findings-10-more

fix: address 10 more AI findings across 4 files

9 of 45 new or added lines in 7 files covered. (20.0%)

4 existing lines in 4 files now uncovered.

1030 of 5128 relevant lines covered (20.09%)

7.89 hits per line

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

7.08
/src/imitatepass.cpp
1
// SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
#include "imitatepass.h"
4
#include "qtpasssettings.h"
5
#include "util.h"
6
#include <QDirIterator>
7
#include <QRegularExpression>
8
#include <utility>
9

10
#ifdef QT_DEBUG
11
#include "debughelper.h"
12
#endif
13

14
using Enums::CLIPBOARD_ALWAYS;
15
using Enums::CLIPBOARD_NEVER;
16
using Enums::CLIPBOARD_ON_DEMAND;
17
using Enums::GIT_ADD;
18
using Enums::GIT_COMMIT;
19
using Enums::GIT_COPY;
20
using Enums::GIT_INIT;
21
using Enums::GIT_MOVE;
22
using Enums::GIT_PULL;
23
using Enums::GIT_PUSH;
24
using Enums::GIT_RM;
25
using Enums::GPG_GENKEYS;
26
using Enums::INVALID;
27
using Enums::PASS_COPY;
28
using Enums::PASS_INIT;
29
using Enums::PASS_INSERT;
30
using Enums::PASS_MOVE;
31
using Enums::PASS_OTP_GENERATE;
32
using Enums::PASS_REMOVE;
33
using Enums::PASS_SHOW;
34
using Enums::PROCESS_COUNT;
35

36
/**
37
 * @brief ImitatePass::ImitatePass for situaions when pass is not available
38
 * we imitate the behavior of pass https://www.passwordstore.org/
39
 */
40
ImitatePass::ImitatePass() = default;
20✔
41

42
static auto pgit(const QString &path) -> QString {
×
43
  if (!QtPassSettings::getGitExecutable().startsWith("wsl ")) {
×
44
    return path;
45
  }
46
  QString res = "$(wslpath " + path + ")";
×
47
  return res.replace('\\', '/');
×
48
}
49

50
static auto pgpg(const QString &path) -> QString {
×
51
  if (!QtPassSettings::getGpgExecutable().startsWith("wsl ")) {
×
52
    return path;
53
  }
54
  QString res = "$(wslpath " + path + ")";
×
55
  return res.replace('\\', '/');
×
56
}
57

58
/**
59
 * @brief ImitatePass::GitInit git init wrapper
60
 */
61
void ImitatePass::GitInit() {
×
62
  executeGit(GIT_INIT, {"init", pgit(QtPassSettings::getPassStore())});
×
63
}
×
64

65
/**
66
 * @brief ImitatePass::GitPull git init wrapper
67
 */
68
void ImitatePass::GitPull() { executeGit(GIT_PULL, {"pull"}); }
×
69

70
/**
71
 * @brief ImitatePass::GitPull_b git pull wrapper
72
 */
73
void ImitatePass::GitPull_b() {
×
74
  Executor::executeBlocking(QtPassSettings::getGitExecutable(), {"pull"});
×
75
}
×
76

77
/**
78
 * @brief ImitatePass::GitPush git init wrapper
79
 */
80
void ImitatePass::GitPush() {
×
81
  if (QtPassSettings::isUseGit()) {
×
82
    executeGit(GIT_PUSH, {"push"});
×
83
  }
84
}
×
85

86
/**
87
 * @brief ImitatePass::Show shows content of file
88
 */
89
void ImitatePass::Show(QString file) {
×
90
  file = QtPassSettings::getPassStore() + file + ".gpg";
×
91
  QStringList args = {"-d",      "--quiet",     "--yes",   "--no-encrypt-to",
92
                      "--batch", "--use-agent", pgpg(file)};
×
93
  executeGpg(PASS_SHOW, args);
×
94
}
×
95

96
/**
97
 * @brief ImitatePass::OtpGenerate generates an otp code
98
 */
99
void ImitatePass::OtpGenerate(QString file) {
×
100
#ifdef QT_DEBUG
101
  dbg() << "No OTP generation code for fake pass yet, attempting for file: " +
102
               file;
103
#else
104
  Q_UNUSED(file)
105
#endif
106
}
×
107

108
/**
109
 * @brief ImitatePass::Insert create new file with encrypted content
110
 *
111
 * @param file      file to be created
112
 * @param newValue  value to be stored in file
113
 * @param overwrite whether to overwrite existing file
114
 */
115
void ImitatePass::Insert(QString file, QString newValue, bool overwrite) {
×
116
  file = file + ".gpg";
×
117
  QString gpgIdPath = Pass::getGpgIdPath(file);
×
118
  if (!verifyGpgIdFile(gpgIdPath)) {
×
119
    emit critical(tr("Check .gpgid file signature!"),
×
120
                  tr("Signature for %1 is invalid.").arg(gpgIdPath));
×
121
    return;
×
122
  }
123
  transactionHelper trans(this, PASS_INSERT);
×
124
  QStringList recipients = Pass::getRecipientList(file);
×
125
  if (recipients.isEmpty()) {
×
126
    // Already emit critical signal to notify user of error - no need to throw
127
    emit critical(tr("Can not edit"),
×
128
                  tr("Could not read encryption key to use, .gpg-id "
×
129
                     "file missing or invalid."));
130
    return;
131
  }
132
  QStringList args = {"--batch", "-eq", "--output", pgpg(file)};
×
133
  for (auto &r : recipients) {
×
134
    args.append("-r");
×
135
    args.append(r);
136
  }
137
  if (overwrite) {
×
138
    args.append("--yes");
×
139
  }
140
  args.append("-");
×
141
  executeGpg(PASS_INSERT, args, newValue);
×
142
  if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit()) {
×
143
    // Git is used when enabled - this is the standard pass workflow
144
    if (!overwrite) {
×
145
      executeGit(GIT_ADD, {"add", pgit(file)});
×
146
    }
147
    QString path = QDir(QtPassSettings::getPassStore()).relativeFilePath(file);
×
148
    path.replace(Util::endsWithGpg(), "");
×
149
    QString msg =
150
        QString(overwrite ? "Edit" : "Add") + " for " + path + " using QtPass.";
×
151
    GitCommit(file, msg);
×
152
  }
153
}
×
154

155
/**
156
 * @brief ImitatePass::GitCommit commit a file to git with an appropriate commit
157
 * message
158
 * @param file
159
 * @param msg
160
 */
161
void ImitatePass::GitCommit(const QString &file, const QString &msg) {
×
162
  if (file.isEmpty()) {
×
163
    executeGit(GIT_COMMIT, {"commit", "-m", msg});
×
164
  } else {
165
    executeGit(GIT_COMMIT, {"commit", "-m", msg, "--", pgit(file)});
×
166
  }
167
}
×
168

169
/**
170
 * @brief ImitatePass::Remove custom implementation of "pass remove"
171
 */
172
void ImitatePass::Remove(QString file, bool isDir) {
×
173
  file = QtPassSettings::getPassStore() + file;
×
174
  transactionHelper trans(this, PASS_REMOVE);
×
175
  if (!isDir) {
×
176
    file += ".gpg";
×
177
  }
178
  if (QtPassSettings::isUseGit()) {
×
179
    executeGit(GIT_RM, {"rm", (isDir ? "-rf" : "-f"), pgit(file)});
×
180
    // Normalize path the same way as add/edit operations
181
    QString path = QDir(QtPassSettings::getPassStore()).relativeFilePath(file);
×
182
    path.replace(Util::endsWithGpg(), "");
×
183
    GitCommit(file, "Remove for " + path + " using QtPass.");
×
184
  } else {
185
    if (isDir) {
×
186
      QDir dir(file);
×
187
      dir.removeRecursively();
×
188
    } else {
×
189
      QFile(file).remove();
×
190
    }
191
  }
192
}
×
193

194
/**
195
 * @brief ImitatePass::Init initialize pass repository
196
 *
197
 * @param path      path in which new password-store will be created
198
 * @param users     list of users who shall be able to decrypt passwords in
199
 * path
200
 */
201
auto ImitatePass::checkSigningKeys(const QStringList &signingKeys) -> bool {
×
202
  QString out;
×
203
  QStringList args =
204
      QStringList{"--status-fd=1", "--list-secret-keys"} + signingKeys;
×
205
  int result =
NEW
206
      Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args, &out);
×
NEW
207
  if (result != 0) {
×
208
#ifdef QT_DEBUG
209
    dbg() << "GPG list-secret-keys failed with code:" << result;
210
#endif
211
    return false;
212
  }
213
  for (auto &key : signingKeys) {
×
214
    if (out.contains("[GNUPG:] KEY_CONSIDERED " + key)) {
×
215
      return true;
216
    }
217
  }
218
  return false;
219
}
×
220

221
void ImitatePass::writeGpgIdFile(const QString &gpgIdFile,
×
222
                                 const QList<UserInfo> &users) {
223
  QFile gpgId(gpgIdFile);
×
224
  if (!gpgId.open(QIODevice::WriteOnly | QIODevice::Text)) {
×
225
    emit critical(tr("Cannot update"),
×
226
                  tr("Failed to open .gpg-id for writing."));
×
227
    return;
228
  }
229
  bool secret_selected = false;
230
  for (const UserInfo &user : users) {
×
231
    if (user.enabled) {
×
232
      gpgId.write((user.key_id + "\n").toUtf8());
×
233
      secret_selected |= user.have_secret;
×
234
    }
235
  }
236
  gpgId.close();
×
237
  if (!secret_selected) {
×
238
    emit critical(
×
239
        tr("Check selected users!"),
×
240
        tr("None of the selected keys have a secret key available.\n"
×
241
           "You will not be able to decrypt any newly added passwords!"));
242
  }
243
}
×
244

NEW
245
auto ImitatePass::signGpgIdFile(const QString &gpgIdFile,
×
246
                                const QStringList &signingKeys) -> bool {
247
  QStringList args;
×
248
  // Use only the first signing key; multiple --default-key options would
249
  // override each other and only the last one would take effect.
NEW
250
  if (!signingKeys.isEmpty()) {
×
251
#ifdef QT_DEBUG
252
    if (signingKeys.size() > 1) {
253
      dbg() << "Multiple signing keys configured; using only the first key:"
254
            << signingKeys.first();
255
    }
256
#endif
NEW
257
    args.append(QStringList{"--default-key", signingKeys.first()});
×
258
  }
259
  args.append(QStringList{"--yes", "--detach-sign", gpgIdFile});
×
260
  int result =
NEW
261
      Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args);
×
NEW
262
  if (result != 0) {
×
263
#ifdef QT_DEBUG
264
    dbg() << "GPG signing failed with code:" << result;
265
#endif
NEW
266
    emit critical(tr("GPG signing failed!"),
×
NEW
267
                  tr("Failed to sign %1.").arg(gpgIdFile));
×
NEW
268
    return false;
×
269
  }
270
  if (!verifyGpgIdFile(gpgIdFile)) {
×
271
    emit critical(tr("Check .gpgid file signature!"),
×
272
                  tr("Signature for %1 is invalid.").arg(gpgIdFile));
×
NEW
273
    return false;
×
274
  }
275
  return true;
UNCOV
276
}
×
277

278
void ImitatePass::gitAddGpgId(const QString &gpgIdFile,
×
279
                              const QString &gpgIdSigFile, bool addFile,
280
                              bool addSigFile) {
281
  if (addFile) {
×
282
    executeGit(GIT_ADD, {"add", pgit(gpgIdFile)});
×
283
  }
284
  QString commitPath = gpgIdFile;
285
  commitPath.replace(Util::endsWithGpg(), "");
×
286
  GitCommit(gpgIdFile, "Added " + commitPath + " using QtPass.");
×
287
  if (!addSigFile) {
×
288
    return;
289
  }
290
  executeGit(GIT_ADD, {"add", pgit(gpgIdSigFile)});
×
291
  commitPath = gpgIdSigFile;
×
292
  commitPath.replace(QRegularExpression("\\.gpg$"), "");
×
293
  GitCommit(gpgIdSigFile, "Added " + commitPath + " using QtPass.");
×
294
}
×
295

296
void ImitatePass::Init(QString path, const QList<UserInfo> &users) {
×
297
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
298
  QStringList signingKeys =
299
      QtPassSettings::getPassSigningKey().split(" ", Qt::SkipEmptyParts);
×
300
#else
301
  QStringList signingKeys =
302
      QtPassSettings::getPassSigningKey().split(" ", QString::SkipEmptyParts);
303
#endif
304
  QString gpgIdSigFile = path + ".gpg-id.sig";
×
305
  bool addSigFile = false;
306
  if (!signingKeys.isEmpty()) {
×
307
    if (!checkSigningKeys(signingKeys)) {
×
308
      emit critical(tr("No signing key!"),
×
309
                    tr("None of the secret signing keys is available.\n"
×
310
                       "You will not be able to change the user list!"));
311
      return;
×
312
    }
313
    QFileInfo checkFile(gpgIdSigFile);
×
314
    if (!checkFile.exists() || !checkFile.isFile()) {
×
315
      addSigFile = true;
316
    }
317
  }
×
318

319
  QString gpgIdFile = path + ".gpg-id";
×
320
  bool addFile = false;
321
  transactionHelper trans(this, PASS_INIT);
×
322
  if (QtPassSettings::isAddGPGId(true)) {
×
323
    QFileInfo checkFile(gpgIdFile);
×
324
    if (!checkFile.exists() || !checkFile.isFile()) {
×
325
      addFile = true;
326
    }
327
  }
×
328
  writeGpgIdFile(gpgIdFile, users);
×
329

330
  if (!signingKeys.isEmpty()) {
×
NEW
331
    if (!signGpgIdFile(gpgIdFile, signingKeys)) {
×
332
      return;
333
    }
334
  }
335

336
  if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit() &&
×
337
      !QtPassSettings::getGitExecutable().isEmpty()) {
×
338
    gitAddGpgId(gpgIdFile, gpgIdSigFile, addFile, addSigFile);
×
339
  }
340
  reencryptPath(path);
×
341
}
342

343
/**
344
 * @brief ImitatePass::verifyGpgIdFile verify detached gpgid file signature.
345
 * @param file which gpgid file.
346
 * @return was verification succesful?
347
 */
348
auto ImitatePass::verifyGpgIdFile(const QString &file) -> bool {
×
349
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
350
  QStringList signingKeys =
351
      QtPassSettings::getPassSigningKey().split(" ", Qt::SkipEmptyParts);
×
352
#else
353
  QStringList signingKeys =
354
      QtPassSettings::getPassSigningKey().split(" ", QString::SkipEmptyParts);
355
#endif
356
  if (signingKeys.isEmpty()) {
×
357
    return true;
358
  }
359
  QString out;
×
360
  QStringList args =
361
      QStringList{"--verify", "--status-fd=1", pgpg(file) + ".sig", pgpg(file)};
×
362
  int result =
NEW
363
      Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args, &out);
×
NEW
364
  if (result != 0) {
×
365
#ifdef QT_DEBUG
366
    dbg() << "GPG verify failed with code:" << result;
367
#endif
368
    return false;
369
  }
370
  QRegularExpression re(
371
      R"(^\[GNUPG:\] VALIDSIG ([A-F0-9]{40}) .* ([A-F0-9]{40})\r?$)",
372
      QRegularExpression::MultilineOption);
×
373
  QRegularExpressionMatch m = re.match(out);
×
374
  if (!m.hasMatch()) {
×
375
    return false;
376
  }
377
  QStringList fingerprints = m.capturedTexts();
×
378
  fingerprints.removeFirst();
×
379
  for (auto &key : signingKeys) {
×
380
    if (fingerprints.contains(key)) {
×
381
      return true;
×
382
    }
383
  }
384
  return false;
×
385
}
×
386

387
/**
388
 * @brief ImitatePass::removeDir delete folder recursive.
389
 * @param dirName which folder.
390
 * @return was removal succesful?
391
 */
392
auto ImitatePass::removeDir(const QString &dirName) -> bool {
1✔
393
  bool result = true;
394
  QDir dir(dirName);
1✔
395

396
  if (dir.exists(dirName)) {
1✔
397
    for (const QFileInfo &info :
398
         dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden |
399
                               QDir::AllDirs | QDir::Files,
400
                           QDir::DirsFirst)) {
1✔
401
      if (info.isDir()) {
×
402
        result = removeDir(info.absoluteFilePath());
×
403
      } else {
404
        result = QFile::remove(info.absoluteFilePath());
×
405
      }
406

407
      if (!result) {
×
408
        return result;
409
      }
410
    }
411
    result = dir.rmdir(dirName);
1✔
412
  }
413
  return result;
414
}
1✔
415

416
/**
417
 * @brief ImitatePass::reencryptPath reencrypt all files under the chosen
418
 * directory
419
 *
420
 * This is stil quite experimental..
421
 * @param dir
422
 */
423
void ImitatePass::verifyGpgIdForDir(const QString &file,
×
424
                                    QStringList &gpgIdFilesVerified,
425
                                    QStringList &gpgId) {
426
  QString gpgIdPath = Pass::getGpgIdPath(file);
×
427
  if (gpgIdFilesVerified.contains(gpgIdPath)) {
×
428
    return;
429
  }
430
  if (!verifyGpgIdFile(gpgIdPath)) {
×
431
    emit critical(tr("Check .gpgid file signature!"),
×
432
                  tr("Signature for %1 is invalid.").arg(gpgIdPath));
×
433
    emit endReencryptPath();
×
434
    return;
435
  }
436
  gpgIdFilesVerified.append(gpgIdPath);
437
  gpgId = getRecipientList(file);
×
438
  gpgId.sort();
439
}
440

441
auto ImitatePass::getKeysFromFile(const QString &fileName) -> QStringList {
×
442
  QStringList args = {
443
      "-v",          "--no-secmem-warning", "--no-permission-warning",
444
      "--list-only", "--keyid-format=long", pgpg(fileName)};
×
445
  QString keys;
×
446
  QString err;
×
447
  Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args, &keys,
×
448
                            &err);
449
  QStringList actualKeys;
×
450
  keys += err;
451
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
452
  QStringList key = keys.split(Util::newLinesRegex(), Qt::SkipEmptyParts);
×
453
#else
454
  QStringList key = keys.split(Util::newLinesRegex(), QString::SkipEmptyParts);
455
#endif
456
  QListIterator<QString> itr(key);
457
  while (itr.hasNext()) {
×
458
    QString current = itr.next();
459
    QStringList cur = current.split(" ");
×
460
    if (cur.length() > 4) {
×
461
      QString actualKey = cur.takeAt(4);
×
462
      if (actualKey.length() == 16) {
×
463
        actualKeys << actualKey;
464
      }
465
    }
466
  }
467
  actualKeys.sort();
468
  return actualKeys;
×
469
}
×
470

471
void ImitatePass::reencryptSingleFile(const QString &fileName,
×
472
                                      const QStringList &recipients) {
473
#ifdef QT_DEBUG
474
  dbg() << "reencrypt " << fileName << " for " << recipients;
475
#endif
476
  QString local_lastDecrypt = "Could not decrypt";
×
477
  QStringList args = {
478
      "-d",      "--quiet",     "--yes",       "--no-encrypt-to",
479
      "--batch", "--use-agent", pgpg(fileName)};
×
480
  Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args,
×
481
                            &local_lastDecrypt);
482

483
  if (local_lastDecrypt.isEmpty() || local_lastDecrypt == "Could not decrypt") {
×
484
#ifdef QT_DEBUG
485
    dbg() << "Decrypt error on re-encrypt";
486
#endif
487
    return;
×
488
  }
489

490
  if (local_lastDecrypt.right(1) != "\n") {
×
491
    local_lastDecrypt += "\n";
×
492
  }
493

494
  QStringList recipientsList = Pass::getRecipientList(fileName);
×
495
  if (recipientsList.isEmpty()) {
×
496
    emit critical(tr("Can not edit"),
×
497
                  tr("Could not read encryption key to use, .gpg-id "
×
498
                     "file missing or invalid."));
499
    return;
500
  }
501
  args = QStringList{"--yes", "--batch", "-eq", "--output", pgpg(fileName)};
×
502
  for (auto &i : recipientsList) {
×
503
    args.append("-r");
×
504
    args.append(i);
505
  }
506
  args.append("-");
×
507
  Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args,
×
508
                            local_lastDecrypt);
509

510
  if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit()) {
×
511
    Executor::executeBlocking(QtPassSettings::getGitExecutable(),
×
512
                              {"add", pgit(fileName)});
513
    QString path =
514
        QDir(QtPassSettings::getPassStore()).relativeFilePath(fileName);
×
515
    path.replace(Util::endsWithGpg(), "");
×
516
    Executor::executeBlocking(QtPassSettings::getGitExecutable(),
×
517
                              {"commit", pgit(fileName), "-m",
518
                               "Edit for " + path + " using QtPass."});
×
519
  }
520
}
×
521

522
void ImitatePass::reencryptPath(const QString &dir) {
×
523
  emit statusMsg(tr("Re-encrypting from folder %1").arg(dir), 3000);
×
524
  emit startReencryptPath();
×
525
  if (QtPassSettings::isAutoPull()) {
×
526
    emit statusMsg(tr("Updating password-store"), 2000);
×
527
    GitPull_b();
×
528
  }
529
  QDir currentDir;
×
530
  QDirIterator gpgFiles(dir, QStringList() << "*.gpg", QDir::Files,
×
531
                        QDirIterator::Subdirectories);
×
532
  QStringList gpgIdFilesVerified;
×
533
  QStringList gpgId;
×
534
  while (gpgFiles.hasNext()) {
×
535
    QString fileName = gpgFiles.next();
×
536
    if (gpgFiles.fileInfo().path() != currentDir.path()) {
×
537
      verifyGpgIdForDir(fileName, gpgIdFilesVerified, gpgId);
×
538
      if (gpgId.isEmpty() && !gpgIdFilesVerified.isEmpty()) {
×
539
        return;
540
      }
541
    }
542
    QStringList actualKeys = getKeysFromFile(fileName);
×
543
    if (actualKeys != gpgId) {
×
544
      reencryptSingleFile(fileName, gpgId);
×
545
    }
546
  }
547
  if (QtPassSettings::isAutoPush()) {
×
548
    emit statusMsg(tr("Updating password-store"), 2000);
×
549
    GitPush();
×
550
  }
551
  emit endReencryptPath();
×
552
}
×
553

554
auto ImitatePass::resolveMoveDestination(const QString &src,
5✔
555
                                         const QString &dest, bool force)
556
    -> QString {
557
  QFileInfo srcFileInfo(src);
5✔
558
  QFileInfo destFileInfo(dest);
5✔
559
  QString destFile;
5✔
560
  QString srcFileBaseName = srcFileInfo.fileName();
5✔
561

562
  if (srcFileInfo.isFile()) {
5✔
563
    if (destFileInfo.isFile()) {
4✔
564
      if (!force) {
2✔
565
#ifdef QT_DEBUG
566
        dbg() << "Destination file already exists";
567
#endif
568
        return QString();
569
      }
570
      destFile = dest;
1✔
571
    } else if (destFileInfo.isDir()) {
2✔
572
      destFile = QDir(dest).filePath(srcFileBaseName);
2✔
573
    } else {
574
      destFile = dest;
1✔
575
    }
576

577
    if (destFile.endsWith(".gpg", Qt::CaseInsensitive)) {
6✔
578
      destFile.chop(4);
3✔
579
    }
580
    destFile.append(".gpg");
3✔
581
  } else if (srcFileInfo.isDir()) {
1✔
582
    if (destFileInfo.isDir()) {
×
583
      destFile = QDir(dest).filePath(srcFileBaseName);
×
584
    } else if (destFileInfo.isFile()) {
×
585
#ifdef QT_DEBUG
586
      dbg() << "Destination is a file";
587
#endif
588
      return QString();
589
    } else {
590
      destFile = dest;
×
591
    }
592
  } else {
593
#ifdef QT_DEBUG
594
    dbg() << "Source file does not exist";
595
#endif
596
    return QString();
597
  }
598
  return destFile;
599
}
5✔
600

601
void ImitatePass::executeMoveGit(const QString &src, const QString &destFile,
×
602
                                 bool force) {
603
  QStringList args;
×
604
  args << "mv";
×
605
  if (force) {
×
606
    args << "-f";
×
607
  }
608
  args << pgit(src);
×
609
  args << pgit(destFile);
×
610
  executeGit(GIT_MOVE, args);
×
611

612
  QString relSrc = QDir(QtPassSettings::getPassStore()).relativeFilePath(src);
×
613
  relSrc.replace(Util::endsWithGpg(), "");
×
614
  QString relDest =
615
      QDir(QtPassSettings::getPassStore()).relativeFilePath(destFile);
×
616
  relDest.replace(Util::endsWithGpg(), "");
×
617
  QString message = QString("Moved for %1 to %2 using QtPass.");
×
618
  message = message.arg(relSrc, relDest);
×
619
  GitCommit("", message);
×
620
}
×
621

622
void ImitatePass::Move(const QString src, const QString dest,
×
623
                       const bool force) {
624
  transactionHelper trans(this, PASS_MOVE);
×
625
  QString destFile = resolveMoveDestination(src, dest, force);
×
626
  if (destFile.isEmpty()) {
×
627
    return;
628
  }
629

630
#ifdef QT_DEBUG
631
  dbg() << "Move Source: " << src;
632
  dbg() << "Move Destination: " << destFile;
633
#endif
634

635
  if (QtPassSettings::isUseGit()) {
×
636
    executeMoveGit(src, destFile, force);
×
637
  } else {
638
    QDir qDir;
×
639
    if (force) {
×
640
      qDir.remove(destFile);
×
641
    }
642
    qDir.rename(src, destFile);
×
643
  }
×
644
}
645

646
void ImitatePass::Copy(const QString src, const QString dest,
×
647
                       const bool force) {
648
  QFileInfo destFileInfo(dest);
×
649
  transactionHelper trans(this, PASS_COPY);
×
650
  if (QtPassSettings::isUseGit()) {
×
651
    QStringList args;
×
652
    args << "cp";
×
653
    if (force) {
×
654
      args << "-f";
×
655
    }
656
    args << pgit(src);
×
657
    args << pgit(dest);
×
658
    executeGit(GIT_COPY, args);
×
659

660
    QString message = QString("Copied from %1 to %2 using QtPass.");
×
661
    message = message.arg(src, dest);
×
662
    GitCommit("", message);
×
663
  } else {
664
    QDir qDir;
×
665
    if (force) {
×
666
      qDir.remove(dest);
×
667
    }
668
    QFile::copy(src, dest);
×
669
  }
×
670
  // reecrypt all files under the new folder
671
  if (destFileInfo.isDir()) {
×
672
    reencryptPath(destFileInfo.absoluteFilePath());
×
673
  } else if (destFileInfo.isFile()) {
×
674
    reencryptPath(destFileInfo.dir().path());
×
675
  }
676
}
×
677

678
/**
679
 * @brief ImitatePass::executeGpg easy wrapper for running gpg commands
680
 * @param args
681
 */
682
void ImitatePass::executeGpg(PROCESS id, const QStringList &args, QString input,
×
683
                             bool readStdout, bool readStderr) {
684
  executeWrapper(id, QtPassSettings::getGpgExecutable(), args, std::move(input),
×
685
                 readStdout, readStderr);
686
}
×
687
/**
688
 * @brief ImitatePass::executeGit easy wrapper for running git commands
689
 * @param args
690
 */
691
void ImitatePass::executeGit(PROCESS id, const QStringList &args, QString input,
×
692
                             bool readStdout, bool readStderr) {
693
  executeWrapper(id, QtPassSettings::getGitExecutable(), args, std::move(input),
×
694
                 readStdout, readStderr);
695
}
×
696

697
/**
698
 * @brief ImitatePass::finished this function is overloaded to ensure
699
 *                              identical behaviour to RealPass ie. only PASS_*
700
 *                              processes are visible inside Pass::finish, so
701
 *                              that interface-wise it all looks the same
702
 * @param id
703
 * @param exitCode
704
 * @param out
705
 * @param err
706
 */
707
void ImitatePass::finished(int id, int exitCode, const QString &out,
×
708
                           const QString &err) {
709
#ifdef QT_DEBUG
710
  dbg() << "Imitate Pass";
711
#endif
712
  static QString transactionOutput;
×
713
  PROCESS pid = transactionIsOver(static_cast<PROCESS>(id));
×
714
  transactionOutput.append(out);
×
715

716
  if (exitCode == 0) {
×
717
    if (pid == INVALID) {
×
718
      return;
719
    }
720
  } else {
721
    while (pid == INVALID) {
×
722
      id = exec.cancelNext();
×
723
      if (id == -1) {
×
724
        //  this is probably irrecoverable and shall not happen
725
#ifdef QT_DEBUG
726
        dbg() << "No such transaction!";
727
#endif
728
        return;
729
      }
730
      pid = transactionIsOver(static_cast<PROCESS>(id));
×
731
    }
732
  }
733
  Pass::finished(pid, exitCode, transactionOutput, err);
×
734
  transactionOutput.clear();
×
735
}
736

737
/**
738
 * @brief executeWrapper    overrided so that every execution is a transaction
739
 * @param id
740
 * @param app
741
 * @param args
742
 * @param input
743
 * @param readStdout
744
 * @param readStderr
745
 */
746
void ImitatePass::executeWrapper(PROCESS id, const QString &app,
×
747
                                 const QStringList &args, QString input,
748
                                 bool readStdout, bool readStderr) {
749
  transactionAdd(id);
×
750
  Pass::executeWrapper(id, app, args, input, readStdout, readStderr);
×
751
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc