Krita Source Code Documentation
Loading...
Searching...
No Matches
QtLP_Private::QtLockedFile Class Reference

#include <qtlockedfile.h>

+ Inheritance diagram for QtLP_Private::QtLockedFile:

Public Types

enum  LockMode { NoLock = 0 , ReadLock , WriteLock }
 

Public Member Functions

bool isLocked () const
 
bool lock (LockMode mode, bool block=true)
 
LockMode lockMode () const
 
bool open (OpenMode mode)
 
 QtLockedFile ()
 
 QtLockedFile (const QString &name)
 
bool unlock ()
 
 ~QtLockedFile ()
 

Private Attributes

LockMode m_lock_mode
 

Detailed Description

Definition at line 14 of file qtlockedfile.h.

Member Enumeration Documentation

◆ LockMode

Enumerator
NoLock 
ReadLock 
WriteLock 

Definition at line 17 of file qtlockedfile.h.

Constructor & Destructor Documentation

◆ QtLockedFile() [1/2]

QtLockedFile::QtLockedFile ( )

Constructs an unlocked QtLockedFile object. This constructor behaves in the same way as QFile::QFile().

See also
QFile::QFile()

Definition at line 46 of file qtlockedfile.cpp.

47 : QFile()
48{
49#ifdef Q_OS_WIN
50 wmutex = 0;
51 rmutex = 0;
52#endif
54}

References m_lock_mode, and NoLock.

◆ QtLockedFile() [2/2]

QtLockedFile::QtLockedFile ( const QString & name)

Constructs an unlocked QtLockedFile object with file name. This constructor behaves in the same way as QFile::QFile(const QString&).

See also
QFile::QFile()

Definition at line 63 of file qtlockedfile.cpp.

64 : QFile(name)
65{
66#ifdef Q_OS_WIN
67 wmutex = 0;
68 rmutex = 0;
69#endif
71}

References m_lock_mode, and NoLock.

◆ ~QtLockedFile()

QtLockedFile::~QtLockedFile ( )

Destroys the QtLockedFile object. If any locks were held, they are released.

Definition at line 73 of file qtlockedfile_unix.cpp.

74{
75 if (isOpen())
76 unlock();
77}

Member Function Documentation

◆ isLocked()

bool QtLockedFile::isLocked ( ) const

Returns true if this object has a in read or write lock; otherwise returns false.

See also
lockMode()

Definition at line 101 of file qtlockedfile.cpp.

102{
103 return m_lock_mode != NoLock;
104}

References m_lock_mode, and NoLock.

◆ lock()

bool QtLockedFile::lock ( LockMode mode,
bool block = true )

Obtains a lock of type mode. The file must be opened before it can be locked.

If block is true, this function will block until the lock is acquired. If block is false, this function returns false immediately if the lock cannot be acquired.

If this object already has a lock of type mode, this function returns true immediately. If this object has a lock of a different type than mode, the lock is first released and then a new lock is obtained.

This function returns true if, after it executes, the file is locked by this object, and false otherwise.

See also
unlock(), isLocked(), lockMode()

Definition at line 11 of file qtlockedfile_unix.cpp.

12{
13 if (!isOpen()) {
14 qWarning("QtLockedFile::lock(): file is not opened");
15 return false;
16 }
17
18 if (mode == NoLock)
19 return unlock();
20
21 if (mode == m_lock_mode)
22 return true;
23
24 if (m_lock_mode != NoLock)
25 unlock();
26
27 struct flock fl;
28 fl.l_whence = SEEK_SET;
29 fl.l_start = 0;
30 fl.l_len = 0;
31 fl.l_type = (mode == ReadLock) ? F_RDLCK : F_WRLCK;
32 int cmd = block ? F_SETLKW : F_SETLK;
33 int ret = fcntl(handle(), cmd, &fl);
34
35 if (ret == -1) {
36 if (errno != EINTR && errno != EAGAIN)
37 qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno));
38 return false;
39 }
40
41
42 m_lock_mode = mode;
43 return true;
44}

◆ lockMode()

QtLockedFile::LockMode QtLockedFile::lockMode ( ) const

Returns the type of lock currently held by this object, or QtLockedFile::NoLock.

See also
isLocked()

Definition at line 112 of file qtlockedfile.cpp.

113{
114 return m_lock_mode;
115}

References m_lock_mode.

◆ open()

bool QtLockedFile::open ( OpenMode mode)

Opens the file in OpenMode mode.

This is identical to QFile::open(), with the one exception that the Truncate mode flag is disallowed. Truncation would conflict with the advisory file locking, since the file would be modified before the write lock is obtained. If truncation is required, use resize(0) after obtaining the write lock.

Returns true if successful; otherwise false.

See also
QFile::open(), QFile::resize()

Definition at line 86 of file qtlockedfile.cpp.

87{
88 if (mode & QIODevice::Truncate) {
89 qWarning("QtLockedFile::open(): Truncate mode not allowed.");
90 return false;
91 }
92 return QFile::open(mode);
93}

◆ unlock()

bool QtLockedFile::unlock ( )

Releases a lock.

If the object has no lock, this function returns immediately.

This function returns true if, after it executes, the file is not locked by this object, and false otherwise.

See also
lock(), isLocked(), lockMode()

Definition at line 47 of file qtlockedfile_unix.cpp.

48{
49 if (!isOpen()) {
50 qWarning("QtLockedFile::unlock(): file is not opened");
51 return false;
52 }
53
54 if (!isLocked())
55 return true;
56
57 struct flock fl;
58 fl.l_whence = SEEK_SET;
59 fl.l_start = 0;
60 fl.l_len = 0;
61 fl.l_type = F_UNLCK;
62 int ret = fcntl(handle(), F_SETLKW, &fl);
63
64 if (ret == -1) {
65 qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno));
66 return false;
67 }
68
70 return true;
71}

Member Data Documentation

◆ m_lock_mode

LockMode QtLP_Private::QtLockedFile::m_lock_mode
private

Definition at line 41 of file qtlockedfile.h.


The documentation for this class was generated from the following files: