简体   繁体   English

如何在C中使用inotify?

[英]How to use inotify in C?

I searched for questions related to inotify, and this one is somewhat different... 我搜索了与inotify相关的问题,这个问题有点不同......

I use the following code to monitor change of one file (not directory). 我使用以下代码来监视一个文件(而不是目录)的更改。 In testing, the read() does return when I save the target file which means it works. 在测试中,当我保存目标文件时,read()会返回,这意味着它可以工作。 But event->mask is 32768 which is not IN_MODIFY and name is empty. 但是event-> mask是32768,它不是IN_MODIFY,名称是空的。 Another issue: it cannot monitor continuously. 另一个问题:它无法持续监控。 When I change the file the second time, it has no response. 当我第二次更改文件时,它没有响应。 Thank you for the help! 感谢您的帮助!

#include <sys/inotify.h>
#include <unistd.h>
#include <stdio.h>

#define EVENT_SIZE  (sizeof (struct inotify_event))
#define BUF_LEN        (16 * (EVENT_SIZE + 16))

int main()
{
    int fd;
    fd = inotify_init();
    if (fd < 0)
        perror("inotify_init()");

    int wd;
    wd = inotify_add_watch(fd, "target.txt", IN_MODIFY);
    if (wd < 0)
        perror("inotify_add_watch");

    char buf[BUF_LEN];
    int len;

start:

    len = read(fd, buf, BUF_LEN);

    if (len > 0)
    {
        int i = 0;
        while (i < len)
        {
            struct inotify_event *event;
            event = (struct inotify_event *) &buf[i];

            printf("wd=%d mask=%x cookie=%u len=%u\n",
                event->wd, event->mask,
                event->cookie, event->len);

            if (event->mask & IN_MODIFY)
                printf("file modified %s", event->name);

            if (event->len)
                printf("name=%s\n", event->name);

            i += EVENT_SIZE + event->len;
        }
    }

    goto start;

    return 0;
}

The 0x8000 corresponds to IN_IGNORED . 0x8000对应于IN_IGNORED Its presence in the mask indicates that the inotify watch had been removed because the file had been removed. 它在掩码中的存在表明因为文件已被删除而已删除了inotify手表。 Your editor probably removed the old file and put a new file in its place. 您的编辑器可能删除了旧文件并将新文件放在其位置。 Changing the file a second time had no effect because the watch had been removed. 第二次更改文件没有效果,因为手表已被删除。

The name is not being returned because you are not watching a directory. 由于您没有观看目录,因此未返回该名称。

From the inotify man page . inotify手册页

The name field is only present when an event is returned for a file inside a watched directory; 只有在监视目录内的文件返回事件时,才会显示name字段。 it identifies the file pathname relative to the watched directory. 它标识相对于监视目录的文件路径名。

... ...

IN_IGNORED -- Watch was removed explicitly (inotify_rm_watch(2)) or automatically (file was deleted, or file system was unmounted). IN_IGNORED - 显式删除了监视(inotify_rm_watch(2))或自动删除(文件已删除,或文件系统已卸载)。

event->mask 32768 is equivalent to 0x8000 that is IN_IGNORED For more information : "/usr/include/linux/inotify.h" event-> mask 32768相当于0x8000,即IN_IGNORED。更多信息:“/ usr /include/linux/inotify.h”

    if (event->mask & IN_IGNORED) {
        /*Remove watch*/ inotify_rm_watch(fileDescriptor,watchDescriptor)
        /*Add watch again*/ inotify_add_watch
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM