FreeBSD (and thus Mac OS X) has a filesystem notification API called kqueues which allow you to autodetect changes in watched files/folders. Apple uses it behind the scenes for indexing (Spotlight). It’s really fast. If I modify a file in one window via “touch /tmp/foo.txt” then the watcher program prints a message in the other window before the touch command exits. I’m using the Obj-C UKKQueue wrapper to simplify access to the kqueue system call.
I’ve found a few cases where the watcher fails to trigger when I rapidly delete and recreate the file… I’m not sure if it’s a bug in the system or in my app (probably the latter, maybe I’m not processing the input fast enough).
[UPDATE: oh, I just learned that kqueue can only watch a file that exists. If I wanted to know when a file was created, I’d have to watch the parent directory and then start watching the file when/if it was created. So that explains my problem with recreates]
In OS X 10.5, Apple added a new FSEvents API which uses a daemon to do a system-wide kqueue for the whole filesystem and distributes the messages to listeners in user space. I expect this is probably to reduce the kernel load of a pile of apps each requesting notifications for the same files.
My current project is targetting 10.4, so I’m avoiding the 10.5-specific FSEvents API.