在Linux相关项目中,遇到过这样子的需求,要监控某个目录下文件的改动,比如新建,删除等,这时候inotify就派上用场了。
相应头文件是:#include
读取事件是调用系统read()函数,其中参数为inotify初始化返回的文件描述符,而读取事件会返回一个inotify_event结构体,如下:
struct inotify_event
{
int wd; /* Watch descriptor. */
unit32_t mask; /* Watch mask */
unit32_t cookie; /* Cookie to synchronize two events. */
unit32_t len; /* Length (including NULLs) of name. */
char name[]; /* Name. */
};
wd:inotify标识符(就是inotify_add_watch的返回值)
mask:事件的掩码
cookie:文件被修改时才用到
name:就是发生改变的文件/目录的名字
len:就是name的长度
如同文件操作一样,inotify在最后也要调用close()函数关闭监听,参数为初始化时候返回的文件描述符。
#include
#include
#include
#include
#include
#include
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
int main(void)
{
int length, i = 0;
int fd;
int wd;
char buffer[EVENT_BUF_LEN];
//inotify初始化
fd = inotify_init();
if (fd < 0)
{
perror("inotify_init");
return -1;
}
//监听/mnt和/home目录
wd = inotify_add_watch(fd, "/mnt", IN_CREATE | IN_DELETE);
printf("1.wd = %d\n",wd);
wd = inotify_add_watch(fd, "/home", IN_CREATE | IN_DELETE);
printf("2.wd = %d\n",wd);
while(1)
{
i = 0;
length = read(fd, buffer, EVENT_BUF_LEN);
if (length < 0)
{
perror("read");
continue;
}
while (i < length)
{
struct inotify_event *event = (struct inotify_event *) &buffer[i];
if (event->len)
{
if (event->mask & IN_CREATE)
{
if (event->mask & IN_ISDIR)
{
printf("New directory wd = %d .\n", event->wd);
printf("New directory %s created.\n", event->name);
}
else
{
printf("New file wd = %d .\n", event->wd);
printf("New file %s created.\n", event->name);
}
}
else if (event->mask & IN_DELETE)
{
if (event->mask & IN_ISDIR)
{
printf("Directory wd = %d deleted.\n", event->wd);
printf("Directory %s deleted.\n", event->name);
}
else
{
printf("File wd = %d deleted.\n", event->wd);
printf("File %s deleted.\n", event->name);
}
}
}
i += EVENT_SIZE + event->len;
}
}
inotify_rm_watch(fd, wd);
close(fd);
return 0;
}
注意在事件发生时候,inotify.event.name的问题,可以参考这篇博文:
https://ixyzero.com/blog/archives/3513.html