diff options
author | kh1 <karsten.heimrich@digia.com> | 2014-11-11 11:30:36 +0100 |
---|---|---|
committer | Karsten Heimrich <karsten.heimrich@theqtcompany.com> | 2014-11-12 21:22:26 +0100 |
commit | ca0369791c672b72cf47974ba3e3a4e0b81cce28 (patch) | |
tree | 08507805e3e7f9090efb1c36a2683797ee40ed37 /src/libs/kdtools | |
parent | 11964021d9f261fe2d04a41d41c3ad0c5f4a945f (diff) |
Adjust implementation to match the Unix one and fix unlock.
Add error messages. Fix Unlock, we did use the right argument
but passed on the wrong position(was index 5, should be at 4).
Change-Id: I12564f64997a311a504eaa9b7dd21e75ded8db87
Reviewed-by: Niels Weber <niels.weber@digia.com>
Reviewed-by: Kai Koehne <kai.koehne@theqtcompany.com>
Diffstat (limited to 'src/libs/kdtools')
-rw-r--r-- | src/libs/kdtools/kdlockfile_win.cpp | 54 |
1 files changed, 35 insertions, 19 deletions
diff --git a/src/libs/kdtools/kdlockfile_win.cpp b/src/libs/kdtools/kdlockfile_win.cpp index c9ace2de8..9a24e19fa 100644 --- a/src/libs/kdtools/kdlockfile_win.cpp +++ b/src/libs/kdtools/kdlockfile_win.cpp @@ -36,41 +36,57 @@ #include "kdlockfile.h" #include "kdlockfile_p.h" +#include <utils.h> + #include <QCoreApplication> #include <QFileInfo> bool KDLockFile::Private::lock() { - const QFileInfo fi(filename); - handle = CreateFile(filename.toStdWString().data(), - GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, - NULL, fi.exists() ? OPEN_EXISTING : CREATE_NEW, - FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL); + if (locked) + return locked; + + errorString.clear(); + handle = CreateFile(filename.toStdWString().data(), GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ, NULL, QFileInfo(filename).exists() ? OPEN_EXISTING : CREATE_NEW, + FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL); - if (!handle) + if (handle == INVALID_HANDLE_VALUE) { + errorString = QCoreApplication::translate("KDLockFile", "Could not create lock file '%1': " + "%2").arg(filename, QInstaller::windowsErrorString(GetLastError())); return false; - QString pid = QString::number(qApp->applicationPid()); - QByteArray data = pid.toLatin1(); + } + DWORD bytesWritten; - const bool wrotePid = WriteFile(handle, data.data(), data.size(), &bytesWritten, NULL); - if (!wrotePid) + const QByteArray pid = QString::number(QCoreApplication::applicationPid()).toLatin1(); + if (!WriteFile(handle, pid.data(), pid.size(), &bytesWritten, NULL)) { + errorString = QCoreApplication::translate("KDLockFile", "Could not write PID to lock file " + "'%1': %2").arg(filename, QInstaller::windowsErrorString(GetLastError())); return false; + } FlushFileBuffers(handle); - const bool locked = LockFile(handle, 0, 0, fi.size(), 0); - - this->locked = locked; + if (!LockFile(handle, 0, 0, QFileInfo(filename).size(), 0)) { + errorString = QCoreApplication::translate("KDLockFile", "Could not obtain the lock for " + "file' %1': %2").arg(filename, QInstaller::windowsErrorString(GetLastError())); + } else { + locked = true; + } return locked; } bool KDLockFile::Private::unlock() { - const QFileInfo fi(filename); - if (locked) { - const bool success = UnlockFile(handle, 0, 0, 0, fi.size()); - this->locked = !success; + errorString.clear(); + if (!locked) + return true; + + if (!UnlockFile(handle, 0, 0, QFileInfo(filename).size(), 0)) { + errorString = QCoreApplication::translate("KDLockFile", "Could not release the lock for " + "file '%1': %2").arg(filename, QInstaller::windowsErrorString(GetLastError())); + } else { + locked = false; CloseHandle(handle); - return success; } - return true; + return !locked; } |