linux - 如何检测文件已被删除

标签 linux fanotify

我正在编写一个程序来监视文件系统。但是我无法检测到文件何时被删除。我尝试使用 FAN_MARK_ONLYDIR 标志进行监视,希望在删除受监视目录中的文件时 fanotify 引发一些事件,但没有结果。

甚至可以使用 fanotify 来做到这一点?有什么可以帮助我做到这一点?

最佳答案

根据 linuxquestions.org thread fanotify 不检测文件替换或删除或子目录创建、重命名或删除。另见 baach.de discussion ,比较(或提及)inotify、dnotify、fam、Fanotify、tripwire、Python-fusellfuse (python) 等文件或目录更改监视器。

inotify 支持 IN_DELETEIN_DELETE_SELF 事件,如果您使用的目录数量有限,而不是整个文件系统,这很实用使用。

编辑 inotify 网页中提到的限制或注意事项如下:

inotify monitoring of directories is not recursive: to monitor subdirectories under a directory, additional watches must be created. This can take a significant amount time for large directory trees. ... If monitoring an entire directory subtree, and a new subdirectory is created in that tree, be aware that by the time you create a watch for the new subdirectory, new files may already have been created in the subdirectory. Therefore, you may want to scan the contents of the subdirectory immediately after adding the watch.

关于linux - 如何检测文件已被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20636334/

相关文章:

linux - 在不同平台的Amazon ec2中预留实例

c++ - fanotify - 内核 5.1 中引入的新标志的问题

linux - fanotify获取文件名,为什么是/proc/self/fd/"data->fd"?

c - 如何在启动另一个使用它的进程之前完全关闭文件写入?

linux - 使用 xinit 控制非 x 实用程序的放置

linux - 通过 `creact-react-app` react 应用程序不工作

linux - 替换linux内核3中的系统调用

linux - 如何判断 fanotify 事件指的是哪条路径

fanotify 递归真的有效吗?

linux - 无需 inotify 即可查看目录树