| 1 | // Copyright (C) 2013 David Faure <faure+bluesystems@kde.org> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:significant reason:default |
| 4 | |
| 5 | #ifndef QLOCKFILE_H |
| 6 | #define QLOCKFILE_H |
| 7 | |
| 8 | #include <QtCore/qstring.h> |
| 9 | #include <QtCore/qscopedpointer.h> |
| 10 | |
| 11 | #include <chrono> |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | class QLockFilePrivate; |
| 16 | |
| 17 | class Q_CORE_EXPORT QLockFile |
| 18 | { |
| 19 | public: |
| 20 | explicit QLockFile(const QString &fileName); |
| 21 | ~QLockFile(); |
| 22 | |
| 23 | QString fileName() const; |
| 24 | |
| 25 | bool lock(); |
| 26 | QT_CORE_INLINE_SINCE(6, 10) |
| 27 | bool tryLock(int timeout); |
| 28 | void unlock(); |
| 29 | |
| 30 | QT_CORE_INLINE_SINCE(6, 10) |
| 31 | void setStaleLockTime(int); |
| 32 | QT_CORE_INLINE_SINCE(6, 10) |
| 33 | int staleLockTime() const; |
| 34 | |
| 35 | bool tryLock(std::chrono::milliseconds timeout = std::chrono::milliseconds::zero()); |
| 36 | |
| 37 | void setStaleLockTime(std::chrono::milliseconds value); |
| 38 | std::chrono::milliseconds staleLockTimeAsDuration() const; |
| 39 | |
| 40 | bool isLocked() const; |
| 41 | bool getLockInfo(qint64 *pid, QString *hostname, QString *appname) const; |
| 42 | bool removeStaleLockFile(); |
| 43 | |
| 44 | enum LockError { |
| 45 | NoError = 0, |
| 46 | LockFailedError = 1, |
| 47 | PermissionError = 2, |
| 48 | UnknownError = 3 |
| 49 | }; |
| 50 | LockError error() const; |
| 51 | |
| 52 | protected: |
| 53 | QScopedPointer<QLockFilePrivate> d_ptr; |
| 54 | |
| 55 | private: |
| 56 | Q_DECLARE_PRIVATE(QLockFile) |
| 57 | Q_DISABLE_COPY(QLockFile) |
| 58 | }; |
| 59 | |
| 60 | #if QT_CORE_INLINE_IMPL_SINCE(6, 10) |
| 61 | bool QLockFile::tryLock(int timeout) |
| 62 | { |
| 63 | return tryLock(timeout: std::chrono::milliseconds{timeout}); |
| 64 | } |
| 65 | |
| 66 | void QLockFile::setStaleLockTime(int staleLockTime) |
| 67 | { |
| 68 | setStaleLockTime(std::chrono::milliseconds{staleLockTime}); |
| 69 | } |
| 70 | |
| 71 | int QLockFile::staleLockTime() const |
| 72 | { |
| 73 | return int(staleLockTimeAsDuration().count()); |
| 74 | } |
| 75 | #endif // QT_CORE_INLINE_IMPL_SINCE(6, 10) |
| 76 | |
| 77 | QT_END_NAMESPACE |
| 78 | |
| 79 | #endif // QLOCKFILE_H |
| 80 | |