【问题标题】:inotify_add_watch for several masks几个掩码的 inotify_add_watch
【发布时间】:2014-07-01 21:33:24
【问题描述】:

我想监控对目录/文件的更改。如果我想监控只说 5 个事件 带有以下面具

IN_OPEN
IN_CREATE
IN_DELETE
IN_CLOSE_WRITE

例如,

我必须在每个掩码上调用inotify_add_watch(),还是有办法将掩码捆绑在一起,以便调用一次inotify_add_watch?

【问题讨论】:

  • 要让所有掩码使用 "wfd = inotify_add_watch(ifd,"",IN_ALL_EVENTS);"
  • 也可以,这是对fork() 的一些错误调用,这给了我不可预知的结果。IN_ALL_EVENTS 很短,可以节省时间...

标签: c linux operating-system filesystems inotify


【解决方案1】:

他们可以在一次调用inotify_add_watch() 中进行“或”运算,即:

wd = inotify_add_watch(fd, path, IN_CREATE|IN_DELETE);

任何位组合都可以在以下限制内进行“或”运算:

/* the following are legal, implemented events that user-space can watch for */
#define IN_ACCESS           0x00000001  /* File was accessed */
#define IN_MODIFY           0x00000002  /* File was modified */
#define IN_ATTRIB           0x00000004  /* Metadata changed */
#define IN_CLOSE_WRITE      0x00000008  /* Writtable file was closed */
#define IN_CLOSE_NOWRITE    0x00000010  /* Unwrittable file closed */
#define IN_OPEN             0x00000020  /* File was opened */
#define IN_MOVED_FROM       0x00000040  /* File was moved from X */
#define IN_MOVED_TO         0x00000080  /* File was moved to Y */
#define IN_CREATE           0x00000100  /* Subfile was created */
#define IN_DELETE           0x00000200  /* Subfile was deleted */
#define IN_DELETE_SELF      0x00000400  /* Self was deleted */

/* the following are legal events.  they are sent as needed to any watch */
#define IN_UNMOUNT          0x00002000  /* Backing fs was unmounted */
#define IN_Q_OVERFLOW       0x00004000  /* Event queued overflowed */
#define IN_IGNORED          0x00008000  /* File was ignored */

/* helper events */
#define IN_CLOSE            (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) /* close */
#define IN_MOVE             (IN_MOVED_FROM | IN_MOVED_TO) /* moves */

/* special flags */
#define IN_ISDIR            0x40000000  /* event occurred against dir */
#define IN_ONESHOT          0x80000000  /* only send event once */

/*
 * All of the events - we build the list by hand so that we can add flags in
 * the future and not break backward compatibility.  Apps will get only the
 * events that they originally wanted.  Be sure to add new events here!
 */
#define IN_ALL_EVENTS (IN_ACCESS | IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE | \
         IN_CLOSE_NOWRITE | IN_OPEN | IN_MOVED_FROM | \
         IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF)

(以上来自inotify.h)

【讨论】:

  • 对要进行或运算的掩码是否有限制...我以某种方式错过了它的语义。我的意思是,在我的情况下,我可以或 5 个掩码
  • 我从你上面的回答中得到了它,但我仍然没有在这里得到什么,也许是我的代码有错误。我遇到了IN_ALL_EVENTS,它检测到所有事件,但是这会不断重复执行的事件。不是确定我是否正确使用了IN_ALL_EVENTS。
  • @Emma,从我更新的(再次)答案中可以看出,“IN_ALL_EVENTS”是所有合法用户空间位的定义宏;全部“或”在一起。
【解决方案2】:

您可以使用“或”操作来组合掩码:

  wd = inotify_add_watch( fd, "<path of dir>", IN_CREATE | IN_DELETE );

否则,如果您想使用所有掩码,请使用

  wfd = inotify_add_watch(ifd,"<path of dir>",IN_ALL_EVENTS);

这是所有面具的组合。

【讨论】:

    猜你喜欢
    • 2014-07-14
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多