views:

429

answers:

1

I want to monitor file-system events for a couple of directories on the mac. The directories I want to monitor might change at runtime, so using FSEvents here's what my app does:

  • creates a global callback function to handle callbacks
  • create a new FSEventStreamRef per folder, associating it with the callback created above and adding a context to the eventStream that helps me associate the change callback with this folder

Stuff seems to mostly work, but I've noticed some weirdness in when the callbacks are invoked and the 'eventPaths' values being sent to the callback.

For instance, if I've created StreamRefs for /Foo and /Bar, if I add a file in /Bar my callback is invoked almost immediately but the eventPaths points to a location in /Foo, and the context I associated with the StreamRef is also that of /Foo.

Or, say I'm monitoring /Foo and /Bar and then remove /Bar (by stopping and closing the StreamRef for /Bar correctly). I now create a new FSEventStreamRef for /Fee and associate with the same callback. Any changes I make to /Fee don't cause the callback to be invoked but changes to /Foo continue raising the callback.

Any example or documentation I've seen online only talks of monitoring a single folder. Is something busted with how I'm associating the single callbacks with multiple FSEventStreamRefs? It sounds like that shouldn't be a problem though...

Has anyone done something similar in a way that works reliably, or any suggestions for what I might try differently?

One thing I did attempt to do as I was experimenting with this is use a single FSEventStreamRef and pass it a CFArrayRef with all the paths I wanted, and when my watch list changes close and re-create a new FSEventStreamRef - this works even worse that the above.

+2  A: 

Works for me. I emulated these characteristics:

  1. One path per stream
  2. One context per path/stream
  3. One callback for all streams

Can you show the code that's failing?

Peter Hosey
+1 for answering releasing your source. On bitbucket ;)
NicDumZ
Yup, good call. I've just pointed someone wondering about FSEvents here: http://stackoverflow.com/questions/2815502/automatic-screenshot-uploading-on-mac-like-cloud-app/ to this answer, as it's a nice example of how to monitor a couple of folders.
Matt Gibson