iNotify单读

时间:2013-09-06 12:07:10

标签: c++ inotify

如何让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检查修改读取两次,可能是因为清除然后写入?

有办法解决这个问题吗?

0 个答案:

没有答案