-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwachdir.c
65 lines (54 loc) · 1.57 KB
/
wachdir.c
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
/*
Watches a directory for changes. When one happens, writes the path that
changed to stdout.
Only works on OS X 10.7 (Lion) and up
*/
#include <CoreServices/CoreServices.h>
static void _eventStreamCallback(
__unused ConstFSEventStreamRef streamRef,
__unused void* clientCallBackInfo,
size_t numEvents,
void* paths,
__unused const FSEventStreamEventFlags eventFlags[],
__unused const FSEventStreamEventId eventIds[]
) {
char** pathsArr = paths; // woo! a C type
for (unsigned long i = 0; i < numEvents; i++) { puts(pathsArr[i]); }
fflush(stdout);
}
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr, "usage: %s <directory-to-watch>\n", argv[0]);
exit(1);
}
CFStringRef path = CFStringCreateWithCString(
kCFAllocatorDefault,
argv[1],
kCFStringEncodingUTF8
);
CFArrayRef pathsToWatch = CFArrayCreate(
kCFAllocatorDefault,
(const void **)&path,
1,
NULL
);
// create stream
FSEventStreamRef stream = FSEventStreamCreate(
kCFAllocatorDefault,
_eventStreamCallback,
NULL, // context for callback
pathsToWatch,
kFSEventStreamEventIdSinceNow,
0, // latency
kFSEventStreamCreateFlagFileEvents // this flag was introduced in 10.7
);
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
CFRunLoopRun();
return EXIT_SUCCESS; // CFRunLoopRun never returns, we never get here
}
/*
Potential Improvements
- condition on eventFlags in callback to watch for specific types of changes
- support more than one dir to watch
*/