FSEvents weirdness on OS X Leopard
Asked Answered
S

1

2

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.

Septuplet answered 27/5, 2009 at 0:18 Comment(0)
B
4

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?

Barbarese answered 27/5, 2009 at 2:12 Comment(5)
Yup, good call. I've just pointed someone wondering about FSEvents here: #2816002 to this answer, as it's a nice example of how to monitor a couple of folders.Endres
Mostly the - (id) initWithCallback:(FSEventStreamCallback)newCallback path:(NSString *)newPath; part... what do I pass in for newCallback?Alexipharmic
@cksubs: You need to pass in a callback function that matches the FSEventStreamCallback type.Barbarese
Peter: thanks. Basically all examples seem to show passing just &callback rather than a "real" function call, which gives an error for me. I asked a question here: #7377892Alexipharmic
@cksubs: A function call won't help unless the function you call returns a FSEventStreamCallback function. (More information in my answer on your question.)Barbarese

© 2022 - 2024 — McMap. All rights reserved.