I am trying to get unison to fire whenever a file changes in a certain directory. The whole of my code is:
and then in terminal:
for i in {1..10000}; do touch ~/sync/temp.txt; sleep 0.001; done
Then in instruments, i use objectalloc on my process, and just watch the memory grow. Obviously, I would normally do a latency of ~30s, so this problem would occur much slower. However, the idea is to have this background app running constantly. I would love any suggestions! I have been coding with cocoa ~10 days, so I would also enjoy any feedback.
--Tom
Code:
#import <Foundation/Foundation.h>
#include <CoreServices/CoreServices.h>
void mycallback(ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]) {
sync();
}
void sync() {
//NSString* unisonPath = [[NSString alloc] initWithString: @"/usr/local/bin/unison"];
// ** Using /bin/ls for testing purposes **
NSString* unisonPath = [[NSString alloc] initWithString: @"/bin/ls"];
unisonPath = [unisonPath stringByStandardizingPath];
NSTask *unison;
unison = [[NSTask alloc] init];
[unison setLaunchPath:(NSString *)unisonPath];
[unison launch];
[unison release];
[unisonPath release];
}
int main (int argc, const char * argv[]) {
// Based on http://cocoadevcentral.com/articles/000025.php
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *syncPath = @"/Users/tommyboy/sync";
syncPath = [syncPath stringByStandardizingPath];
/* Define variables and create a CFArray object containing
CFString objects containing paths to watch.
*/
CFStringRef mypath = (CFStringRef)syncPath;
CFArrayRef pathsToWatch = CFArrayCreate(NULL, (const void **)&mypath, 1, NULL);
void *callbackInfo = NULL; // could put stream-specific data here.
FSEventStreamRef stream;
CFAbsoluteTime latency = 0; /* Latency in seconds */
/* Create the stream, passing in a callback */
stream = FSEventStreamCreate(NULL,
&mycallback,
callbackInfo,
pathsToWatch,
kFSEventStreamEventIdSinceNow, /* Or a previous event ID */
latency,
kFSEventStreamCreateFlagNone /* Flags explained in reference */
);
FSEventStreamScheduleWithRunLoop(stream,
CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
CFRunLoopRun();
[pool drain];
return 0;
}
and then in terminal:
for i in {1..10000}; do touch ~/sync/temp.txt; sleep 0.001; done
Then in instruments, i use objectalloc on my process, and just watch the memory grow. Obviously, I would normally do a latency of ~30s, so this problem would occur much slower. However, the idea is to have this background app running constantly. I would love any suggestions! I have been coding with cocoa ~10 days, so I would also enjoy any feedback.
--Tom