我正在使用FSEvents API來獲取我要跟蹤的本地目錄中的更改通知。是否可以使用FSEvents來獲取文件夾已被移動的通知?
是否有可能使用FSEvent或其他方式獲得通知,指出已監視的目錄已被移動到磁盤上的另一個位置?
更新:
這裏是我到目前爲止的代碼,我現在嘗試使用kFSEventStreamCreateFlagWatchRoot標誌與FSEventStreamCreate得到根本改變的通知,至今沒有成功。
- (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, 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
}
謝謝!我正在嘗試這個,但迄今沒有任何運氣。我已將現有的代碼添加到問題中。我能找到的唯一使用F_GETPATH的例子是http://www.cocoadev.com/index.pl?DescriptorToPath。感謝您的幫助! – 2011-04-28 03:30:38
太棒了。謝謝 :) – 2011-04-28 19:24:01