Mac OS X 中的文件级文件系统更改通知
我希望当给定目录下的任何文件(直接或间接)被修改时,我的代码会收到通知。通过“修改”,我的意思是我希望每当文件内容被更改、重命名或删除时我的代码都会收到通知;或者如果添加了新文件。对于我的应用程序,可能有数千个文件。
我查看了 FSEvents,但其技术概述部分指出:
需要注意的重要一点是通知的粒度是目录级别的。它只告诉您目录中的某些内容已更改,但不会告诉您更改的内容。
它还说:
文件系统事件 API 也不是为查找特定文件何时发生更改而设计的。对于这样的目的,kqueues机制更合适。
然而,为了在给定文件上使用 kqueue,必须打开该文件以获得文件描述符。管理数千个文件描述符是不切实际的(并且可能会超过打开文件描述符的最大允许数量)。
奇怪的是,在 Windows 下,我可以使用 ReadDirectoryChangesW() 函数,而且它正是我想要的。
那么如何在 Mac OS X 下做我想要的事情呢?或者,换一种方式问:如何在用户空间中为 Mac OS X 编写等效的 ReadDirectoryChangesW()
(并且这样做非常有效)?
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 a file's contents are altered, it's renamed, or it's deleted; or if a new file is added. For my application, there can be thousands of files.
I looked as FSEvents, but its Technology Overview says, in part:
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.
It also says:
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.
However, in order to use kqueue on a given file, one has to open the file to obtain a file descriptor. It's impractical to manage thousands of file descriptors (and would probably exceed the maximum allowable number of open file descriptors anyway).
Curiously, under Windows, I can use the ReadDirectoryChangesW()
function and it does precisely what I want.
So how can one do what I want under Mac OS X? Or, asked another way: how would one go about writing the equivalent of ReadDirectoryChangesW()
for Mac OS X in user-space (and do so very efficiently)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:未经验证,但 Konstantin 在下面指出该代码示例自 2012 年起已过时。
我不相信有特定的 API 适合您正在寻找的内容。 Apple 提供了名为 Watcher 的类似问题的示例代码。这不是您正在寻找的,但它是您目前可以做的最好的事情。您必须拍摄该目录的快照,并在发现某些更改时重新扫描它。当然,如果您可以信任修改时间,则修改时间是最好检查的事情。
您可能是正确的,尝试注册无限数量的 kqueues 可能是行不通的。
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.
我自己还没有尝试过,但从 10.7 (Lion) 开始,FSEvents 似乎能够提供文件级通知。来自 FSEventStreamCreateFlags的说明:
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:
据我所知,在 Mac OS X 上最能满足您的需求的实用程序是
fslogger
。请参阅链接以获取描述、dmg 和源代码:
OSXBook - fslogger
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
您可能想查看
man fs_usage
,尽管它不特定于目录并且需要 root 权限。You might want to check out
man fs_usage
, though it's not specific to a directory and requires root privileges.