Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8005409
  • 博文数量: 159
  • 博客积分: 10424
  • 博客等级: 少将
  • 技术积分: 14615
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-14 12:45
个人简介

啦啦啦~~~

文章分类
文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(10)

2011年(116)

2010年(22)

分类: LINUX

2013-03-06 23:13:36

作者:gfree.wind@gmail.com
博客:blog.focus-linux.net   linuxfocus.blog.chinaunix.net
 
微博:weibo.com/glinuxer
QQ技术群:4367710
 
本文的copyleft归gfree.wind@gmail.com所有,使用GPL发布,可以自由拷贝,转载。但转载请保持文档的完整性,注明原作者及原链接,严禁用于任何商业用途。

============================================================================================================================================================

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

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

  1. #include <stdlib.h>
  2. #include <stdio.h>

  3. #include <inotifytools/inotifytools.h>
  4. #include <inotifytools/inotify.h>

  5. enum {
  6.     ACTION_NULL_WD,
  7.     ACTION_ADD_WD,
  8.     ACTION_DEL_WD,
  9. };

  10. int main(int argc, const char **argv)
  11. {
  12.     int err = 0;

  13.     if (!inotifytools_initialize()) {
  14.         printf("inotifytools_initialize failedn");
  15.         goto error;
  16.     }

  17.     inotifytools_initialize_stats();

  18.     const char *monitor_path = ".";
  19.     if (argc > 1) {
  20.         monitor_path = argv[1];
  21.     }
  22.     printf("Monitor dir(%s)n", monitor_path);

  23.     if (!inotifytools_watch_recursively(monitor_path, IN_ALL_EVENTS)) {
  24.         printf("inotifytools_watch_recursively failedn");
  25.         goto error;
  26.     }

  27.     inotifytools_set_printf_timefmt("%F %T");

  28.     struct inotify_event * event = inotifytools_next_event(-1);
  29.     char path[256];
  30.     while (event) {
  31.         inotifytools_printf( event, "%T %w%f %en" );

  32.         if (IN_ISDIR&event->mask) {
  33.             int action = ACTION_NULL_WD;

  34.             if ((IN_DELETE|IN_DELETE_SELF|IN_MOVED_FROM)&event->mask) {
  35.                 action = ACTION_DEL_WD;
  36.                 snprintf(path, sizeof(path), "%s%s",
  37.                         inotifytools_filename_from_wd(event->wd),
  38.                         event->name);
  39.                 printf("Remove path(%s) from wdn", path);
  40.             } else if (((IN_CREATE|IN_MOVED_TO)&event->mask) && (IN_ISDIR&event->mask)) {
  41.                 action = ACTION_ADD_WD;
  42.                 snprintf(path, sizeof(path), "%s%s",
  43.                         inotifytools_filename_from_wd(event->wd),
  44.                         event->name);
  45.                 printf("Add path(%s) into wdn", path);
  46.             }

  47.             if (ACTION_ADD_WD == action) {
  48.                 if (!inotifytools_watch_recursively(path, IN_ALL_EVENTS)) {
  49.                     printf("inotifytools_watch_recursively failedn");
  50.                     goto error;
  51.                 }
  52.             } else if (ACTION_DEL_WD == action) {
  53.                 if (!inotifytools_remove_watch_by_wd(event->wd)) {
  54.                     printf("inotifytools_remove_watch_by_wd failed. event->wd(%d)n", event->wd);
  55.                     goto error;
  56.                 }
  57.             }
  58.         }
  59.         event = inotifytools_next_event(-1);
  60.     }

  61.     printf("Exitn");

  62.     return 0;

  63. error:
  64.     err = inotifytools_error();
  65.     printf("Error(%d)n", err);
  66.     return -1;
  67. }

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



阅读(18138) | 评论(6) | 转发(9) |
给主人留下些什么吧!~~

Bean_lee2013-03-07 12:31:36

我没有细细的读这份code,我比较好奇,加入某个进程往file A里面写东西,但是这个进程不是daemon,他瞬间就消失了,这个inotify_tool能监控的到是哪个进程往file里面写东西了吗? 

因为这个场景是我在项目中遇到的一个问题。