C++ 发生inotify的IN_DELETE事件时,选择方法失败
我正在尝试使用inotify在Linux上递归地监视目录。我在应用程序中使用select方法来监视目录更改。我收到除删除之外的所有事件的通知。每当我使用rm命令删除目录时,我的select方法都会失败 我得到以下错误“无效或不完整的多字节或宽字符” 我怎样才能解决这个问题 谢谢你的回复C++ 发生inotify的IN_DELETE事件时,选择方法失败,c++,linux,filesystems,C++,Linux,Filesystems,我正在尝试使用inotify在Linux上递归地监视目录。我在应用程序中使用select方法来监视目录更改。我收到除删除之外的所有事件的通知。每当我使用rm命令删除目录时,我的select方法都会失败 我得到以下错误“无效或不完整的多字节或宽字符” 我怎样才能解决这个问题 谢谢你的回复 fd = inotify_init(); struct timeval time; fd_set rfds; int ret; /* timeout after five seconds */ time.t
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 );
}
}
}
}
fd=inotify_init();
结构时间值时间;
fd_集rfds;
int ret;
/*五秒钟后超时*/
time.tv_sec=5;
time.tv_usec=0;
/*将fd_集归零*/
FD_ZERO(和RFD);
/*
*将inotify fd添加到fd_集合——当然,
*您的应用程序可能需要添加
*这里还有其他文件描述符
*/
FD_集(FD和RFD);
ret=select(fd+1,&rfds,NULL,NULL,&time);
如果(ret<0)
佩罗(“选择”);
否则如果(!ret)
/*超时*/
否则如果(FD_ISSET(FD和RFD)
/*inotify事件可用*/
{
//inotify事件可用
len=读取(fd、buf、EVENT_buf_len);
if(镜头遮罩和镜头创建)
{
if(事件->掩码和IN_ISDIR)
{
printf(“目录%s已创建。\n”,事件->名称);
}
其他的
{
printf(“文件%s已创建。\n”,事件->名称);
}
}
else if(事件->屏蔽和删除中)
{
if(事件->掩码和IN_ISDIR)
{
printf(“目录%s已被删除。\n”,事件->名称);
}
其他的
{
printf(“文件%s已被删除。\n”,事件->名称);
}
}
else if(事件->掩码和修改中)
{
if(事件->掩码和IN_ISDIR)
{
printf(“目录%s已修改。\n”,事件->名称);
}
其他的
{
printf(“文件%s已修改。\n”,事件->名称);
}
}
}
}
发现了问题。当我删除一个包含子目录的目录时,in_DELETE_SELF事件也与in_DELETE事件一起收到,我正在in_DELETE_SELF事件中关闭我正在监视的目录的fd。因此,现在再次调用select时,无法找到fd,并向我提供了该错误