qBittorrent
qtlockedfile.h
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2019 Mike Tzou (Chocobo1)
4  *
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.
9  *
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.
14  *
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.
18  *
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.
27  *
28 ****************************************************************************
29 **
30 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
31 ** Contact: http://www.qt-project.org/legal
32 **
33 ** This file is part of the Qt Solutions component.
34 **
35 ** $QT_BEGIN_LICENSE:BSD$
36 ** You may use this file under the terms of the BSD license as follows:
37 **
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.
50 **
51 **
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."
63 **
64 ** $QT_END_LICENSE$
65 **
66 ****************************************************************************
67 */
68 
69 #pragma once
70 
71 #include <QFile>
72 
73 #ifdef Q_OS_WIN
74 #include <QString>
75 #include <QVector>
76 #endif
77 
78 namespace QtLP_Private
79 {
80  class QtLockedFile final : public QFile
81  {
82  public:
83  enum LockMode
84  {
85  NoLock = 0,
87  WriteLock
88  };
89 
91  QtLockedFile(const QString &name);
92  ~QtLockedFile();
93 
94  bool open(OpenMode mode) override;
95 
96  bool lock(LockMode mode, bool block = true);
97  bool unlock();
98  bool isLocked() const;
99  LockMode lockMode() const;
100 
101  private:
102 #ifdef Q_OS_WIN
103  Qt::HANDLE getMutexHandle(int idx, bool doCreate);
104  bool waitMutex(Qt::HANDLE mutex, bool doBlock) const;
105 
106  Qt::HANDLE m_writeMutex = nullptr;
107  Qt::HANDLE m_readMutex = nullptr;
108  QVector<Qt::HANDLE> m_readMutexes;
109  QString m_mutexName;
110 #endif
111 
113  };
114 }
bool open(OpenMode mode) override
bool lock(LockMode mode, bool block=true)