简体   繁体   English

是否可以从FSEvent id获取文件名?

[英]Is it possible to get the filename from an FSEvent id?

I've created a simple filesystem watcher in C++ using the mac File System Events API as documented here: apple developer doc . 我使用mac文件系统事件API在C ++中创建了一个简单的文件系统观察器,如下所示: apple developer doc My question is in the title. 我的问题在标题中。 How do I get the path of the thing that was changed? 我如何获得被改变的东西的路径? The eventPaths array only gives me the path of the parent directory (or set thereof) that I'm essentially watching. eventPaths数组只给出了我实际上正在观看的父目录(或其集合)的路径。 But not the underlying directory entry that was modified. 但不是被修改的基础目录条目。

I can grab useful info using the callback, ie: 我可以使用回调获取有用的信息,即:

   static void mycallback(
            ConstFSEventStreamRef streamRef,
            void *clientCallBackInfo,
            size_t numEvents,
            void *eventPaths,
            const FSEventStreamEventFlags eventFlags[],
            const FSEventStreamEventId eventIds[])

I'm thinking that perhaps the name can be extracted from one of these items? 我想也许可以从其中一个项目中提取名称?

EDIT: 编辑:

For example, one might find that a file is created in which case a 例如,可能会发现在这种情况下创建了一个文件a

kFSEventStreamEventFlagItemCreated kFSEventStreamEventFlagItemCreated

is raised. 被提出来了。 But how should I go about getting the actual name of that item? 但是我应该如何获得该项目的实际名称? (Similarly for other events, like when an item is deleted etc.). (类似于其他事件,例如删除项目等)。

EDIT 2: 编辑2:

I am starting to think that it won't be possible to do what I want. 我开始认为不可能做我想做的事。 From FS tech overview : 来自FS技术概述

"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." “重要的一点是,通知的粒度在目录级别。它只告诉你目录中的某些内容已经改变,但不会告诉你改变了什么。”

Aka. 阿卡。 Bugger. 开溜。

Cheers, 干杯,

Ben. 本。

Actually, this is possible, but only on 10.7+. 实际上,这是可能的,但仅限10.7+。 Check out the kFSEventStreamCreateFlagFileEvents flag for FSEventStreamCreate (docs here ). 查看FSEventStreamCreate的kFSEventStreamCreateFlagFileEvents标志( 此处为docs)。

Unfortunately as I discovered, it isn't possible; 不幸的是,我发现,这是不可能的; see FS tech overview for an explanation / motivation. 请参阅FS技术概述以获得解释/动机。 A work-around would be to take a snapshot of the directory and then whenever a change is discovered, take a new snapshot and diff it with the original, to find the specific directory entries (and their names) that changed. 解决方法是获取目录的快照,然后每当发现更改时,获取新快照并将其与原始快照区分开,以查找更改的特定目录条目(及其名称)。

EDIT: It is possible in >=10.7 (See chosen answer) 编辑:有可能> = 10.7(参见选择的答案)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM