1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <qplatformdefs.h>
#include "qfilesystemwatcher.h"
#include "qfilesystemwatcher_fsevents_p.h"
#include <qdebug.h>
#include <qfile.h>
#include <qdatetime.h>
#include <qfileinfo.h>
#include <qvarlengtharray.h>
#include <mach/mach.h>
#include <sys/types.h>
#include <CoreFoundation/CFRunLoop.h>
#include <CoreFoundation/CFUUID.h>
#include <CoreServices/CoreServices.h>
#include <AvailabilityMacros.h>
#include <private/qcore_mac_p.h>
QT_BEGIN_NAMESPACE
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
// Static operator overloading so for the sake of some convieniece.
// They only live in this compilation unit to avoid polluting Qt in general.
static bool operator==(const struct ::timespec &left, const struct ::timespec &right)
{
return left.tv_sec == right.tv_sec
&& left.tv_nsec == right.tv_nsec;
}
static bool operator==(const struct ::stat64 &left, const struct ::stat64 &right)
{
return left.st_dev == right.st_dev
&& left.st_mode == right.st_mode
&& left.st_size == right.st_size
&& left.st_ino == right.st_ino
&& left.st_uid == right.st_uid
&& left.st_gid == right.st_gid
&& left.st_mtimespec == right.st_mtimespec
&& left.st_ctimespec == right.st_ctimespec
&& left.st_flags == right.st_flags;
}
static bool operator!=(const struct ::stat64 &left, const struct ::stat64 &right)
{
return !(operator==(left, right));
}
static void addPathToHash(PathHash &pathHash, const QString &key, const QFileInfo &fileInfo,
const QString &path)
{
PathInfoList &list = pathHash[key];
list.push_back(PathInfo(path,
fileInfo.canonicalFilePath().normalized(QString::NormalizationForm_D).toUtf8()));
pathHash.insert(key, list);
}
static void removePathFromHash(PathHash &pathHash, const QString &key, const QString &path)
{
PathInfoList &list = pathHash[key];
// We make the assumption that the list contains unique paths
PathInfoList::iterator End = list.end();
PathInfoList::iterator it = list.begin();
while (it != End) {
if (it->originalPath == path) {
list.erase(it);
break;
}
++it;
}
if (list.isEmpty())
pathHash.remove(key);
}
static void stopFSStream(FSEventStreamRef stream)
{
if (stream) {
FSEventStreamStop(stream);
FSEventStreamInvalidate(stream);
}
}
static QString createFSStreamPath(const QString &absolutePath)
{
// The path returned has a trailing slash, so ensure that here.
QString string = absolutePath;
string.reserve(string.size() + 1);
string.append(QLatin1Char('/'));
return string;
}
static void cleanupFSStream(FSEventStreamRef stream)
{
if (stream)
FSEventStreamRelease(stream);
}
const FSEventStreamCreateFlags QtFSEventFlags = (kFSEventStreamCreateFlagUseCFTypes | kFSEventStreamCreateFlagNoDefer /* | kFSEventStreamCreateFlagWatchRoot*/);
const CFTimeInterval Latency = 0.033; // This will do updates 30 times a second which is probably more than you need.
#endif
QFSEventsFileSystemWatcherEngine::QFSEventsFileSystemWatcherEngine()
: fsStream(0), pathsToWatch(0), threadsRunLoop(0)
{
}
QFSEventsFileSystemWatcherEngine::~QFSEventsFileSystemWatcherEngine()
{
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
// I assume that at this point, QFileSystemWatcher has already called stop
// on me, so I don't need to invalidate or stop my stream, simply
// release it.
cleanupFSStream(fsStream);
if (pathsToWatch)
CFRelease(pathsToWatch);
#endif
}
QFSEventsFileSystemWatcherEngine *QFSEventsFileSystemWatcherEngine::create()
{
return new QFSEventsFileSystemWatcherEngine();
}
QStringList QFSEventsFileSystemWatcherEngine::addPaths(const QStringList &paths,
QStringList *files,
QStringList *directories)
{
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
stop();
QMutexLocker locker(&mutex);
QStringList failedToAdd;
// if we have a running FSStreamEvent, we have to kill it, we'll re-add the stream soon.
FSEventStreamEventId idToCheck;
if (fsStream) {
idToCheck = FSEventStreamGetLatestEventId(fsStream);
cleanupFSStream(fsStream);
} else {
idToCheck = kFSEventStreamEventIdSinceNow;
}
// Brain-dead approach, but works. FSEvents actually can already read sub-trees, but since it's
// work to figure out if we are doing a double register, we just register it twice as FSEvents
// seems smart enough to only deliver one event. We also duplicate directory entries in here
// (e.g., if you watch five files in the same directory, you get that directory included in the
// array 5 times). This stupidity also makes remove work correctly though. I'll freely admit
// that we could make this a bit smarter. If you do, check the auto-tests, they should catch at
// least a couple of the issues.
QCFType<CFMutableArrayRef> tmpArray = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
for (int i = 0; i < paths.size(); ++i) {
const QString &path = paths.at(i);
QFileInfo fileInfo(path);
if (!fileInfo.exists()) {
failedToAdd.append(path);
continue;
}
if (fileInfo.isDir()) {
if (directories->contains(path)) {
failedToAdd.append(path);
continue;
} else {
directories->append(path);
// Full file path for dirs.
QCFString cfpath(createFSStreamPath(fileInfo.canonicalFilePath()));
addPathToHash(dirPathInfoHash, cfpath, fileInfo, path);
CFArrayAppendValue(tmpArray, cfpath);
}
} else {
if (files->contains(path)) {
failedToAdd.append(path);
continue;
} else {
// Just the absolute path (minus it's filename) for files.
QCFString cfpath(createFSStreamPath(fileInfo.canonicalPath()));
files->append(path);
addPathToHash(filePathInfoHash, cfpath, fileInfo, path);
CFArrayAppendValue(tmpArray, cfpath);
}
}
}
if (!pathsToWatch && failedToAdd.size() == paths.size()) {
return failedToAdd;
}
if (CFArrayGetCount(tmpArray) > 0) {
if (pathsToWatch) {
CFArrayAppendArray(tmpArray, pathsToWatch, CFRangeMake(0, CFArrayGetCount(pathsToWatch)));
CFRelease(pathsToWatch);
}
pathsToWatch = CFArrayCreateCopy(kCFAllocatorDefault, tmpArray);
}
FSEventStreamContext context = { 0, this, 0, 0, 0 };
fsStream = FSEventStreamCreate(kCFAllocatorDefault,
QFSEventsFileSystemWatcherEngine::fseventsCallback,
&context, pathsToWatch,
idToCheck, Latency, QtFSEventFlags);
warmUpFSEvents();
return failedToAdd;
#else
Q_UNUSED(paths);
Q_UNUSED(files);
Q_UNUSED(directories);
return QStringList();
#endif
}
void QFSEventsFileSystemWatcherEngine::warmUpFSEvents()
{
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
// This function assumes that the mutex has already been grabbed before calling it.
// It exits with the mutex still locked (Q_ASSERT(mutex.isLocked()) ;-).
start();
waitCondition.wait(&mutex);
#endif
}
QStringList QFSEventsFileSystemWatcherEngine::removePaths(const QStringList &paths,
QStringList *files,
QStringList *directories)
{
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
stop();
QMutexLocker locker(&mutex);
// short circuit for smarties that call remove before add and we have nothing.
if (pathsToWatch == 0)
return paths;
QStringList failedToRemove;
// if we have a running FSStreamEvent, we have to stop it, we'll re-add the stream soon.
FSEventStreamEventId idToCheck;
if (fsStream) {
idToCheck = FSEventStreamGetLatestEventId(fsStream);
cleanupFSStream(fsStream);
fsStream = 0;
} else {
idToCheck = kFSEventStreamEventIdSinceNow;
}
CFIndex itemCount = CFArrayGetCount(pathsToWatch);
QCFType<CFMutableArrayRef> tmpArray = CFArrayCreateMutableCopy(kCFAllocatorDefault, itemCount,
pathsToWatch);
CFRelease(pathsToWatch);
pathsToWatch = 0;
for (int i = 0; i < paths.size(); ++i) {
// Get the itemCount at the beginning to avoid any overruns during the iteration.
itemCount = CFArrayGetCount(tmpArray);
const QString &path = paths.at(i);
QFileInfo fi(path);
QCFString cfpath(createFSStreamPath(fi.canonicalPath()));
CFIndex index = CFArrayGetFirstIndexOfValue(tmpArray, CFRangeMake(0, itemCount), cfpath);
if (index != -1) {
CFArrayRemoveValueAtIndex(tmpArray, index);
files->removeAll(path);
removePathFromHash(filePathInfoHash, cfpath, path);
} else {
// Could be a directory we are watching instead.
QCFString cfdirpath(createFSStreamPath(fi.canonicalFilePath()));
index = CFArrayGetFirstIndexOfValue(tmpArray, CFRangeMake(0, itemCount), cfdirpath);
if (index != -1) {
CFArrayRemoveValueAtIndex(tmpArray, index);
directories->removeAll(path);
removePathFromHash(dirPathInfoHash, cfpath, path);
} else {
failedToRemove.append(path);
}
}
}
itemCount = CFArrayGetCount(tmpArray);
if (itemCount != 0) {
pathsToWatch = CFArrayCreateCopy(kCFAllocatorDefault, tmpArray);
FSEventStreamContext context = { 0, this, 0, 0, 0 };
fsStream = FSEventStreamCreate(kCFAllocatorDefault,
QFSEventsFileSystemWatcherEngine::fseventsCallback,
&context, pathsToWatch, idToCheck, Latency, QtFSEventFlags);
warmUpFSEvents();
}
return failedToRemove;
#else
Q_UNUSED(paths);
Q_UNUSED(files);
Q_UNUSED(directories);
return QStringList();
#endif
}
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
void QFSEventsFileSystemWatcherEngine::updateList(PathInfoList &list, bool directory, bool emitSignals)
{
PathInfoList::iterator End = list.end();
PathInfoList::iterator it = list.begin();
while (it != End) {
struct ::stat64 newInfo;
if (::stat64(it->absolutePath, &newInfo) == 0) {
if (emitSignals) {
if (newInfo != it->savedInfo) {
it->savedInfo = newInfo;
if (directory)
emit directoryChanged(it->originalPath, false);
else
emit fileChanged(it->originalPath, false);
}
} else {
it->savedInfo = newInfo;
}
} else {
if (errno == ENOENT) {
if (emitSignals) {
if (directory)
emit directoryChanged(it->originalPath, true);
else
emit fileChanged(it->originalPath, true);
}
it = list.erase(it);
continue;
} else {
qWarning("%s:%d:QFSEventsFileSystemWatcherEngine: stat error on %s:%s",
__FILE__, __LINE__, qPrintable(it->originalPath), strerror(errno));
}
}
++it;
}
}
void QFSEventsFileSystemWatcherEngine::updateHash(PathHash &pathHash)
{
PathHash::iterator HashEnd = pathHash.end();
PathHash::iterator it = pathHash.begin();
const bool IsDirectory = (&pathHash == &dirPathInfoHash);
while (it != HashEnd) {
updateList(it.value(), IsDirectory, false);
if (it.value().isEmpty())
it = pathHash.erase(it);
else
++it;
}
}
#endif
void QFSEventsFileSystemWatcherEngine::fseventsCallback(ConstFSEventStreamRef ,
void *clientCallBackInfo, size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId [])
{
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
QFSEventsFileSystemWatcherEngine *watcher = static_cast<QFSEventsFileSystemWatcherEngine *>(clientCallBackInfo);
QMutexLocker locker(&watcher->mutex);
CFArrayRef paths = static_cast<CFArrayRef>(eventPaths);
for (size_t i = 0; i < numEvents; ++i) {
const QString path = QCFString::toQString(
static_cast<CFStringRef>(CFArrayGetValueAtIndex(paths, i)));
const FSEventStreamEventFlags pathFlags = eventFlags[i];
// There are several flags that may be passed, but we really don't care about them ATM.
// Here they are and why we don't care.
// kFSEventStreamEventFlagHistoryDone--(very unlikely to be gotten, but even then, not much changes).
// kFSEventStreamEventFlagMustScanSubDirs--Likely means the data is very much out of date, we
// aren't coalescing our directories, so again not so much of an issue
// kFSEventStreamEventFlagRootChanged | kFSEventStreamEventFlagMount | kFSEventStreamEventFlagUnmount--
// These three flags indicate something has changed, but the stat will likely show this, so
// there's not really much to worry about.
// (btw, FSEvents is not the correct way of checking for mounts/unmounts,
// there are real CarbonCore events for that.)
Q_UNUSED(pathFlags);
if (watcher->filePathInfoHash.contains(path))
watcher->updateList(watcher->filePathInfoHash[path], false, true);
if (watcher->dirPathInfoHash.contains(path))
watcher->updateList(watcher->dirPathInfoHash[path], true, true);
}
#else
Q_UNUSED(clientCallBackInfo);
Q_UNUSED(numEvents);
Q_UNUSED(eventPaths);
Q_UNUSED(eventFlags);
#endif
}
void QFSEventsFileSystemWatcherEngine::stop()
{
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
QMutexLocker locker(&mutex);
stopFSStream(fsStream);
if (threadsRunLoop) {
CFRunLoopStop(threadsRunLoop);
waitForStop.wait(&mutex);
}
#endif
}
void QFSEventsFileSystemWatcherEngine::updateFiles()
{
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
QMutexLocker locker(&mutex);
updateHash(filePathInfoHash);
updateHash(dirPathInfoHash);
if (filePathInfoHash.isEmpty() && dirPathInfoHash.isEmpty()) {
// Everything disappeared before we got to start, don't bother.
stop();
cleanupFSStream(fsStream);
}
waitCondition.wakeAll();
#endif
}
void QFSEventsFileSystemWatcherEngine::run()
{
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
threadsRunLoop = CFRunLoopGetCurrent();
FSEventStreamScheduleWithRunLoop(fsStream, threadsRunLoop, kCFRunLoopDefaultMode);
bool startedOK = FSEventStreamStart(fsStream);
// It's recommended by Apple that you only update the files after you've started
// the stream, because otherwise you might miss an update in between starting it.
updateFiles();
#ifdef QT_NO_DEBUG
Q_UNUSED(startedOK);
#else
Q_ASSERT(startedOK);
#endif
// If for some reason we called stop up above (and invalidated our stream), this call will return
// immediately.
CFRunLoopRun();
threadsRunLoop = 0;
QMutexLocker locker(&mutex);
waitForStop.wakeAll();
#endif
}
QT_END_NAMESPACE
|