1 /****************************************************************************
3 ** This file is part of a Qt Solutions component.
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Qt Software Information (qt-info@nokia.com)
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Solutions Commercial License Agreement provided
12 ** with the Software or, alternatively, in accordance with the terms
13 ** contained in a written agreement between you and Nokia.
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file. Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file. Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
36 ** Please note Third Party Software included with Qt Solutions may impose
37 ** additional restrictions and it is the user's responsibility to ensure
38 ** that they have met the licensing requirements of the GPL, LGPL, or Qt
39 ** Solutions Commercial license and the relevant license of the Third
40 ** Party Software they are using.
42 ** If you are unsure which license is appropriate for your use, please
43 ** contact the sales department at qt-sales@nokia.com.
45 ****************************************************************************/
47 #include "qtlockedfile.h"
52 \brief The QtLockedFile class extends QFile with advisory locking
55 A file may be locked in read or write mode. Multiple instances of
56 \e QtLockedFile, created in multiple processes running on the same
57 machine, may have a file locked in read mode. Exactly one instance
58 may have it locked in write mode. A read and a write lock cannot
59 exist simultaneously on the same file.
61 The file locks are advisory. This means that nothing prevents
62 another process from manipulating a locked file using QFile or
63 file system functions offered by the OS. Serialization is only
64 guaranteed if all processes that access the file use
65 QLockedFile. Also, while holding a lock on a file, a process
66 must not open the same file again (through any API), or locks
67 can be unexpectedly lost.
69 The lock provided by an instance of \e QtLockedFile is released
70 whenever the program terminates. This is true even when the
71 program crashes and no destructors are called.
74 /*! \enum QtLockedFile::LockMode
76 This enum describes the available lock modes.
78 \value ReadLock A read lock.
79 \value WriteLock A write lock.
80 \value NoLock Neither a read lock nor a write lock.
84 Constructs an unlocked \e QtLockedFile object. This constructor
85 behaves in the same way as \e QFile::QFile().
89 QtLockedFile::QtLockedFile()
100 Constructs an unlocked QtLockedFile object with file \a name. This
101 constructor behaves in the same way as \e QFile::QFile(const
106 QtLockedFile::QtLockedFile(const QString &name)
113 m_lock_mode = NoLock;
117 Opens the file in OpenMode \a mode.
119 This is identical to QFile::open(), with the one exception that the
120 Truncate mode flag is disallowed. Truncation would conflict with the
121 advisory file locking, since the file would be modified before the
122 write lock is obtained. If truncation is required, use resize(0)
123 after obtaining the write lock.
125 Returns true if successful; otherwise false.
127 \sa QFile::open(), QFile::resize()
129 bool QtLockedFile::open(OpenMode mode)
131 if (mode & QIODevice::Truncate) {
132 qWarning("QtLockedFile::open(): Truncate mode not allowed.");
135 return QFile::open(mode);
139 Returns \e true if this object has a in read or write lock;
140 otherwise returns \e false.
144 bool QtLockedFile::isLocked() const
146 return m_lock_mode != NoLock;
150 Returns the type of lock currently held by this object, or \e
151 QtLockedFile::NoLock.
155 QtLockedFile::LockMode QtLockedFile::lockMode() const
161 \fn bool QtLockedFile::lock(LockMode mode, bool block = true)
163 Obtains a lock of type \a mode. The file must be opened before it
166 If \a block is true, this function will block until the lock is
167 aquired. If \a block is false, this function returns \e false
168 immediately if the lock cannot be aquired.
170 If this object already has a lock of type \a mode, this function
171 returns \e true immediately. If this object has a lock of a
172 different type than \a mode, the lock is first released and then a
173 new lock is obtained.
175 This function returns \e true if, after it executes, the file is
176 locked by this object, and \e false otherwise.
178 \sa unlock(), isLocked(), lockMode()
182 \fn bool QtLockedFile::unlock()
186 If the object has no lock, this function returns immediately.
188 This function returns \e true if, after it executes, the file is
189 not locked by this object, and \e false otherwise.
191 \sa lock(), isLocked(), lockMode()
195 \fn QtLockedFile::~QtLockedFile()
197 Destroys the \e QtLockedFile object. If any locks were held, they