Cocoa,FSEvents,kFSEventStreamCreateFlagFileEvents标志和“重命名”事件 [英] Cocoa, FSEvents, kFSEventStreamCreateFlagFileEvents flag and "renamed" events

查看:556
本文介绍了Cocoa,FSEvents,kFSEventStreamCreateFlagFileEvents标志和“重命名”事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个小应用程序中使用 FSEvents 来同步我的应用程序的内容与硬盘驱动器上的内容(基本上,它是一个小图像查看器,我希望其内容更新时,硬盘的内容更改)

I've been playing with FSEvents in a little application of mine to synchronize the content of my application with what's on the hard drive (basically, it's a little image viewer, and I want its content to update when the content of the HDD changes)

我创建我的流使用 kFSEventStreamCreateFlagFileEvents flag,但我很难弄清楚这些事件是如何生成的操作系统(或内核,或任何)和不幸的是,没有文档与使用此标志生成的事件...似乎他们是新的10.7,仍然没有记录。

I create my stream using the kFSEventStreamCreateFlagFileEvents flag, but I have a hard time figuring out how those events are generated by the OS (or the kernel, or whatever) And unfortunately, there is no documentation with the events generated with this flag ... It seems they are new to 10.7, and still not documented.

所以,我的主要问题是重命名。当我做一个简单的重命名,2 kFSEventStreamEventFlagItemRenamed事件发送到我的回调。一个包含旧的文件名,第二个包含新的文件名。问题发生在重命名一批文件时,这些事件可能不是顺序的。例如,出现以下情况:

So, my main problem is "rename". When I'm doing a simple rename, 2 kFSEventStreamEventFlagItemRenamed events are sent to my callback. One contains the old file name, the second contains the new file name. Problem occurs when you are renaming a batch of files, those events might not be sequencial. For example, it the following case :


  1. file1 - >new_file_1

  2. file2 - >new_file_2

我可能会收到此订单中的活动:

I might receive the events in this order :


  1. 重命名/file1

  2. 重命名/file2 /new_file_1

  3. renamed/new_file_2

似乎是任何方式获取第一个重命名事件的id,当你抓住第二个...所以我做的是:当接收重命名事件,我做一个stat()与文件名。如果统计信息成功返回,则意味着它是新的文件名。如果不是,这意味着它是旧的。我还是没有办法链接这两个事件,但至少我可以通过删除旧文件并添加新的文件。

And there doesn't seem to be any way of getting the id of the first rename event when you catch the second one ... So what I did is : when receiving a "renamed" event, I do a stat() with the file name. If the stat returns successfully, it means that it's the new file name. If not, it means it's the old one. I still have NO WAY to link both events, but at least I can work around by deleting the old files and adding the new ones.

因此,我基本上有2个问题:

So, I basically have 2 questions :

第一个是:我完全失明,没有看到通过fsevent正确捕获重命名事件的明显方法?

The first is: Am I totally blind, and not seeing the obvious way to correctly catch a "renamed" event through fsevents ?

第二个i:我有时会有一个奇怪的错误,而不是只有2个重命名的事件,3发送!所以我得到一个文件添加两次...我不知道这是否是一个错误,或者这是因为我完全错过了fsevent API与 kFSEventStreamCreateFlagFileEvents flag ...

The second i: I sometimes have a strange bug where instead of only 2 renamed events, 3 are sent ! So I get a file added twice ... I'm not sure if this is a bug, or if this comes from the fact I'm completely missusing the fsevent API with the kFSEventStreamCreateFlagFileEvents flag ...

欢迎任何帮助,我完全没有想法来解决这个问题!

Any help is welcome, I'm completely out of ideas to fix this !

推荐答案

您可以从路径创建一个URL,然后调用:

You can get the file ID by creating an URL from the path, then calling:

NSString *fileID = nil;

[url getResourceValue:&fileID forKey:NSURLFileResourceIdentifierKey error:&error]; //NS_AVAILABLE(10_7, 5_0);

(此标识符在系统重新启动时不会持续)

(This identifier is not persistent across system restarts)

这篇关于Cocoa,FSEvents,kFSEventStreamCreateFlagFileEvents标志和“重命名”事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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