你如何让 iNotify 只读取一次文件?
我有以下代码:
int settingsCheck(int &length, int &i, char * buffer, int &fd, string setRead[])
{
int f_change = 0;
struct pollfd pfd = { fd, POLLIN, 0 };
/* Poll with a timeout of 100ms */
int ret = poll(&pfd, 1, 100);
/* Check to see the result of the poll */
if (ret < 0) {
fprintf(stderr, "Poll failed: %s\n", strerror(errno));
}
else if (ret == 0) {
/* Timeout with no events -> move on */
}
else {
/* Process the new event */
struct inotify_event event;
length = read(fd, buffer, BUF_LEN);
printf("read\n");
while (i < length)
{
struct inotify_event *event = (struct inotify_event *) &buffer[i];
if (event->len)
{
if (event->mask & IN_MODIFY)
{
printf("The file %s was modified.\n", event->name);
readSettings(setRead);
//f_change = 1;
}
}
i += EVENT_SIZE + event->len;
}
//readSettings(setRead);
return 1;
}
return 0;
}
这使用 poll 来确保函数不会因读取而阻塞。当一个文件被修改时,“read\n”被打印两次,文件被读取两次(我猜第一次读取是为了清除文件,第二次是为了写入文件)。我确保访问该文件的 php 代码只执行一个“fprintf”语句,因此只有一次写入,但 iNotify 检查修改读取两次,可能是因为先清除然后再写入?
有没有办法解决这个问题?