On Wed, May 15, 2013 at 01:52:45PM +0200, Peter J. Philipp wrote: > On 05/15/13 13:41, Jérémie Courrèges-Anglas wrote: > >Doesn't kqueue() fit your needs? > > > > Thank you for your reply, > > I've never used kqueue before, does this only report events on > descriptors that have been opened?
Yes, but that's a good thing, IMO. > I'm wondering if an implementation is done to recurseively watch > directories in inotify (as written about in the limitations), then it > would require a lot less filedescriptors even for kqueue correct? And > thus make monitoring a filesystem's events a lot more efficient? > inotify doesn't report recursively either. The problem with inotify is handling overflow. The kernel can't be expected to buffer every single event forever. Eventually it starts to drop events, so no matter what interface you use, you always need to write code to manually walk directories and look for additions, deletions, or updates yourself. So inotify isn't as slick as it appears on its face. The nice thing about kqueue is that with a reference to the descriptor events are never lost--they just flip a bit in the descriptor book-keeping. That has downsides, obviously, but for the most part makes it easier to use. For my Lua cqueues* project I've written a small notification library in a single C source file which can use BSD kqueue EVFILT_VNODE, Linux inotify, and Solaris port_associate PORT_SOURCE_FILE. You can find it in src/lib/notify.c, and you should be able to use it standalone with no or minimal tweaking. The C interface is basically the same as the Lua interface described in the User Guide--you create a notification object bound to a single directory using notify_opendir(), add files to watch with notify_add(), process events with notify_step(), retrieve notifications with notify_get(), and use notify_pollfd() and notify_timeout() for interfacing with your event loop. It doesn't handle recursion, and in fact doesn't even handle overflow yet (it sets a critical flag which I need to write code to process). I also want to add globbing support so that, e.g., "*" would report on all files in a directory. It's a work-in-progress, but might help you understand how to use the different APIs. * http://25thandclement.com/~william/projects/cqueues.html

