Mac OS X中的文件级文件系统更改通知 [英] File-level filesystem change notification in Mac OS X

查看:155
本文介绍了Mac OS X中的文件级文件系统更改通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当要修改给定目录下的任何文件(直接或间接)时,我希望我的代码得到通知.所谓修改",是指我希望在文件的内容发生更改,重命名或删除时得到通知.或是否添加了新文件.对于我的应用程序,可以有成千上万个文件.

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.

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

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.

它也说:

文件系统事件API也不旨在确定特定文件何时更改.为此,kqueues机制更为合适.

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,必须打开文件以获得文件描述符.管理数千个文件描述符是不切实际的(并且可能会超过打开文件描述符的最大允许数量).

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

奇怪的是,在Windows下,我可以使用ReadDirectoryChangesW()函数,它恰好实现了我想要的功能.

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

那么在Mac OS X下我该怎么办?或者,以另一种方式问:如何在用户空间中为Mac OS X编写等效于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)?

推荐答案

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

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.

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

Available in OS X v10.7 and later.

这篇关于Mac OS X中的文件级文件系统更改通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆