我下面的示例代码监视一个文件是否被修改。假设被监视的文件是 foo.txt,示例代码中的二进制名称是 inotify。我对示例代码做了两次测试。
测试1:
1) ./inotify foo.txt
2) echo "你好"> foo.txt
然后一切正常,并打印出“文件已修改”。
测试2:
1) ./infity foo.txt
2) vim foo.txt
3) 以某种方式编辑并保存,但不要退出 vim
打印出来的行是unknown Mask 0x00008000,检查inotify头文件发现这个事件掩码意味着IN_CLOSE_WRITE。
在我看来,“编辑并保存”只是修改。但显然 inotify 代码有不同的解释。我很奇怪,谁能帮忙解释一下背后的事情?
#include <sys/inotify.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int fdnotify = -1;
if (argc !=2) {
fprintf(stderr, "usage: ./inotify dir_name\n");
exit(1);
}
printf("argc is %d\n", argc);
fdnotify = inotify_init();
if (fdnotify < 0) {
fprintf(stderr, "inotity_init failed: %s\n", strerror(errno));
}
int wd = inotify_add_watch(fdnotify, argv[1], IN_MODIFY);
if (wd < 0) {
fprintf(stderr, "inotify_add_watch failed: %s\n", strerror(errno));
}
while(1) {
char buffer[4096];
struct inotify_event *event = NULL;
int len = read(fdnotify, buffer, sizeof(buffer));
if (len < 0) {
fprintf(stderr, "read error %s\n", strerror(errno));
}
event = (struct inotify_event *) buffer;
while(event != NULL) {
if ((event->mask & IN_MODIFY) ) {
printf("File modified %s\n", event->name);
} else {
printf("unknown Mask 0x%.8x\n", event->mask);
}
len -= sizeof(*event) + event->len;
if (len > 0)
event = ((void *) event) + sizeof(event) + event->len;
else
event = NULL;
}
}
}
最佳答案
Vim 是一个在你保存文件时不直接保存到文件的程序。它创建一个通常名为 .filename.swp
的临时文件,只有当您关闭 vim 时,该文件才会重命名为 filename
。来自 inotify 的常见问题解答:
The IN_MODIFY event is emitted on a file content change (e.g. via the write() syscall) while IN_CLOSE_WRITE occurs on closing the changed file. It means each change operation causes one IN_MODIFY event (it may occur many times during manipulations with an open file) whereas IN_CLOSE_WRITE is emitted only once (on closing the file).
所以你得到的事件实际上是有意义的。假设您有一个名为 filename
的文件。当您打开文件 filename
时,会创建另一个名为 .filename.swp
的文件。如果受到监视,您对此文件执行的所有修改都会生成许多 IN_MODIFY
事件。当你真正保存时,最终发生的是,vim 重命名这个文件并关闭它,从而生成 IN_CLOSE_WRITE
事件。
关于linux - inotify 不将 vim 编辑视为修改事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13312794/