ここでkqueue
入手できる UKKQueue というラッパーを使用して、単一のファイルのエディションを監視しようとしています。このラッパーは非常にシンプルです。使用しているテスト コードは次のとおりです。
@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 はフラグを使用してopen()O_EVTONLY
で unix のようなファイル記述子を取得します。何らかの理由で、TextEdit (および TextWrangler) はUKKQueueFileDeletedNotification
、ファイルを保存するときにこの通知を生成します。
私が必要としているのは、ファイルの変更を「永遠に」聞き続けることです。が到着したらモニターを再作成できると思いますが、UKKQueueFileDeletedNotification
もっときれいなものを探しています。
ありがとう
編集: Google Toolbox For Macで、問題を解決するGTMFileSystemKQueueというクラスが 見つかりました。私の質問に対する答えはまだありません。