Check in the Makefile.
[fseventw.git] / fseventw.c
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <CoreServices/CoreServices.h>
16 #include <dispatch/dispatch.h>
17 #include <stdio.h>
18
19 void EventStreamCallback(ConstFSEventStreamRef event_stream,
20 void* callback_info,
21 size_t num_events,
22 void* event_paths_void,
23 const FSEventStreamEventFlags event_flags[],
24 const FSEventStreamEventId event_ids[]) {
25 dispatch_queue_t log_queue = callback_info;
26 const char** event_paths = event_paths_void;
27
28 for (size_t i = 0; i < num_events; ++i) {
29 const FSEventStreamEventFlags flags = event_flags[i];
30 const FSEventStreamEventId ids = event_ids[i];
31
32 char* path = strdup(event_paths[i]);
33
34 dispatch_async(log_queue, ^{
35 printf("event %llx %x at %s\n", ids, flags, path);
36 free(path);
37 });
38 }
39 }
40
41 int main() {
42 dispatch_queue_t log_queue = dispatch_queue_create("com.google.fseventw.logq", DISPATCH_QUEUE_SERIAL);
43 dispatch_queue_t event_queue = dispatch_queue_create("com.google.fseventw.eventq", DISPATCH_QUEUE_CONCURRENT);
44
45 const CFStringRef kPath = CFSTR("/");
46 const void* paths_to_watch_c[] = { kPath };
47 CFArrayRef paths_to_watch = CFArrayCreate(NULL, paths_to_watch_c, 1, &kCFTypeArrayCallBacks);
48
49 FSEventStreamContext context = {
50 .version = 0,
51 .info = log_queue,
52 .retain = (const void* (*)(const void*))&dispatch_retain,
53 .release = (void (*)(const void*))&dispatch_release,
54 .copyDescription = NULL
55 };
56 FSEventStreamRef event_stream =
57 FSEventStreamCreate(NULL,
58 &EventStreamCallback,
59 &context,
60 paths_to_watch,
61 kFSEventStreamEventIdSinceNow,
62 1,
63 kFSEventStreamCreateFlagFileEvents);
64
65 CFRelease(paths_to_watch);
66
67 FSEventStreamSetDispatchQueue(event_stream, event_queue);
68
69 if (!FSEventStreamStart(event_stream)) {
70 fprintf(stderr, "Failed to start FSEventStream\n");
71 return EXIT_FAILURE;
72 }
73
74 dispatch_main();
75
76 FSEventStreamStop(event_stream);
77 FSEventStreamSetDispatchQueue(event_stream, NULL);
78 FSEventStreamRelease(event_stream);
79
80 dispatch_release(event_queue);
81 dispatch_barrier_sync(log_queue, ^{
82 dispatch_release(log_queue);
83 });
84 }