iNotify多次读取

时间:2013-09-05 08:19:07

标签: c++ linux inotify

我使用以下代码使用I notify监视文件。我首先进行民意调查,然后阅读是否存在阻止阻止的更改:

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 && event->name == "setitngs.txt")
                {
                    printf("The file %s was modified.\n", event->name);
                    //f_change = 1;
                }
                else if(event->mask & IN_MODIFY)
                {
                    printf("Not settings\n");
                }
            }
            i += EVENT_SIZE + event->len;
        }
        readSettings(setRead);
        return 1;
    }

    return 0;
}

当我修改被监控的文本文件(由php脚本修改)时,我的程序多次读取文件(2-4),有时只读取最终读取的正确数据。

有谁知道为什么会发生这种情况/如何解决?

PS:我说如果我在下面的内容中调用readSettings()函数是正确的:

if (event->mask & IN_MODIFY && event->name == "setitngs.txt")

那么当监视“settings.txt”时,只应调用一次readSettings()吗?

0 个答案:

没有答案