| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 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 QFILESYSTEMWATCHER_POLLING_P_H |
| 6 | #define QFILESYSTEMWATCHER_POLLING_P_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. It exists purely as an |
| 13 | // implementation detail. This header file may change from version to |
| 14 | // version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include <QtCore/qbasictimer.h> |
| 20 | #include <QtCore/qfileinfo.h> |
| 21 | #include <QtCore/qmutex.h> |
| 22 | #include <QtCore/qdatetime.h> |
| 23 | #include <QtCore/qdir.h> |
| 24 | #include <QtCore/qdirlisting.h> |
| 25 | #include <QtCore/qhash.h> |
| 26 | |
| 27 | #include "qfilesystemwatcher_p.h" |
| 28 | |
| 29 | QT_REQUIRE_CONFIG(filesystemwatcher); |
| 30 | QT_BEGIN_NAMESPACE |
| 31 | |
| 32 | class QPollingFileSystemWatcherEngine : public QFileSystemWatcherEngine |
| 33 | { |
| 34 | Q_OBJECT |
| 35 | |
| 36 | class FileInfo |
| 37 | { |
| 38 | uint ownerId; |
| 39 | uint groupId; |
| 40 | QFile::Permissions permissions; |
| 41 | QDateTime lastModified; |
| 42 | QStringList entries; |
| 43 | |
| 44 | static QStringList dirEntryList(const QFileInfo &fileInfo) |
| 45 | { |
| 46 | Q_ASSERT(fileInfo.isDir()); |
| 47 | |
| 48 | QStringList fileNames; |
| 49 | using F = QDirListing::IteratorFlag; |
| 50 | constexpr auto flags = F::ExcludeOther | F::IncludeDotAndDotDot; |
| 51 | for (const auto &entry : QDirListing(fileInfo.absoluteFilePath(), flags)) |
| 52 | fileNames.emplace_back(args: entry.fileName()); |
| 53 | return fileNames; |
| 54 | } |
| 55 | |
| 56 | public: |
| 57 | FileInfo(const QFileInfo &fileInfo) |
| 58 | : ownerId(fileInfo.ownerId()), |
| 59 | groupId(fileInfo.groupId()), |
| 60 | permissions(fileInfo.permissions()), |
| 61 | lastModified(fileInfo.lastModified(tz: QTimeZone::UTC)) |
| 62 | { |
| 63 | if (fileInfo.isDir()) |
| 64 | entries = dirEntryList(fileInfo); |
| 65 | } |
| 66 | FileInfo &operator=(const QFileInfo &fileInfo) |
| 67 | { |
| 68 | *this = FileInfo(fileInfo); |
| 69 | return *this; |
| 70 | } |
| 71 | |
| 72 | bool operator!=(const QFileInfo &fileInfo) const |
| 73 | { |
| 74 | if (fileInfo.isDir() && entries != dirEntryList(fileInfo)) |
| 75 | return true; |
| 76 | return (ownerId != fileInfo.ownerId() |
| 77 | || groupId != fileInfo.groupId() |
| 78 | || permissions != fileInfo.permissions() |
| 79 | || lastModified != fileInfo.lastModified(tz: QTimeZone::UTC)); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | QHash<QString, FileInfo> files, directories; |
| 84 | |
| 85 | public: |
| 86 | QPollingFileSystemWatcherEngine(QObject *parent); |
| 87 | |
| 88 | QStringList addPaths(const QStringList &paths, QStringList *files, QStringList *directories) override; |
| 89 | QStringList removePaths(const QStringList &paths, QStringList *files, QStringList *directories) override; |
| 90 | |
| 91 | private: |
| 92 | void timerEvent(QTimerEvent *) final; |
| 93 | |
| 94 | private: |
| 95 | QBasicTimer timer; |
| 96 | }; |
| 97 | |
| 98 | QT_END_NAMESPACE |
| 99 | #endif // QFILESYSTEMWATCHER_POLLING_P_H |
| 100 | |
| 101 | |