@eduardo191labs/watcher 0.0.0-stage → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/CMakeLists.txt +82 -0
  2. package/LICENSE +21 -0
  3. package/README.md +135 -2
  4. package/binding.gyp +125 -0
  5. package/index.d.ts +49 -0
  6. package/index.js +55 -0
  7. package/index.js.flow +45 -0
  8. package/package.json +61 -3
  9. package/scripts/build-from-source.js +16 -0
  10. package/src/Backend.cc +186 -0
  11. package/src/Backend.hh +37 -0
  12. package/src/Debounce.cc +113 -0
  13. package/src/Debounce.hh +49 -0
  14. package/src/DirTree.cc +164 -0
  15. package/src/DirTree.hh +50 -0
  16. package/src/Event.hh +109 -0
  17. package/src/Glob.cc +22 -0
  18. package/src/Glob.hh +34 -0
  19. package/src/PromiseRunner.hh +101 -0
  20. package/src/Signal.hh +46 -0
  21. package/src/Watcher.cc +241 -0
  22. package/src/Watcher.hh +73 -0
  23. package/src/binding.cc +268 -0
  24. package/src/kqueue/KqueueBackend.cc +306 -0
  25. package/src/kqueue/KqueueBackend.hh +35 -0
  26. package/src/linux/InotifyBackend.cc +236 -0
  27. package/src/linux/InotifyBackend.hh +34 -0
  28. package/src/macos/FSEventsBackend.cc +338 -0
  29. package/src/macos/FSEventsBackend.hh +20 -0
  30. package/src/shared/BruteForceBackend.cc +41 -0
  31. package/src/shared/BruteForceBackend.hh +25 -0
  32. package/src/unix/fts.cc +50 -0
  33. package/src/unix/legacy.cc +77 -0
  34. package/src/wasm/WasmBackend.cc +132 -0
  35. package/src/wasm/WasmBackend.hh +34 -0
  36. package/src/wasm/include.h +74 -0
  37. package/src/watchman/BSER.cc +302 -0
  38. package/src/watchman/BSER.hh +69 -0
  39. package/src/watchman/IPC.hh +175 -0
  40. package/src/watchman/WatchmanBackend.cc +342 -0
  41. package/src/watchman/WatchmanBackend.hh +35 -0
  42. package/src/windows/WindowsBackend.cc +282 -0
  43. package/src/windows/WindowsBackend.hh +18 -0
  44. package/src/windows/win_utils.cc +44 -0
  45. package/src/windows/win_utils.hh +11 -0
  46. package/wrapper.js +88 -0
package/src/binding.cc ADDED
@@ -0,0 +1,268 @@
1
+ #include <unordered_set>
2
+ #include <node_api.h>
3
+ #include "wasm/include.h"
4
+ #include <napi.h>
5
+ #include "Glob.hh"
6
+ #include "Event.hh"
7
+ #include "Backend.hh"
8
+ #include "Watcher.hh"
9
+ #include "PromiseRunner.hh"
10
+
11
+ using namespace Napi;
12
+
13
+ std::unordered_set<std::string> getIgnorePaths(Env env, Value opts) {
14
+ std::unordered_set<std::string> result;
15
+
16
+ if (opts.IsObject()) {
17
+ Value v = opts.As<Object>().Get(String::New(env, "ignorePaths"));
18
+ if (v.IsArray()) {
19
+ Array items = v.As<Array>();
20
+ for (size_t i = 0; i < items.Length(); i++) {
21
+ Value item = items.Get(Number::New(env, static_cast<double>(i)));
22
+ if (item.IsString()) {
23
+ result.insert(std::string(item.As<String>().Utf8Value().c_str()));
24
+ }
25
+ }
26
+ }
27
+ }
28
+
29
+ return result;
30
+ }
31
+
32
+ std::unordered_set<Glob> getIgnoreGlobs(Env env, Value opts) {
33
+ std::unordered_set<Glob> result;
34
+
35
+ if (opts.IsObject()) {
36
+ Value v = opts.As<Object>().Get(String::New(env, "ignoreGlobs"));
37
+ if (v.IsArray()) {
38
+ Array items = v.As<Array>();
39
+ for (size_t i = 0; i < items.Length(); i++) {
40
+ Value item = items.Get(Number::New(env, static_cast<double>(i)));
41
+ if (item.IsString()) {
42
+ auto key = item.As<String>().Utf8Value();
43
+ try {
44
+ result.emplace(key);
45
+ } catch (const std::regex_error& e) {
46
+ Error::New(env, e.what()).ThrowAsJavaScriptException();
47
+ }
48
+ }
49
+ }
50
+ }
51
+ }
52
+
53
+ return result;
54
+ }
55
+
56
+ std::shared_ptr<Backend> getBackend(Env env, Value opts) {
57
+ Value b = opts.As<Object>().Get(String::New(env, "backend"));
58
+ std::string backendName;
59
+ if (b.IsString()) {
60
+ backendName = std::string(b.As<String>().Utf8Value().c_str());
61
+ }
62
+
63
+ return Backend::getShared(backendName);
64
+ }
65
+
66
+ class WriteSnapshotRunner : public PromiseRunner {
67
+ public:
68
+ WriteSnapshotRunner(Env env, Value dir, Value snap, Value opts)
69
+ : PromiseRunner(env),
70
+ snapshotPath(std::string(snap.As<String>().Utf8Value().c_str())) {
71
+ watcher = Watcher::getShared(
72
+ std::string(dir.As<String>().Utf8Value().c_str()),
73
+ getIgnorePaths(env, opts),
74
+ getIgnoreGlobs(env, opts)
75
+ );
76
+
77
+ backend = getBackend(env, opts);
78
+ }
79
+
80
+ ~WriteSnapshotRunner() {
81
+ watcher->unref();
82
+ backend->unref();
83
+ }
84
+ private:
85
+ std::shared_ptr<Backend> backend;
86
+ WatcherRef watcher;
87
+ std::string snapshotPath;
88
+
89
+ void execute() override {
90
+ backend->writeSnapshot(watcher, &snapshotPath);
91
+ }
92
+ };
93
+
94
+ class GetEventsSinceRunner : public PromiseRunner {
95
+ public:
96
+ GetEventsSinceRunner(Env env, Value dir, Value snap, Value opts)
97
+ : PromiseRunner(env),
98
+ snapshotPath(std::string(snap.As<String>().Utf8Value().c_str())) {
99
+ watcher = std::make_shared<Watcher>(
100
+ std::string(dir.As<String>().Utf8Value().c_str()),
101
+ getIgnorePaths(env, opts),
102
+ getIgnoreGlobs(env, opts)
103
+ );
104
+
105
+ backend = getBackend(env, opts);
106
+ }
107
+
108
+ ~GetEventsSinceRunner() {
109
+ watcher->unref();
110
+ backend->unref();
111
+ }
112
+ private:
113
+ std::shared_ptr<Backend> backend;
114
+ WatcherRef watcher;
115
+ std::string snapshotPath;
116
+
117
+ void execute() override {
118
+ backend->getEventsSince(watcher, &snapshotPath);
119
+ if (watcher->mEvents.hasError()) {
120
+ throw std::runtime_error(watcher->mEvents.getError());
121
+ }
122
+ }
123
+
124
+ Value getResult() override {
125
+ std::vector<Event> events = watcher->mEvents.getEvents();
126
+ Array eventsArray = Array::New(env, events.size());
127
+ uint32_t i = 0;
128
+ for (auto it = events.begin(); it != events.end(); it++) {
129
+ eventsArray.Set(i++, it->toJS(env));
130
+ }
131
+ return eventsArray;
132
+ }
133
+ };
134
+
135
+ template<class Runner>
136
+ Value queueSnapshotWork(const CallbackInfo& info) {
137
+ Env env = info.Env();
138
+ if (info.Length() < 1 || !info[0].IsString()) {
139
+ TypeError::New(env, "Expected a string").ThrowAsJavaScriptException();
140
+ return env.Null();
141
+ }
142
+
143
+ if (info.Length() < 2 || !info[1].IsString()) {
144
+ TypeError::New(env, "Expected a string").ThrowAsJavaScriptException();
145
+ return env.Null();
146
+ }
147
+
148
+ if (info.Length() >= 3 && !info[2].IsObject()) {
149
+ TypeError::New(env, "Expected an object").ThrowAsJavaScriptException();
150
+ return env.Null();
151
+ }
152
+
153
+ Runner *runner = new Runner(info.Env(), info[0], info[1], info[2]);
154
+ return runner->queue();
155
+ }
156
+
157
+ Value writeSnapshot(const CallbackInfo& info) {
158
+ return queueSnapshotWork<WriteSnapshotRunner>(info);
159
+ }
160
+
161
+ Value getEventsSince(const CallbackInfo& info) {
162
+ return queueSnapshotWork<GetEventsSinceRunner>(info);
163
+ }
164
+
165
+ class SubscribeRunner : public PromiseRunner {
166
+ public:
167
+ SubscribeRunner(Env env, Value dir, Value fn, Value opts) : PromiseRunner(env) {
168
+ watcher = Watcher::getShared(
169
+ std::string(dir.As<String>().Utf8Value().c_str()),
170
+ getIgnorePaths(env, opts),
171
+ getIgnoreGlobs(env, opts)
172
+ );
173
+
174
+ backend = getBackend(env, opts);
175
+ watcher->watch(fn.As<Function>());
176
+ }
177
+
178
+ private:
179
+ WatcherRef watcher;
180
+ std::shared_ptr<Backend> backend;
181
+ FunctionReference callback;
182
+
183
+ void execute() override {
184
+ try {
185
+ backend->watch(watcher);
186
+ } catch (std::exception&) {
187
+ watcher->destroy();
188
+ throw;
189
+ }
190
+ }
191
+ };
192
+
193
+ class UnsubscribeRunner : public PromiseRunner {
194
+ public:
195
+ UnsubscribeRunner(Env env, Value dir, Value fn, Value opts) : PromiseRunner(env) {
196
+ watcher = Watcher::getShared(
197
+ std::string(dir.As<String>().Utf8Value().c_str()),
198
+ getIgnorePaths(env, opts),
199
+ getIgnoreGlobs(env, opts)
200
+ );
201
+
202
+ backend = getBackend(env, opts);
203
+ shouldUnwatch = watcher->unwatch(fn.As<Function>());
204
+ }
205
+
206
+ private:
207
+ WatcherRef watcher;
208
+ std::shared_ptr<Backend> backend;
209
+ bool shouldUnwatch;
210
+
211
+ void execute() override {
212
+ if (shouldUnwatch) {
213
+ backend->unwatch(watcher);
214
+ }
215
+ }
216
+ };
217
+
218
+ template<class Runner>
219
+ Value queueSubscriptionWork(const CallbackInfo& info) {
220
+ Env env = info.Env();
221
+ if (info.Length() < 1 || !info[0].IsString()) {
222
+ TypeError::New(env, "Expected a string").ThrowAsJavaScriptException();
223
+ return env.Null();
224
+ }
225
+
226
+ if (info.Length() < 2 || !info[1].IsFunction()) {
227
+ TypeError::New(env, "Expected a function").ThrowAsJavaScriptException();
228
+ return env.Null();
229
+ }
230
+
231
+ if (info.Length() >= 3 && !info[2].IsObject()) {
232
+ TypeError::New(env, "Expected an object").ThrowAsJavaScriptException();
233
+ return env.Null();
234
+ }
235
+
236
+ Runner *runner = new Runner(info.Env(), info[0], info[1], info[2]);
237
+ return runner->queue();
238
+ }
239
+
240
+ Value subscribe(const CallbackInfo& info) {
241
+ return queueSubscriptionWork<SubscribeRunner>(info);
242
+ }
243
+
244
+ Value unsubscribe(const CallbackInfo& info) {
245
+ return queueSubscriptionWork<UnsubscribeRunner>(info);
246
+ }
247
+
248
+ Object Init(Env env, Object exports) {
249
+ exports.Set(
250
+ String::New(env, "writeSnapshot"),
251
+ Function::New(env, writeSnapshot)
252
+ );
253
+ exports.Set(
254
+ String::New(env, "getEventsSince"),
255
+ Function::New(env, getEventsSince)
256
+ );
257
+ exports.Set(
258
+ String::New(env, "subscribe"),
259
+ Function::New(env, subscribe)
260
+ );
261
+ exports.Set(
262
+ String::New(env, "unsubscribe"),
263
+ Function::New(env, unsubscribe)
264
+ );
265
+ return exports;
266
+ }
267
+
268
+ NODE_API_MODULE(watcher, Init)
@@ -0,0 +1,306 @@
1
+ #include <memory>
2
+ #include <poll.h>
3
+ #include <unistd.h>
4
+ #include <libgen.h>
5
+ #include <dirent.h>
6
+ #include <fcntl.h>
7
+ #include <sys/stat.h>
8
+ #include "KqueueBackend.hh"
9
+
10
+ #if __APPLE__
11
+ #define st_mtim st_mtimespec
12
+ #endif
13
+
14
+ #if !defined(O_EVTONLY)
15
+ #define O_EVTONLY O_RDONLY
16
+ #endif
17
+
18
+ #define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec)
19
+
20
+ void KqueueBackend::start() {
21
+ if ((mKqueue = kqueue()) < 0) {
22
+ throw std::runtime_error(std::string("Unable to open kqueue: ") + strerror(errno));
23
+ }
24
+
25
+ // Create a pipe that we will write to when we want to end the thread.
26
+ int err = pipe(mPipe);
27
+ if (err == -1) {
28
+ throw std::runtime_error(std::string("Unable to open pipe: ") + strerror(errno));
29
+ }
30
+
31
+ // Subscribe kqueue to this pipe.
32
+ struct kevent ev;
33
+ EV_SET(
34
+ &ev,
35
+ mPipe[0],
36
+ EVFILT_READ,
37
+ EV_ADD | EV_CLEAR,
38
+ 0,
39
+ 0,
40
+ 0
41
+ );
42
+
43
+ if (kevent(mKqueue, &ev, 1, NULL, 0, 0)) {
44
+ close(mPipe[0]);
45
+ close(mPipe[1]);
46
+ throw std::runtime_error(std::string("Unable to watch pipe: ") + strerror(errno));
47
+ }
48
+
49
+ notifyStarted();
50
+
51
+ struct kevent events[128];
52
+
53
+ while (true) {
54
+ int event_count = kevent(mKqueue, NULL, 0, events, 128, 0);
55
+ if (event_count < 0 || events[0].flags == EV_ERROR) {
56
+ throw std::runtime_error(std::string("kevent error: ") + strerror(errno));
57
+ }
58
+
59
+ // Track all of the watchers that are touched so we can notify them at the end of the events.
60
+ std::unordered_set<WatcherRef> watchers;
61
+
62
+ for (int i = 0; i < event_count; i++) {
63
+ int flags = events[i].fflags;
64
+ int fd = events[i].ident;
65
+ if (fd == mPipe[0]) {
66
+ // pipe was written to. break out of the loop.
67
+ goto done;
68
+ }
69
+
70
+ auto it = mFdToEntry.find(fd);
71
+ if (it == mFdToEntry.end()) {
72
+ // If fd wasn't in our map, we may have already stopped watching it. Ignore the event.
73
+ continue;
74
+ }
75
+
76
+ DirEntry *entry = it->second;
77
+
78
+ if (flags & NOTE_WRITE && entry && entry->isDir) {
79
+ // If a write occurred on a directory, we have to diff the contents of that
80
+ // directory to determine what file was added/deleted.
81
+ compareDir(fd, entry->path, watchers);
82
+ } else {
83
+ std::vector<KqueueSubscription *> subs = findSubscriptions(entry->path);
84
+ for (auto it = subs.begin(); it != subs.end(); it++) {
85
+ KqueueSubscription *sub = *it;
86
+ watchers.insert(sub->watcher);
87
+ if (flags & (NOTE_DELETE | NOTE_RENAME | NOTE_REVOKE)) {
88
+ sub->watcher->mEvents.remove(sub->path);
89
+ sub->tree->remove(sub->path);
90
+ mFdToEntry.erase((int)(size_t)entry->state);
91
+ mSubscriptions.erase(sub->path);
92
+ } else if (flags & (NOTE_WRITE | NOTE_ATTRIB | NOTE_EXTEND)) {
93
+ struct stat st;
94
+ lstat(sub->path.c_str(), &st);
95
+ if (entry->mtime != CONVERT_TIME(st.st_mtim)) {
96
+ entry->mtime = CONVERT_TIME(st.st_mtim);
97
+ sub->watcher->mEvents.update(sub->path);
98
+ }
99
+ }
100
+ }
101
+ }
102
+ }
103
+
104
+ for (auto it = watchers.begin(); it != watchers.end(); it++) {
105
+ (*it)->notify();
106
+ }
107
+ }
108
+
109
+ done:
110
+ close(mPipe[0]);
111
+ close(mPipe[1]);
112
+ mEndedSignal.notify();
113
+ }
114
+
115
+ KqueueBackend::~KqueueBackend() {
116
+ write(mPipe[1], "X", 1);
117
+ mEndedSignal.wait();
118
+ }
119
+
120
+ void KqueueBackend::subscribe(WatcherRef watcher) {
121
+ // Build a full directory tree recursively, and watch each directory.
122
+ std::shared_ptr<DirTree> tree = getTree(watcher);
123
+
124
+ for (auto it = tree->entries.begin(); it != tree->entries.end(); it++) {
125
+ bool success = watchDir(watcher, it->second.path, tree);
126
+ if (!success) {
127
+ throw WatcherError(std::string("error watching " + watcher->mDir + ": " + strerror(errno)), watcher);
128
+ }
129
+ }
130
+ }
131
+
132
+ bool KqueueBackend::watchDir(WatcherRef watcher, std::string path, std::shared_ptr<DirTree> tree) {
133
+ if (watcher->isIgnored(path)) {
134
+ return false;
135
+ }
136
+
137
+ DirEntry *entry = tree->find(path);
138
+ if (!entry) {
139
+ return false;
140
+ }
141
+
142
+ KqueueSubscription sub = {
143
+ .watcher = watcher,
144
+ .path = path,
145
+ .tree = tree
146
+ };
147
+
148
+ if (!entry->state) {
149
+ int fd = open(path.c_str(), O_EVTONLY);
150
+ if (fd <= 0) {
151
+ return false;
152
+ }
153
+
154
+ struct kevent event;
155
+ EV_SET(
156
+ &event,
157
+ fd,
158
+ EVFILT_VNODE,
159
+ EV_ADD | EV_CLEAR | EV_ENABLE,
160
+ NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE,
161
+ 0,
162
+ 0
163
+ );
164
+
165
+ if (kevent(mKqueue, &event, 1, NULL, 0, 0)) {
166
+ close(fd);
167
+ return false;
168
+ }
169
+
170
+ entry->state = (void *)(size_t)fd;
171
+ mFdToEntry.emplace(fd, entry);
172
+ }
173
+
174
+ sub.fd = (int)(size_t)entry->state;
175
+ mSubscriptions.emplace(path, sub);
176
+ return true;
177
+ }
178
+
179
+ std::vector<KqueueSubscription *> KqueueBackend::findSubscriptions(std::string &path) {
180
+ // Find the subscriptions affected by this path.
181
+ // Copy pointers to them into a vector so that modifying mSubscriptions doesn't invalidate the iterator.
182
+ auto range = mSubscriptions.equal_range(path);
183
+ std::vector<KqueueSubscription *> subs;
184
+ for (auto it = range.first; it != range.second; it++) {
185
+ subs.push_back(&it->second);
186
+ }
187
+
188
+ return subs;
189
+ }
190
+
191
+ bool KqueueBackend::compareDir(int fd, std::string &path, std::unordered_set<WatcherRef> &watchers) {
192
+ // macOS doesn't support fdclosedir, so we have to duplicate the file descriptor
193
+ // to ensure the closedir doesn't also stop watching.
194
+ #if __APPLE__
195
+ fd = dup(fd);
196
+ #endif
197
+
198
+ DIR *dir = fdopendir(fd);
199
+ if (dir == NULL) {
200
+ return false;
201
+ }
202
+
203
+ // fdopendir doesn't rewind to the beginning.
204
+ rewinddir(dir);
205
+
206
+ std::vector<KqueueSubscription *> subs = findSubscriptions(path);
207
+ std::string dirStart = path + DIR_SEP;
208
+
209
+ std::unordered_set<std::shared_ptr<DirTree>> trees;
210
+ for (auto it = subs.begin(); it != subs.end(); it++) {
211
+ trees.emplace((*it)->tree);
212
+ }
213
+
214
+ std::unordered_set<std::string> entries;
215
+ struct dirent *entry;
216
+ while ((entry = readdir(dir))) {
217
+ if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
218
+ continue;
219
+ }
220
+
221
+ std::string fullpath = dirStart + entry->d_name;
222
+ entries.emplace(fullpath);
223
+
224
+ for (auto it = trees.begin(); it != trees.end(); it++) {
225
+ std::shared_ptr<DirTree> tree = *it;
226
+ if (!tree->find(fullpath)) {
227
+ struct stat st;
228
+ fstatat(fd, entry->d_name, &st, AT_SYMLINK_NOFOLLOW);
229
+ tree->add(fullpath, CONVERT_TIME(st.st_mtim), S_ISDIR(st.st_mode));
230
+
231
+ // Notify all watchers with the same tree.
232
+ for (auto i = subs.begin(); i != subs.end(); i++) {
233
+ KqueueSubscription *sub = *i;
234
+ if (sub->tree == tree) {
235
+ if (sub->watcher->isIgnored(fullpath)) {
236
+ continue;
237
+ }
238
+
239
+ sub->watcher->mEvents.create(fullpath);
240
+ watchers.emplace(sub->watcher);
241
+
242
+ bool success = watchDir(sub->watcher, fullpath, sub->tree);
243
+ if (!success) {
244
+ sub->tree->remove(fullpath);
245
+ return false;
246
+ }
247
+ }
248
+ }
249
+ }
250
+ }
251
+ }
252
+
253
+ for (auto it = trees.begin(); it != trees.end(); it++) {
254
+ std::shared_ptr<DirTree> tree = *it;
255
+ for (auto entry = tree->entries.begin(); entry != tree->entries.end();) {
256
+
257
+ if (
258
+ entry->first.rfind(dirStart, 0) == 0 &&
259
+ entry->first.find(DIR_SEP, dirStart.length()) == std::string::npos &&
260
+ entries.count(entry->first) == 0
261
+ ) {
262
+ // Notify all watchers with the same tree.
263
+ for (auto i = subs.begin(); i != subs.end(); i++) {
264
+ if ((*i)->tree == tree) {
265
+ KqueueSubscription *sub = *i;
266
+ if (!sub->watcher->isIgnored(entry->first)) {
267
+ sub->watcher->mEvents.remove(entry->first);
268
+ watchers.emplace(sub->watcher);
269
+ }
270
+ }
271
+ }
272
+
273
+ mFdToEntry.erase((int)(size_t)entry->second.state);
274
+ mSubscriptions.erase(entry->first);
275
+ entry = tree->entries.erase(entry);
276
+ } else {
277
+ entry++;
278
+ }
279
+ }
280
+ }
281
+
282
+ #if __APPLE__
283
+ closedir(dir);
284
+ #else
285
+ fdclosedir(dir);
286
+ #endif
287
+
288
+ return true;
289
+ }
290
+
291
+ void KqueueBackend::unsubscribe(WatcherRef watcher) {
292
+ // Find any subscriptions pointing to this watcher, and remove them.
293
+ for (auto it = mSubscriptions.begin(); it != mSubscriptions.end();) {
294
+ if (it->second.watcher.get() == watcher.get()) {
295
+ if (mSubscriptions.count(it->first) == 1) {
296
+ // Closing the file descriptor automatically unwatches it in the kqueue.
297
+ close(it->second.fd);
298
+ mFdToEntry.erase(it->second.fd);
299
+ }
300
+
301
+ it = mSubscriptions.erase(it);
302
+ } else {
303
+ it++;
304
+ }
305
+ }
306
+ }
@@ -0,0 +1,35 @@
1
+ #ifndef KQUEUE_H
2
+ #define KQUEUE_H
3
+
4
+ #include <unordered_map>
5
+ #include <sys/event.h>
6
+ #include "../shared/BruteForceBackend.hh"
7
+ #include "../DirTree.hh"
8
+ #include "../Signal.hh"
9
+
10
+ struct KqueueSubscription {
11
+ WatcherRef watcher;
12
+ std::string path;
13
+ std::shared_ptr<DirTree> tree;
14
+ int fd;
15
+ };
16
+
17
+ class KqueueBackend : public BruteForceBackend {
18
+ public:
19
+ void start() override;
20
+ ~KqueueBackend();
21
+ void subscribe(WatcherRef watcher) override;
22
+ void unsubscribe(WatcherRef watcher) override;
23
+ private:
24
+ int mKqueue;
25
+ int mPipe[2];
26
+ std::unordered_multimap<std::string, KqueueSubscription> mSubscriptions;
27
+ std::unordered_map<int, DirEntry *> mFdToEntry;
28
+ Signal mEndedSignal;
29
+
30
+ bool watchDir(WatcherRef watcher, std::string path, std::shared_ptr<DirTree> tree);
31
+ bool compareDir(int fd, std::string &dir, std::unordered_set<WatcherRef> &watchers);
32
+ std::vector<KqueueSubscription *> findSubscriptions(std::string &path);
33
+ };
34
+
35
+ #endif