summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMårten Nordheim <[email protected]>2025-10-16 15:54:02 +0200
committerMårten Nordheim <[email protected]>2025-10-20 14:31:26 +0000
commitb7be1a2ca8d4afb80cbe7910109cc680d65d05b7 (patch)
treeb8dab8f7e01788e9f622ee053b44573114e4f4ce
parentc5aa1f4f1eb4925d64670128887c38e67c2e1856 (diff)
QLockFile: refactor local helper into static member functions
To prepare for the Windows backend doing extra work. We want to open the file with FILE_SHARE_DELETE so that we can eventually create the file with the DELETE permission, enabling us to delete it without closing the handle first. Since we can't do that through QFile directly we need to open the file descriptor first and then pass that to QFile. Change-Id: I0baf0ee3089b489f3bd1e0259cc06195e9256647 Reviewed-by: Thiago Macieira <[email protected]>
-rw-r--r--src/corelib/io/qlockfile.cpp28
-rw-r--r--src/corelib/io/qlockfile_p.h12
-rw-r--r--src/corelib/io/qlockfile_unix.cpp5
-rw-r--r--src/corelib/io/qlockfile_win.cpp7
4 files changed, 34 insertions, 18 deletions
diff --git a/src/corelib/io/qlockfile.cpp b/src/corelib/io/qlockfile.cpp
index 908db7b9d38..47229c8e6a1 100644
--- a/src/corelib/io/qlockfile.cpp
+++ b/src/corelib/io/qlockfile.cpp
@@ -24,19 +24,6 @@ QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
-namespace {
-struct LockFileInfo
-{
- qint64 pid;
- QString appname;
- QString hostname;
- QByteArray hostid;
- QByteArray bootid;
-};
-}
-
-static bool getLockInfo_helper(const QString &fileName, LockFileInfo *info);
-
static QString machineName()
{
#ifdef Q_OS_WIN
@@ -364,8 +351,8 @@ bool QLockFile::tryLock(std::chrono::milliseconds timeout)
bool QLockFile::getLockInfo(qint64 *pid, QString *hostname, QString *appname) const
{
Q_D(const QLockFile);
- LockFileInfo info;
- if (!getLockInfo_helper(d->fileName, &info))
+ QLockFilePrivate::LockFileInfo info;
+ if (!QLockFilePrivate::getLockInfo_helper(d->fileName, &info))
return false;
if (pid)
*pid = info.pid;
@@ -399,11 +386,16 @@ QByteArray QLockFilePrivate::lockFileContents() const
% QSysInfo::bootUniqueId() % '\n';
}
-static bool getLockInfo_helper(const QString &fileName, LockFileInfo *info)
+bool QLockFilePrivate::getLockInfo_helper(const QString &fileName, LockFileInfo *info)
{
- QFile reader(fileName);
- if (!reader.open(QIODevice::ReadOnly | QIODevice::Text))
+ int fd = openNewFileDescriptor(fileName);
+ if (fd < 0)
+ return false;
+ QFile reader;
+ if (!reader.open(fd, QFile::ReadOnly | QFile::Text, QFile::AutoCloseHandle)) {
+ QT_CLOSE(fd);
return false;
+ }
QByteArray pidLine = reader.readLine();
pidLine.chop(1);
diff --git a/src/corelib/io/qlockfile_p.h b/src/corelib/io/qlockfile_p.h
index 2a7ebe1926d..ea9b29e9f57 100644
--- a/src/corelib/io/qlockfile_p.h
+++ b/src/corelib/io/qlockfile_p.h
@@ -25,6 +25,15 @@ QT_BEGIN_NAMESPACE
class QLockFilePrivate
{
public:
+ struct LockFileInfo
+ {
+ qint64 pid;
+ QString appname;
+ QString hostname;
+ QByteArray hostid;
+ QByteArray bootid;
+ };
+
explicit QLockFilePrivate(const QString &fn);
~QLockFilePrivate();
@@ -41,6 +50,9 @@ public:
QString fileName;
+ static bool getLockInfo_helper(const QString &fileName, LockFileInfo *info);
+ static int openNewFileDescriptor(const QString &fileName);
+
#ifdef Q_OS_WIN
Qt::HANDLE fileHandle;
#else
diff --git a/src/corelib/io/qlockfile_unix.cpp b/src/corelib/io/qlockfile_unix.cpp
index 87faac8b33d..34276373a1f 100644
--- a/src/corelib/io/qlockfile_unix.cpp
+++ b/src/corelib/io/qlockfile_unix.cpp
@@ -285,6 +285,11 @@ QString QLockFilePrivate::processNameByPid(qint64 pid)
#endif
}
+int QLockFilePrivate::openNewFileDescriptor(const QString &fileName)
+{
+ return QT_OPEN(fileName.toLocal8Bit().constData(), QT_OPEN_RDONLY);
+}
+
void QLockFile::unlock()
{
Q_D(QLockFile);
diff --git a/src/corelib/io/qlockfile_win.cpp b/src/corelib/io/qlockfile_win.cpp
index 12a668def0f..611f90679d5 100644
--- a/src/corelib/io/qlockfile_win.cpp
+++ b/src/corelib/io/qlockfile_win.cpp
@@ -16,6 +16,8 @@
#include <qt_windows.h>
#include <psapi.h>
+#include <io.h>
+#include <fcntl.h>
QT_BEGIN_NAMESPACE
@@ -142,6 +144,11 @@ QString QLockFilePrivate::processNameByPid(qint64 pid)
return name;
}
+int QLockFilePrivate::openNewFileDescriptor(const QString &fileName)
+{
+ return _open(fileName.toLocal8Bit().constData(), _O_RDONLY);
+}
+
void QLockFile::unlock()
{
Q_D(QLockFile);