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

IJHack / QtPass / 23548427933

25 Mar 2026 03:12PM UTC coverage: 17.411% (-0.1%) from 17.516%
23548427933

Pull #769

github

web-flow
Merge 2e804a75f into 8c5fc9456
Pull Request #769: CodeFactor: Fix complex method and style recommendations

0 of 140 new or added lines in 2 files covered. (0.0%)

6 existing lines in 1 file now uncovered.

866 of 4974 relevant lines covered (17.41%)

7.19 hits per line

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

0.3
/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;
8✔
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
    // TODO(bezet): probably throw here
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
    // TODO(bezet): why not?
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
    // TODO(bezet): commit message used to have pass-like file name inside(ie.
181
    //  getFile(file, true)
182
    GitCommit(file, "Remove for " + file + " using QtPass.");
×
183
  } else {
184
    if (isDir) {
×
185
      QDir dir(file);
×
186
      dir.removeRecursively();
×
187
    } else {
×
188
      QFile(file).remove();
×
189
    }
190
  }
191
}
×
192

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

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

NEW
237
void ImitatePass::signGpgIdFile(const QString &gpgIdFile,
×
238
                                const QStringList &signingKeys) {
NEW
239
  QStringList args;
×
NEW
240
  for (auto &key : signingKeys) {
×
NEW
241
    args.append(QStringList{"--default-key", key});
×
242
  }
NEW
243
  args.append(QStringList{"--yes", "--detach-sign", gpgIdFile});
×
NEW
244
  Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args);
×
NEW
245
  if (!verifyGpgIdFile(gpgIdFile)) {
×
NEW
246
    emit critical(tr("Check .gpgid file signature!"),
×
NEW
247
                  tr("Signature for %1 is invalid.").arg(gpgIdFile));
×
248
  }
NEW
249
}
×
250

NEW
251
void ImitatePass::gitAddGpgId(const QString &gpgIdFile, const QString &gpgIdSigFile,
×
252
                              bool addFile, bool addSigFile) {
NEW
253
  if (addFile) {
×
NEW
254
    executeGit(GIT_ADD, {"add", pgit(gpgIdFile)});
×
255
  }
256
  QString commitPath = gpgIdFile;
NEW
257
  commitPath.replace(Util::endsWithGpg(), "");
×
NEW
258
  GitCommit(gpgIdFile, "Added " + commitPath + " using QtPass.");
×
NEW
259
  if (!addSigFile) {
×
260
    return;
261
  }
NEW
262
  executeGit(GIT_ADD, {"add", pgit(gpgIdSigFile)});
×
NEW
263
  commitPath = gpgIdSigFile;
×
NEW
264
  commitPath.replace(QRegularExpression("\\.gpg$"), "");
×
NEW
265
  GitCommit(gpgIdSigFile, "Added " + commitPath + " using QtPass.");
×
NEW
266
}
×
267

UNCOV
268
void ImitatePass::Init(QString path, const QList<UserInfo> &users) {
×
269
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
270
  QStringList signingKeys =
271
      QtPassSettings::getPassSigningKey().split(" ", Qt::SkipEmptyParts);
×
272
#else
273
  QStringList signingKeys =
274
      QtPassSettings::getPassSigningKey().split(" ", QString::SkipEmptyParts);
275
#endif
276
  QString gpgIdSigFile = path + ".gpg-id.sig";
×
277
  bool addSigFile = false;
278
  if (!signingKeys.isEmpty()) {
×
NEW
279
    if (!checkSigningKeys(signingKeys)) {
×
UNCOV
280
      emit critical(tr("No signing key!"),
×
281
                    tr("None of the secret signing keys is available.\n"
×
282
                       "You will not be able to change the user list!"));
UNCOV
283
      return;
×
284
    }
285
    QFileInfo checkFile(gpgIdSigFile);
×
286
    if (!checkFile.exists() || !checkFile.isFile()) {
×
287
      addSigFile = true;
288
    }
289
  }
×
290

291
  QString gpgIdFile = path + ".gpg-id";
×
292
  bool addFile = false;
UNCOV
293
  transactionHelper trans(this, PASS_INIT);
×
294
  if (QtPassSettings::isAddGPGId(true)) {
×
295
    QFileInfo checkFile(gpgIdFile);
×
296
    if (!checkFile.exists() || !checkFile.isFile()) {
×
297
      addFile = true;
298
    }
299
  }
×
NEW
300
  writeGpgIdFile(gpgIdFile, users);
×
301

302
  if (!signingKeys.isEmpty()) {
×
NEW
303
    signGpgIdFile(gpgIdFile, signingKeys);
×
304
  }
305

306
  if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit() &&
×
307
      !QtPassSettings::getGitExecutable().isEmpty()) {
×
NEW
308
    gitAddGpgId(gpgIdFile, gpgIdSigFile, addFile, addSigFile);
×
309
  }
310
  reencryptPath(path);
×
311
}
312

313
/**
314
 * @brief ImitatePass::verifyGpgIdFile verify detached gpgid file signature.
315
 * @param file which gpgid file.
316
 * @return was verification succesful?
317
 */
318
auto ImitatePass::verifyGpgIdFile(const QString &file) -> bool {
×
319
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
320
  QStringList signingKeys =
321
      QtPassSettings::getPassSigningKey().split(" ", Qt::SkipEmptyParts);
×
322
#else
323
  QStringList signingKeys =
324
      QtPassSettings::getPassSigningKey().split(" ", QString::SkipEmptyParts);
325
#endif
326
  if (signingKeys.isEmpty()) {
×
327
    return true;
328
  }
329
  QString out;
×
330
  QStringList args =
331
      QStringList{"--verify", "--status-fd=1", pgpg(file) + ".sig", pgpg(file)};
×
332
  Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args, &out);
×
333
  QRegularExpression re(
334
      R"(^\[GNUPG:\] VALIDSIG ([A-F0-9]{40}) .* ([A-F0-9]{40})\r?$)",
335
      QRegularExpression::MultilineOption);
×
336
  QRegularExpressionMatch m = re.match(out);
×
337
  if (!m.hasMatch()) {
×
338
    return false;
339
  }
340
  QStringList fingerprints = m.capturedTexts();
×
341
  fingerprints.removeFirst();
×
342
  for (auto &key : signingKeys) {
×
343
    if (fingerprints.contains(key)) {
×
344
      return true;
×
345
    }
346
  }
347
  return false;
×
348
}
×
349

350
/**
351
 * @brief ImitatePass::removeDir delete folder recursive.
352
 * @param dirName which folder.
353
 * @return was removal succesful?
354
 */
355
auto ImitatePass::removeDir(const QString &dirName) -> bool {
×
356
  bool result = true;
357
  QDir dir(dirName);
×
358

359
  if (dir.exists(dirName)) {
×
360
    for (const QFileInfo &info :
361
         dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden |
362
                               QDir::AllDirs | QDir::Files,
363
                           QDir::DirsFirst)) {
×
364
      if (info.isDir()) {
×
365
        result = removeDir(info.absoluteFilePath());
×
366
      } else {
367
        result = QFile::remove(info.absoluteFilePath());
×
368
      }
369

370
      if (!result) {
×
371
        return result;
372
      }
373
    }
374
    result = dir.rmdir(dirName);
×
375
  }
376
  return result;
377
}
×
378

379
/**
380
 * @brief ImitatePass::reencryptPath reencrypt all files under the chosen
381
 * directory
382
 *
383
 * This is stil quite experimental..
384
 * @param dir
385
  */
NEW
386
void ImitatePass::verifyGpgIdForDir(const QString &file,
×
387
                                     QStringList &gpgIdFilesVerified,
388
                                     QStringList &gpgId) {
NEW
389
  QString gpgIdPath = Pass::getGpgIdPath(file);
×
NEW
390
  if (gpgIdFilesVerified.contains(gpgIdPath)) {
×
391
    return;
392
  }
NEW
393
  if (!verifyGpgIdFile(gpgIdPath)) {
×
NEW
394
    emit critical(tr("Check .gpgid file signature!"),
×
NEW
395
                  tr("Signature for %1 is invalid.").arg(gpgIdPath));
×
NEW
396
    emit endReencryptPath();
×
397
    return;
398
  }
399
  gpgIdFilesVerified.append(gpgIdPath);
NEW
400
  gpgId = getRecipientList(file);
×
401
  gpgId.sort();
402
}
403

NEW
404
auto ImitatePass::getKeysFromFile(const QString &fileName) -> QStringList {
×
405
  QStringList args = {
406
      "-v",          "--no-secmem-warning", "--no-permission-warning",
NEW
407
      "--list-only", "--keyid-format=long", pgpg(fileName)};
×
NEW
408
  QString keys;
×
NEW
409
  QString err;
×
NEW
410
  Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args, &keys,
×
411
                            &err);
NEW
412
  QStringList actualKeys;
×
413
  keys += err;
414
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
NEW
415
  QStringList key = keys.split(Util::newLinesRegex(), Qt::SkipEmptyParts);
×
416
#else
417
  QStringList key = keys.split(Util::newLinesRegex(), QString::SkipEmptyParts);
418
#endif
419
  QListIterator<QString> itr(key);
NEW
420
  while (itr.hasNext()) {
×
421
    QString current = itr.next();
NEW
422
    QStringList cur = current.split(" ");
×
NEW
423
    if (cur.length() > 4) {
×
NEW
424
      QString actualKey = cur.takeAt(4);
×
NEW
425
      if (actualKey.length() == 16) {
×
426
        actualKeys << actualKey;
427
      }
428
    }
429
  }
430
  actualKeys.sort();
NEW
431
  return actualKeys;
×
NEW
432
}
×
433

NEW
434
void ImitatePass::reencryptSingleFile(const QString &fileName,
×
435
                                       const QStringList &recipients) {
436
#ifdef QT_DEBUG
437
  dbg() << "reencrypt " << fileName << " for " << recipients;
438
#endif
NEW
439
  QString local_lastDecrypt = "Could not decrypt";
×
440
  QStringList args = {
441
      "-d",      "--quiet",     "--yes",       "--no-encrypt-to",
NEW
442
      "--batch", "--use-agent", pgpg(fileName)};
×
NEW
443
  Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args,
×
444
                            &local_lastDecrypt);
445

NEW
446
  if (local_lastDecrypt.isEmpty() ||
×
447
      local_lastDecrypt == "Could not decrypt") {
448
#ifdef QT_DEBUG
449
    dbg() << "Decrypt error on re-encrypt";
450
#endif
NEW
451
    return;
×
452
  }
453

NEW
454
  if (local_lastDecrypt.right(1) != "\n") {
×
NEW
455
    local_lastDecrypt += "\n";
×
456
  }
457

NEW
458
  QStringList recipientsList = Pass::getRecipientList(fileName);
×
NEW
459
  if (recipientsList.isEmpty()) {
×
NEW
460
    emit critical(tr("Can not edit"),
×
NEW
461
                  tr("Could not read encryption key to use, .gpg-id "
×
462
                     "file missing or invalid."));
463
    return;
464
  }
NEW
465
  args = QStringList{"--yes", "--batch", "-eq", "--output", pgpg(fileName)};
×
NEW
466
  for (auto &i : recipientsList) {
×
NEW
467
    args.append("-r");
×
468
    args.append(i);
469
  }
NEW
470
  args.append("-");
×
NEW
471
  Executor::executeBlocking(QtPassSettings::getGpgExecutable(), args,
×
472
                            local_lastDecrypt);
473

NEW
474
  if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit()) {
×
NEW
475
    Executor::executeBlocking(QtPassSettings::getGitExecutable(),
×
476
                              {"add", pgit(fileName)});
477
    QString path =
NEW
478
        QDir(QtPassSettings::getPassStore()).relativeFilePath(fileName);
×
NEW
479
    path.replace(Util::endsWithGpg(), "");
×
NEW
480
    Executor::executeBlocking(QtPassSettings::getGitExecutable(),
×
481
                              {"commit", pgit(fileName), "-m",
NEW
482
                               "Edit for " + path + " using QtPass."});
×
483
  }
NEW
484
}
×
485

486
void ImitatePass::reencryptPath(const QString &dir) {
×
487
  emit statusMsg(tr("Re-encrypting from folder %1").arg(dir), 3000);
×
488
  emit startReencryptPath();
×
489
  if (QtPassSettings::isAutoPull()) {
×
UNCOV
490
    emit statusMsg(tr("Updating password-store"), 2000);
×
491
    GitPull_b();
×
492
  }
493
  QDir currentDir;
×
494
  QDirIterator gpgFiles(dir, QStringList() << "*.gpg", QDir::Files,
×
495
                        QDirIterator::Subdirectories);
×
496
  QStringList gpgIdFilesVerified;
×
497
  QStringList gpgId;
×
498
  while (gpgFiles.hasNext()) {
×
499
    QString fileName = gpgFiles.next();
×
500
    if (gpgFiles.fileInfo().path() != currentDir.path()) {
×
NEW
501
      verifyGpgIdForDir(fileName, gpgIdFilesVerified, gpgId);
×
NEW
502
      if (gpgId.isEmpty() && !gpgIdFilesVerified.isEmpty()) {
×
503
        return;
504
      }
505
    }
NEW
506
    QStringList actualKeys = getKeysFromFile(fileName);
×
507
    if (actualKeys != gpgId) {
×
NEW
508
      reencryptSingleFile(fileName, gpgId);
×
509
    }
510
  }
511
  if (QtPassSettings::isAutoPush()) {
×
512
    emit statusMsg(tr("Updating password-store"), 2000);
×
UNCOV
513
    GitPush();
×
514
  }
515
  emit endReencryptPath();
×
516
}
×
517

NEW
518
auto ImitatePass::resolveMoveDestination(const QString &src,
×
519
                                          const QString &dest,
520
                                          bool force) -> QString {
521
  QFileInfo srcFileInfo(src);
×
522
  QFileInfo destFileInfo(dest);
×
523
  QString destFile;
×
524
  QString srcFileBaseName = srcFileInfo.fileName();
×
525

526
  if (srcFileInfo.isFile()) {
×
527
    if (destFileInfo.isFile()) {
×
528
      if (!force) {
×
529
#ifdef QT_DEBUG
530
        dbg() << "Destination file already exists";
531
#endif
532
        return QString();
533
      }
534
    } else if (destFileInfo.isDir()) {
×
535
      destFile = QDir(dest).filePath(srcFileBaseName);
×
536
    } else {
537
      destFile = dest;
×
538
    }
539

540
    if (destFile.endsWith(".gpg", Qt::CaseInsensitive)) {
×
NEW
541
      destFile.chop(4);
×
542
    }
543
    destFile.append(".gpg");
×
544
  } else if (srcFileInfo.isDir()) {
×
545
    if (destFileInfo.isDir()) {
×
546
      destFile = QDir(dest).filePath(srcFileBaseName);
×
547
    } else if (destFileInfo.isFile()) {
×
548
#ifdef QT_DEBUG
549
      dbg() << "Destination is a file";
550
#endif
551
      return QString();
552
    } else {
553
      destFile = dest;
×
554
    }
555
  } else {
556
#ifdef QT_DEBUG
557
    dbg() << "Source file does not exist";
558
#endif
559
    return QString();
560
  }
561
  return destFile;
NEW
562
}
×
563

NEW
564
void ImitatePass::executeMoveGit(const QString &src, const QString &destFile,
×
565
                                  bool force) {
NEW
566
  QStringList args;
×
NEW
567
  args << "mv";
×
NEW
568
  if (force) {
×
NEW
569
    args << "-f";
×
570
  }
NEW
571
  args << pgit(src);
×
NEW
572
  args << pgit(destFile);
×
NEW
573
  executeGit(GIT_MOVE, args);
×
574

NEW
575
  QString relSrc = QDir(QtPassSettings::getPassStore()).relativeFilePath(src);
×
NEW
576
  relSrc.replace(Util::endsWithGpg(), "");
×
577
  QString relDest =
NEW
578
      QDir(QtPassSettings::getPassStore()).relativeFilePath(destFile);
×
NEW
579
  relDest.replace(Util::endsWithGpg(), "");
×
NEW
580
  QString message = QString("Moved for %1 to %2 using QtPass.");
×
NEW
581
  message = message.arg(relSrc, relDest);
×
NEW
582
  GitCommit("", message);
×
NEW
583
}
×
584

NEW
585
void ImitatePass::Move(const QString src, const QString dest,
×
586
                       const bool force) {
NEW
587
  transactionHelper trans(this, PASS_MOVE);
×
NEW
588
  QString destFile = resolveMoveDestination(src, dest, force);
×
NEW
589
  if (destFile.isEmpty()) {
×
590
    return;
591
  }
592

593
#ifdef QT_DEBUG
594
  dbg() << "Move Source: " << src;
595
  dbg() << "Move Destination: " << destFile;
596
#endif
597

598
  if (QtPassSettings::isUseGit()) {
×
NEW
599
    executeMoveGit(src, destFile, force);
×
600
  } else {
601
    QDir qDir;
×
602
    if (force) {
×
603
      qDir.remove(destFile);
×
604
    }
605
    qDir.rename(src, destFile);
×
606
  }
×
607
}
608

609
void ImitatePass::Copy(const QString src, const QString dest,
×
610
                       const bool force) {
611
  QFileInfo destFileInfo(dest);
×
612
  transactionHelper trans(this, PASS_COPY);
×
613
  if (QtPassSettings::isUseGit()) {
×
614
    QStringList args;
×
615
    args << "cp";
×
616
    if (force) {
×
617
      args << "-f";
×
618
    }
619
    args << pgit(src);
×
620
    args << pgit(dest);
×
621
    executeGit(GIT_COPY, args);
×
622

623
    QString message = QString("copied from %1 to %2 using QTPass.");
×
624
    message = message.arg(src, dest);
×
625
    GitCommit("", message);
×
626
  } else {
627
    QDir qDir;
×
628
    if (force) {
×
629
      qDir.remove(dest);
×
630
    }
631
    QFile::copy(src, dest);
×
632
  }
×
633
  // reecrypt all files under the new folder
634
  if (destFileInfo.isDir()) {
×
635
    reencryptPath(destFileInfo.absoluteFilePath());
×
636
  } else if (destFileInfo.isFile()) {
×
637
    reencryptPath(destFileInfo.dir().path());
×
638
  }
639
}
×
640

641
/**
642
 * @brief ImitatePass::executeGpg easy wrapper for running gpg commands
643
 * @param args
644
 */
645
void ImitatePass::executeGpg(PROCESS id, const QStringList &args, QString input,
×
646
                             bool readStdout, bool readStderr) {
647
  executeWrapper(id, QtPassSettings::getGpgExecutable(), args, std::move(input),
×
648
                 readStdout, readStderr);
649
}
×
650
/**
651
 * @brief ImitatePass::executeGit easy wrapper for running git commands
652
 * @param args
653
 */
654
void ImitatePass::executeGit(PROCESS id, const QStringList &args, QString input,
×
655
                             bool readStdout, bool readStderr) {
656
  executeWrapper(id, QtPassSettings::getGitExecutable(), args, std::move(input),
×
657
                 readStdout, readStderr);
658
}
×
659

660
/**
661
 * @brief ImitatePass::finished this function is overloaded to ensure
662
 *                              identical behaviour to RealPass ie. only PASS_*
663
 *                              processes are visible inside Pass::finish, so
664
 *                              that interface-wise it all looks the same
665
 * @param id
666
 * @param exitCode
667
 * @param out
668
 * @param err
669
 */
670
void ImitatePass::finished(int id, int exitCode, const QString &out,
×
671
                           const QString &err) {
672
#ifdef QT_DEBUG
673
  dbg() << "Imitate Pass";
674
#endif
675
  static QString transactionOutput;
×
676
  PROCESS pid = transactionIsOver(static_cast<PROCESS>(id));
×
677
  transactionOutput.append(out);
×
678

679
  if (exitCode == 0) {
×
680
    if (pid == INVALID) {
×
681
      return;
682
    }
683
  } else {
684
    while (pid == INVALID) {
×
685
      id = exec.cancelNext();
×
686
      if (id == -1) {
×
687
        //  this is probably irrecoverable and shall not happen
688
#ifdef QT_DEBUG
689
        dbg() << "No such transaction!";
690
#endif
691
        return;
692
      }
693
      pid = transactionIsOver(static_cast<PROCESS>(id));
×
694
    }
695
  }
696
  Pass::finished(pid, exitCode, transactionOutput, err);
×
697
  transactionOutput.clear();
×
698
}
699

700
/**
701
 * @brief executeWrapper    overrided so that every execution is a transaction
702
 * @param id
703
 * @param app
704
 * @param args
705
 * @param input
706
 * @param readStdout
707
 * @param readStderr
708
 */
709
void ImitatePass::executeWrapper(PROCESS id, const QString &app,
×
710
                                 const QStringList &args, QString input,
711
                                 bool readStdout, bool readStderr) {
712
  transactionAdd(id);
×
713
  Pass::executeWrapper(id, app, args, input, readStdout, readStderr);
×
714
}
×
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