File-level filesystem change notification in Mac OS X

后端 未结 4 852
难免孤独
难免孤独 2021-02-01 04:08

I want my code to be notified when any file under (either directly or indirectly) a given directory is modified. By \"modified\", I mean I want my code to be notified whenever

相关标签:
4条回答
  • 2021-02-01 04:20

    You might want to check out man fs_usage, though it's not specific to a directory and requires root privileges.

    0 讨论(0)
  • 2021-02-01 04:24

    The closest utility (that I know of) that matches your needs on Mac OS X is fslogger. See the link for a description, dmg and source code: OSXBook - fslogger

    0 讨论(0)
  • 2021-02-01 04:33

    I haven't tried this myself, but it seems like FSEvents is able to provide file-level notifications as of 10.7 (Lion). From the description of FSEventStreamCreateFlags:

    kFSEventStreamCreateFlagFileEvents

    Request file-level notifications. Your stream will receive events about individual files in the hierarchy you're watching instead of only receiving directory level notifications. Use this flag with care as it will generate significantly more events than without it.

    Available in OS X v10.7 and later.

    0 讨论(0)
  • 2021-02-01 04:37

    EDIT: Not verified, but Konstantin indicates below that this code sample is obsolete as of 2012.

    I don't believe there's a specific API for what you're looking for. Apple provides sample code for a similar problem called Watcher. It is not what you are looking for, but it is about the best you can do at this point. You have to take snapshots of the directory, and rescan it when you find out something changed. Modification time is the best thing to check of course, if you can trust modification time.

    You are probably correct that trying to register for an unbounded number of kqueues would likely be unworkable.

    0 讨论(0)
提交回复
热议问题