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:应用程序通过
3:告诉后台进程开始文件监控
4:如果事件产生,然后就回调函数FSEventStreamCallback
5:停止后台进程发送通知
6:作废事件
7:释放事件
上一个简单例子:
- (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(
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
}
}
}
system events mechanism)由3部分构成:
1:内核传递事件到用户层
2: 一个后台进程过滤事件流,然后发送通知
3:一个数据库保存这些改变
开发者注册通知,后台进程发送出了通知以后,就能接收到,然后做相应的处理。
简单使用介绍:
1:应用程序通过
FSEventStreamCreateor
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
}
}
}
相关文章推荐
- 安装Win7和Ubuntu12.04双系统后,意外删除Ubuntu12.04引导文件,出现error:unknown filesystem;grub rescue>错误的解决方案
- linux inotify 监控文件系统事件(incron,基于事件驱动的cron-<path> <mask> <command>)
- 使用windows服务和.NET FileSystemWatcher对象来监控磁盘文件目录的改变
- MacOS 开发 - FSEventStream(文件系统改变事件监控)
- <转载>Android中调用系统所装的软件打开文件
- <<自己动手写操作系统>>学习笔记(一)---MBR,PBR,BPB,文件系统
- 任意美化你的文件域 <input type="file" />
- <input type="file">定义样式并获取上传文件路径及指定文件类型
- <input type="file" />浏览时只显示指定文件类型
- <button>的onmoseovert js事件改变this.src地址失败
- 解决<s:file>上传文件大小和 不能获取文件问题
- Linux虚拟文件系统(内核初始化<一>)
- 错误集_ nib文件加载,Unknown class <XXClass> in Interface Builder file.,Undefined symbolsfor architecture
- <input type="file"/> 文件上传
- win7下Qt 5建立空白项目 无法打开包括文件<QApplication> No such file or directory
- Linux-(20)Linux的文件系统<2>
- JSP引入文件的<%@ include file="FourthJsp.jsp" %>方法阐述
- 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 <configuration>\<system.web>\
- 使用<input type="file">实现文件上传
- <the google file system>阅读感受