Detecting file changes on macOS with kqueue

(vegardstikbakke.com)

40 points | by benhoyt 4 days ago

4 comments

  • lzhgusapp 1 hour ago
    Nice writeup. I work on native macOS utilities and have dealt with file monitoring quite a bit. kqueue is reliable but the per-file descriptor requirement can get tricky when you need to watch large directory trees.

    For anyone looking at this space, FSEvents is the higher-level alternative Apple provides. It watches directory-level changes without needing a file descriptor per file, which scales better for broad monitoring. But kqueue gives you more granular control, like detecting attribute changes or renames specifically, which FSEvents sometimes lumps together.

    In practice I've found a combination works well: FSEvents for broad directory watching to catch that something changed, then kqueue for targeted monitoring of specific files you care about.

  • WhyNotHugo 44 minutes ago
    kqueue is quite portable and works across all the BSDs.

    The OpenBSD documentation for it is top notch, as usual. No idea about the rest (I suspect they’ve all converged at this point).

    • technothrasher 13 minutes ago
      Kqueue originated in FreeBSD, and like most all of FreeBSD, it is very well documented.
  • somat 1 hour ago
    I love small personal projects like this, especially their writeups. I always have a hard time learning unless I am actually using it for something.

    For more software in this domain see also the excellent entr https://eradman.com/entrproject/

  • justanotherunit 48 minutes ago
    Love it. Are there any benchmarks available on how fast this is from file change, kernel event received, event dispatched? I would expect it to be really quick.