On Wed, Nov 5, 2014, at 12:50 AM, Gerriet M. Denkmann wrote: > I want to monitor a file for changed content. > Polling is generally not a good idea, so I tried dispatch_source. > > But it does not work. > > Here the code: > > static NSString *const kTestPath = @"/tmp/a.test"; > > - (void)applicationDidFinishLaunching:(NSNotification *)aNotification > { > const char *filename = kTestPath.fileSystemRepresentation; > int fd = open(filename, O_RDONLY); > fcntl(fd, F_SETFL, O_NONBLOCK); > uintptr_t fD = (uintptr_t)fd; > dispatch_queue_t queue = > dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); > const uint64_t flags = DISPATCH_VNODE_WRITE | DISPATCH_VNODE_LINK; > dispatch_source_t my_source = > dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, fD, flags, queue); > dispatch_source_set_event_handler(my_source, ^{ [self fileDidChange]; } > ); > dispatch_resume(my_source); > } > > [snip] > > > What am I doing wrong? (Writing directly to kTestPath without rename() > also does not work). >
My own test setup, which mimics yours, only works if the file already exists when the dispatch source is created. This makes sense, since without O_CREAT, open() returns -1 (an invalid file descriptor). You need to pass O_RDONLY | O_CREAT to open() to ensure that the file exists before you start trying to monitor it for changes. --Kyle Sluder _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com