1 // Copyright 2015 Google Inc. All rights reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <CoreServices/CoreServices.h>
16 #include <dispatch/dispatch.h>
19 void EventStreamCallback(ConstFSEventStreamRef event_stream
,
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
;
28 for (size_t i
= 0; i
< num_events
; ++i
) {
29 const FSEventStreamEventFlags flags
= event_flags
[i
];
30 const FSEventStreamEventId ids
= event_ids
[i
];
32 char* path
= strdup(event_paths
[i
]);
34 dispatch_async(log_queue
, ^{
35 printf("event %llx %x at %s\n", ids
, flags
, path
);
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
);
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
);
49 FSEventStreamContext context
= {
52 .retain
= (const void* (*)(const void*))&dispatch_retain
,
53 .release
= (void (*)(const void*))&dispatch_release
,
54 .copyDescription
= NULL
56 FSEventStreamRef event_stream
=
57 FSEventStreamCreate(NULL
,
61 kFSEventStreamEventIdSinceNow
,
63 kFSEventStreamCreateFlagFileEvents
);
65 CFRelease(paths_to_watch
);
67 FSEventStreamSetDispatchQueue(event_stream
, event_queue
);
69 if (!FSEventStreamStart(event_stream
)) {
70 fprintf(stderr
, "Failed to start FSEventStream\n");
76 FSEventStreamStop(event_stream
);
77 FSEventStreamSetDispatchQueue(event_stream
, NULL
);
78 FSEventStreamRelease(event_stream
);
80 dispatch_release(event_queue
);
81 dispatch_barrier_sync(log_queue
, ^{
82 dispatch_release(log_queue
);