我试图通过名为UKKQueue的包装程序here使用kqueue
来监视单个文件的版本。这个包装很简单,这里是我使用的测试代码:保存文件后停止文件监视器
@implementation FileMonitorTestAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
fileWatcher = [[UKKQueue alloc] init];
[fileWatcher addPath:@"/Users/bruno/Desktop/SyncTestLog"];
[fileWatcher setDelegate:self];
}
- (void)dealloc {
[fileWatcher release];
}
-(void) watcher: (id<UKFileWatcher>)kq receivedNotification: (NSString*)nm forPath: (NSString*)fpath {
NSLog(@"UKFileWatcher: %@ - notification: %@ - filePath: %@", kq, nm, fpath);
}
@end
文件在/Users/bruno/Desktop/SyncTestLog
是一个纯文本文件。当我使用nano
来自终端的输出显示编辑预期:
2011-08-17 11:46:27.316 FileMonitorTest[1235:707] UKFileWatcher: <UKKQueue: 0x100117da0> - notification: UKKQueueFileWrittenToNotification - filePath: /Users/bruno/Desktop/SyncTestLog
2011-08-17 11:46:27.317 FileMonitorTest[1235:707] UKFileWatcher: <UKKQueue: 0x100117da0> - notification: UKKQueueFileSizeIncreasedNotification - filePath: /Users/bruno/Desktop/SyncTestLog
2011-08-17 11:46:27.751 FileMonitorTest[1235:707] UKFileWatcher: <UKKQueue: 0x100117da0> - notification: UKKQueueFileAttributesChangedNotification - filePath: /Users/bruno/Desktop/SyncTestLog
现在,当我使用TextEdit或在的TextWrangler我第一次保存文件后监测停止报告的变化进行编辑。继承人是最后事件报道:
2011-08-17 10:57:45.792 FileMonitorTest[897:707] UKFileWatcher: <UKKQueue: 0x10035ae10> - notification: UKKQueueFileAttributesChangedNotification - filePath: /Users/bruno/Desktop/SyncTestLog
2011-08-17 10:57:46.463 FileMonitorTest[897:707] UKFileWatcher: <UKKQueue: 0x10035ae10> - notification: UKKQueueFileAttributesChangedNotification - filePath: /Users/bruno/Desktop/SyncTestLog
2011-08-17 10:57:54.043 FileMonitorTest[897:707] UKFileWatcher: <UKKQueue: 0x10035ae10> - notification: UKKQueueFileDeletedNotification - filePath: /Users/bruno/Desktop/SyncTestLog
据我了解UKKQueue得到一个类似于Unix的文件描述符使用该标志O_EVTONLY
open()。出于某种原因,TextEdit(和TextWrangler)在保存文件时会生成此UKKQueueFileDeletedNotification
通知。
我需要的是不断地倾听文件中的变化“永远”。我想我可以在UKKQueueFileDeletedNotification
到达时重新创建显示器,但我正在寻找更干净的东西。
感谢
编辑: 我只是找到了在Google Toolbox For Mac一类叫做GTMFileSystemKQueue这是解决我的问题。仍然没有回答我的问题。
嗨,布莱恩。非常感谢你让mi(我们)知道这件事。最好的问候 –
我和GojaN在UKKQueue上遇到了同样的VDKQueue问题。保存文本文件后,它会触发一次然后停止。有任何想法吗? – bijan
@bijan和GojaN,你有没有在UKKQueue或VDKQueue中找到任何解决方案。我也面临同样的问题。 – Subhash