-
Notifications
You must be signed in to change notification settings - Fork 971
Expand file tree
/
Copy pathfolderwatcher_mac.cpp
More file actions
201 lines (168 loc) · 7.42 KB
/
Copy pathfolderwatcher_mac.cpp
File metadata and controls
201 lines (168 loc) · 7.42 KB
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
/*
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2014 ownCloud GmbH
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "folder.h"
#include "folderwatcher.h"
#include "folderwatcher_mac.h"
#include <cerrno>
#include <QDirIterator>
#include <QSet>
#include <QStringList>
namespace OCC {
namespace {
/* Upper bound on the paths one event batch may be expanded into by addCoalescedPaths().
*
* Every reported path costs a journal lookup and a few stats on the GUI thread later on, so for a
* large tree it is both faster and gentler on the UI to stop enumerating and let the next sync
* rediscover the tree instead.
*/
constexpr auto maxCoalescedPaths = 1000;
}
FolderWatcherPrivate::FolderWatcherPrivate(FolderWatcher *p, const QString &path)
: _parent(p)
, _folder(path)
{
this->startWatching();
}
FolderWatcherPrivate::~FolderWatcherPrivate()
{
if (_stream) {
FSEventStreamStop(_stream);
FSEventStreamInvalidate(_stream);
FSEventStreamRelease(_stream);
_stream = nullptr;
}
if (_queue) {
// A callback block may already be running on the queue and still touching this object,
// so let it drain before the members it reads are destroyed.
dispatch_sync(_queue, ^{});
dispatch_release(_queue);
_queue = nullptr;
}
}
static void callback(
ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPathsVoid,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[])
{
Q_UNUSED(streamRef)
Q_UNUSED(eventIds)
qCDebug(lcFolderWatcher) << "FolderWatcherPrivate::callback by OS X";
// These flags indicate that either due to system error or for unknown reasons, the entire
// directory structure must be rescanned (including files)
const auto c_fullScanFlags = kFSEventStreamEventFlagMustScanSubDirs
| kFSEventStreamEventFlagKernelDropped
| kFSEventStreamEventFlagUserDropped;
for (int i = 0; i < static_cast<int>(numEvents); ++i) {
const auto flag = eventFlags[i];
if (flag & c_fullScanFlags) {
reinterpret_cast<FolderWatcherPrivate *>(clientCallBackInfo)->notifyAll();
return;
}
}
const auto c_interestingFlags = kFSEventStreamEventFlagItemCreated // for new folder/file
| kFSEventStreamEventFlagItemRemoved // for rm
| kFSEventStreamEventFlagItemInodeMetaMod // for mtime change
| kFSEventStreamEventFlagItemRenamed // also coming for moves to trash in finder
| kFSEventStreamEventFlagItemModified // for content change
| kFSEventStreamEventFlagItemCloned; // for cloned items (since 10.13)
//We ignore other flags, e.g. for owner change, xattr change, Finder label change etc
QStringList paths;
CFArrayRef eventPaths = (CFArrayRef)eventPathsVoid;
for (int i = 0; i < static_cast<int>(numEvents); ++i) {
CFStringRef path = reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(eventPaths, i));
QString qstring;
CFIndex pathLength = CFStringGetLength(path);
qstring.resize(pathLength);
CFStringGetCharacters(path, CFRangeMake(0, pathLength), reinterpret_cast<UniChar *>(qstring.data()));
QString fn = qstring.normalized(QString::NormalizationForm_C);
if (!(eventFlags[i] & c_interestingFlags)) {
qCDebug(lcFolderWatcher) << "Ignoring non-content changes for" << fn << eventFlags[i];
continue;
}
paths.append(fn);
}
reinterpret_cast<FolderWatcherPrivate *>(clientCallBackInfo)->doNotifyParent(paths);
}
void FolderWatcherPrivate::startWatching()
{
qCDebug(lcFolderWatcher) << "FolderWatcherPrivate::startWatching()" << _folder;
CFStringRef folderCF = CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar *>(_folder.unicode()),
_folder.length());
CFArrayRef pathsToWatch = CFStringCreateArrayBySeparatingStrings(nullptr, folderCF, CFSTR(":"));
FSEventStreamContext ctx = { 0, this, nullptr, nullptr, nullptr };
_stream = FSEventStreamCreate(nullptr,
&callback,
&ctx,
pathsToWatch,
kFSEventStreamEventIdSinceNow,
0, // latency
kFSEventStreamCreateFlagUseCFTypes | kFSEventStreamCreateFlagFileEvents | kFSEventStreamCreateFlagIgnoreSelf);
CFRelease(pathsToWatch);
CFRelease(folderCF);
// Deliver events on a private queue rather than the main one: dropping a large tree into the
// sync folder produces event batches big enough that handling them on the main queue blocks
// the GUI. This mirrors what the Windows watcher does with its own WatcherThread.
_queue = dispatch_queue_create("com.nextcloud.desktopclient.folderwatcher", DISPATCH_QUEUE_SERIAL);
FSEventStreamSetDispatchQueue(_stream, _queue);
FSEventStreamStart(_stream);
}
void FolderWatcherPrivate::addCoalescedPaths(const QStringList &paths, QStringList &coalesced) const
{
// Only the expansion below is bounded, not `paths` itself: those are events FSEvents actually
// reported, they are capped by the kernel's own buffer, and reporting them is what drives
// things a rediscovery cannot redo, such as releasing office file locks.
//
// FSEvents reports a renamed or moved directory, but not the items below it, so the tree is
// walked to report those too.
coalesced = paths;
QSet<QString> seen{paths.begin(), paths.end()};
for (const auto &eventPath : paths) {
if (!QDir(eventPath).exists()) {
continue;
}
QDirIterator it(eventPath, QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
const auto path = it.next();
if (coalesced.size() >= maxCoalescedPaths) {
// Give up on expanding this batch rather than report a whole tree path by path.
// Nothing is lost by stopping: the directory that is being expanded was itself
// reported, and SyncEngine::shouldDiscoverLocally() descends into a touched
// directory's entire subtree, so the entries below it are discovered anyway.
return;
}
if (!seen.contains(path)) {
seen.insert(path);
coalesced.append(path);
}
}
}
}
void FolderWatcherPrivate::doNotifyParent(const QStringList &paths)
{
if (paths.isEmpty()) {
return;
}
QStringList totalPaths;
addCoalescedPaths(paths, totalPaths);
// We are on the private dispatch queue here, but everything FolderWatcher touches from
// changeDetected() (its timers, the lock-file sets, the pathChanged() receivers) may only be
// used on the thread it lives on, so hand the batch over instead of processing it here.
QMetaObject::invokeMethod(
_parent, [parent = _parent, totalPaths] { parent->changeDetected(totalPaths); }, Qt::QueuedConnection);
}
void FolderWatcherPrivate::notifyAll()
{
// FSEvents either dropped events or asked for a full rescan. Enumerating the whole tree to
// report every path would take minutes on a large folder, and a full local discovery re-reads
// it anyway, so ask for one of those instead of reporting anything.
QMetaObject::invokeMethod(
_parent, [parent = _parent, folder = _folder] { parent->changesLost(folder); }, Qt::QueuedConnection);
}
} // ns mirall