我正在尝试使用 inotify 监视配置文件,并且它在第一个事件之后不报告事件。我的程序适用于普通文本文件,但不适用于配置文件,顺便说一下,配置文件也是文本文件。这是我的代码:
int watchFile(char* filePath, void(*handleChange)(void)){
int inotifyFileDescriptor;
int watchDescriptor;
ssize_t numRead;
struct inotify_event* event;
char buffer[INOTIFY_BUFFER_LENGTH] __attribute__((aligned(8)));
char buffer_second[INOTIFY_BUFFER_LENGTH] __attribute__((aligned(8)));
inotifyFileDescriptor = inotify_init();
if(inotifyFileDescriptor == -1){
return inotifyInitError;
}
watchDescriptor = inotify_add_watch(inotifyFileDescriptor, filePath, IN_MODIFY);
if(watchDescriptor == -1){
return failureAddingWatch;
}
while(1){
printf("At the start of the loop ...\n");
numRead = read(inotifyFileDescriptor, buffer, INOTIFY_BUFFER_LENGTH);
if(numRead == 0){
return zeroLengthBufferReturn;
}
if(numRead == -1){
return inotifyEventReadError;
}
/*
Got some event. Check them
printf("got some event\n");
handleChange();*/
for(char* p=buffer; p < buffer + numRead;){
event = (struct inotify_event*) p;
printf("got some event\n");
handleChange();
p += sizeof(struct inotify_event) + event->len;
}
}
return 0;
}