2015-04-25 00:53:19 +08:00
|
|
|
#include "profilelocker.h"
|
|
|
|
#include "src/misc/settings.h"
|
|
|
|
#include <QDir>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
unique_ptr<QLockFile> ProfileLocker::lockfile;
|
|
|
|
QString ProfileLocker::curLockName;
|
|
|
|
|
|
|
|
QString ProfileLocker::lockPathFromName(const QString& name)
|
|
|
|
{
|
|
|
|
return Settings::getInstance().getSettingsDirPath()+'/'+name+".lock";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileLocker::isLockable(QString profile)
|
|
|
|
{
|
|
|
|
// If we already have the lock, it's definitely lockable
|
|
|
|
if (lockfile && curLockName == profile)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
QLockFile newLock(lockPathFromName(profile));
|
|
|
|
return newLock.tryLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileLocker::lock(QString profile)
|
|
|
|
{
|
|
|
|
if (lockfile && curLockName == profile)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
QLockFile* newLock = new QLockFile(lockPathFromName(profile));
|
|
|
|
if (!newLock->tryLock())
|
|
|
|
{
|
|
|
|
delete newLock;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock();
|
|
|
|
lockfile.reset(newLock);
|
|
|
|
curLockName = profile;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileLocker::unlock()
|
|
|
|
{
|
|
|
|
if (!lockfile)
|
|
|
|
return;
|
|
|
|
lockfile->unlock();
|
|
|
|
delete lockfile.release();
|
|
|
|
lockfile = nullptr;
|
|
|
|
curLockName.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileLocker::clearAllLocks()
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qDebug() << "clearAllLocks: Wiping out all lock files";
|
2015-04-25 00:53:19 +08:00
|
|
|
if (lockfile)
|
|
|
|
unlock();
|
|
|
|
|
|
|
|
QDir dir(Settings::getInstance().getSettingsDirPath());
|
|
|
|
dir.setFilter(QDir::Files);
|
|
|
|
dir.setNameFilters({"*.lock"});
|
|
|
|
QFileInfoList files = dir.entryInfoList();
|
|
|
|
for (QFileInfo fileInfo : files)
|
|
|
|
{
|
|
|
|
QFile file(fileInfo.absoluteFilePath());
|
|
|
|
file.remove();
|
|
|
|
}
|
|
|
|
}
|
2015-04-25 03:05:19 +08:00
|
|
|
|
|
|
|
void ProfileLocker::assertLock()
|
|
|
|
{
|
|
|
|
if (!lockfile)
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qCritical() << "assertLock: We don't seem to own any lock!";
|
2015-04-25 03:05:19 +08:00
|
|
|
deathByBrokenLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!QFile(lockPathFromName(curLockName)).exists())
|
|
|
|
{
|
|
|
|
QString tmp = curLockName;
|
|
|
|
unlock();
|
|
|
|
if (lock(tmp))
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qCritical() << "assertLock: Lock file was lost, but could be restored";
|
2015-04-25 03:05:19 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qCritical() << "assertLock: Lock file was lost, and could *NOT* be restored";
|
2015-04-25 03:05:19 +08:00
|
|
|
deathByBrokenLock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileLocker::deathByBrokenLock()
|
|
|
|
{
|
2015-05-11 20:54:03 +08:00
|
|
|
qCritical() << "Lock is *BROKEN*, exiting immediately";
|
2015-04-25 03:05:19 +08:00
|
|
|
abort();
|
|
|
|
}
|