make use of QLockFile

use_qt_lockfile
Bret Curtis 1 year ago
parent 406eb1f9cc
commit a95eddd466

@ -1,6 +1,7 @@
#include "editor.hpp"
#include <QApplication>
#include <QFileInfo>
#include <QLocalServer>
#include <QLocalSocket>
#include <QMessageBox>
@ -22,6 +23,8 @@
#ifdef _WIN32
#include <components/windows.hpp>
#endif
#include <QLockFile>
#include <components/debug/debugging.hpp>
#include <components/debug/debuglog.hpp>
#include <components/esm3/esmreader.hpp>
@ -42,8 +45,8 @@ CS::Editor::Editor(int argc, char** argv)
: mConfigVariables(readConfiguration())
, mSettingsState(mCfgMgr)
, mDocumentManager(mCfgMgr)
, mPid("")
, mLock()
, mPid(std::filesystem::temp_directory_path() /= "openmw-cs.pid")
, mLockFile(QFileInfo(mPid.c_str()).absoluteFilePath() + ".lock")
, mMerge(mDocumentManager)
, mIpcServerName("org.openmw.OpenCS")
, mServer(nullptr)
@ -95,8 +98,11 @@ CS::Editor::~Editor()
{
delete mViewManager;
QLockFile lock = QLockFile(QFileInfo(mPid.c_str()).absoluteFilePath() + ".lock");
lock.unlock();
mPidFile.close();
if (mServer && std::filesystem::exists(mPid))
std::filesystem::remove(mPid);
}
@ -336,14 +342,11 @@ bool CS::Editor::makeIPCServer()
{
try
{
mPid = std::filesystem::temp_directory_path();
mPid /= "openmw-cs.pid";
bool pidExists = std::filesystem::exists(mPid);
mPidFile.open(mPid);
mLock = boost::interprocess::file_lock(mPid.c_str());
if (!mLock.try_lock())
if (!mLockFile.tryLock())
{
Log(Debug::Error) << "Error: OpenMW-CS is already running.";
return false;

@ -1,9 +1,9 @@
#ifndef CS_EDITOR_H
#define CS_EDITOR_H
#include <boost/interprocess/sync/file_lock.hpp>
#include <boost/program_options/variables_map.hpp>
#include <QLockFile>
#include <QObject>
#include <QString>
@ -58,7 +58,7 @@ namespace CS
std::filesystem::path mLocal;
std::filesystem::path mResources;
std::filesystem::path mPid;
boost::interprocess::file_lock mLock;
QLockFile mLockFile;
std::ofstream mPidFile;
bool mFsStrict;
CSVTools::Merge mMerge;

Loading…
Cancel
Save