Specify Unicode for resource block (#15370)
[qBittorrent.git] / src / app / qtlocalpeer / qtlockedfile_win.cpp
blob75848ad73131ecf19bfb592b1b00917ad2be2cc5
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2019 Mike Tzou (Chocobo1)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
28 ****************************************************************************
30 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
31 ** Contact: http://www.qt-project.org/legal
33 ** This file is part of the Qt Solutions component.
35 ** $QT_BEGIN_LICENSE:BSD$
36 ** You may use this file under the terms of the BSD license as follows:
38 ** "Redistribution and use in source and binary forms, with or without
39 ** modification, are permitted provided that the following conditions are
40 ** met:
41 ** * Redistributions of source code must retain the above copyright
42 ** notice, this list of conditions and the following disclaimer.
43 ** * Redistributions in binary form must reproduce the above copyright
44 ** notice, this list of conditions and the following disclaimer in
45 ** the documentation and/or other materials provided with the
46 ** distribution.
47 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
48 ** of its contributors may be used to endorse or promote products derived
49 ** from this software without specific prior written permission.
52 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
64 ** $QT_END_LICENSE$
66 ****************************************************************************
69 #include "qtlockedfile.h"
71 #include <QFileInfo>
73 #define MUTEX_PREFIX "QtLockedFile mutex "
74 // Maximum number of concurrent read locks. Must not be greater than MAXIMUM_WAIT_OBJECTS
75 #define MAX_READERS MAXIMUM_WAIT_OBJECTS
77 #define QT_WA(unicode, ansi) unicode
79 Qt::HANDLE QtLockedFile::getMutexHandle(int idx, bool doCreate)
81 if (mutexname.isEmpty()) {
82 QFileInfo fi(*this);
83 mutexname = QString::fromLatin1(MUTEX_PREFIX)
84 + fi.absoluteFilePath().toLower();
86 QString mname(mutexname);
87 if (idx >= 0)
88 mname += QString::number(idx);
90 Qt::HANDLE mutex;
91 if (doCreate) {
92 QT_WA( { mutex = CreateMutexW(NULL, FALSE, reinterpret_cast<const TCHAR *>(mname.utf16())); },
93 { mutex = CreateMutexA(NULL, FALSE, mname.toLocal8Bit().constData()); } );
94 if (!mutex) {
95 qErrnoWarning("QtLockedFile::lock(): CreateMutex failed");
96 return 0;
99 else {
100 QT_WA( { mutex = OpenMutexW(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, reinterpret_cast<const TCHAR *>(mname.utf16())); },
101 { mutex = OpenMutexA(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, mname.toLocal8Bit().constData()); } );
102 if (!mutex) {
103 if (GetLastError() != ERROR_FILE_NOT_FOUND)
104 qErrnoWarning("QtLockedFile::lock(): OpenMutex failed");
105 return 0;
108 return mutex;
111 bool QtLockedFile::waitMutex(Qt::HANDLE mutex, bool doBlock)
113 Q_ASSERT(mutex);
114 DWORD res = WaitForSingleObject(mutex, doBlock ? INFINITE : 0);
115 switch (res) {
116 case WAIT_OBJECT_0:
117 case WAIT_ABANDONED:
118 return true;
119 break;
120 case WAIT_TIMEOUT:
121 break;
122 default:
123 qErrnoWarning("QtLockedFile::lock(): WaitForSingleObject failed");
125 return false;
128 bool QtLockedFile::lock(LockMode mode, bool block)
130 if (!isOpen()) {
131 qWarning("QtLockedFile::lock(): file is not opened");
132 return false;
135 if (mode == NoLock)
136 return unlock();
138 if (mode == m_lock_mode)
139 return true;
141 if (m_lock_mode != NoLock)
142 unlock();
144 if (!wmutex && !(wmutex = getMutexHandle(-1, true)))
145 return false;
147 if (!waitMutex(wmutex, block))
148 return false;
150 if (mode == ReadLock) {
151 int idx = 0;
152 for (; idx < MAX_READERS; idx++) {
153 rmutex = getMutexHandle(idx, false);
154 if (!rmutex || waitMutex(rmutex, false))
155 break;
156 CloseHandle(rmutex);
158 bool ok = true;
159 if (idx >= MAX_READERS) {
160 qWarning("QtLockedFile::lock(): too many readers");
161 rmutex = 0;
162 ok = false;
164 else if (!rmutex) {
165 rmutex = getMutexHandle(idx, true);
166 if (!rmutex || !waitMutex(rmutex, false))
167 ok = false;
169 if (!ok && rmutex) {
170 CloseHandle(rmutex);
171 rmutex = 0;
173 ReleaseMutex(wmutex);
174 if (!ok)
175 return false;
177 else {
178 Q_ASSERT(rmutexes.isEmpty());
179 for (int i = 0; i < MAX_READERS; i++) {
180 Qt::HANDLE mutex = getMutexHandle(i, false);
181 if (mutex)
182 rmutexes.append(mutex);
184 if (rmutexes.size()) {
185 DWORD res = WaitForMultipleObjects(rmutexes.size(), rmutexes.constData(),
186 TRUE, block ? INFINITE : 0);
187 if (res != WAIT_OBJECT_0 && res != WAIT_ABANDONED) {
188 if (res != WAIT_TIMEOUT)
189 qErrnoWarning("QtLockedFile::lock(): WaitForMultipleObjects failed");
190 m_lock_mode = WriteLock; // trick unlock() to clean up - semiyucky
191 unlock();
192 return false;
197 m_lock_mode = mode;
198 return true;
201 bool QtLockedFile::unlock()
203 if (!isOpen()) {
204 qWarning("QtLockedFile::unlock(): file is not opened");
205 return false;
208 if (!isLocked())
209 return true;
211 if (m_lock_mode == ReadLock) {
212 ReleaseMutex(rmutex);
213 CloseHandle(rmutex);
214 rmutex = 0;
216 else {
217 foreach(Qt::HANDLE mutex, rmutexes) {
218 ReleaseMutex(mutex);
219 CloseHandle(mutex);
221 rmutexes.clear();
222 ReleaseMutex(wmutex);
225 m_lock_mode = QtLockedFile::NoLock;
226 return true;
229 QtLockedFile::~QtLockedFile()
231 if (isOpen())
232 unlock();
233 if (wmutex)
234 CloseHandle(wmutex);