简体   繁体   English

除了使用inotify之外,如何递归监视目录

[英]How to recursively monitor an directory besides using inotify

I want to make an C++ application that will monitor file changes in its directory or its sub-directories. 我想制作一个C ++应用程序,它将监视其目录或其子目录中的文件更改。 I know inotify can monitor single level directory changes and i need to manually add a watch for each sub-directory to detect changes in the sub-directory. 我知道inotify可以监视单级目录更改,并且我需要为每个子目录手动添加监视以检测子目录中的更改。 I need to know if there is any other way to recursively monitor changes in a directory in Linux other than inotify. 我需要知道除inotify之外是否还有其他方法可以递归监视Linux目录中的更改。

Yes there is the old classic way: Simply check the directory (recursively) in intervals. 是的,这是一种古老的经典方法:只需间隔一定时间(递归)检查目录即可。 Store a list of all files and directories and their modification dates. 存储所有文件和目录及其修改日期的列表。 Then it's easy to see if you have a new file/directory, if one has been removed, or if otherwise modified. 然后,很容易看到您是否有一个新文件/目录,是否已被删除,或是否进行了其他修改。

It is time consuming though, and you need to store data about every file and directory, so if you try to do it on the root directory you will use a lot of memory or storage. 但是,这耗时,并且您需要存储有关每个文件和目录的数据,因此,如果尝试在根目录上进行操作,则会占用大量内存或存储空间。

To monitor recursively a directory you have to: 要递归监视目录,您必须:

  1. Create an inotify(7) object with inotify_init(2) or inotify_init2(2) . 使用inotify_init(2)inotify_init2(2)创建一个inotify(7)对象。
  2. Descend recursively on your directory, using inotify_add_watch(2) for all the nodes you want to be notified on (add the watch for the directory itself before scanning, or you'll lose events ---se below). 使用inotify_add_watch(2)对要在其上通知的所有节点递归下降到目录中在扫描之前添加目录本身的监视,否则将丢失事件--- se在下面)。
  3. Wait for events to come on the inotify descriptor you have received. 等待事件发生在收到的inotify描述符上。

Take into account that directory creation forces you to possible rescan of directory contents, as you can get an event for a new subdirectory on a watched directory, but files can be created on it before you had a chance to add watches for the recently created directory, so you will not be informed of that files creation. 考虑到目录创建会迫使您可能重新扫描目录内容,因为您可以在监视的目录上获得新子目录的事件,但是可以在上面创建文件,然后才能为最近创建的目录添加监视,因此不会通知您该文件的创建。

For this last reason, you'll need to consider putting all things in a subroutine and to call it each time a new directory gets created. 由于最后一个原因,您需要考虑将所有内容放在子例程中,并在每次创建新目录时调用它。 This way perhaps you'll scan files for which you'll receive creation events, but the other side you can lost events. 这样,也许您将扫描将接收创建事件的文件,但另一方面,您可能会丢失事件。

Also, you must prepare yourshelf to do a complete tree rescan in case you lose some events (in a full queue overflow). 另外,您必须准备好架子以进行完整的树重新扫描,以防丢失某些事件(在整个队列中溢出)。

And believe me, this is by far more efficient than to do it the classic way. 并且相信我,这比经典的方式有效得多。 And you can bypass short lived files between rescans. 您可以在重新扫描之间绕过短暂的文件。

The reason there's not a recursive solution to this problem has been pointed above in one comment (You'll need the kernel to do the search for you, even if you are not interested on it) 上面已在一个注释中指出了对此问题没有递归解决方案的原因(即使您对此不感兴趣,也需要内核来进行搜索)

可能您想混合inotify和dnotify来实现。

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

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