Created
July 18, 2019 14:53
-
-
Save muirdm/ea8c63c22ca7624f304f805ba77b25f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// clang -framework CoreFoundation -framework CoreServices fs_events.c | |
#include <CoreFoundation/CoreFoundation.h> | |
#include <CoreServices/CoreServices.h> | |
#include <stdio.h> | |
static void callback(ConstFSEventStreamRef stream, | |
void *info, | |
size_t num_events, | |
const char **event_paths, | |
const FSEventStreamEventFlags *event_flags, | |
const FSEventStreamEventId *event_ids) | |
{ | |
printf("%d events\n", (int)num_events); | |
for (int i = 0; i < num_events; i++) { | |
printf("%s\n", event_paths[i]); | |
if ((event_flags[i] & kFSEventStreamEventFlagMustScanSubDirs) > 0) { | |
printf("kFSEventStreamEventFlagMustScanSubDirs\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagUserDropped) > 0) { | |
printf("kFSEventStreamEventFlagUserDropped\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagKernelDropped) > 0) { | |
printf("kFSEventStreamEventFlagKernelDropped\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagEventIdsWrapped) > 0) { | |
printf("kFSEventStreamEventFlagEventIdsWrapped\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagHistoryDone) > 0) { | |
printf("kFSEventStreamEventFlagHistoryDone\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagRootChanged) > 0) { | |
printf("kFSEventStreamEventFlagRootChanged\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagMount) > 0) { | |
printf("kFSEventStreamEventFlagMount\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagUnmount) > 0) { | |
printf("kFSEventStreamEventFlagUnmount\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagItemChangeOwner) > 0) { | |
printf("kFSEventStreamEventFlagItemChangeOwner\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagItemCreated) > 0) { | |
printf("kFSEventStreamEventFlagItemCreated\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagItemFinderInfoMod) > 0) { | |
printf("kFSEventStreamEventFlagItemFinderInfoMod\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagItemInodeMetaMod) > 0) { | |
printf("kFSEventStreamEventFlagItemInodeMetaMod\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagItemIsDir) > 0) { | |
printf("kFSEventStreamEventFlagItemIsDir\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagItemIsFile) > 0) { | |
printf("kFSEventStreamEventFlagItemIsFile\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagItemIsHardlink) > 0) { | |
printf("kFSEventStreamEventFlagItemIsHardlink\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagItemIsLastHardlink) > 0) { | |
printf("kFSEventStreamEventFlagItemIsLastHardlink\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagItemIsSymlink) > 0) { | |
printf("kFSEventStreamEventFlagItemIsSymlink\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagItemModified) > 0) { | |
printf("kFSEventStreamEventFlagItemModified\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagItemRemoved) > 0) { | |
printf("kFSEventStreamEventFlagItemRemoved\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagItemRenamed) > 0) { | |
printf("kFSEventStreamEventFlagItemRenamed\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagItemXattrMod) > 0) { | |
printf("kFSEventStreamEventFlagItemXattrMod\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagOwnEvent) > 0) { | |
printf("kFSEventStreamEventFlagOwnEvent\n"); | |
} | |
if ((event_flags[i] & kFSEventStreamEventFlagItemCloned) > 0) { | |
printf("kFSEventStreamEventFlagItemCloned\n"); | |
} | |
} | |
} | |
int main(int argc, const char **argv) { | |
if (argc != 2) { | |
printf("Please specify a directory.\n"); | |
return 1; | |
} | |
CFStringRef dir = CFStringCreateWithCString(NULL, | |
argv[1], | |
kCFStringEncodingUTF8); | |
CFArrayRef pathsToWatch = CFArrayCreate(NULL, | |
(const void**)&dir, | |
1, | |
&kCFTypeArrayCallBacks); | |
FSEventStreamContext context = { | |
.copyDescription = NULL, | |
.version = 0, | |
.info = NULL, | |
.release = NULL, | |
.retain = NULL, | |
}; | |
FSEventStreamCreateFlags streamFlags = kFSEventStreamCreateFlagFileEvents | kFSEventStreamCreateFlagNoDefer; | |
FSEventStreamRef stream = FSEventStreamCreate(NULL, | |
(FSEventStreamCallback)&callback, | |
&context, | |
pathsToWatch, | |
kFSEventStreamEventIdSinceNow, | |
0.01, | |
streamFlags); | |
assert(stream); | |
CFRunLoopRef run_loop = CFRunLoopGetCurrent(); | |
FSEventStreamScheduleWithRunLoop(stream, run_loop, kCFRunLoopDefaultMode); | |
assert(FSEventStreamStart(stream)); | |
CFRunLoopRun(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment