mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2025-01-06 15:04:34 +08:00
Merge pull request #15648 from Chocobo1/lockfile
Create lock file in config folder instead of temp folder
This commit is contained in:
commit
49976bcd83
@ -154,17 +154,11 @@ Application::Application(int &argc, char **argv)
|
|||||||
const QString profileDir = portableModeEnabled
|
const QString profileDir = portableModeEnabled
|
||||||
? QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(DEFAULT_PORTABLE_MODE_PROFILE_DIR)
|
? QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(DEFAULT_PORTABLE_MODE_PROFILE_DIR)
|
||||||
: m_commandLineArgs.profileDir;
|
: m_commandLineArgs.profileDir;
|
||||||
#ifdef Q_OS_WIN
|
|
||||||
const QString instanceId = (profileDir + (m_commandLineArgs.configurationName.isEmpty() ? QString {} : ('/' + m_commandLineArgs.configurationName))).toLower();
|
|
||||||
#else
|
|
||||||
const QString instanceId = profileDir + (m_commandLineArgs.configurationName.isEmpty() ? QString {} : ('/' + m_commandLineArgs.configurationName));
|
|
||||||
#endif
|
|
||||||
const QString appId = QLatin1String("qBittorrent-") + Utils::Misc::getUserIDString() + '@' + instanceId;
|
|
||||||
m_instanceManager = new ApplicationInstanceManager {appId, this};
|
|
||||||
|
|
||||||
Profile::initInstance(profileDir, m_commandLineArgs.configurationName,
|
Profile::initInstance(profileDir, m_commandLineArgs.configurationName,
|
||||||
(m_commandLineArgs.relativeFastresumePaths || portableModeEnabled));
|
(m_commandLineArgs.relativeFastresumePaths || portableModeEnabled));
|
||||||
|
|
||||||
|
m_instanceManager = new ApplicationInstanceManager {Profile::instance()->location(SpecialFolder::Config), this};
|
||||||
|
|
||||||
Logger::initInstance();
|
Logger::initInstance();
|
||||||
SettingsStorage::initInstance();
|
SettingsStorage::initInstance();
|
||||||
Preferences::initInstance();
|
Preferences::initInstance();
|
||||||
|
@ -28,24 +28,27 @@
|
|||||||
|
|
||||||
#include "applicationinstancemanager.h"
|
#include "applicationinstancemanager.h"
|
||||||
|
|
||||||
|
#include <QtGlobal>
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QSharedMemory>
|
#include <QSharedMemory>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "qtlocalpeer/qtlocalpeer.h"
|
#include "qtlocalpeer/qtlocalpeer.h"
|
||||||
|
|
||||||
ApplicationInstanceManager::ApplicationInstanceManager(const QString &appId, QObject *parent)
|
ApplicationInstanceManager::ApplicationInstanceManager(const QString &instancePath, QObject *parent)
|
||||||
: QObject {parent}
|
: QObject {parent}
|
||||||
, m_peer {new QtLocalPeer {this, appId}}
|
, m_peer {new QtLocalPeer {instancePath, this}}
|
||||||
, m_isFirstInstance {!m_peer->isClient()}
|
, m_isFirstInstance {!m_peer->isClient()}
|
||||||
{
|
{
|
||||||
connect(m_peer, &QtLocalPeer::messageReceived, this, &ApplicationInstanceManager::messageReceived);
|
connect(m_peer, &QtLocalPeer::messageReceived, this, &ApplicationInstanceManager::messageReceived);
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
auto sharedMem = new QSharedMemory {appId + QLatin1String {"-shared-memory-key"}, this};
|
const QString sharedMemoryKey = instancePath + QLatin1String {"/shared-memory"};
|
||||||
|
auto sharedMem = new QSharedMemory {sharedMemoryKey, this};
|
||||||
if (m_isFirstInstance)
|
if (m_isFirstInstance)
|
||||||
{
|
{
|
||||||
// First instance creates shared memory and store PID
|
// First instance creates shared memory and store PID
|
||||||
@ -79,8 +82,3 @@ bool ApplicationInstanceManager::sendMessage(const QString &message, const int t
|
|||||||
{
|
{
|
||||||
return m_peer->sendMessage(message, timeout);
|
return m_peer->sendMessage(message, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString ApplicationInstanceManager::appId() const
|
|
||||||
{
|
|
||||||
return m_peer->applicationId();
|
|
||||||
}
|
|
||||||
|
@ -32,16 +32,15 @@
|
|||||||
|
|
||||||
class QtLocalPeer;
|
class QtLocalPeer;
|
||||||
|
|
||||||
class ApplicationInstanceManager : public QObject
|
class ApplicationInstanceManager final : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_DISABLE_COPY_MOVE(ApplicationInstanceManager)
|
Q_DISABLE_COPY_MOVE(ApplicationInstanceManager)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ApplicationInstanceManager(const QString &appId, QObject *parent = nullptr);
|
explicit ApplicationInstanceManager(const QString &instancePath, QObject *parent = nullptr);
|
||||||
|
|
||||||
bool isFirstInstance() const;
|
bool isFirstInstance() const;
|
||||||
QString appId() const;
|
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
bool sendMessage(const QString &message, int timeout = 5000);
|
bool sendMessage(const QString &message, int timeout = 5000);
|
||||||
|
@ -68,20 +68,16 @@
|
|||||||
|
|
||||||
#include "qtlocalpeer.h"
|
#include "qtlocalpeer.h"
|
||||||
|
|
||||||
#if defined(Q_OS_UNIX)
|
#include <QtGlobal>
|
||||||
#include <sys/types.h>
|
|
||||||
#include <time.h>
|
#if defined(Q_OS_WIN)
|
||||||
#include <unistd.h>
|
#include <Windows.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <QCoreApplication>
|
|
||||||
#include <QDataStream>
|
#include <QDataStream>
|
||||||
#include <QDir>
|
#include <QFileInfo>
|
||||||
#include <QLocalServer>
|
#include <QLocalServer>
|
||||||
#include <QLocalSocket>
|
#include <QLocalSocket>
|
||||||
#include <QRegularExpression>
|
|
||||||
|
|
||||||
#include "base/utils/misc.h"
|
|
||||||
|
|
||||||
namespace QtLP_Private
|
namespace QtLP_Private
|
||||||
{
|
{
|
||||||
@ -96,41 +92,14 @@ namespace QtLP_Private
|
|||||||
|
|
||||||
const char* QtLocalPeer::ack = "ack";
|
const char* QtLocalPeer::ack = "ack";
|
||||||
|
|
||||||
QtLocalPeer::QtLocalPeer(QObject *parent, const QString &appId)
|
QtLocalPeer::QtLocalPeer(const QString &path, QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, id(appId)
|
, socketName(path + QLatin1String("/ipc-socket"))
|
||||||
|
, server(new QLocalServer(this))
|
||||||
{
|
{
|
||||||
QString prefix = id;
|
|
||||||
if (id.isEmpty())
|
|
||||||
{
|
|
||||||
id = QCoreApplication::applicationFilePath();
|
|
||||||
#if defined(Q_OS_WIN)
|
|
||||||
id = id.toLower();
|
|
||||||
#endif
|
|
||||||
prefix = id.section(QLatin1Char('/'), -1);
|
|
||||||
}
|
|
||||||
prefix.remove(QRegularExpression("[^a-zA-Z]"));
|
|
||||||
prefix.truncate(6);
|
|
||||||
|
|
||||||
QByteArray idc = id.toUtf8();
|
|
||||||
quint16 idNum = qChecksum(idc.constData(), idc.size());
|
|
||||||
socketName = QLatin1String("qtsingleapp-") + prefix
|
|
||||||
+ QLatin1Char('-') + QString::number(idNum, 16);
|
|
||||||
|
|
||||||
#if defined(Q_OS_WIN)
|
|
||||||
DWORD sessionId = 0;
|
|
||||||
::ProcessIdToSessionId(GetCurrentProcessId(), &sessionId);
|
|
||||||
socketName += (QLatin1Char('-') + QString::number(sessionId, 16));
|
|
||||||
#else
|
|
||||||
socketName += (QLatin1Char('-') + QString::number(::getuid(), 16));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
server = new QLocalServer(this);
|
|
||||||
server->setSocketOptions(QLocalServer::UserAccessOption);
|
server->setSocketOptions(QLocalServer::UserAccessOption);
|
||||||
QString lockName = QDir(QDir::tempPath()).absolutePath()
|
|
||||||
+ QLatin1Char('/') + socketName
|
lockFile.setFileName(path + QLatin1String("/lockfile"));
|
||||||
+ QLatin1String("-lockfile");
|
|
||||||
lockFile.setFileName(lockName);
|
|
||||||
lockFile.open(QIODevice::ReadWrite);
|
lockFile.open(QIODevice::ReadWrite);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,12 +125,13 @@ bool QtLocalPeer::isClient()
|
|||||||
// ### Workaround
|
// ### Workaround
|
||||||
if (!res && server->serverError() == QAbstractSocket::AddressInUseError)
|
if (!res && server->serverError() == QAbstractSocket::AddressInUseError)
|
||||||
{
|
{
|
||||||
QFile::remove(QDir::cleanPath(QDir::tempPath())+QLatin1Char('/')+socketName);
|
QFile::remove(socketName);
|
||||||
res = server->listen(socketName);
|
res = server->listen(socketName);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if (!res)
|
if (!res)
|
||||||
qWarning("QtSingleCoreApplication: listen on local socket failed, %s", qPrintable(server->errorString()));
|
qWarning("QtSingleCoreApplication: listen on local socket failed, %s", qPrintable(server->errorString()));
|
||||||
|
|
||||||
connect(server, &QLocalServer::newConnection, this, &QtLocalPeer::receiveConnection);
|
connect(server, &QLocalServer::newConnection, this, &QtLocalPeer::receiveConnection);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -204,11 +174,6 @@ bool QtLocalPeer::sendMessage(const QString &message, const int timeout)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QtLocalPeer::applicationId() const
|
|
||||||
{
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
void QtLocalPeer::receiveConnection()
|
void QtLocalPeer::receiveConnection()
|
||||||
{
|
{
|
||||||
QLocalSocket* socket = server->nextPendingConnection();
|
QLocalSocket* socket = server->nextPendingConnection();
|
||||||
|
@ -68,21 +68,23 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
#include "qtlockedfile.h"
|
#include "qtlockedfile.h"
|
||||||
|
|
||||||
class QLocalServer;
|
class QLocalServer;
|
||||||
|
|
||||||
class QtLocalPeer : public QObject
|
class QtLocalPeer final : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
Q_DISABLE_COPY_MOVE(QtLocalPeer)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
QtLocalPeer(QObject *parent = nullptr, const QString &appId = QString());
|
QtLocalPeer(const QString &path, QObject *parent = nullptr);
|
||||||
~QtLocalPeer() override;
|
~QtLocalPeer() override;
|
||||||
|
|
||||||
bool isClient();
|
bool isClient();
|
||||||
bool sendMessage(const QString &message, int timeout);
|
bool sendMessage(const QString &message, int timeout);
|
||||||
QString applicationId() const;
|
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void messageReceived(const QString &message);
|
void messageReceived(const QString &message);
|
||||||
|
@ -71,6 +71,7 @@
|
|||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
|
#include <QString>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -169,9 +169,9 @@ SettingsPtr Private::CustomProfile::applicationSettings(const QString &name) con
|
|||||||
{
|
{
|
||||||
// here we force QSettings::IniFormat format always because we need it to be portable across platforms
|
// here we force QSettings::IniFormat format always because we need it to be portable across platforms
|
||||||
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
|
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
|
||||||
constexpr const char *CONF_FILE_EXTENSION = ".ini";
|
const char CONF_FILE_EXTENSION[] = ".ini";
|
||||||
#else
|
#else
|
||||||
constexpr const char *CONF_FILE_EXTENSION = ".conf";
|
const char CONF_FILE_EXTENSION[] = ".conf";
|
||||||
#endif
|
#endif
|
||||||
const QString settingsFileName {QDir(configLocation()).absoluteFilePath(name + QLatin1String(CONF_FILE_EXTENSION))};
|
const QString settingsFileName {QDir(configLocation()).absoluteFilePath(name + QLatin1String(CONF_FILE_EXTENSION))};
|
||||||
return SettingsPtr(new QSettings(settingsFileName, QSettings::IniFormat));
|
return SettingsPtr(new QSettings(settingsFileName, QSettings::IniFormat));
|
||||||
|
Loading…
Reference in New Issue
Block a user