Inside Leopard FSEvents
I pieced together some of Apple’s sample code for working with Filesystem Events, the API that powers TimeMachine. Might be useful to see what exactly gets modified between backups, or what directories are being modified on a running system. You can drop this source file into an XCode project with CoreFoundation and CoreServices frameworks, or download a universal binary. Should go without saying, but this requires OS X 10.5.
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
/*
* Watch for filesystem changes using FSEvents API.
* 11/14/2007, Greg Heartsfield
*
* No copyright claimed by me, this is mostly Apple
* example code, with a run loop added.
*/
void printDirectories
(
ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[])
{
int i;
char **paths = eventPaths;
for (i=0; i<numEvents; i++) {
printf("%s\n", paths[i],
eventFlags[i]);
}
}
int main (int argc, const char * argv[]) {
fprintf(stderr, "Watching for filesystem changes to /\n");
CFStringRef mypath = CFSTR("/");
CFArrayRef pathsToWatch =
CFArrayCreate(NULL,
(const void **)&mypath,
1, NULL);
void *callbackInfo = NULL;
FSEventStreamRef stream;
CFAbsoluteTime latency = 2.0;
stream =
FSEventStreamCreate(NULL,
printDirectories,
callbackInfo,
pathsToWatch,
kFSEventStreamEventIdSinceNow,
latency,
kFSEventStreamCreateFlagNone
);
CFRunLoopRef mainLoop = CFRunLoopGetCurrent();
FSEventStreamScheduleWithRunLoop(stream,
mainLoop,
kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
CFRunLoopRun();
return 0;
}