我的下面的示例代碼觀看被修改的文件。讓我們說被移動的文件是foo.txt,並且來自示例代碼的二進制名稱是inotify。我爲示例代碼做了兩個測試。inotify不把Vim編輯作爲修改事件
TEST1:
1)./inotify foo.txt的
2)回聲 「你好」> foo.txt的
然後一切正常,而 「文件改性」 已被打印出來。
TEST2:
1)./infity foo.txt的
2)VIM foo.txt的
3)編輯以某種方式和保存,但不退出VIM
印刷出來線是未知面膜0x00008000,簽出inotify頭文件發現此事件掩碼意味着IN_CLOSE_WRITE。
從我的角度來看,「編輯和保存」只是menas修改。但是令人難以置信的是inotify代碼有一個不同的interpration它。這對我來說很奇怪,誰能幫助解釋背後的事情?
#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;
}
}
}
什麼'inotifywait -m foo.txt'不得不說關於正在生成的事件? – arcticmac