mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Assert profile locks before writing
This fixes the potential edge case where a frozen then unfrozen qTox instance could have its locks delete by a new qTox instance. We now check that we still own our locks, restoring them if we can, before saving
This commit is contained in:
parent
694933d24b
commit
42d7a7bcef
|
@ -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()))
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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<QLockFile> lockfile;
|
||||
|
|
Loading…
Reference in New Issue
Block a user