c - inotify_add_watch 用于几个掩码

标签 c linux operating-system filesystems inotify

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

IN_OPEN
IN_CREATE
IN_DELETE
IN_CLOSE_WRITE

例如,

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

最佳答案

它们可以在对 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)

关于c - inotify_add_watch 用于几个掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23644313/

相关文章:

linux - 如何配置 Docker 以使用我的 ens34 网络接口(interface)(而不是 eth0)?

Ruby如何判断执行环境

operating-system - 什么决定了内存模型?

c# - 我如何测量在进程上下文切换中花费的时间的近似值?

c - typedef'ing 指针类型被认为是不好的做法吗?

c++ - 获取错误 : "error: conflicting types for ‘call_celsius’ "and "note: previous implicit declaration of ‘call_celsius’ was here"

linux - Bamboo 5.5.0 - 如何通过 bamboo-capabilities.properties 文件删除远程代理的能力?

在递归中正确调用显示函数(汉诺塔)

c - lex 和 yacc 输出

linux - 如何在 git bash 终端中打开 wsl 目录