How to listen for file system changes MAC - kFSEventStreamCreateFlagWatchRoot
Posted
by Cocoa Newbie
on Stack Overflow
See other posts from Stack Overflow
or by Cocoa Newbie
Published on 2010-01-28T13:39:49Z
Indexed on
2010/03/12
22:07 UTC
Read the original article
Hit count: 287
Hi All,
I am listening for Directory and disk changes in a COCOA project using FSEvents. I need to get events when a root folder is renamed or deleted. So, I passed kFSEventStreamCreateFlagWatchRoot while creating the FSEventStream.But even if I delete or rename the root folder I am not getting corresponding FSEventStreamEventFlags. Any idea what could possibly be the issue. I am listening for changes in a USB mounted device. I used both FSEventStreamCreate and FSEventStreamCreateRelativeToDevice. One thing I notices is when I try with FSEventStreamCreate I get the following error message while creating FSEventStream:
(CarbonCore.framework) FSEventStreamCreate: watch_all_parents: error trying to add kqueue for fd 7 (/Volumes/NO NAME; Operation not supported)
But with FSEventStreamCreateRelativeToDevice there are no errors but still not getting kFSEventStreamEventFlagRootChanged in event flags. Also, while creation using FSEventStreamCreateRelativeToDevice apple say's if I want to listen to root path changes pass emty string "". But I am not able to listen to root path changes by passing empty string. But when I pass "/" it works. But even for "/" I do not get any proper FSEventStreamEventFlags. I am pasting the code here:
-(void) subscribeFileSystemChanges:(NSString*) path { PRINT_FUNCTION_BEGIN;
// if already subscribed then unsubscribe
if (stream)
{
FSEventStreamStop(stream);
FSEventStreamInvalidate(stream); /* will remove from runloop */
FSEventStreamRelease(stream);
}
FSEventStreamContext cntxt = {0};
cntxt.info = self;
CFArrayRef pathsToWatch = CFArrayCreate(NULL, (const void**)&path, 1, NULL);
stream = FSEventStreamCreate(NULL, &feCallback, &cntxt,
pathsToWatch, kFSEventStreamEventIdSinceNow, 1,
kFSEventStreamCreateFlagWatchRoot );
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
}
call back function:
static void feCallback(ConstFSEventStreamRef streamRef, void* pClientCallBackInfo,
size_t numEvents, void* pEventPaths, const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[])
{? char** ppPaths = (char**)pEventPaths; int i;
for (i = 0; i < numEvents; i++)
{
NSLog(@"Event Flags %lu Event Id %llu", eventFlags[i], eventIds[i]);
NSLog(@"Path changed: %@",
[NSString stringWithUTF8String:ppPaths[i]]);
}
}
Thanks a lot in advance.
© Stack Overflow or respective owner