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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1772209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:33:49  来源:igfitidea点击:

File-level filesystem change notification in Mac OS X

macosfseventsreaddirectorychangeswkqueue

提问by Paul J. Lucas

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:

我看起来像 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.

需要注意的重要一点是通知的粒度是在目录级别。它只告诉您目录中的某些内容发生了变化,但不会告诉您发生了什么变化。

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.

文件系统事件 API 也不是为找出特定文件何时更改而设计的。对于这样的目的,kqueues 机制更合适。

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).

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

Curiously, under Windows, I can use the ReadDirectoryChangesW()function and it does precisely what I want.

奇怪的是,在 Windows 下,我可以使用该ReadDirectoryChangesW()功能并且它完全符合我的要求。

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)?

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

采纳答案by legoscia

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:

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

kFSEventStreamCreateFlagFileEvents

请求文件级通知。您的流将接收有关您正在观看的层次结构中的单个文件的事件,而不是仅接收目录级别的通知。小心使用这个标志,因为它会比没有它产生更多的事件。

在 OS X v10.7 及更高版本中可用。

回答by Rob Napier

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

编辑:未验证,但康斯坦丁在下面指出此代码示例自 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.

我不相信有您正在寻找的特定 API。Apple 为名为Watcher的类似问题提供了示例代码。这不是您要寻找的东西,而是您此时可以做的最好的事情。您必须拍摄目录的快照,并在发现某些内容发生变化时重新扫描它。如果您可以信任修改时间,当然最好检查修改时间。

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

您可能是正确的,尝试注册无限数量的 kqueue 可能是行不通的。

回答by Honcho

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

在 Mac OS X 上符合您需求的最接近的实用程序(据我所知)是fslogger. 有关说明、dmg 和源代码,请参阅链接: OSXBook - fslogger

回答by JWWalker

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

您可能想要签出man fs_usage,尽管它不是特定于目录并且需要 root 权限。