summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/io/qlockfile.cpp13
-rw-r--r--src/corelib/io/qlockfile_p.h5
-rw-r--r--tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp34
3 files changed, 32 insertions, 20 deletions
diff --git a/src/corelib/io/qlockfile.cpp b/src/corelib/io/qlockfile.cpp
index fef20455732..908db7b9d38 100644
--- a/src/corelib/io/qlockfile.cpp
+++ b/src/corelib/io/qlockfile.cpp
@@ -458,18 +458,7 @@ bool QLockFilePrivate::isApparentlyStale() const
return staleLockTime > 0ms && abs(age) > staleLockTime;
}
-int QLockFilePrivate::getLockFileHandle(QLockFile *f)
-{
- int fd;
-#ifdef Q_OS_WIN
- // Use of this function on Windows WILL leak a file descriptor.
- fd = _open_osfhandle(intptr_t(f->d_func()->fileHandle), 0);
-#else
- fd = f->d_func()->fileHandle;
-#endif
- QT_LSEEK(fd, 0, SEEK_SET);
- return fd;
-}
+
/*!
Attempts to forcefully remove an existing lock file.
diff --git a/src/corelib/io/qlockfile_p.h b/src/corelib/io/qlockfile_p.h
index 8758adfb71e..2a7ebe1926d 100644
--- a/src/corelib/io/qlockfile_p.h
+++ b/src/corelib/io/qlockfile_p.h
@@ -52,7 +52,10 @@ public:
bool isLocked = false;
// used in tst_QLockFile:
- Q_CORE_EXPORT static int getLockFileHandle(QLockFile *f);
+ static auto getLockFileHandle(QLockFile *f)
+ {
+ return f->d_func()->fileHandle;
+ }
};
QT_END_NAMESPACE
diff --git a/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp b/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp
index 48c90e263bd..bec14851dd4 100644
--- a/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp
+++ b/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp
@@ -11,8 +11,10 @@
#include <QFutureSynchronizer>
#include <qlockfile.h>
-#include <qtemporarydir.h>
#include <qsysinfo.h>
+#include <qplatformdefs.h>
+#include <qtemporarydir.h>
+
#if defined(Q_OS_UNIX) && !defined(Q_OS_VXWORKS)
#include <unistd.h>
@@ -544,6 +546,28 @@ void tst_QLockFile::corruptedLockFileInTheFuture()
#endif
}
+static bool openLockFile(QFile *f, QLockFile *lockfile)
+{
+ int fd;
+ QFile::FileHandleFlags flags = {};
+#ifdef Q_OS_WIN
+ // Since _open_osfhandle() takes ownership of the handle, we need to first
+ // duplicate the HANDLE from QLockFilePrivate.
+ HANDLE h = QLockFilePrivate::getLockFileHandle(lockfile);
+ bool bInheritHandle = false;
+ DWORD dwDesiredAccess = 0;
+ if (!DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), &h, dwDesiredAccess,
+ bInheritHandle, DUPLICATE_SAME_ACCESS))
+ return false;
+ fd = _open_osfhandle(intptr_t(h), 0);
+ flags = QFile::AutoCloseHandle;
+#else
+ fd = QLockFilePrivate::getLockFileHandle(lockfile);
+#endif
+ QT_LSEEK(fd, 0, SEEK_SET);
+ return f->open(fd, QIODevice::ReadWrite | QIODevice::Text, flags);
+}
+
void tst_QLockFile::hostnameChange()
{
const QByteArray hostid = QSysInfo::machineUniqueId();
@@ -557,9 +581,7 @@ void tst_QLockFile::hostnameChange()
{
// now modify it
QFile f;
- QVERIFY(f.open(QLockFilePrivate::getLockFileHandle(&lock1),
- QIODevice::ReadWrite | QIODevice::Text,
- QFile::DontCloseHandle));
+ QVERIFY(openLockFile(&f, &lock1));
QVERIFY(overwriteLineInLockFile(f, 3, "this is not a hostname"));
}
@@ -583,9 +605,7 @@ void tst_QLockFile::differentMachines()
{
// now modify it
QFile f;
- QVERIFY(f.open(QLockFilePrivate::getLockFileHandle(&lock1),
- QIODevice::ReadWrite | QIODevice::Text,
- QFile::DontCloseHandle));
+ QVERIFY(openLockFile(&f, &lock1));
QVERIFY(overwriteLineInLockFile(f, 1, QT_STRINGIFY(INT_MAX)));
QVERIFY(overwriteLineInLockFile(f, 4, "this is not a UUID"));
}