2015-06-06 09:40:08 +08:00
|
|
|
/*
|
2018-04-13 04:02:28 +08:00
|
|
|
Copyright © 2015-2018 by The qTox Project Contributors
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2015-04-25 00:53:19 +08:00
|
|
|
#include "profilelocker.h"
|
2016-12-19 10:26:26 +08:00
|
|
|
#include "src/persistence/settings.h"
|
2015-04-25 00:53:19 +08:00
|
|
|
#include <QDebug>
|
2017-02-26 19:52:45 +08:00
|
|
|
#include <QDir>
|
2015-04-25 00:53:19 +08:00
|
|
|
|
2016-07-27 06:20:54 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @class ProfileLocker
|
|
|
|
* @brief Locks a Tox profile so that multiple instances can not use the same profile.
|
|
|
|
* Only one lock can be acquired at the same time, which means
|
|
|
|
* that there is little need for manually unlocking.
|
|
|
|
* The current lock will expire if you exit or acquire a new one.
|
|
|
|
*/
|
2016-07-27 06:20:54 +08:00
|
|
|
|
2015-04-25 00:53:19 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
unique_ptr<QLockFile> ProfileLocker::lockfile;
|
|
|
|
QString ProfileLocker::curLockName;
|
|
|
|
|
|
|
|
QString ProfileLocker::lockPathFromName(const QString& name)
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
return Settings::getInstance().getSettingsDirPath() + '/' + name + ".lock";
|
2015-04-25 00:53:19 +08:00
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:54 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Checks if a profile is currently locked by *another* instance.
|
|
|
|
* If we own the lock, we consider it lockable.
|
|
|
|
* There is no guarantee that the result will still be valid by the
|
|
|
|
* time it is returned, this is provided on a best effort basis.
|
|
|
|
* @param profile Profile name to check.
|
|
|
|
* @return True, if profile locked, false otherwise.
|
|
|
|
*/
|
2015-04-25 00:53:19 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:54 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Tries to acquire the lock on a profile, will not block.
|
|
|
|
* @param profile Profile to lock.
|
|
|
|
* @return Returns true if we already own the lock.
|
|
|
|
*/
|
2015-04-25 00:53:19 +08:00
|
|
|
bool ProfileLocker::lock(QString profile)
|
|
|
|
{
|
|
|
|
if (lockfile && curLockName == profile)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
QLockFile* newLock = new QLockFile(lockPathFromName(profile));
|
2015-09-03 23:24:33 +08:00
|
|
|
newLock->setStaleLockTime(0);
|
2017-02-26 19:52:45 +08:00
|
|
|
if (!newLock->tryLock()) {
|
2015-04-25 00:53:19 +08:00
|
|
|
delete newLock;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock();
|
|
|
|
lockfile.reset(newLock);
|
|
|
|
curLockName = profile;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:54 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Releases the lock on the current profile.
|
|
|
|
*/
|
2015-04-25 00:53:19 +08:00
|
|
|
void ProfileLocker::unlock()
|
|
|
|
{
|
|
|
|
if (!lockfile)
|
|
|
|
return;
|
2016-07-27 06:20:54 +08:00
|
|
|
|
2015-04-25 00:53:19 +08:00
|
|
|
lockfile->unlock();
|
|
|
|
delete lockfile.release();
|
|
|
|
lockfile = nullptr;
|
|
|
|
curLockName.clear();
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:54 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief 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 first place, exit immediately.
|
|
|
|
*/
|
2015-04-25 03:05:19 +08:00
|
|
|
void ProfileLocker::assertLock()
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
if (!QFile(lockPathFromName(curLockName)).exists()) {
|
2015-04-25 03:05:19 +08:00
|
|
|
QString tmp = curLockName;
|
|
|
|
unlock();
|
2017-02-26 19:52:45 +08:00
|
|
|
if (lock(tmp)) {
|
2015-05-11 20:54:03 +08:00
|
|
|
qCritical() << "assertLock: Lock file was lost, but could be restored";
|
2017-02-26 19:52:45 +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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:54 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Print an error then exit immediately.
|
|
|
|
*/
|
2015-04-25 03:05:19 +08:00
|
|
|
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();
|
|
|
|
}
|
2015-06-04 07:30:17 +08:00
|
|
|
|
2016-07-27 06:20:54 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Chacks, that profile locked.
|
|
|
|
* @return Returns true if we're currently holding a lock.
|
|
|
|
*/
|
2015-06-04 07:30:17 +08:00
|
|
|
bool ProfileLocker::hasLock()
|
|
|
|
{
|
|
|
|
return lockfile.operator bool();
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:54 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Get current locked profile name.
|
|
|
|
* @return Return the name of the currently loaded profile, a null string if there is none.
|
|
|
|
*/
|
2015-06-04 07:30:17 +08:00
|
|
|
QString ProfileLocker::getCurLockName()
|
|
|
|
{
|
|
|
|
if (lockfile)
|
|
|
|
return curLockName;
|
|
|
|
else
|
|
|
|
return QString();
|
|
|
|
}
|