ff7tk  0.02
Toolkit for making FF7 Tools
QLockedFile.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  ** Makou Reactor Final Fantasy VII Field Script Editor
3  ** Copyright (C) 2009-2012 Arzel Jérôme <myst6re@gmail.com>
4  **
5  ** This program is free software: you can redistribute it and/or modify
6  ** it under the terms of the GNU General Public License as published by
7  ** the Free Software Foundation, either version 3 of the License, or
8  ** (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, see <http://www.gnu.org/licenses/>.
17  ****************************************************************************/
18 #include "QLockedFile.h"
19 
21 {
22 }
23 
24 QLockedFile::QLockedFile(const QString &name) :
25  QFile(name)
26 {
27 }
28 
29 QLockedFile::QLockedFile(QObject *parent) :
30  QFile(parent)
31 {
32 }
33 
34 QLockedFile::QLockedFile(const QString &name, QObject *parent) :
35  QFile(name, parent)
36 {
37 }
38 
40 {
41  close();
42 }
43 
45 {
46  // Unlock file
47 #ifdef Q_OS_WIN
48  CloseHandle(handle);
49 #else
50  ::flock(handle(), LOCK_UN);
51 #endif
52  QFile::close();
53 }
54 
55 bool QLockedFile::open(OpenMode mode)
56 {
57  if(!mode.testFlag(QIODevice::ReadOnly)) {
58  qWarning() << "QLockedFile::open must be opened in ReadOnly mode";
59  return false;
60  }
61 
62  // Lock file
63 #ifdef Q_OS_WIN
64  handle = CreateFileA(QDir::toNativeSeparators(fileName()).toLatin1().data(),
65  GENERIC_READ,
66  FILE_SHARE_READ,
67  NULL,
68  OPEN_EXISTING,
69  FILE_ATTRIBUTE_NORMAL,
70  NULL);
71  if(handle == INVALID_HANDLE_VALUE) {
72  qWarning() << "QLockedFile::open error lock";
73  return false;
74  }
75 
76  return QFile::open(mode);
77 #else
78  bool isOpen = QFile::open(mode);
79 
80  if(isOpen && ::flock(handle(), LOCK_SH) < 0) {
81  qWarning() << "QLockedFile::open error flock";
82  return false;
83  }
84 
85  return isOpen;
86 #endif
87 }
virtual ~QLockedFile()
Definition: QLockedFile.cpp:39
virtual void close()
Definition: QLockedFile.cpp:44
virtual bool open(OpenMode mode)
Definition: QLockedFile.cpp:55