当inotify的IN_DELETE事件发生时,select方法失败
select method fails when IN_DELETE event of inotify occurs
我正试图使用inotify在Linux上递归地监视一个目录。我在应用程序中使用select方法来监视目录更改。除删除外,我收到所有事件的通知。当我使用rm命令删除目录时,我的select方法会失败。
我得到以下错误"无效或不完整的多字节或宽字符"
我怎样才能解决这个问题。
感谢的回复
fd = inotify_init();
struct timeval time;
fd_set rfds;
int ret;
/* timeout after five seconds */
time.tv_sec = 5;
time.tv_usec = 0;
/* zero-out the fd_set */
FD_ZERO (&rfds);
/*
* add the inotify fd to the fd_set -- of course,
* your application will probably want to add
* other file descriptors here, too
*/
FD_SET (fd, &rfds);
ret = select (fd + 1, &rfds, NULL, NULL, &time);
if (ret < 0)
perror ("select");
else if (!ret)
/* timed out! */
else if (FD_ISSET (fd, &rfds)
/* inotify events are available! */
{
// inotify events are available
len = read(fd, buf, EVENT_BUF_LEN);
if(len <= 0)
{
qDebug()<<"Error : read failed..";
perror("read : ");
}
while(i < len)
{
struct inotify_event *event;
event = (struct inotify_event *) &buf[i];
eventNotification(event);
i += EVENT_SIZE + event->len;
count++;
}
}
void eventNotification (struct inotify_event *event)
{
if ( event->len )
{
if ( event->mask & IN_CREATE )
{
if ( event->mask & IN_ISDIR )
{
printf( "The directory %s was created.n", event->name );
}
else
{
printf( "The file %s was created.n", event->name );
}
}
else if ( event->mask & IN_DELETE )
{
if ( event->mask & IN_ISDIR )
{
printf( "The directory %s was deleted.n", event->name );
}
else
{
printf( "The file %s was deleted.n", event->name );
}
}
else if ( event->mask & IN_MODIFY )
{
if ( event->mask & IN_ISDIR )
{
printf( "The directory %s was modified.n", event->name );
}
else
{
printf( "The file %s was modified.n", event->name );
}
}
}
}
发现问题。当我删除了一个包含子目录的目录时,in_DELETE_SELF事件和in_DELETE事件也被接收到,并且我正在关闭in_DELETE_SELF事件中我正在监视的目录的fd。所以现在当再次调用select时,找不到fd,并给了我这个错误。
相关文章:
- 如果没有malloc,链表实现将失败
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 视图中的参数推导失败:take_while
- 链接到自行创建的dll失败
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- gcc和c++17的过载解析失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 从父数组测试用例构造二叉树失败
- LibGit2 SSH身份验证失败
- 如何让LLDB在成功时退出,在失败时等待
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- 生成MRPT库时cmake配置失败
- 为什么select()总是以EINTR错误号失败
- 当inotify的IN_DELETE事件发生时,select方法失败