1 /****************************************************************************
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
5 ** This file is part of the QxtCore module of the Qt eXTension library
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
21 ** <http://libqxt.sourceforge.net> <foundation@libqxt.org>
23 ****************************************************************************/
24 #include "qxtfilelock.h"
25 #include "qxtfilelock_p.h"
27 * \class QxtFileLock QxtFileLock
29 * \brief The QxtFileLock class provides a crossplattform way to lock a QFile.
31 * It supports the range locking of a file. The File will take parentship of the lock.<br>
32 * The lock gets cleaned up with the QFile, and it is released when the QFile is closed.<br>
36 * off_t lockstart = 0x10;
37 * off_t locklength = 30
39 * QFile file("test.lock");
41 * //the lock gets deleted when file is cleaned up
42 * QxtFileLock * writeLock = new QxtFileLock(&file,lockstart,locklength,QxtFileLock::WriteLock);
43 * if(file.open(QIODevice::ReadWrite))
45 * if(writeLock->lock())
47 * // some write operations
48 * writeLock->unlock();
54 * \note QxtFileLock behaves different than normal unix locks on *nix. A thread can writelock the region of a file only ONCE if it uses two different handles.
55 * A different thread can not writelock a region that is owned by a other thread even if it is the SAME process.
56 * \note On *nix this class uses fctnl to lock the file. This may not be compatible to other locking functions like flock and lockf
57 * \note Please do not mix QxtFileLock and native file lock calls on the same QFile. The behaviour is undefined
58 * \note QxtFileLock lives in the same thread as the passed QFile
59 * \warning due to a refactoring issues of QFile this class will not work with Qt from 4.3 on. This will be fixed in 4.3.2
60 * \warning not part of 0.2.4
66 * @enum QxtFileLock::mode
67 * @brief The Mode of the lock
71 * @var QxtFileLock::mode QxtFileLock::ReadLock
72 * @brief A non blocking read lock
76 * @var QxtFileLock::mode QxtFileLock::WriteLock
77 * @brief A non blocking write lock
81 * @var QxtFileLock::mode QxtFileLock::ReadLockWait
82 * @brief A blocking read lock. The lock() function will block until the lock is created.
86 * @var QxtFileLock::mode QxtFileLock::WriteLockWait
87 * @brief A blocking write lock. The lock() function will block until the lock is created.
90 QxtFileLockPrivate::QxtFileLockPrivate() : offset(0), length(0), mode(QxtFileLock::WriteLockWait), isLocked(false)
95 * Contructs a new QxtFileLock. The lock is not activated.
96 * @param file the file that should be locked
97 * @param offset the offset where the lock starts
98 * @param length the length of the lock
99 * @param mode the lockmode
101 QxtFileLock::QxtFileLock(QFile *file,const off_t offset,const off_t length,const QxtFileLock::Mode mode) : QObject(file)
103 QXT_INIT_PRIVATE(QxtFileLock);
104 connect(file,SIGNAL(aboutToClose()),this,SLOT(unlock()));
105 qxt_d().offset = offset;
106 qxt_d().length = length;
110 QxtFileLock::~QxtFileLock()
116 *@return the offset of the lock
118 off_t QxtFileLock::offset() const
120 return qxt_d().offset;
124 * @return true if the lock is active otherwise it returns false
126 bool QxtFileLock::isActive() const
128 return qxt_d().isLocked;
132 * @return the length of the lock
134 off_t QxtFileLock::length() const
136 return qxt_d().length;
140 * the file the lock is created on
142 QFile * QxtFileLock::file() const
144 return qobject_cast<QFile *>(parent());
148 * @return the mode of the lock
150 QxtFileLock::Mode QxtFileLock::mode() const