2011-08-17 64 views
0

我试图通过名为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_EVTONLYopen()。出于某种原因,TextEdit(和TextWrangler)在保存文件时会生成此UKKQueueFileDeletedNotification通知。

我需要的是不断地倾听文件中的变化“永远”。我想我可以在UKKQueueFileDeletedNotification到达时重新创建显示器,但我正在寻找更干净的东西。

感谢

编辑: 我只是找到了在Google Toolbox For Mac一类叫做GTMFileSystemKQueue这是解决我的问题。仍然没有回答我的问题。

回答

3

我在现代Objective-C中重写了UKKQueue。新班级的工作方式相同,只是更好,更快速和简化。它还修复了本文中描述的错误以及其他几个错误。

你可以找到新的类,VDKQueue,在这里:http://github.com/bdkjones/VDKQueue

+0

嗨,布莱恩。非常感谢你让mi(我们)知道这件事。最好的问候 –

+0

我和GojaN在UKKQueue上遇到了同样的VDKQueue问题。保存文本文件后,它会触发一次然后停止。有任何想法吗? – bijan

+0

@bijan和GojaN,你有没有在UKKQueue或VDKQueue中找到任何解决方案。我也面临同样的问题。 – Subhash

1

这里的疑难杂症是文字编辑,并在的TextWrangler使用安全保存(或...atomically:YES)不直接写入文件,但首先写入一个临时文件,然后重命名文件以替换保存在临时位置的文件的原始路径。

这样做的效果是你的kqueue将监视原来的文件,这些文件将被安全保存机制删除。

GTMFileSystemKQueue因参数acrossReplace而工作,该参数监视删除/重命名操作并将kqueue重新注册为原始路径。 UKKQueue和VDKQueue的快速检查似乎表明,这也不是。