如何在C使用inotify的? [英] How to use inotify in C?

查看:373
本文介绍了如何在C使用inotify的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索到的inotify相关的问题,而这一次却有所不同...

我用下面的code监视一个文件(不是目录)的变化。在测试中,当我保存的目标文件,这意味着它的工作原理则read()不会返回。但事件 - >面膜为32768未IN_MODIFY和名称为空。另一个问题:它不能连续监测。当我更改该文件的第二次,它没有反应。感谢您的帮助!

 的#include< SYS / inotify.h>
#包括LT&;&unistd.h中GT;
#包括LT&;&stdio.h中GT;#定义EVENT_SIZE(的sizeof(结构inotify_event))
的#define BUF_LEN(16 *(EVENT_SIZE + 16))诠释的main()
{
    INT的fd;
    FD = inotify_init();
    如果(FD℃,)
        PERROR(inotify_init());    INT WD;
    WD = inotify_add_watch(FD,target.txt,IN_MODIFY);
    如果(WD℃,)
        PERROR(inotify_add_watch);    烧焦的buf [BUF_LEN]
    INT LEN;开始:    LEN =读(FD,BUF,BUF_LEN);    如果(LEN大于0)
    {
        INT I = 0;
        而(I< LEN)
        {
            结构inotify_event *事件;
            事件=(结构inotify_event *)及BUF [I]            的printf(WD =%d个面具=%×饼干=%U LEN =%U \\ N,
                事件 - > WD,事件 - >面膜,
                事件 - >饼干,事件 - > LEN);            如果(事件 - >&面膜放大器; IN_MODIFY)
                的printf(文件修改%S事件 - >名);            如果(事件 - > LEN)
                的printf(NAME =%S \\ n,事件 - >名);            I + = EVENT_SIZE +事件 - > LEN;
        }
    }    转到启动;    返回0;
}


解决方案

为0x8000 对应 IN_IGNORED 。在面具的presence表示,因为该文件已被删除的inotify 手表已被删除。您大概编辑删除旧文件,并把新的文件在其位置。改变该文件,因为手表已除去第二时间没有影响。

不被返回的名称,因为你不是在看一个目录。

的inotify 手册页


  

名称字段只有当返回一个事件的监控目录中的文件present;它标识相对于观看目录中的文件路径名。


  
  

...


  
  

IN_IGNORED - 手表显式删除(inotify_rm_watch(2))或自动(文件被删除或文件系统已卸载)


I searched for questions related to inotify, and this one is somewhat different...

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. But event->mask is 32768 which is not IN_MODIFY and name is empty. 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. Its presence in the mask indicates that the inotify watch had been removed because the file had been removed. 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.

The name field is only present when an event is returned for a file inside a watched directory; 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).

这篇关于如何在C使用inotify的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆