缺少删除事件的提示

时间:2018-07-10 00:44:54

标签: c++ linux file monitoring 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行中,打印为事件通知计数(12)

1 个答案:

答案 0 :(得分:0)

我解决了。

inotify_init为阻止模式,使用inotify_init1(IN_NONBLOCK)。

inotify_init读取时需要锁定

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