DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1. 简介:

Inotify 是一个 Linux 内核特性,它监控文件系统,并且及时向专门的应用程序发出相关的事件警告,比如删除、读、写和卸载操作等。您还可以跟踪活动的源头和目标等细节。

使用 inotify 很简单:创建一个文件描述符,附加一个或多个监视器(一个监视器 是一个路径和一组事件),然后使用 read() 方法从描述符获取事件信息。read() 并不会用光整个周期,它在事件发生之前是被阻塞的。

更好的是,因为 inotify 通过传统的文件描述符工作,您可以利用传统的 select() 系统调用来被动地监控监视器和许多其他输入源。两种方法 — 阻塞文件描述符和使用 select()— 都避免了繁忙轮询。

要使用 inotify,您必须具备一台带有 2.6.13 或更新内核的 Linux 机器(以前的 Linux 内核版本使用更低级的文件监控器 dnotify)。如果您不知道内核的版本,请转到 shell,输入 uname -a

2. 首先看一下inotify.h中定义的watch mask:

IN_ACCESS
The file was read from.

IN_MODIFY
The file was written to.

IN_ATTRIB
The file’s metadata (for example, the owner, permissions, or extended attributes) was changed.

IN_CLOSE_WRITE
The file was closed and had been open for writing.

IN_CLOSE_NOWRITE
The file was closed and had not been open for writing.

IN_OPEN
The file was opened.

IN_MOVED_FROM
A file was moved away from the watched directory.

IN_MOVED_TO
A file was moved into the watched directory.

IN_CREATE
A file was created in the watched directory.

IN_DELETE
A file was deleted from the watched directory.

IN_DELETE_SELF
The watched object itself was deleted.

IN_MOVE_SELF
The watched object itself was moved.

The following events are also defined, grouping two or more events into a single value:

IN_ALL_EVENTS
All legal events.

IN_CLOSE
All events related to closing (currently, both IN_CLOSE_WRITE and IN_CLOSE_NOWRITE).

IN_MOVE

All move-related events (currently, both IN_MOVED_FROM and IN_MOVED_TO).

有了以上的watch mask就可以从终端的输出清晰的得知文件进行了什么样的操作。

2. 上代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/inotify.h>
  5. #include <unistd.h>
  6. #define EVENT_NUM 12
  7. char *event_str[EVENT_NUM] =
  8. {
  9. "IN_ACCESS",
  10. "IN_MODIFY",
  11. "IN_ATTRIB",
  12. "IN_CLOSE_WRITE",
  13. "IN_CLOSE_NOWRITE",
  14. "IN_OPEN",
  15. "IN_MOVED_FROM",
  16. "IN_MOVED_TO",
  17. "IN_CREATE",
  18. "IN_DELETE",
  19. "IN_DELETE_SELF",
  20. "IN_MOVE_SELF"
  21. };
  22. int main(int argc, char *argv[])
  23. {
  24. int fd;
  25. int wd;
  26. int len;
  27. int nread;
  28. char buf[BUFSIZ];
  29. struct inotify_event *event;
  30. int i;
  31. if (argc < 2)
  32. {
  33. fprintf(stderr, "%s path\n", argv[0]);
  34. return -1;
  35. }
  36. fd = inotify_init();
  37. if (fd < 0)
  38. {
  39. fprintf(stderr, "inotify_init failed\n");
  40. return -1;
  41. }
  42. wd = inotify_add_watch(fd, argv[1], IN_ALL_EVENTS);
  43. if (wd < 0)
  44. {
  45. fprintf(stderr, "inotify_add_watch %s failed\n", argv[1]);
  46. return -1;
  47. }
  48. buf[sizeof(buf) - 1] = 0;
  49. while ((len = read(fd, buf, sizeof(buf) - 1)) > 0)
  50. {
  51. nread = 0;
  52. while (len > 0)
  53. {
  54. event = (struct inotify_event *)&buf[nread];
  55. for (i = 0; i<EVENT_NUM; i++)
  56. {
  57. if ((event->mask >> i) & 1)
  58. {
  59. if (event->len > 0)
  60. fprintf(stdout, "%s --- %s\n", event->name, event_str[i]);
  61. else
  62. fprintf(stdout, "%s --- %s\n", " ", event_str[i]);
  63. }
  64. }
  65. nread = nread + sizeof(struct inotify_event) + event->len;
  66. len = len - sizeof(struct inotify_event) - event->len;
  67. }
  68. }
  69. return 0;
  70. }

然后新建inotify.c文件,将代码粘贴进去中,终端使用如下命令编译测试:

gcc inotify.c -o ity

终端运行可执行文件ity:

$./ity //path to watch

然后可以复制该路径下Untitled Document文件到该路径Untitled Document(copy),可以看到终端输出如下,然后就可以对照watch mask查看下输出的意思了:




posted on 2021-10-28 11:56  DoubleLi  阅读(498)  评论(0编辑  收藏  举报