c++ - Inotify 删除事件丢失

标签 c++ linux file monitoring inotify

删除同一目录下的文件时,事件丢失。

inotify监控目录:./test 删除命令:rm -rf ./test/*

文件数:20;

但是事件通知12,这是怎么回事?

int fileDescripter = inotify_init();
char buffer[BUF_LEN];
int watchDescripter = inotify_add_watch(fileDescripter, path.c_str(), IN_MODIFY | IN_CREATE | IN_DELETE);

while(true)
{
    read(fileDescripter, buffer, BUF_LEN);

    struct inotify_event *evt_inotify = (struct inotify_event*) &buffer;

string filename(evt_inotify->name);
cout << " CATCH : " << path+"/"+filename << endl;

    if(evt_inotify->len){

        if(evt_inotify->mask & IN_CREATE)
        {
            if(evt_inotify->mask & IN_ISDIR)
            {
                cout << "DIR CREATE" << endl;
            }
            else
            {
                cout << "FILE CREATE" << endl;
            }
        }
        else if(evt_inotify->mask & IN_DELETE)
        {
            if(evt_inotify->mask & IN_ISDIR)
            {
               cout << "DIR DELETE" << endl;
            }
            else
            {
               cout << "FILE DELETE" << endl;
            }

        }
        else if(evt_inotify->mask & IN_MODIFY)
        {
            if(evt_inotify->mask & IN_ISDIR)
            {

            }
            else
            {

            }
        }
    }

}\

在第12行,打印的是event notify count(12)

最佳答案

我解决了。

inotify_init 是阻塞模式,inotify_init1(IN_NONBLOCK) 使用。

inotify_init 读取时需要锁

有用的帖子 http://man7.org/linux/man-pages/man7/inotify.7.html

关于c++ - Inotify 删除事件丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51255795/

相关文章:

python - 写入文件的精确位置

linux - 释放函数在另一个进程上下文中被调用

c++ - 为什么在同一个文本文件上运行多次的程序会有不同的输出?

c++ - 如何为小部件创建自定义布局

linux - 操作系统如何知道缺失页面的磁盘地址?

java - 如何解决java.net.BindException : Address already in use

python - 获取指针而不是字符串

c++ - 在嵌套类型名上制作模板的通用方法

android - 在 Android NDK 上通过 JNI 从 OpenGL ES 帧缓冲区创建位图图像

linux - 在 bash 中替换太多的 if 语句