简体   繁体   English

inotify不会在文件修改时触发通知

[英]inotify not firing notifications on file modification

I have been tweaking the example here to make it repeatedly watch a file for 'modifications'. 我一直在调整的例子在这里 ,使其反复观看了“修改”的文件。 My code is here . 我的代码在这里 In my tests the inotify notification is only firing the first time the file is 'modified'(ie touch 'ed). 在我的测试中,inotify通知仅在文件第一次被“修改”时触发(即touch 'ed)。 Any subsequent modifications to the file are not causing any notifications to fire. 对文件的任何后续修改都不会导致触发任何通知。 stat shows that the 'Modify' time has changed. stat显示“修改”时间已更改。 Also, modifying the code to remove the watch and re-add each time a notification fires(ie move inotify_add_watch and inotify_rm_watch to be inside the while(1) loop in my sample ) did not help resolve this issue. 此外,修改代码以删除手表并在每次通知触发时重新添加(即将inotify_add_watchinotify_rm_watch移动到我的示例中while(1)循环内)并没有帮助解决此问题。

I was wondering if any one here could help with what I may be doing wrong. 我想知道这里有没有人可以帮助解决我可能做错的事情。 Also, although I have added a watch for IN_ALL_EVENTS , I really only care about IN_MODIFY events. 此外,虽然我已经为IN_ALL_EVENTS添加了一个IN_ALL_EVENTS ,但我真的只关心IN_MODIFY事件。 Not sure if that makes any difference. 不确定这是否有所不同。

Also, does this use case not work? 此外,这个用例不起作用吗? Should I change my approach to watch the directory instead? 我应该改变观察目录的方法吗? Please advice. 请指教。

TIA. TIA。

Edit 1: As noted by themel , the handling of i needed some fixing. 编辑1:正如themel所述, i的处理需要一些修复。 However even the fixed version is not firing notifications for subsequent filesystem 'events'. 但是,即使是固定版本也不会为后续文件系统“事件”触发通知。 Also, adding a watch on the directory as opposed to the file is exhibiting similar non-deterministic behavior. 此外,在目录上添加监视而不是文件表现出类似的非确定性行为。

Edit 2: I would like to get this asio + inotify example based on this answer to work. 编辑2:我想根据这个工作答案得到这个asio + inotify示例 Unfortunately that example hasn't been working for me at all. 不幸的是,这个例子根本不适用于我。 Any help would be much appreciated. 任何帮助将非常感激。 TIA. TIA。

After themel's fix, your code works fine when watching a directory, in my tests. 在主题修复之后,在我的测试中,在查看目录时,您的代码可以正常工作。 When watching a file, event->len is zero, and your code ignores the notifications. 在观看文件时, event->len为零,您的代码会忽略通知。

With the test for event->len removed and all event->name replaced by file_path in the printf statements, it works fine when watching a file as well. 通过printf语句中的event->len删除测试和所有event->name替换为file_path ,它在查看文件时也能正常工作。

PS: Just noticed you mention touch . PS:刚刚注意到你提到touch

touch sends the following events: touch发送以下事件:

IN_OPEN
IN_ATTRIB
IN_CLOSE_WRITE

no IN_MODIFY 没有IN_MODIFY

Also, don't test modification by editing it with vim as I just did - it deletes the file while shuffling the working copy and the swap, which removes the watch. 另外,不要像我刚才那样用vim编辑它来测试修改 - 它会在洗牌工作副本和交换时删除文件,从而删除手表。 pico works. pico工作。

Your handling of i is broken, you never reset it to 0 in the loop. 你对i处理被破坏了,你永远不会在循环中将它重置为0。 This causes any later inotify events to be considered only when they are longer than the longest event before them, which is not likely what you want. 这会导致任何后来的inotify事件仅在它们长于它们之前的最长事件时被考虑,这可能不是您想要的。

Fixed version . 固定版本

when monitoring an individual file event->len will be 0 because the file name is not returned. 监视单个文件时 - > len将为0,因为不返回文件名。 I noticed a lot of the example programs have this issue. 我注意到很多示例程序都有这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM