是否可以使用FSEvents或其他任何内容获得已监视目录已移至磁盘上其他位置的通知?
更新:
这是我到目前为止的代码,我现在正在尝试使用带有FSEventStreamCreate的kFSEventStreamCreateFlagWatchRoot标志来获取根更改通知,到目前为止还没有成功.
- (voID)registerForfileSystemNotifications { Nsstring *watchedDirectoryPath = [[NSUserDefaults standardUserDefaults] valueForKey:kMyWatchedDirectoryPathKey]; self.watchedDirectoryfileDescriptor = open([watchedDirectoryPath cStringUsingEnCoding:NSUTF8StringEnCoding],O_RDONLY); NSArray *paths = [NSArray arrayWithObject:watchedDirectoryPath]; voID *appController = (voID *)self; FSEventStreamContext context = {0,appController,NulL,NulL}; FSEventStreamRef streamRef = FSEventStreamCreate(NulL,&fsevents_callback,&context,(CFArrayRef) paths,kFSEventStreamEventIDSinceNow,(CFTimeInterval)2.0,kFSEventStreamCreateFlagUseCFTypes | kFSEventStreamCreateFlagWatchRoot); FSEventStreamScheduleWithRunLoop(streamRef,CFRunLoopGetCurrent(),kcfRunLoopDefaultMode); FSEventStreamStart(streamRef);}voID fsevents_callback(ConstFSEventStreamRef streamRef,voID *userData,size_t numumberOfEvents,voID *eventPaths,const FSEventStreamEventFlags eventFlags[],const FSEventStreamEventID eventIDs[]) { MyAppController *appController = (MyAppController *)userData; char *newPath = calloc(4096,sizeof(char)); int pathIntPointer = (int)newPath; int length = fcntl(appController.watchedDirectoryfileDescriptor,F_GETPATH,pathIntPointer); Nsstring *newPathString = [[Nsstring alloc] initWithBytes:newPath length:(NSUInteger)length enCoding:NSUTF8StringEnCoding]; NSLog(@"newPathString: %@",newPathString); // empty}解决方法 是.将kFSEventStreamCreateFlagWatchRoot作为FSEventStreamCreate的最后一个参数传递,如果目录被移动或重命名,您将收到通知.从 docs:
Request notifications of changes along the path to the path(s) you’re watching. For example,with this flag,if you watch “/foo/bar” and it is renamed to “/foo/bar.old”,you would receive a RootChanged event. The same is true if the directory “/foo” were renamed. The event you receive is a special event: the path for the event is the original path you specifIEd,the flag kFSEventStreamEventFlagRootChanged is set and event ID is zero. RootChanged events are useful to indicate that you should rescan a particular hIErarchy because it changed completely (as opposed to the things insIDe of it changing). If you want to track the current location of a directory,it is best to open the directory before creating the stream so that you have a file descriptor for it and can issue an F_GETPATH fcntl() to find the current path.
编辑:添加fcntl示例
那个cocoadev的例子表明作者对指针有点缺乏经验. pathIntPointer不仅是不必要的,它也是你的问题的原因.从fnctl检查返回码的错误会抓住它.这是你的回调的修订版本:
voID fsevents_callback(ConstFSEventStreamRef streamRef,const FSEventStreamEventID eventIDs[]) { MyAppController *appController = (MyAppController *)userData; char newPath[ MAXPATHLEN ]; int rc; rc = fcntl( appController.watchedDirectoryfileDescriptor,newPath ); if ( rc == -1 ) { perror( "fnctl F_GETPATH" ); return; } Nsstring *newPathString = [[Nsstring alloc] initWithUTF8String: newPath ]; NSLog(@"newPathString: %@",newPathString); [ newPathString release ];}总结
以上是内存溢出为你收集整理的cocoa – 是否可以使用FSEvents获取文件夹已被移动的通知?全部内容,希望文章能够帮你解决cocoa – 是否可以使用FSEvents获取文件夹已被移动的通知?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)