File indexing completed on 2024-12-22 04:40:50

0001 /****************************************************************************
0002 **
0003 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
0004 ** Contact: http://www.qt-project.org/legal
0005 **
0006 ** This file is part of the Qt Solutions component.
0007 **
0008 ** $QT_BEGIN_LICENSE:BSD$
0009 ** You may use this file under the terms of the BSD license as follows:
0010 **
0011 ** "Redistribution and use in source and binary forms, with or without
0012 ** modification, are permitted provided that the following conditions are
0013 ** met:
0014 **   * Redistributions of source code must retain the above copyright
0015 **     notice, this list of conditions and the following disclaimer.
0016 **   * Redistributions in binary form must reproduce the above copyright
0017 **     notice, this list of conditions and the following disclaimer in
0018 **     the documentation and/or other materials provided with the
0019 **     distribution.
0020 **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
0021 **     of its contributors may be used to endorse or promote products derived
0022 **     from this software without specific prior written permission.
0023 **
0024 **
0025 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0026 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0027 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0028 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0029 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0030 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0031 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0032 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0033 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0034 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0035 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
0036 **
0037 ** $QT_END_LICENSE$
0038 **
0039 ****************************************************************************/
0040 
0041 #include "qtlockedfile.h"
0042 
0043 /*!
0044     \class QtLockedFile
0045 
0046     \brief The QtLockedFile class extends QFile with advisory locking
0047     functions.
0048 
0049     A file may be locked in read or write mode. Multiple instances of
0050     \e QtLockedFile, created in multiple processes running on the same
0051     machine, may have a file locked in read mode. Exactly one instance
0052     may have it locked in write mode. A read and a write lock cannot
0053     exist simultaneously on the same file.
0054 
0055     The file locks are advisory. This means that nothing prevents
0056     another process from manipulating a locked file using QFile or
0057     file system functions offered by the OS. Serialization is only
0058     guaranteed if all processes that access the file use
0059     QLockedFile. Also, while holding a lock on a file, a process
0060     must not open the same file again (through any API), or locks
0061     can be unexpectedly lost.
0062 
0063     The lock provided by an instance of \e QtLockedFile is released
0064     whenever the program terminates. This is true even when the
0065     program crashes and no destructors are called.
0066 */
0067 
0068 /*! \enum QtLockedFile::LockMode
0069 
0070     This enum describes the available lock modes.
0071 
0072     \value ReadLock A read lock.
0073     \value WriteLock A write lock.
0074     \value NoLock Neither a read lock nor a write lock.
0075 */
0076 
0077 /*!
0078     Constructs an unlocked \e QtLockedFile object. This constructor
0079     behaves in the same way as \e QFile::QFile().
0080 
0081     \sa QFile::QFile()
0082 */
0083 QtLockedFile::QtLockedFile()
0084     : QFile()
0085 {
0086 #ifdef Q_OS_WIN
0087     wmutex = 0;
0088     rmutex = 0;
0089 #endif
0090     m_lock_mode = NoLock;
0091 }
0092 
0093 /*!
0094     Constructs an unlocked QtLockedFile object with file \a name. This
0095     constructor behaves in the same way as \e QFile::QFile(const
0096     QString&).
0097 
0098     \sa QFile::QFile()
0099 */
0100 QtLockedFile::QtLockedFile(const QString &name)
0101     : QFile(name)
0102 {
0103 #ifdef Q_OS_WIN
0104     wmutex = 0;
0105     rmutex = 0;
0106 #endif
0107     m_lock_mode = NoLock;
0108 }
0109 
0110 /*!
0111   Opens the file in OpenMode \a mode.
0112 
0113   This is identical to QFile::open(), with the one exception that the
0114   Truncate mode flag is disallowed. Truncation would conflict with the
0115   advisory file locking, since the file would be modified before the
0116   write lock is obtained. If truncation is required, use resize(0)
0117   after obtaining the write lock.
0118 
0119   Returns true if successful; otherwise false.
0120 
0121   \sa QFile::open(), QFile::resize()
0122 */
0123 bool QtLockedFile::open(OpenMode mode)
0124 {
0125     if (mode & QIODevice::Truncate) {
0126         qWarning("QtLockedFile::open(): Truncate mode not allowed.");
0127         return false;
0128     }
0129     return QFile::open(mode);
0130 }
0131 
0132 /*!
0133     Returns \e true if this object has a in read or write lock;
0134     otherwise returns \e false.
0135 
0136     \sa lockMode()
0137 */
0138 bool QtLockedFile::isLocked() const
0139 {
0140     return m_lock_mode != NoLock;
0141 }
0142 
0143 /*!
0144     Returns the type of lock currently held by this object, or \e
0145     QtLockedFile::NoLock.
0146 
0147     \sa isLocked()
0148 */
0149 QtLockedFile::LockMode QtLockedFile::lockMode() const
0150 {
0151     return m_lock_mode;
0152 }
0153 
0154 /*!
0155     \fn bool QtLockedFile::lock(LockMode mode, bool block = true)
0156 
0157     Obtains a lock of type \a mode. The file must be opened before it
0158     can be locked.
0159 
0160     If \a block is true, this function will block until the lock is
0161     acquired. If \a block is false, this function returns \e false
0162     immediately if the lock cannot be acquired.
0163 
0164     If this object already has a lock of type \a mode, this function
0165     returns \e true immediately. If this object has a lock of a
0166     different type than \a mode, the lock is first released and then a
0167     new lock is obtained.
0168 
0169     This function returns \e true if, after it executes, the file is
0170     locked by this object, and \e false otherwise.
0171 
0172     \sa unlock(), isLocked(), lockMode()
0173 */
0174 
0175 /*!
0176     \fn bool QtLockedFile::unlock()
0177 
0178     Releases a lock.
0179 
0180     If the object has no lock, this function returns immediately.
0181 
0182     This function returns \e true if, after it executes, the file is
0183     not locked by this object, and \e false otherwise.
0184 
0185     \sa lock(), isLocked(), lockMode()
0186 */
0187 
0188 /*!
0189     \fn QtLockedFile::~QtLockedFile()
0190 
0191     Destroys the \e QtLockedFile object. If any locks were held, they
0192     are released.
0193 */