本文介绍了在Cocoa / Cocoa Touch中监视目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法来监视目录的内容进行更改。我尝试了两种方法。

I am trying to find a way to monitor the contents of a directory for changes. I have tried two approaches.


  1. 使用kqueue监视目录

  2. 使用GCD监视目录

我遇到的问题是我找不到一种方法来检测哪个文件已更改。我试图监视一个目录与潜在的成千上万的文件,我不想调用stat每一个,以找出哪些改变。我也不想为该目录中的每个文件设置单独的分派源。这是目前可能吗?

The problem I am encountering is that I can't find a way to detect which file has changed. I am attempting to monitor a directory with potentially thousands of files in it and I do not want to call stat on every one of them to find out which ones changed. I also do not want to set up a separate dispatch source for every file in that directory. Is this currently possible?

注意:我已经用和

推荐答案

我的建议是只是咬了子弹,并在另一个线程中做目录扫描,即使你谈论成千上万的文件。但如果你坚持,这是答案:

My advice is to just bite the bullet and do a directory scan in another thread, even if you're talking about thousands of files. But if you insist, here's the answer:

没有办法做到这一点,没有卷起你的袖子和去内核潜水。

There's no way to do this without rolling up your sleeves and going kernel-diving.

您的第一个选项是使用FSEvents框架,当文件被创建,编辑或删除时发送通知(以及与属性相关的事情)。概述,有人撰写了围绕API,虽然我没有尝试过。但是概述没有提及有关文件更改周围的事件的部分,只是目录(如kqueue)。我最后使用的代码以及头文件编译我自己的记录器,我可以使用它来获取事件单个文件级别。您必须在应用程式中撰写一些程式码,才能在后台执行纪录器并进行监控。

Your first option is to use the FSEvents framework, which sends out notifications when a file is created, edited or deleted (as well as things to do with attributes). Overview is here, and someone wrote an Objective C wrapper around the API, although I haven't tried it. But the overview doesn't mention the part about events surrounding file changes, just directories (like with kqueue). I ended up using the code from here along with the header file here to compile my own logger which I could use to get events at the individual file level. You'd have to write some code in your app to run the logger in the background and monitor it.

或者,请参阅命令,它不断地监视所有文件系统活动(我的意思是全部)。这已经与达尔文一起,所以你不必自己编译。您可以使用kqueue监听目录更改,同时监视。如果从kqueue收到一个目录已更改的通知,您可以查看fs_usage的输出,查看写入哪些文件,并根据修改的目录检查文件名。 fs_usage是一个firehose,所以准备使用一些选项,以及grep来驯服它。

Alternatively, take a look at the "fs_usage" command, which constantly monitors all filesystem activity (and I do mean all). This comes with Darwin already, so you don't have to compile it yourself. You can use kqueue to listen for directory changes, while at the same time monitoring the output from "fs_usage". If you get a notification from kqueue that a directory has changed, you can look at the output from fs_usage, see which files were written to, and check the filenames against the directory that was modified. fs_usage is a firehose, so be prepared to use some options along with Grep to tame it.

为了使事情更有趣,您的FSEvents记录器和fs_usage都需要root访问权限,因此您必须先获得用户的授权,然后才能在操作系统中使用它们X应用程式(请参阅,了解如何操作)。

To make things more fun, both your FSEvents logger and fs_usage require root access, so you'll have to get authorization from the user before you can use them in your OS X app (check out the Authorization Services Programming Guide for info on how to do it).

如果这一切听起来可怕复杂,那是因为它是。但至少你不必找出硬的方式!

If this all sounds horribly complicated, that's because it is. But at least you didn't have to find out the hard way!

这篇关于在Cocoa / Cocoa Touch中监视目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 01:04