-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCmdivatorScanner.m
115 lines (100 loc) · 3.79 KB
/
CmdivatorScanner.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
#import "CmdivatorDirectoryEnumerator.h"
#import "CmdivatorScanner.h"
#import "Common.h"
#define COMMANDS_DIRECTORY_MAXDEPTH 20
#define FS_EVENT_SCAN_DELAY_SECONDS 3
#define FALLBACK_SCAN_INTERVAL_SECONDS 43200
@implementation CmdivatorScanner {
CmdivatorScannerCallback _callback;
CmdivatorDirectoryEnumerator *_enumerator;
NSTimer *_timer;
NSArray *_dirs;
NSMutableArray *_sources;
}
- (instancetype)init {
if ((self = [super init])) {
_enumerator = [[CmdivatorDirectoryEnumerator alloc] initWithMaxDepth:COMMANDS_DIRECTORY_MAXDEPTH includePropertiesForKeys:@[NSURLIsExecutableKey]];
}
return self;
}
- (void)dealloc {
[self stop];
}
- (void)startWithCallback:(CmdivatorScannerCallback)callback {
[self stop];
_callback = callback;
_dirs = @[SYSTEM_COMMANDS_DIRECTORY, USER_COMMANDS_DIRECTORY];
_sources = [[NSMutableArray alloc] init];
NSError * __autoreleasing error = nil;
if (![NSFileManager.defaultManager createDirectoryAtPath:USER_COMMANDS_DIRECTORY withIntermediateDirectories:YES attributes:nil error:&error]) {
LOG(@"Unable to create user commands directory %@: %@", USER_COMMANDS_DIRECTORY, error);
}
for (NSString *dir in _dirs) {
int eventFd = open(dir.fileSystemRepresentation, O_EVTONLY);
if (eventFd >= 0) {
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, eventFd, DISPATCH_VNODE_WRITE, dispatch_get_main_queue());
if (source) {
[_sources addObject:[NSValue valueWithPointer:source]];
CmdivatorScanner * __weak w_self = self;
dispatch_source_set_event_handler(source, ^{
[w_self scheduleScan:FS_EVENT_SCAN_DELAY_SECONDS];
});
dispatch_source_set_cancel_handler(source, ^{
close(eventFd);
});
dispatch_resume(source);
} else {
LOG(@"dispatch_source_create: %@", dir);
close(eventFd);
}
} else {
LOG(@"Unable to watch commands directory %@: [%i] %s", dir, errno, strerror(errno));
}
}
[self scan];
}
- (void)stop {
_callback = nil;
[_timer invalidate];
_timer = nil;
for (NSValue *sourceValue in _sources) {
dispatch_source_t source = sourceValue.pointerValue;
dispatch_source_cancel(source);
dispatch_release(source);
}
_sources = nil;
}
- (void)scan {
[_timer invalidate];
_timer = nil;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableSet *paths = [[NSMutableSet alloc] init];
CmdivatorDirectoryEnumeratorCallback callback = ^(NSURL *url) {
NSError * __autoreleasing error = nil;
NSNumber *isExecutable = nil;
if (![url getResourceValue:&isExecutable forKey:NSURLIsExecutableKey error:&error]) {
LOG(@"Error while scanning filesystem: NSURLIsExecutableKey: at %@: %@", url, error);
} else if (isExecutable.boolValue) {
[paths addObject:url.path];
}
};
for (NSString *dir in _dirs) {
NSURL *dirURL = [[NSURL fileURLWithPath:dir] URLByResolvingSymlinksInPath];
[_enumerator enumerateFilesAtURL:dirURL callback:callback];
}
dispatch_async(dispatch_get_main_queue(), ^{
if (_callback) {
_callback(paths);
[self scheduleScan:FALLBACK_SCAN_INTERVAL_SECONDS];
}
});
});
}
- (void)scheduleScan:(NSUInteger)seconds {
[_timer invalidate];
_timer = nil;
if (_callback) {
_timer = [NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(scan) userInfo:nil repeats:NO];
}
}
@end