gpt4 book ai didi

macos - Mac OS X 中的文件级文件系统更改通知

转载 作者:行者123 更新时间:2023-12-03 10:05:01 25 4
gpt4 key购买 nike

我希望在给定目录下的任何文件(直接或间接)被修改时通知我的代码。通过“修改”,我的意思是我希望我的代码在文件内容被更改、重命名或删除时得到通知;或者是否添加了新文件。对于我的应用程序,可能有数千个文件。

我看起来像 FSEvents,但它的技术概述部分说:

The important point to take away is that the granularity of notifications is at a directory level. It tells you only that something in the directory has changed, but does not tell you what changed.



它还说:

The file system events API is also not designed for finding out when a particular file changes. For such purposes, the kqueues mechanism is more appropriate.



但是,为了在给定文件上使用 kqueue,必须打开该文件以获取文件描述符。管理数以千计的文件描述符是不切实际的(而且可能会超过允许的最大打开文件描述符数)。

奇怪的是,在 Windows 下,我可以使用 ReadDirectoryChangesW()功能,它正是我想要的。

那么如何在 Mac OS X 下做我想做的事呢?或者,换一种方式问:人们将如何编写等效于 ReadDirectoryChangesW() 的内容。对于用户空间中的 Mac OS X(并且这样做非常有效)?

最佳答案

我自己还没有尝试过,但 FSEvents 似乎能够提供从 10.7 (Lion) 开始的文件级通知。来自 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.

关于macos - Mac OS X 中的文件级文件系统更改通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1772209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com