-
Notifications
You must be signed in to change notification settings - Fork 3
/
MCCFileEventQueue.m
511 lines (389 loc) · 17.6 KB
/
MCCFileEventQueue.m
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//
// MCCFileEventQueue.m
// MCCMailCommon
//
// Created by Scott Little on 23/2/14.
// Copyright (c) 2014 Little Known Software, Inc. All rights reserved.
//
#import "MCCFileEventQueue.h"
//#import <unistd.h>
//#import <fcntl.h>
//#include <sys/stat.h>
NSString *MCC_PREFIXED_NAME(FileEventRenameNotification) = @"MCCFileEventRenameNotification";
NSString *MCC_PREFIXED_NAME(FileEventWriteNotification) = @"MCCFileEventWriteNotification";
NSString *MCC_PREFIXED_NAME(FileEventDeleteNotification) = @"MCCFileEventDeleteNotification";
NSString *MCC_PREFIXED_NAME(FileEventAttributeChangeNotification) = @"MCCFileEventAttributeChangeNotification";
NSString *MCC_PREFIXED_NAME(FileEventSizeIncreaseNotification) = @"MCCFileEventSizeIncreaseNotification";
NSString *MCC_PREFIXED_NAME(FileEventLinkCountChangeNotification) = @"MCCFileEventLinkCountChangeNotification";
NSString *MCC_PREFIXED_NAME(FileEventAccessRevocationNotification) = @"MCCFileEventAccessRevocationNotification";
// This is a simple model class used to hold info about each path we watch.
@interface MCC_PREFIXED_NAME(PathEntry) : NSObject
@property (atomic, strong) NSString *path;
@property (atomic, assign) BOOL handleAtomically;
@property (atomic, assign) int watchedFD;
@property (atomic, assign) NSUInteger subscriptionFlags;
@property (strong) MCC_PREFIXED_NAME(PathBlock) block;
- (id)initWithPath:(NSString *)aPath block:(MCC_PREFIXED_NAME(PathBlock))aBlock subscriptionFlags:(NSUInteger)flags atomically:(BOOL)isAtomic;
@end
@interface MCC_PREFIXED_NAME(ProcessEntry) : NSObject
@property (strong) NSString *name;
@property (strong) NSString *bundleID;
@property (assign) pid_t processID;
@property (strong) MCC_PREFIXED_NAME(ProcessQuitBlock) block;
- (id)initWithBundleID:(NSString *)bundleIdentifier block:(MCC_PREFIXED_NAME(ProcessQuitBlock))aBlock;
- (id)initWithProcessID:(pid_t)processIdentifier block:(MCC_PREFIXED_NAME(ProcessQuitBlock))aBlock;
@end
@interface MCC_PREFIXED_NAME(FileEventQueue) ()
@property (strong) NSMutableDictionary *watchedPathEntries;
@property (strong) NSMutableArray *watchedAtomicEntries;
@property (strong) NSMutableArray *watchedProcessEntries;
@property (strong) dispatch_queue_t modifyEventQueue;
@property (assign) int coreQueueFD;
@property (assign) BOOL keepWatcherThreadRunning;
@end
@implementation MCC_PREFIXED_NAME(FileEventQueue)
#pragma mark Public Methods
- (void)executeBlock:(MCC_PREFIXED_NAME(ProcessQuitBlock))processBlock forProcessIDOnExit:(pid_t)processID {
id pe = [[MCC_PREFIXED_NAME(ProcessEntry) alloc] initWithProcessID:processID block:processBlock];
MCC_AUTORELEASE(pe);
[self watchForExitOfProcessEntry:pe];
}
- (void)executeBlock:(MCC_PREFIXED_NAME(ProcessQuitBlock))processBlock forBundleIDOnExit:(NSString *)bundleID {
id pe = [[MCC_PREFIXED_NAME(ProcessEntry) alloc] initWithBundleID:bundleID block:processBlock];
MCC_AUTORELEASE(pe);
[self watchForExitOfProcessEntry:pe];
}
- (void)addPath:(NSString *)aPath {
if (!aPath) return;
[self addPath:aPath withBlock:nil notifyingAbout:MCCNotifyFileDefault];
}
- (void)addPath:(NSString *)aPath notifyingAbout:(NSUInteger)flags {
if (!aPath) return;
[self addPath:aPath withBlock:nil notifyingAbout:flags];
}
- (void)addPath:(NSString *)aPath withBlock:(MCC_PREFIXED_NAME(PathBlock))aBlock notifyingAbout:(NSUInteger)flags; {
if (!aPath) return;
if ([self addPathToQueue:aPath block:aBlock notifyingAbout:flags atomically:NO] == nil) {
NSLog(@"%@ tried to add the path %@ to watchedPathEntries, but the PathEntry was nil. \nIt's possible that the host process has hit its max open file descriptors limit.", [self className], aPath);
}
}
- (void)readdPath:(NSString *)aPath {
MCC_PREFIXED_NAME(PathEntry) *pathEntry = [self.watchedPathEntries objectForKey:aPath];
dispatch_sync(self.modifyEventQueue, ^{
[self synchronouslyRemovePath:aPath];
[self synchronouslyAddPath:aPath withBlock:pathEntry.block notifyingAbout:pathEntry.subscriptionFlags atomically:pathEntry.handleAtomically];
});
}
- (void)addAtomicPath:(NSString *)aPath {
if (!aPath) return;
[self addAtomicPath:aPath withBlock:nil notifyingAbout:MCCNotifyFileDefault];
}
- (void)addAtomicPath:(NSString *)aPath notifyingAbout:(NSUInteger)flags {
if (!aPath) return;
[self addAtomicPath:aPath withBlock:nil notifyingAbout:flags];
}
- (void)addAtomicPath:(NSString *)aPath withBlock:(MCC_PREFIXED_NAME(PathBlock))aBlock notifyingAbout:(NSUInteger)flags; {
if (!aPath) return;
if ([self addPathToQueue:aPath block:aBlock notifyingAbout:flags atomically:YES] == nil) {
NSLog(@"%@ tried to add the path %@ to watchedPathEntries, but the PathEntry was nil. \nIt's possible that the host process has hit its max open file descriptors limit.", [self className], aPath);
}
}
- (void)removePath:(NSString *)aPath {
if (!aPath) return;
dispatch_sync(self.modifyEventQueue, ^{
[self synchronouslyRemovePath:aPath];
});
}
- (void)removeAllPaths {
NSMutableDictionary *pathEntries = self.watchedPathEntries;
dispatch_sync(self.modifyEventQueue, ^{
for (id pathKey in [pathEntries allKeys]) {
[self synchronouslyRemovePath:pathKey];
}
[pathEntries removeAllObjects];
});
}
- (NSUInteger)numberOfWatchedPaths {
NSUInteger __block count;
NSMutableDictionary *pathEntries = self.watchedPathEntries;
dispatch_sync(self.modifyEventQueue, ^{
count = [pathEntries count];
});
return count;
}
#pragma mark Private Methods
- (MCC_PREFIXED_NAME(PathEntry) *)addPathToQueue:(NSString *)aPath block:(MCC_PREFIXED_NAME(PathBlock))aBlock notifyingAbout:(NSUInteger)flags atomically:(BOOL)isAtomic {
MCC_PREFIXED_NAME(PathEntry) __block *pathEntry = nil;
dispatch_sync(self.modifyEventQueue, ^{
pathEntry = [self synchronouslyAddPath:aPath withBlock:aBlock notifyingAbout:flags atomically:isAtomic];
});
// Start the thread that fetches and processes our events if it's not already running.
if (pathEntry && !self.keepWatcherThreadRunning) {
self.keepWatcherThreadRunning = YES;
[NSThread detachNewThreadSelector:@selector(watcherThread:) toTarget:self withObject:nil];
}
return pathEntry;
}
- (MCC_PREFIXED_NAME(PathEntry) *)synchronouslyAddPath:(NSString *)aPath withBlock:(id)aBlock notifyingAbout:(NSUInteger)blockFlags atomically:(BOOL)isAtomic {
// Are we already watching this path?
MCC_PREFIXED_NAME(PathEntry) *pathEntry = [self.watchedPathEntries objectForKey:aPath];
if (pathEntry) {
// All flags already set?
if(([pathEntry subscriptionFlags] & blockFlags) == blockFlags) {
return pathEntry;
}
blockFlags |= [pathEntry subscriptionFlags];
}
if (!pathEntry) {
pathEntry = [[MCC_PREFIXED_NAME(PathEntry) alloc] initWithPath:aPath block:aBlock subscriptionFlags:blockFlags atomically:isAtomic];
MCC_AUTORELEASE(pathEntry);
}
if (pathEntry) {
struct timespec nullts = { 0, 0 };
struct kevent ev;
EV_SET(&ev, [pathEntry watchedFD], EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, blockFlags, 0, (__bridge void *)(pathEntry));
[pathEntry setSubscriptionFlags:blockFlags];
[self.watchedPathEntries setObject:pathEntry forKey:aPath];
kevent(self.coreQueueFD, &ev, 1, NULL, 0, &nullts);
if (isAtomic) {
[self.watchedAtomicEntries addObject:aPath];
}
}
return pathEntry;
}
- (void)synchronouslyRemovePath:(NSString *)aPath {
MCC_PREFIXED_NAME(PathEntry) *entry = [self.watchedPathEntries objectForKey:aPath];
// Remove it only if we're watching it.
if (entry) {
// Remove the kevent for this path
struct timespec nullts = { 0, 0 };
struct kevent ev;
EV_SET(&ev, entry.watchedFD, EVFILT_VNODE, EV_DELETE, entry.subscriptionFlags, 0, NULL);
kevent(self.coreQueueFD, &ev, 1, NULL, 0, &nullts);
// Remove from our list
[self.watchedPathEntries removeObjectForKey:aPath];
}
}
//
// WARNING: This thread has no active autorelease pool, so if you make changes, you must manually manage
// memory without relying on autorelease. Otherwise, you will leak!
//
- (void) watcherThread:(id)sender {
int n;
struct kevent ev;
struct timespec timeout = { 1, 0 }; // 1 second timeout. Should be longer, but we need this thread to exit when a kqueue is dealloced, so 1 second timeout is quite a while to wait.
int theFD = self.coreQueueFD; // So we don't have to risk accessing iVars when the thread is terminated.
NSMutableArray *notesToPost = [NSMutableArray arrayWithCapacity:5];
#if DEBUG_LOG_THREAD_LIFETIME
NSLog(@"watcherThread started.");
#endif
while(self.keepWatcherThreadRunning) {
@try {
n = kevent(theFD, NULL, 0, &ev, 1, &timeout);
if (n > 0) {
//NSLog( @"KEVENT returned %d", n );
if (ev.filter == EVFILT_VNODE) {
//NSLog( @"KEVENT filter is EVFILT_VNODE" );
if (ev.fflags) {
//NSLog( @"KEVENT flags are set" );
id pe = (__bridge id)(ev.udata);
if (pe && [pe respondsToSelector:@selector(path)]) {
MCC_PREFIXED_NAME(PathEntry) *pathEntry = (MCC_PREFIXED_NAME(PathEntry) *)pe;
NSString *fpath = pathEntry.path;
if (!fpath) continue;
if (![self.watchedPathEntries valueForKey:fpath]) continue;
[[NSWorkspace sharedWorkspace] noteFileSystemChanged:fpath];
// Clear any old notifications
[notesToPost removeAllObjects];
// Figure out which notifications we need to issue
if ((ev.fflags & NOTE_RENAME) == NOTE_RENAME) {
[notesToPost addObject:MCC_PREFIXED_NAME(FileEventRenameNotification)];
}
if ((ev.fflags & NOTE_WRITE) == NOTE_WRITE) {
[notesToPost addObject:MCC_PREFIXED_NAME(FileEventWriteNotification)];
}
if ((ev.fflags & NOTE_DELETE) == NOTE_DELETE) {
[notesToPost addObject:MCC_PREFIXED_NAME(FileEventDeleteNotification)];
}
if ((ev.fflags & NOTE_ATTRIB) == NOTE_ATTRIB) {
[notesToPost addObject:MCC_PREFIXED_NAME(FileEventAttributeChangeNotification)];
}
if ((ev.fflags & NOTE_EXTEND) == NOTE_EXTEND) {
[notesToPost addObject:MCC_PREFIXED_NAME(FileEventSizeIncreaseNotification)];
}
if ((ev.fflags & NOTE_LINK) == NOTE_LINK) {
[notesToPost addObject:MCC_PREFIXED_NAME(FileEventLinkCountChangeNotification)];
}
if ((ev.fflags & NOTE_REVOKE) == NOTE_REVOKE) {
[notesToPost addObject:MCC_PREFIXED_NAME(FileEventAccessRevocationNotification)];
}
NSArray *notes = [[NSArray alloc] initWithArray:notesToPost]; // notesToPost will be changed in the next loop iteration, which will likely occur before the block below runs.
// Post the notifications (or call the delegate method) on the main thread.
MCC_PREFIXED_NAME(FileEventQueue) MCC_WEAK_BLOCK * blockSelf = self;
id<MCC_PREFIXED_NAME(FileEventDelegate)> MCC_WEAK_BLOCK myDelegate = self.delegate;
BOOL forceNotifications = self.shouldAlwaysPostNotifications;
dispatch_async(dispatch_get_main_queue(),^{
for (NSString *note in notes) {
[myDelegate fileEvent:blockSelf receivedNotification:note forPath:fpath];
if (pathEntry.block) {
pathEntry.block(blockSelf, note, pathEntry.path);
}
if ((!myDelegate && !pathEntry.block) || forceNotifications) {
[[[NSWorkspace sharedWorkspace] notificationCenter] postNotificationName:note object:blockSelf userInfo:@{@"path": fpath}];
}
// If the path is marked as always being done atomically, readd the path to the queue.
if (pathEntry.handleAtomically) {
[self readdPath:pathEntry.path];
}
}
});
}
}
}
else if (ev.filter == EVFILT_PROC) {
id pe = (__bridge id)(ev.udata);
if (pe && [pe respondsToSelector:@selector(bundleID)]) {
MCC_PREFIXED_NAME(FileEventQueue) MCC_WEAK_BLOCK * blockSelf = self;
MCC_PREFIXED_NAME(ProcessEntry) * processEntry = (MCC_PREFIXED_NAME(ProcessEntry) *)pe;
if (processEntry.block) {
dispatch_async(dispatch_get_main_queue(), ^{
processEntry.block(blockSelf, processEntry.name, processEntry.bundleID, processEntry.processID);
});
}
int theFileDescriptor = self.coreQueueFD;
NSMutableArray MCC_WEAK_BLOCK * processEntries = self.watchedProcessEntries;
dispatch_async(self.modifyEventQueue, ^{
// Remove the event from the queue
// Remove the kevent for this path
struct timespec nullts = { 0, 0 };
struct kevent removeEvent;
EV_SET(&removeEvent, processEntry.processID, EVFILT_PROC, EV_DELETE, NOTE_EXIT, 0, NULL);
kevent(theFileDescriptor, &removeEvent, 1, NULL, 0, &nullts);
// Remove the entry from our list
[processEntries removeObject:processEntry];
});
}
}
}
}
@catch (NSException *localException) {
NSLog(@"Error in %@ watcherThread: %@", [self className], localException);
}
}
// Close our kqueue's file descriptor
if (close(theFD) == -1) {
NSLog(@"%@ watcherThread: Couldn't close main kqueue (%d)", [self className], errno);
}
#if DEBUG_LOG_THREAD_LIFETIME
NSLog(@"watcherThread finished.");
#endif
}
- (void)watchForExitOfProcessEntry:(MCC_PREFIXED_NAME(ProcessEntry) *)processEntry {
if (processEntry == nil) {
return;
}
int theFileDescriptor = self.coreQueueFD;
NSMutableArray MCC_WEAK_BLOCK * processEntries = self.watchedProcessEntries;
dispatch_async(self.modifyEventQueue, ^{
// Add the kevent for this process
struct timespec nullts = { 0, 0 };
struct kevent ev;
EV_SET(&ev, processEntry.processID, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, (__bridge void *)processEntry);
if (kevent(theFileDescriptor, &ev, 1, NULL, 0, &nullts) != -1) {
[processEntries addObject:processEntry];
}
});
}
#pragma mark - Memory Management
- (id) init {
self = [super init];
if (self) {
self.coreQueueFD = kqueue();
if (self.coreQueueFD == -1) {
MCC_RELEASE(self);
return nil;
}
self.shouldAlwaysPostNotifications = NO;
self.watchedPathEntries = [NSMutableDictionary dictionary];
self.watchedAtomicEntries = [NSMutableArray array];
self.watchedProcessEntries = [NSMutableArray array];
NSString * queueName = [NSString stringWithFormat:@"%@.modifyEventQueue", [self className]];
self.modifyEventQueue = dispatch_queue_create([queueName UTF8String], 0);
MCC_AUTORELEASE(self.modifyEventQueue);
}
return self;
}
- (void)dealloc {
// Shut down the thread that's scanning for kQueue events
self.keepWatcherThreadRunning = NO;
#if !__has_feature(objc_arc)
self.watchedPathEntries = nil;
self.watchedProcessEntries = nil;
self.modifyEventQueue = nil;
#endif
// Do this to close all the open file descriptors for files we're watching
[self removeAllPaths];
MCC_DEALLOC();
}
@end
#pragma mark - PathEntry
@implementation MCC_PREFIXED_NAME(PathEntry)
- (instancetype)initWithPath:(NSString *)aPath block:(MCC_PREFIXED_NAME(PathBlock))aBlock subscriptionFlags:(NSUInteger)flags atomically:(BOOL)isAtomic {
self = [super init];
if (self) {
self.watchedFD = open([aPath fileSystemRepresentation], O_EVTONLY, 0);
if (self.watchedFD < 0) {
MCC_RELEASE(self);
return nil;
}
self.path = aPath;
self.subscriptionFlags = flags;
self.block = aBlock;
self.handleAtomically = isAtomic;
}
return self;
}
- (void)dealloc {
if (self.watchedFD >= 0) close((int)self.watchedFD);
self.watchedFD = -1;
#if !__has_feature(objc_arc)
self.path = nil;
self.block = nil;
#endif
MCC_DEALLOC();
}
@end
#pragma mark - ProcessEntry
@interface MCC_PREFIXED_NAME(ProcessEntry) ()
- (id)initWithRunningApplication:(NSRunningApplication *)runningApp block:(MCC_PREFIXED_NAME(ProcessQuitBlock))aBlock;
@end
@implementation MCC_PREFIXED_NAME(ProcessEntry)
- (id)initWithRunningApplication:(NSRunningApplication *)runningApp block:(MCC_PREFIXED_NAME(ProcessQuitBlock))aBlock {
self = [super init];
if (runningApp == nil) {
MCC_RELEASE(self);
return nil;
}
if (self) {
self.name = runningApp.localizedName;
self.bundleID = runningApp.bundleIdentifier;
self.processID = runningApp.processIdentifier;
self.block = aBlock;
}
return self;
}
- (id)initWithBundleID:(NSString *)bundleIdentifier block:(MCC_PREFIXED_NAME(ProcessQuitBlock))aBlock {
return [self initWithRunningApplication:[[NSRunningApplication runningApplicationsWithBundleIdentifier:bundleIdentifier] lastObject] block:aBlock];
}
- (id)initWithProcessID:(pid_t)processIdentifier block:(MCC_PREFIXED_NAME(ProcessQuitBlock))aBlock {
return [self initWithRunningApplication:[NSRunningApplication runningApplicationWithProcessIdentifier:processIdentifier] block:aBlock];
}
- (void)dealloc {
#if !__has_feature(objc_Arc)
self.name = nil;
self.bundleID = nil;
self.block = nil;
#endif
MCC_DEALLOC();
}
@end