diff --git a/src/core/core.cpp b/src/core/core.cpp index 39af5cd59..d265db96b 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -947,6 +947,8 @@ void Core::saveConfiguration() if (!isReady()) return; + ProfileLocker::assertLock(); + QString dir = Settings::getSettingsDirPath(); QDir directory(dir); if (!directory.exists() && !directory.mkpath(directory.absolutePath())) diff --git a/src/profilelocker.cpp b/src/profilelocker.cpp index 4014f0d2f..b033410c4 100644 --- a/src/profilelocker.cpp +++ b/src/profilelocker.cpp @@ -67,3 +67,33 @@ void ProfileLocker::clearAllLocks() file.remove(); } } + +void ProfileLocker::assertLock() +{ + if (!lockfile) + { + qCritical() << "ProfileLocker::assertLock: We don't seem to own any lock!"; + deathByBrokenLock(); + } + + if (!QFile(lockPathFromName(curLockName)).exists()) + { + QString tmp = curLockName; + unlock(); + if (lock(tmp)) + { + qCritical() << "ProfileLocker::assertLock: Lock file was lost, but could be restored"; + } + else + { + qCritical() << "ProfileLocker::assertLock: Lock file was lost, and could *NOT* be restored"; + deathByBrokenLock(); + } + } +} + +void ProfileLocker::deathByBrokenLock() +{ + qCritical() << "ProfileLocker: Lock is *BROKEN*, exiting immediately"; + abort(); +} diff --git a/src/profilelocker.h b/src/profilelocker.h index 72ce1b93f..be59f043f 100644 --- a/src/profilelocker.h +++ b/src/profilelocker.h @@ -28,9 +28,15 @@ public: /// DO NOT call unless all we're the only qTox instance /// and we don't hold any lock yet. static void clearAllLocks(); + /// Check that we actually own the lock + /// In case the file was deleted on disk, restore it + /// If we can't get a lock, exit qTox immediately + /// If we never had a lock in the firt place, exit immediately + static void assertLock(); private: static QString lockPathFromName(const QString& name); + static void deathByBrokenLock(); ///< Print an error then exit immediately private: static std::unique_ptr lockfile;