diff options
Diffstat (limited to 'src/corelib/io/qlockfile_win.cpp')
-rw-r--r-- | src/corelib/io/qlockfile_win.cpp | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/src/corelib/io/qlockfile_win.cpp b/src/corelib/io/qlockfile_win.cpp index 28f6b02a64..5f82270706 100644 --- a/src/corelib/io/qlockfile_win.cpp +++ b/src/corelib/io/qlockfile_win.cpp @@ -47,9 +47,15 @@ #include "QtCore/qfileinfo.h" #include "QtCore/qdatetime.h" #include "QtCore/qdebug.h" +#include "QtCore/qthread.h" QT_BEGIN_NAMESPACE +static inline QByteArray localHostName() +{ + return qgetenv("COMPUTERNAME"); +} + QLockFile::LockError QLockFilePrivate::tryLock_sys() { const QFileSystemEntry fileEntry(fileName); @@ -98,7 +104,7 @@ QLockFile::LockError QLockFilePrivate::tryLock_sys() fileData += '\n'; fileData += qAppName().toUtf8(); fileData += '\n'; - //fileData += localHostname(); // gethostname requires winsock init, see QHostInfo... + fileData += localHostName(); fileData += '\n'; DWORD bytesWritten = 0; QLockFile::LockError error = QLockFile::NoError; @@ -123,14 +129,16 @@ bool QLockFilePrivate::isApparentlyStale() const // On WinRT there seems to be no way of obtaining information about other // processes due to sandboxing #ifndef Q_OS_WINRT - HANDLE procHandle = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid); - if (!procHandle) - return true; - // We got a handle but check if process is still alive - DWORD dwR = ::WaitForSingleObject(procHandle, 0); - ::CloseHandle(procHandle); - if (dwR == WAIT_TIMEOUT) - return true; + if (hostname == QString::fromLocal8Bit(localHostName())) { + HANDLE procHandle = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid); + if (!procHandle) + return true; + // We got a handle but check if process is still alive + DWORD dwR = ::WaitForSingleObject(procHandle, 0); + ::CloseHandle(procHandle); + if (dwR == WAIT_TIMEOUT) + return true; + } #endif // !Q_OS_WINRT const qint64 age = QFileInfo(fileName).lastModified().msecsTo(QDateTime::currentDateTime()); return staleLockTime > 0 && age > staleLockTime; @@ -142,7 +150,16 @@ void QLockFile::unlock() if (!d->isLocked) return; CloseHandle(d->fileHandle); - QFile::remove(d->fileName); + int attempts = 0; + static const int maxAttempts = 500; // 500ms + while (!QFile::remove(d->fileName) && ++attempts < maxAttempts) { + // Someone is reading the lock file right now (on Windows this prevents deleting it). + QThread::msleep(1); + } + if (attempts == maxAttempts) { + qWarning() << "Could not remove our own lock file" << d->fileName << ". Either other users of the lock file are reading it constantly for 500 ms, or we (no longer) have permissions to delete the file"; + // This is bad because other users of this lock file will now have to wait for the stale-lock-timeout... + } d->lockError = QLockFile::NoError; d->isLocked = false; } |