QLockFile/Windows: Determine host name from environment variable COMPUTERNAME.

Useful when using shared directories.

Task-number: QTBUG-39967
Change-Id: I2c082e33133b00306378b6ff58478e94119e6a0e
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Friedemann Kleint 2014-07-23 12:00:14 +02:00
parent 7aae6219d5
commit 3770f4b148

View File

@ -50,6 +50,11 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
static inline QByteArray localHostName()
{
return qgetenv("COMPUTERNAME");
}
QLockFile::LockError QLockFilePrivate::tryLock_sys() QLockFile::LockError QLockFilePrivate::tryLock_sys()
{ {
const QFileSystemEntry fileEntry(fileName); const QFileSystemEntry fileEntry(fileName);
@ -98,7 +103,7 @@ QLockFile::LockError QLockFilePrivate::tryLock_sys()
fileData += '\n'; fileData += '\n';
fileData += qAppName().toUtf8(); fileData += qAppName().toUtf8();
fileData += '\n'; fileData += '\n';
//fileData += localHostname(); // gethostname requires winsock init, see QHostInfo... fileData += localHostName();
fileData += '\n'; fileData += '\n';
DWORD bytesWritten = 0; DWORD bytesWritten = 0;
QLockFile::LockError error = QLockFile::NoError; QLockFile::LockError error = QLockFile::NoError;
@ -123,14 +128,16 @@ bool QLockFilePrivate::isApparentlyStale() const
// On WinRT there seems to be no way of obtaining information about other // On WinRT there seems to be no way of obtaining information about other
// processes due to sandboxing // processes due to sandboxing
#ifndef Q_OS_WINRT #ifndef Q_OS_WINRT
HANDLE procHandle = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid); if (hostname == QString::fromLocal8Bit(localHostName())) {
if (!procHandle) HANDLE procHandle = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
return true; if (!procHandle)
// We got a handle but check if process is still alive return true;
DWORD dwR = ::WaitForSingleObject(procHandle, 0); // We got a handle but check if process is still alive
::CloseHandle(procHandle); DWORD dwR = ::WaitForSingleObject(procHandle, 0);
if (dwR == WAIT_TIMEOUT) ::CloseHandle(procHandle);
return true; if (dwR == WAIT_TIMEOUT)
return true;
}
#endif // !Q_OS_WINRT #endif // !Q_OS_WINRT
const qint64 age = QFileInfo(fileName).lastModified().msecsTo(QDateTime::currentDateTime()); const qint64 age = QFileInfo(fileName).lastModified().msecsTo(QDateTime::currentDateTime());
return staleLockTime > 0 && age > staleLockTime; return staleLockTime > 0 && age > staleLockTime;