使用inotify监视Linux文件变化,利用这一机制,我们可


Linux系统中提供了一套intotify的机制来监视文件系统的事件,比如创建文件,打开,关闭等等。利用这一机制,我们可以很容易写出监控目录变化的工具。更何况更有了一个inotify_tools的开源工程呢。inotify_tools对inotify的API做了进一步的封装,更加方便使用。

下面的例子来源于inotify_tools的示例代码,但是做了一些改进。inotify_tools监视一个目录时,可以得到该目录变化的信息。但是在该目录下,创建一个子目录后,子目录中的变化,inotify_tools不能获取。所以我添加了动态监控目录的代码。

#include <stdlib.h>

 #include <stdio.h>
 
 #include <inotifytools/inotifytools.h>
 #include <inotifytools/inotify.h>
 
 enum {
 ACTION_NULL_WD,
 ACTION_ADD_WD,
 ACTION_DEL_WD,
 };
 
 int main(int argc, const char **argv)
 {
 int err = 0;
 
 if (!inotifytools_initialize()) {
 printf("inotifytools_initialize failedn");
 goto error;
 }
 
 inotifytools_initialize_stats();
 
 const char *monitor_path = ".";
 if (argc > 1) {
 monitor_path = argv[1];
 }
 printf("Monitor dir(%s)n", monitor_path);
 
 if (!inotifytools_watch_recursively(monitor_path, IN_ALL_EVENTS)) {
 printf("inotifytools_watch_recursively failedn");
 goto error;
 }
 
 inotifytools_set_printf_timefmt("%F %T");
 
 struct inotify_event * event = inotifytools_next_event(-1);
 char path[256];
 while (event) {
 inotifytools_printf( event, "%T %w%f %en" );
 
 if (IN_ISDIR&event->mask) {
 int action = ACTION_NULL_WD;
 
 if ((IN_DELETE|IN_DELETE_SELF|IN_MOVED_FROM)&event->mask) {
 action = ACTION_DEL_WD;
 snprintf(path, sizeof(path), "%s%s",
 inotifytools_filename_from_wd(event->wd),
 event->name);
 printf("Remove path(%s) from wdn", path);
 } else if (((IN_CREATE|IN_MOVED_TO)&event->mask) && (IN_ISDIR&event->mask)) {
 action = ACTION_ADD_WD;
 snprintf(path, sizeof(path), "%s%s",
 inotifytools_filename_from_wd(event->wd),
 event->name);
 printf("Add path(%s) into wdn", path);
 }
 
 if (ACTION_ADD_WD == action) {
 if (!inotifytools_watch_recursively(path, IN_ALL_EVENTS)) {
 printf("inotifytools_watch_recursively failedn");
 goto error;
 }
 } else if (ACTION_DEL_WD == action) {
 if (!inotifytools_remove_watch_by_wd(event->wd)) {
 printf("inotifytools_remove_watch_by_wd failed. event->wd(%d)n", event->wd);
 goto error;
 }
 }
 }
 event = inotifytools_next_event(-1);
 }
 
 printf("Exitn");
 
 return 0;
 
 error:
 err = inotifytools_error();
 printf("Error(%d)n", err);
 return -1;
 }

代码很简单。就是在获得事件以后,检查是否为目录。如果是目录,则需要进行动态监控的检查。如果是创建动作,那么就添加新的监控。如果是删除动作,就去掉已有的监控wd。

相关内容