qtlockedfile_win.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of Qt Creator.
  7. **
  8. ** Commercial License Usage
  9. ** Licensees holding valid commercial Qt licenses may use this file in
  10. ** accordance with the commercial license agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and The Qt Company. For licensing terms
  13. ** and conditions see https://www.qt.io/terms-conditions. For further
  14. ** information use the contact form at https://www.qt.io/contact-us.
  15. **
  16. ** GNU General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU
  18. ** General Public License version 3 as published by the Free Software
  19. ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
  20. ** included in the packaging of this file. Please review the following
  21. ** information to ensure the GNU General Public License requirements will
  22. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  23. **
  24. ****************************************************************************/
  25. #include "qtlockedfile.h"
  26. #include <qt_windows.h>
  27. #include <QFileInfo>
  28. namespace SharedTools {
  29. #define SEMAPHORE_PREFIX "QtLockedFile semaphore "
  30. #define MUTEX_PREFIX "QtLockedFile mutex "
  31. #define SEMAPHORE_MAX 100
  32. static QString errorCodeToString(DWORD errorCode)
  33. {
  34. QString result;
  35. char *data = 0;
  36. FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  37. 0, errorCode, 0,
  38. (char*)&data, 0, 0);
  39. result = QString::fromLocal8Bit(data);
  40. if (data != 0)
  41. LocalFree(data);
  42. if (result.endsWith(QLatin1Char('\n')))
  43. result.truncate(result.length() - 1);
  44. return result;
  45. }
  46. bool QtLockedFile::lock(LockMode mode, bool block)
  47. {
  48. if (!isOpen()) {
  49. qWarning("QtLockedFile::lock(): file is not opened");
  50. return false;
  51. }
  52. if (mode == m_lock_mode)
  53. return true;
  54. if (m_lock_mode != 0)
  55. unlock();
  56. if (m_semaphore_hnd == 0) {
  57. QFileInfo fi(*this);
  58. QString sem_name = QString::fromLatin1(SEMAPHORE_PREFIX)
  59. + fi.absoluteFilePath().toLower();
  60. m_semaphore_hnd = CreateSemaphoreW(0, SEMAPHORE_MAX, SEMAPHORE_MAX,
  61. (TCHAR*)sem_name.utf16());
  62. if (m_semaphore_hnd == 0) {
  63. qWarning("QtLockedFile::lock(): CreateSemaphore: %s",
  64. errorCodeToString(GetLastError()).toLatin1().constData());
  65. return false;
  66. }
  67. }
  68. bool gotMutex = false;
  69. int decrement;
  70. if (mode == ReadLock) {
  71. decrement = 1;
  72. } else {
  73. decrement = SEMAPHORE_MAX;
  74. if (m_mutex_hnd == 0) {
  75. QFileInfo fi(*this);
  76. QString mut_name = QString::fromLatin1(MUTEX_PREFIX)
  77. + fi.absoluteFilePath().toLower();
  78. m_mutex_hnd = CreateMutexW(NULL, FALSE, (TCHAR*)mut_name.utf16());
  79. if (m_mutex_hnd == 0) {
  80. qWarning("QtLockedFile::lock(): CreateMutex: %s",
  81. errorCodeToString(GetLastError()).toLatin1().constData());
  82. return false;
  83. }
  84. }
  85. DWORD res = WaitForSingleObject(m_mutex_hnd, block ? INFINITE : 0);
  86. if (res == WAIT_TIMEOUT)
  87. return false;
  88. if (res == WAIT_FAILED) {
  89. qWarning("QtLockedFile::lock(): WaitForSingleObject (mutex): %s",
  90. errorCodeToString(GetLastError()).toLatin1().constData());
  91. return false;
  92. }
  93. gotMutex = true;
  94. }
  95. for (int i = 0; i < decrement; ++i) {
  96. DWORD res = WaitForSingleObject(m_semaphore_hnd, block ? INFINITE : 0);
  97. if (res == WAIT_TIMEOUT) {
  98. if (i) {
  99. // A failed nonblocking rw locking. Undo changes to semaphore.
  100. if (ReleaseSemaphore(m_semaphore_hnd, i, NULL) == 0) {
  101. qWarning("QtLockedFile::unlock(): ReleaseSemaphore: %s",
  102. errorCodeToString(GetLastError()).toLatin1().constData());
  103. // Fall through
  104. }
  105. }
  106. if (gotMutex)
  107. ReleaseMutex(m_mutex_hnd);
  108. return false;
  109. }
  110. if (res != WAIT_OBJECT_0) {
  111. if (gotMutex)
  112. ReleaseMutex(m_mutex_hnd);
  113. qWarning("QtLockedFile::lock(): WaitForSingleObject (semaphore): %s",
  114. errorCodeToString(GetLastError()).toLatin1().constData());
  115. return false;
  116. }
  117. }
  118. m_lock_mode = mode;
  119. if (gotMutex)
  120. ReleaseMutex(m_mutex_hnd);
  121. return true;
  122. }
  123. bool QtLockedFile::unlock()
  124. {
  125. if (!isOpen()) {
  126. qWarning("QtLockedFile::unlock(): file is not opened");
  127. return false;
  128. }
  129. if (!isLocked())
  130. return true;
  131. int increment;
  132. if (m_lock_mode == ReadLock)
  133. increment = 1;
  134. else
  135. increment = SEMAPHORE_MAX;
  136. DWORD ret = ReleaseSemaphore(m_semaphore_hnd, increment, 0);
  137. if (ret == 0) {
  138. qWarning("QtLockedFile::unlock(): ReleaseSemaphore: %s",
  139. errorCodeToString(GetLastError()).toLatin1().constData());
  140. return false;
  141. }
  142. m_lock_mode = QtLockedFile::NoLock;
  143. remove();
  144. return true;
  145. }
  146. QtLockedFile::~QtLockedFile()
  147. {
  148. if (isOpen())
  149. unlock();
  150. if (m_mutex_hnd != 0) {
  151. DWORD ret = CloseHandle(m_mutex_hnd);
  152. if (ret == 0) {
  153. qWarning("QtLockedFile::~QtLockedFile(): CloseHandle (mutex): %s",
  154. errorCodeToString(GetLastError()).toLatin1().constData());
  155. }
  156. m_mutex_hnd = 0;
  157. }
  158. if (m_semaphore_hnd != 0) {
  159. DWORD ret = CloseHandle(m_semaphore_hnd);
  160. if (ret == 0) {
  161. qWarning("QtLockedFile::~QtLockedFile(): CloseHandle (semaphore): %s",
  162. errorCodeToString(GetLastError()).toLatin1().constData());
  163. }
  164. m_semaphore_hnd = 0;
  165. }
  166. }
  167. } // namespace SharedTools