[127] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[165] | 3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
---|
| 4 | ** Contact: http://www.qt-project.org/legal
|
---|
[127] | 5 | **
|
---|
[165] | 6 | ** This file is part of the Qt Solutions component.
|
---|
[127] | 7 | **
|
---|
[165] | 8 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
[127] | 9 | ** You may use this file under the terms of the BSD license as follows:
|
---|
| 10 | **
|
---|
| 11 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 12 | ** modification, are permitted provided that the following conditions are
|
---|
| 13 | ** met:
|
---|
| 14 | ** * Redistributions of source code must retain the above copyright
|
---|
| 15 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 16 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 17 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 18 | ** the documentation and/or other materials provided with the
|
---|
| 19 | ** distribution.
|
---|
[165] | 20 | ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
|
---|
| 21 | ** of its contributors may be used to endorse or promote products derived
|
---|
| 22 | ** from this software without specific prior written permission.
|
---|
[127] | 23 | **
|
---|
[165] | 24 | **
|
---|
[127] | 25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
| 36 | **
|
---|
[165] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
[127] | 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include "qtlockedfile.h"
|
---|
| 42 |
|
---|
| 43 | /*!
|
---|
| 44 | \class QtLockedFile
|
---|
| 45 |
|
---|
| 46 | \brief The QtLockedFile class extends QFile with advisory locking
|
---|
| 47 | functions.
|
---|
| 48 |
|
---|
| 49 | A file may be locked in read or write mode. Multiple instances of
|
---|
| 50 | \e QtLockedFile, created in multiple processes running on the same
|
---|
| 51 | machine, may have a file locked in read mode. Exactly one instance
|
---|
| 52 | may have it locked in write mode. A read and a write lock cannot
|
---|
| 53 | exist simultaneously on the same file.
|
---|
| 54 |
|
---|
| 55 | The file locks are advisory. This means that nothing prevents
|
---|
| 56 | another process from manipulating a locked file using QFile or
|
---|
| 57 | file system functions offered by the OS. Serialization is only
|
---|
| 58 | guaranteed if all processes that access the file use
|
---|
| 59 | QLockedFile. Also, while holding a lock on a file, a process
|
---|
| 60 | must not open the same file again (through any API), or locks
|
---|
| 61 | can be unexpectedly lost.
|
---|
| 62 |
|
---|
| 63 | The lock provided by an instance of \e QtLockedFile is released
|
---|
| 64 | whenever the program terminates. This is true even when the
|
---|
| 65 | program crashes and no destructors are called.
|
---|
| 66 | */
|
---|
| 67 |
|
---|
| 68 | /*! \enum QtLockedFile::LockMode
|
---|
| 69 |
|
---|
| 70 | This enum describes the available lock modes.
|
---|
| 71 |
|
---|
| 72 | \value ReadLock A read lock.
|
---|
| 73 | \value WriteLock A write lock.
|
---|
| 74 | \value NoLock Neither a read lock nor a write lock.
|
---|
| 75 | */
|
---|
| 76 |
|
---|
| 77 | /*!
|
---|
| 78 | Constructs an unlocked \e QtLockedFile object. This constructor
|
---|
| 79 | behaves in the same way as \e QFile::QFile().
|
---|
| 80 |
|
---|
| 81 | \sa QFile::QFile()
|
---|
| 82 | */
|
---|
| 83 | QtLockedFile::QtLockedFile()
|
---|
| 84 | : QFile()
|
---|
| 85 | {
|
---|
| 86 | #ifdef Q_OS_WIN
|
---|
| 87 | wmutex = 0;
|
---|
| 88 | rmutex = 0;
|
---|
| 89 | #endif
|
---|
| 90 | m_lock_mode = NoLock;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /*!
|
---|
| 94 | Constructs an unlocked QtLockedFile object with file \a name. This
|
---|
| 95 | constructor behaves in the same way as \e QFile::QFile(const
|
---|
| 96 | QString&).
|
---|
| 97 |
|
---|
| 98 | \sa QFile::QFile()
|
---|
| 99 | */
|
---|
| 100 | QtLockedFile::QtLockedFile(const QString &name)
|
---|
| 101 | : QFile(name)
|
---|
| 102 | {
|
---|
| 103 | #ifdef Q_OS_WIN
|
---|
| 104 | wmutex = 0;
|
---|
| 105 | rmutex = 0;
|
---|
| 106 | #endif
|
---|
| 107 | m_lock_mode = NoLock;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /*!
|
---|
| 111 | Opens the file in OpenMode \a mode.
|
---|
| 112 |
|
---|
| 113 | This is identical to QFile::open(), with the one exception that the
|
---|
| 114 | Truncate mode flag is disallowed. Truncation would conflict with the
|
---|
| 115 | advisory file locking, since the file would be modified before the
|
---|
| 116 | write lock is obtained. If truncation is required, use resize(0)
|
---|
| 117 | after obtaining the write lock.
|
---|
| 118 |
|
---|
| 119 | Returns true if successful; otherwise false.
|
---|
| 120 |
|
---|
| 121 | \sa QFile::open(), QFile::resize()
|
---|
| 122 | */
|
---|
| 123 | bool QtLockedFile::open(OpenMode mode)
|
---|
| 124 | {
|
---|
| 125 | if (mode & QIODevice::Truncate) {
|
---|
| 126 | qWarning("QtLockedFile::open(): Truncate mode not allowed.");
|
---|
| 127 | return false;
|
---|
| 128 | }
|
---|
| 129 | return QFile::open(mode);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /*!
|
---|
| 133 | Returns \e true if this object has a in read or write lock;
|
---|
| 134 | otherwise returns \e false.
|
---|
| 135 |
|
---|
| 136 | \sa lockMode()
|
---|
| 137 | */
|
---|
| 138 | bool QtLockedFile::isLocked() const
|
---|
| 139 | {
|
---|
| 140 | return m_lock_mode != NoLock;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /*!
|
---|
| 144 | Returns the type of lock currently held by this object, or \e
|
---|
| 145 | QtLockedFile::NoLock.
|
---|
| 146 |
|
---|
| 147 | \sa isLocked()
|
---|
| 148 | */
|
---|
| 149 | QtLockedFile::LockMode QtLockedFile::lockMode() const
|
---|
| 150 | {
|
---|
| 151 | return m_lock_mode;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /*!
|
---|
| 155 | \fn bool QtLockedFile::lock(LockMode mode, bool block = true)
|
---|
| 156 |
|
---|
| 157 | Obtains a lock of type \a mode. The file must be opened before it
|
---|
| 158 | can be locked.
|
---|
| 159 |
|
---|
| 160 | If \a block is true, this function will block until the lock is
|
---|
| 161 | aquired. If \a block is false, this function returns \e false
|
---|
| 162 | immediately if the lock cannot be aquired.
|
---|
| 163 |
|
---|
| 164 | If this object already has a lock of type \a mode, this function
|
---|
| 165 | returns \e true immediately. If this object has a lock of a
|
---|
| 166 | different type than \a mode, the lock is first released and then a
|
---|
| 167 | new lock is obtained.
|
---|
| 168 |
|
---|
| 169 | This function returns \e true if, after it executes, the file is
|
---|
| 170 | locked by this object, and \e false otherwise.
|
---|
| 171 |
|
---|
| 172 | \sa unlock(), isLocked(), lockMode()
|
---|
| 173 | */
|
---|
| 174 |
|
---|
| 175 | /*!
|
---|
| 176 | \fn bool QtLockedFile::unlock()
|
---|
| 177 |
|
---|
| 178 | Releases a lock.
|
---|
| 179 |
|
---|
| 180 | If the object has no lock, this function returns immediately.
|
---|
| 181 |
|
---|
| 182 | This function returns \e true if, after it executes, the file is
|
---|
| 183 | not locked by this object, and \e false otherwise.
|
---|
| 184 |
|
---|
| 185 | \sa lock(), isLocked(), lockMode()
|
---|
| 186 | */
|
---|
| 187 |
|
---|
| 188 | /*!
|
---|
| 189 | \fn QtLockedFile::~QtLockedFile()
|
---|
| 190 |
|
---|
| 191 | Destroys the \e QtLockedFile object. If any locks were held, they
|
---|
| 192 | are released.
|
---|
| 193 | */
|
---|