2008-02-18 03:27:59 +00:00
|
|
|
//
|
|
|
|
// PathWatcher.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 2/17/08.
|
|
|
|
// Copyright 2008 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "PathWatcher.h"
|
|
|
|
|
|
|
|
static void myFSEventCallback(
|
|
|
|
ConstFSEventStreamRef streamRef,
|
|
|
|
void *clientCallBackInfo,
|
|
|
|
size_t numEvents,
|
|
|
|
void *eventPaths,
|
|
|
|
const FSEventStreamEventFlags eventFlags[],
|
|
|
|
const FSEventStreamEventId eventIds[])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char **paths = eventPaths;
|
2016-05-05 20:05:39 +00:00
|
|
|
PathWatcher *pathWatcher = (__bridge PathWatcher *)clientCallBackInfo;
|
2008-02-18 03:27:59 +00:00
|
|
|
|
|
|
|
printf("Callback called\n");
|
|
|
|
for (i=0; i<numEvents; i++) {
|
|
|
|
NSString *pathString = [[NSString alloc] initWithUTF8String:paths[i]];
|
|
|
|
[[pathWatcher delegate] pathDidChange:pathString];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@implementation PathWatcher
|
|
|
|
|
|
|
|
- (void)cleanUp
|
|
|
|
{
|
|
|
|
if (stream) {
|
|
|
|
FSEventStreamStop(stream);
|
|
|
|
FSEventStreamInvalidate(stream);
|
|
|
|
FSEventStreamRelease(stream);
|
|
|
|
stream = NULL;
|
|
|
|
}
|
|
|
|
if (context) {
|
|
|
|
free(context);
|
|
|
|
context = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setPath:(NSString *)path
|
|
|
|
{
|
|
|
|
[self cleanUp];
|
|
|
|
|
|
|
|
//Create FSEvent stream
|
2022-01-19 02:12:57 +00:00
|
|
|
NSArray *pathsToWatch = @[path];
|
2008-02-18 03:27:59 +00:00
|
|
|
|
|
|
|
context = (FSEventStreamContext*)malloc(sizeof(FSEventStreamContext));
|
|
|
|
context->version = 0;
|
2016-05-05 20:05:39 +00:00
|
|
|
context->info = (__bridge void *)self;
|
2008-02-18 03:27:59 +00:00
|
|
|
context->retain = NULL;
|
|
|
|
context->release = NULL;
|
|
|
|
|
|
|
|
// Create the stream, passing in a callback
|
|
|
|
stream = FSEventStreamCreate(NULL,
|
|
|
|
&myFSEventCallback,
|
|
|
|
context,
|
2016-05-05 20:05:39 +00:00
|
|
|
(__bridge CFArrayRef)pathsToWatch,
|
2008-02-18 03:27:59 +00:00
|
|
|
kFSEventStreamEventIdSinceNow, // Or a previous event ID
|
|
|
|
1.0, //latency in seconds
|
|
|
|
kFSEventStreamCreateFlagNone // Watch this and all its subdirectories
|
|
|
|
);
|
|
|
|
|
|
|
|
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
|
|
|
|
|
|
|
|
FSEventStreamStart(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setDelegate:(id)d
|
|
|
|
{
|
|
|
|
delegate = d;
|
|
|
|
}
|
|
|
|
- (id)delegate
|
|
|
|
{
|
|
|
|
return delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[self cleanUp];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|