Chinaunix首页 | 论坛 | 博客
  • 博客访问: 924714
  • 博文数量: 177
  • 博客积分: 761
  • 博客等级: 上士
  • 技术积分: 1518
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-04 22:37
文章分类

全部博文(177)

文章存档

2017年(1)

2016年(3)

2015年(33)

2014年(48)

2013年(60)

2012年(32)

分类: LINUX

2014-10-04 16:56:39

Linux 2.6.13 内核中新引入的文件系统变化通知机制 inotify.
使用时应该包含头文件:
 
一、inotify event & mask
    * 以下内容引用自头文件
  1. /* Structure describing an inotify event. */
  2. struct inotify_event
  3. {
  4.   int wd;          /* Watch descriptor. */
  5.   uint32_t mask;   /* Watch mask. */
  6.   uint32_t cookie; /* Cookie to synchronize two events. */
  7.   uint32_t len;    /* Length (including NULs) of name. */
  8.   char name __flexarr; /* Name. */
  9. };

  10. /* Supported events suitable for MASK parameter of INOTIFY_ADD_WATCH. */
  11. #define IN_ACCESS 0x00000001 /* File was accessed. */
  12. #define IN_MODIFY 0x00000002 /* File was modified. */
  13. #define IN_ATTRIB 0x00000004 /* Metadata changed. */
  14. #define IN_CLOSE_WRITE 0x00000008 /* Writtable file was closed. */
  15. #define IN_CLOSE_NOWRITE 0x00000010 /* Unwrittable file closed. */
  16. #define IN_CLOSE (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) /* Close. */
  17. #define IN_OPEN 0x00000020 /* File was opened. */
  18. #define IN_MOVED_FROM 0x00000040 /* File was moved from X. */
  19. #define IN_MOVED_TO 0x00000080 /* File was moved to Y. */
  20. #define IN_MOVE (IN_MOVED_FROM | IN_MOVED_TO) /* Moves. */
  21. #define IN_CREATE 0x00000100 /* Subfile was created. */
  22. #define IN_DELETE 0x00000200 /* Subfile was deleted. */
  23. #define IN_DELETE_SELF 0x00000400 /* Self was deleted. */
  24. #define IN_MOVE_SELF 0x00000800 /* Self was moved. */

  25. /* Events sent by the kernel. */
  26. #define IN_UNMOUNT 0x00002000 /* Backing fs was unmounted. */
  27. #define IN_Q_OVERFLOW 0x00004000 /* Event queued overflowed. */
  28. #define IN_IGNORED 0x00008000 /* File was ignored. */

  29. /* Helper events. */
  30. #define IN_CLOSE (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) /* Close. */
  31. #define IN_MOVE (IN_MOVED_FROM | IN_MOVED_TO) /* Moves. */

  32. /* Special flags. */
  33. #define IN_ONLYDIR 0x01000000 /* Only watch the path if it is a
  34.                        directory. */
  35. #define IN_DONT_FOLLOW 0x02000000 /* Do not follow a sym link. */
  36. #define IN_MASK_ADD 0x20000000 /* Add to the mask of an already
  37.                        existing watch. */
  38. #define IN_ISDIR 0x40000000 /* Event occurred against dir. */
  39. #define IN_ONESHOT 0x80000000 /* Only send event once. */

  40. /* All events which a program can wait on. */
  41. #define IN_ALL_EVENTS (IN_ACCESS | IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE \
  42.               | IN_CLOSE_NOWRITE | IN_OPEN | IN_MOVED_FROM \
  43.               | IN_MOVED_TO | IN_CREATE | IN_DELETE \
  44.               | IN_DELETE_SELF | IN_MOVE_SELF)

 

二、示例代码

         //Init inotify
  1.     int fd, wd;
  2.     if((fd = inotify_init()) == -1) return;
  3.     if((wd = inotify_add_watch(fd, wpath, IN_ALL_EVENTS)) == -1) return;

  4.     unsigned char buf[sizeof(struct inotify_event) * 256];
  5.     size_t len, i = 0;

  6.     while(1)
  7.     {
  8.         i = 0;
  9.         len = read(fd, buf, sizeof(buf));

  10.         while(i < len)
  11.         {
  12.             struct inotify_event *event = (struct inotify_event*)(buf + i);
  13.   
  14.             if(event->name != NULL)
  15.                 printf("name=%s\n", event->name);

  16.             if(event->mask & IN_CREATE)
  17.                 printf("Creat a file.\n");

  18.             if(event->mask & IN_DELETE)
  19.                 printf("Delete a file.\n");

  20.             if(event->mask & IN_MODIFY)
  21.                 printf("Modify a file.\n");
  22.     
  23.             i += sizeof(struct inotify_event) + event->len;
  24.         }
  25.     }
阅读(629) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~