forked from SelfControlApp/selfcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCFileWatcher.m
More file actions
104 lines (85 loc) · 3.37 KB
/
SCFileWatcher.m
File metadata and controls
104 lines (85 loc) · 3.37 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
//
// SCFileWatcher.m
// SelfControl
//
// Created by Charlie Stigler on 3/20/21.
//
#import "SCFileWatcher.h"
#include <CoreServices/CoreServices.h>
@implementation SCFileWatcher
static void SCFileWatcherGlobalCallback(
ConstFSEventStreamRef streamRef,
void *callbackCtxInfo,
size_t numEvents,
void *eventPaths, // CFArrayRef
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[])
{
NSArray* paths = (__bridge NSArray*)eventPaths;
SCFileWatcher* watcher = (__bridge SCFileWatcher*)callbackCtxInfo;
[watcher directoryWatcherTriggered: paths flags: eventFlags];
}
- (void)directoryWatcherTriggered:(NSArray<NSString*>*)eventPaths flags:(const FSEventStreamEventFlags[])eventFlags {
BOOL triggerFileWatcher = NO;
for (unsigned int i = 0; i < eventPaths.count; i++) {
NSString* eventPath = [eventPaths[i] stringByStandardizingPath];
if ([eventPath isEqualToString: self.filePath]) {
triggerFileWatcher = YES;
}
}
if (triggerFileWatcher) {
self.callbackBlock(nil);
}
}
+ (instancetype)watcherWithFile:(NSString*)watchPath block:(void(^)(NSError* error))callbackBlock {
return [[SCFileWatcher new] initWithFile: watchPath block: callbackBlock];
}
- (instancetype)initWithFile:(NSString*)watchPath block:(void(^)(NSError* error))callbackBlock {
self = [super init];
NSFileManager* fileMan = [NSFileManager defaultManager];
_filePath = [watchPath stringByStandardizingPath];
BOOL isDirectory;
[fileMan fileExistsAtPath: self.filePath isDirectory: &isDirectory];
NSString* directoryPath;
if (isDirectory) {
directoryPath = self.filePath;
} else {
directoryPath = [self.filePath stringByDeletingLastPathComponent];
}
FSEventStreamContext callbackCtx;
callbackCtx.version = 0;
callbackCtx.info = (__bridge void *)self;
callbackCtx.retain = NULL;
callbackCtx.release = NULL;
callbackCtx.copyDescription = NULL;
FSEventStreamRef eventStream = FSEventStreamCreate(
kCFAllocatorDefault,
&SCFileWatcherGlobalCallback,
&callbackCtx, // context
(__bridge CFArrayRef)@[directoryPath],
kFSEventStreamEventIdSinceNow,
1.5, // seconds to throttle callbacks
kFSEventStreamCreateFlagUseCFTypes | kFSEventStreamCreateFlagMarkSelf | kFSEventStreamCreateFlagIgnoreSelf | kFSEventStreamCreateFlagFileEvents
);
FSEventStreamScheduleWithRunLoop(eventStream,
[[NSRunLoop currentRunLoop] getCFRunLoop],
kCFRunLoopDefaultMode);
if (!FSEventStreamStart(eventStream)) {
NSLog(@"WARNING: failed to start watching file %@", watchPath);
FSEventStreamUnscheduleFromRunLoop(eventStream, [[NSRunLoop currentRunLoop] getCFRunLoop], kCFRunLoopDefaultMode);
FSEventStreamInvalidate(eventStream);
FSEventStreamRelease(eventStream);
return nil;
}
_eventStream = eventStream;
_callbackBlock = callbackBlock;
return self;
}
- (void)stopWatching {
FSEventStreamStop(self.eventStream);
FSEventStreamUnscheduleFromRunLoop(self.eventStream, [[NSRunLoop currentRunLoop] getCFRunLoop], kCFRunLoopDefaultMode);
FSEventStreamInvalidate(self.eventStream);
FSEventStreamRelease(self.eventStream);
_eventStream = NULL;
}
@end