app/core/ProfileLease.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "ProfileLease.h" | ||
| 2 | #include <QDir> | ||
| 3 | #include <QFileInfo> | ||
| 4 | #include <QLockFile> | ||
| 5 | #include <QStandardPaths> | ||
| 6 | |||
| 7 | 20 | ProfileLease::ProfileLease() = default; | |
| 8 | 20 | ProfileLease::~ProfileLease() = default; | |
| 9 | |||
| 10 | ✗ | QString ProfileLease::defaultDirectory() { | |
| 11 | ✗ | return QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + | |
| 12 | ✗ | QStringLiteral("/mailjd"); | |
| 13 | } | ||
| 14 | |||
| 15 | 23 | bool ProfileLease::acquire(const QString &directory) { | |
| 16 | 23 | m_error.clear(); | |
| 17 |
9/12✓ Branch 3 → 4 taken 23 times.
✓ Branch 5 → 6 taken 21 times.
✓ Branch 5 → 8 taken 2 times.
✓ Branch 6 → 7 taken 21 times.
✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 9 taken 20 times.
✓ Branch 10 → 11 taken 23 times.
✗ Branch 10 → 12 not taken.
✓ Branch 12 → 13 taken 3 times.
✓ Branch 12 → 20 taken 20 times.
✗ Branch 111 → 112 not taken.
✗ Branch 111 → 113 not taken.
|
23 | if (directory.trimmed().isEmpty() || !QDir::isAbsolutePath(directory)) { |
| 18 | 3 | m_error = QStringLiteral("An absolute mail profile directory is required."); | |
| 19 | 3 | return false; | |
| 20 | } | ||
| 21 |
2/2✓ Branch 21 → 22 taken 2 times.
✓ Branch 21 → 36 taken 18 times.
|
20 | if (m_lock) { |
| 22 |
4/4✓ Branch 22 → 23 taken 2 times.
✓ Branch 23 → 24 taken 2 times.
✓ Branch 27 → 28 taken 1 time.
✓ Branch 27 → 29 taken 1 time.
|
2 | if (QFileInfo(directory).canonicalFilePath() == m_directory) return true; |
| 23 | 1 | m_error = QStringLiteral("This lease already owns a different mail profile."); | |
| 24 | 1 | return false; | |
| 25 | } | ||
| 26 |
4/4✓ Branch 37 → 38 taken 18 times.
✓ Branch 38 → 39 taken 18 times.
✓ Branch 41 → 42 taken 1 time.
✓ Branch 41 → 52 taken 17 times.
|
18 | if (!QDir().mkpath(directory)) { |
| 27 |
1/1✓ Branch 46 → 47 taken 1 time.
|
2 | m_error = QStringLiteral("Cannot create mail profile directory: %1").arg(directory); |
| 28 | 1 | return false; | |
| 29 | } | ||
| 30 |
2/2✓ Branch 52 → 53 taken 17 times.
✓ Branch 53 → 54 taken 17 times.
|
17 | const QString canonical = QFileInfo(directory).canonicalFilePath(); |
| 31 |
1/2✗ Branch 56 → 57 not taken.
✓ Branch 56 → 67 taken 17 times.
|
17 | if (canonical.isEmpty()) { |
| 32 | ✗ | m_error = QStringLiteral("Cannot resolve mail profile directory: %1").arg(directory); | |
| 33 | ✗ | return false; | |
| 34 | } | ||
| 35 |
4/4✓ Branch 67 → 68 taken 17 times.
✓ Branch 68 → 69 taken 17 times.
✓ Branch 69 → 70 taken 17 times.
✓ Branch 70 → 71 taken 17 times.
|
17 | auto lock = std::make_unique<QLockFile>(QDir(canonical).filePath(".mailjd-session.lock")); |
| 36 | // A slow or suspended live process must never lose its lease due to its age. | ||
| 37 |
1/1✓ Branch 75 → 76 taken 17 times.
|
17 | lock->setStaleLockTime(0); |
| 38 |
3/3✓ Branch 77 → 78 taken 17 times.
✓ Branch 78 → 79 taken 6 times.
✓ Branch 78 → 103 taken 11 times.
|
17 | if (!lock->tryLock(0)) { |
| 39 |
1/1✓ Branch 80 → 81 taken 6 times.
|
6 | m_error = lock->error() == QLockFile::LockFailedError |
| 40 |
3/10✓ Branch 81 → 82 taken 6 times.
✗ Branch 81 → 87 not taken.
✓ Branch 86 → 92 taken 6 times.
✗ Branch 91 → 92 not taken.
✓ Branch 100 → 101 taken 6 times.
✗ Branch 100 → 102 not taken.
✗ Branch 163 → 164 not taken.
✗ Branch 163 → 165 not taken.
✗ Branch 167 → 168 not taken.
✗ Branch 167 → 169 not taken.
|
24 | ? QStringLiteral("Mail profile is already in use by another frontend: %1").arg(canonical) |
| 41 |
3/10✗ Branch 94 → 95 not taken.
✓ Branch 94 → 96 taken 6 times.
✗ Branch 96 → 97 not taken.
✓ Branch 96 → 98 taken 6 times.
✓ Branch 98 → 99 taken 6 times.
✗ Branch 98 → 100 not taken.
✗ Branch 154 → 155 not taken.
✗ Branch 154 → 156 not taken.
✗ Branch 158 → 159 not taken.
✗ Branch 158 → 160 not taken.
|
12 | : QStringLiteral("Cannot lock mail profile directory: %1").arg(canonical); |
| 42 | 6 | return false; | |
| 43 | } | ||
| 44 | 11 | m_directory = canonical; | |
| 45 | 11 | m_lock = std::move(lock); | |
| 46 | 11 | return true; | |
| 47 | 17 | } | |
| 48 |