1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QFILESYSTEMWATCHER_FSEVENTS_P_H
41 #define QFILESYSTEMWATCHER_FSEVENTS_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API. It exists purely as an
48 // implementation detail. This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include "qfilesystemwatcher_p.h"
55 
56 #include <QtCore/qmutex.h>
57 #include <QtCore/qhash.h>
58 #include <QtCore/qthread.h>
59 #include <QtCore/qvector.h>
60 #include <QtCore/qsocketnotifier.h>
61 
62 #include <dispatch/dispatch.h>
63 #include <CoreServices/CoreServices.h>
64 
65 QT_REQUIRE_CONFIG(filesystemwatcher);
66 
67 QT_BEGIN_NAMESPACE
68 
69 class QFseventsFileSystemWatcherEngine : public QFileSystemWatcherEngine
70 {
71     Q_OBJECT
72 public:
73     ~QFseventsFileSystemWatcherEngine();
74 
75     static QFseventsFileSystemWatcherEngine *create(QObject *parent);
76 
77     QStringList addPaths(const QStringList &paths, QStringList *files, QStringList *directories);
78     QStringList removePaths(const QStringList &paths, QStringList *files, QStringList *directories);
79 
80     void processEvent(ConstFSEventStreamRef streamRef, size_t numEvents, char **eventPaths, const FSEventStreamEventFlags eventFlags[], const FSEventStreamEventId eventIds[]);
81 
82 Q_SIGNALS:
83     void emitFileChanged(const QString &path, bool removed);
84     void emitDirectoryChanged(const QString &path, bool removed);
85     void scheduleStreamRestart();
86 
87 private slots:
88     void doEmitFileChanged(const QString &path, bool removed);
89     void doEmitDirectoryChanged(const QString &path, bool removed);
90     bool restartStream();
91 
92 private:
93     struct Info {
94         QString origPath;
95         timespec ctime;
96         mode_t mode;
97         QString watchedPath;
98 
InfoInfo99         Info(): mode(0)
100         {
101             ctime.tv_sec = 0;
102             ctime.tv_nsec = 0;
103         }
104 
InfoInfo105         Info(const QString &origPath, const timespec &ctime, mode_t mode, const QString &watchedPath)
106             : origPath(origPath)
107             , ctime(ctime)
108             , mode(mode)
109             , watchedPath(watchedPath)
110         {}
111     };
112     typedef QHash<QString, Info> InfoByName;
113     typedef QHash<QString, InfoByName> FilesByPath;
114     struct DirInfo {
115         Info dirInfo;
116         InfoByName entries;
117     };
118     typedef QHash<QString, DirInfo> DirsByName;
119     typedef QHash<QString, qint64> PathRefCounts;
120 
121     struct WatchingState {
122         // These fields go hand-in-hand. FSEvents watches paths, and there is no use in watching
123         // the same path multiple times. So, the "refcount" on a path is the number of watched
124         // files that have the same path, plus the number of directories that have the same path.
125         //
126         // If the stream fails to start after adding files/directories, the watcher will try to
127         // keep watching files/directories that it was already watching. It does that by restoring
128         // the previous WatchingState and restarting the stream.
129         FilesByPath watchedFiles;
130         DirsByName watchedDirectories;
131         PathRefCounts watchedPaths;
132     };
133 
134     QFseventsFileSystemWatcherEngine(QObject *parent);
135     bool startStream();
136     void stopStream(bool isStopped = false);
137     InfoByName scanForDirEntries(const QString &path);
138     bool derefPath(const QString &watchedPath);
139     bool checkDir(DirsByName::iterator &it);
140     bool rescanDirs(const QString &path);
141     bool rescanFiles(InfoByName &filesInPath);
142     bool rescanFiles(const QString &path);
143 
144     QMutex lock;
145     dispatch_queue_t queue;
146     FSEventStreamRef stream;
147     FSEventStreamEventId lastReceivedEvent;
148     WatchingState watchingState;
149 };
150 
151 QT_END_NAMESPACE
152 
153 #endif // QFILESYSTEMWATCHER_FSEVENTS_P_H
154