3#include <QtCore/QCryptographicHash>
5#include <QtCore/QStandardPaths>
9 , _lockFilePath(lockDir() + QLatin1String(
"/qgc-") + generateKeyHash(key, QLatin1String(
"_lock")) + QLatin1String(
".lock"))
10 , _lockFile(_lockFilePath)
13 _lockFile.setStaleLockTime(0);
23 if (_lockFile.isLocked()) {
27 if (_lockFile.tryLock(0)) {
32 switch (_lockFile.error()) {
33 case QLockFile::NoError:
35 case QLockFile::LockFailedError:
37 case QLockFile::PermissionError:
38 qWarning() <<
"QLockFile PermissionError: Unable to access lock file at" << _lockFile.fileName();
40 case QLockFile::UnknownError:
41 qWarning() <<
"QLockFile UnknownError: An unknown error occurred with lock file at" << _lockFile.fileName();
52 return (_lockFile.isLocked() ?
true : _lockFile.tryLock(0));
57 if (_lockFile.isLocked()) {
62QString RunGuard::generateKeyHash(
const QString &key,
const QString &salt)
65 (void) data.append(key.toUtf8());
66 (void) data.append(salt.toUtf8());
67 const QByteArray hex = QCryptographicHash::hash(data, QCryptographicHash::Sha1).toHex();
68 return QString::fromLatin1(hex);
71QString RunGuard::lockDir()
73 QString dir = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
75 dir = QDir::tempPath();
78 if (!QDir().mkpath(dir)) {
79 qWarning() <<
"RunGuard: Failed to create lock directory:" << dir;
RunGuard(const QString &key)