Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C inotify真的适用于文件还是仅适用于目录?_C_Linux_Inotify - Fatal编程技术网

C inotify真的适用于文件还是仅适用于目录?

C inotify真的适用于文件还是仅适用于目录?,c,linux,inotify,C,Linux,Inotify,我试图使用inotify来监视对文件/dev/hvc0的更改,方法如下: #include <stdio.h> #include <sys/inotify.h> #include <stdlib.h> #define EVENT_SIZE (sizeof(struct inotify_event)) #define BUF_LEN (1024 * (EVENT_SIZE + 16)) #define WATCH_FILE "/dev/hvc0" /

我试图使用
inotify
来监视对文件
/dev/hvc0
的更改,方法如下:

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

#define EVENT_SIZE  (sizeof(struct inotify_event))
#define BUF_LEN     (1024 * (EVENT_SIZE + 16))
#define WATCH_FILE "/dev/hvc0" /* This file should be present
                                  before this program is run */

int main() {
    int notify_fd;
    int length;
    int i = 0;
    char buffer[BUF_LEN];
    notify_fd = inotify_init();
    if (notify_fd < 0) {
        perror("inotify_init");
    }
    int wd = inotify_add_watch(notify_fd, WATCH_FILE, IN_MODIFY | IN_ACCESS);
    int length_read = read(notify_fd, buffer, BUF_LEN);
    if (length_read) {
        perror("read");
    }
    while (i < length_read) {
        struct inotify_event *event =
            (struct inotify_event *) &buffer[i];
        if (event->len) {
            if (event->mask & IN_ACCESS) {
                printf("The file %s was accessed.\n", event->name);
            } else if (event->mask & IN_MODIFY) {
                printf("The file %s was modified.\n", event->name);
            }
        }
    }

    (void) inotify_rm_watch(notify_fd, wd);
    (void) close(notify_fd);
    return 0;
}
#包括
#包括
#包括
#定义事件大小(sizeof(结构索引事件))
#定义基本长度(1024*(事件大小+16))
#定义监视文件“/dev/hvc0”/*此文件应存在
在运行此程序之前*/
int main(){
int notify_fd;
整数长度;
int i=0;
字符缓冲区[BUF_LEN];
notify_fd=inotify_init();
如果(通知_fd<0){
perror(“inotify_init”);
}
int wd=inotify_add_watch(通知_fd,监视文件,IN_MODIFY|IN_ACCESS);
int length_read=read(通知fd、缓冲区、BUF_LEN);
如果(读取长度){
佩罗(“阅读”);
}
同时(我<长度>读取){
结构inotify_事件*事件=
(结构inotify_事件*)&缓冲区[i];
如果(事件->镜头){
if(事件->掩码和IN_访问){
printf(“已访问文件%s。\n”,事件->名称);
}else if(事件->掩码和修改中){
printf(“文件%s已修改。\n”,事件->名称);
}
}
}
(无效)禁止值班(通知fd、wd);
(无效)关闭(通知财务总监);
返回0;
}
但是,如果文件已被访问/修改,则不会打印。但是,每当我将要监视的路径更改为一个目录并且一个文件被更改时,它都会打印出发生的正确事件

inotify
是否也用于监视文件更改?还是我做错了什么


谢谢

您缺少增加
i
。在循环结束之前添加以下内容:

i += EVENT_SIZE + event->len;

如果要监视更改,还需要将读取/打印操作包装在一个无休止的循环中。

我不理解为什么
事件->len
返回零可能是因为没有返回文件名。但仅出于测试目的,您只需对其进行注释即可。第二点呢 你对i的处理被破坏了,你在循环中从未将其重置为0。这会导致仅当后面的inotify事件比前面最长的事件长时才考虑它们,这可能不是您想要的。请在while循环中请放入
i+=event\u SIZE+event->len

int main() {
    int notify_fd;
    int length;
    int i = 0;
    char buffer[BUF_LEN];
    notify_fd = inotify_init();
    if (notify_fd < 0) {
        perror("inotify_init");
    }
    int wd = inotify_add_watch(notify_fd, WATCH_FILE, IN_MODIFY | IN_ACCESS);
    while(1)
    {
        length = read( notify_fd, buffer, BUF_LEN );

        if ( length < 0 ) {
          perror( "read" );
        }

        while ( i < length ) 
        {
            struct inotify_event *event =
            (struct inotify_event *) &buffer[i];
            // if (event->len) {
            if (event->mask & IN_ACCESS) {
                printf("The file %s was accessed.\n", event->name);
            } else if (event->mask & IN_MODIFY) {
                printf("The file %s was modified.\n", event->name);
            }
            // }
            i += EVENT_SIZE + event->len;
        }
    }

  ( void ) inotify_rm_watch( notify_fd, wd );
  ( void ) close( notify_fd);

  exit( 0 );
}
intmain(){
int notify_fd;
整数长度;
int i=0;
字符缓冲区[BUF_LEN];
notify_fd=inotify_init();
如果(通知_fd<0){
perror(“inotify_init”);
}
int wd=inotify_add_watch(通知_fd,监视文件,IN_MODIFY|IN_ACCESS);
而(1)
{
长度=读取(通知fd、缓冲区、BUF_LEN);
如果(长度<0){
佩罗(“阅读”);
}
while(i镜头){
if(事件->掩码和IN_访问){
printf(“已访问文件%s。\n”,事件->名称);
}else if(事件->掩码和修改中){
printf(“文件%s已修改。\n”,事件->名称);
}
// }
i+=事件大小+事件->镜头;
}
}
(无效)禁止值班(通知fd、wd);
(无效)关闭(通知财务总监);
出口(0);
}

因此,Linux似乎不监视特定文件。如果要收听特定文件,应首先收听其父目录。

如您所说编辑了代码和问题(实际上,创建和删除时我不需要修改;仅访问/修改)要检查访问/修改,仍然不会打印文件的事件。请检查我的更新。我解释得更清楚了。它们是,作为参数提供给inotify\u add\u watch
的路径必须存在,才能使函数工作。这就是为什么你可以跟踪文件的创建,只需观察包含的文件夹,我实际上只需要监视访问/修改,编辑代码和问题,但我不需要相同的事件通知。如果我注释掉'if(event->len)`,代码就行了,但为什么它是零?+1对于非常有用的建议,注释出
event->len
已解决。您是否可以通过注释
if(event->len)
语句再次检查测试结果。@JKB:gr8,现在可以工作了!但是为什么
event->len
0?o、 即使我将I重置为0,程序也会在进行更改后终止,我希望持续监视更改。