您的位置:首页 > 运维架构

watch File System Events(文件系统改变事件监控) < 一 >

2013-12-13 16:11 916 查看
file system events API提供了文件目录层次改变的通知(支持10.5以上),该机制(file
system events mechanism)由3部分构成:
1:内核传递事件到用户层
2: 一个后台进程过滤事件流,然后发送通知
3:一个数据库保存这些改变
开发者注册通知,后台进程发送出了通知以后,就能接收到,然后做相应的处理。
简单使用介绍:
1:应用程序通过
FSEventStreamCreate
or
FSEventStreamCreateRelativeToDevice注册。

2:使用循环来获取FSEventStreamScheduleWithRunLoop

3:告诉后台进程开始文件监控
FSEventStreamStart

4:如果事件产生,然后就回调函数FSEventStreamCallback
5:停止后台进程发送通知
FSEventStreamStop
.
6:作废事件
FSEventStreamInvalidate
.
7:释放事件
FSEventStreamRelease
.

上一个简单例子:
- (void)createFSEvent
{

//paths are the directory you want watch

FSEventStreamRef eventRef =FSEventStreamCreate(kCFAllocatorDefault,eventStreamCallback,nil,
(CFArrayRef)paths,kFSEventStreamEventIdSinceNow,10,kFSEventStreamCreateFlagWatchRoot
|kFSEventStreamCreateFlagUseCFTypes);

FSEventStreamScheduleWithRunLoop(eventRef,CFRunLoopGetMain(),
kCFRunLoopDefaultMode);

FSEventStreamStart(eventRef);
}
可以看下参数解释:
FSEventStreamCreate
extern FSEventStreamRef FSEventStreamCreate(

CFAllocatorRef allocator,
FSEventStreamCallback callback,
FSEventStreamContext *context,
CFArrayRef pathsToWatch,
FSEventStreamEventId sinceWhen,
CFTimeInterval latency,
FSEventStreamCreateFlags flags);


Parameters

allocator
The CFAllocator to be used to allocate memory for the stream. Pass NULL or kCFAllocatorDefault to use the current default allocator.
callback
An FSEventStreamCallback which will be called when FS events occur.
context
A pointer to the FSEventStreamContext structure the client wants to associate with this stream. Its fields are copied out into the stream itself so its memory can be released after the stream is created. Passing
NULL is allowed and has the same effect as passing a structure whose fields are all set to zero.
pathsToWatch
A CFArray of CFStringRefs, each specifying a path to a directory, signifying the root of a filesystem hierarchy to be watched for modifications.
sinceWhen
The service will supply events that have happened after the given event ID. To ask for events "since now" pass the constant kFSEventStreamEventIdSinceNow. Often, clients will supply the highest-numbered FSEventStreamEventId
they have received in a callback, which they can obtain via the FSEventStreamGetLatestEventId() accessor. Do not pass zero for sinceWhen, unless you want to receive events for every directory modified since "the beginning of time" -- an unlikely scenario.
latency
The number of seconds the service should wait after hearing about an event from the kernel before passing it along to the client via its callback. Specifying a larger value may result in more effective temporal
coalescing, resulting in fewer callbacks and greater overall efficiency.
flags
Flags that modify the behavior of the stream being created. See FSEventStreamCreateFlags.


Return Value

A valid FSEventStreamRef.

然后回调:

void eventStreamCallback(ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const
FSEventStreamEventFlags eventFlags[],
const
FSEventStreamEventId eventIds[]){
int i;
NSArray* paths = eventPaths;

NSFileManager* fm =[NSFileManagerdefaultManager];
for (i =
0; i < numEvents; i++)
{

if ((eventFlags[i] &kFSEventStreamEventFlagRootChanged) !=0)
{
//do something you want
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐