Reputation: 31
I am trying to monitor a directory recursively on Linux using inotify. I am using select method in my application to monitor directory changes. I get notification for all the events except for delete. When ever i delete a directory, using rm command, my select method fails.
I get the following error "invalid or incomplete multibyte or wide character"
How can i solve this problem.
Thanks for the reply
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 );
}
}
}
}
Upvotes: 0
Views: 274
Reputation: 31
Found the problem. When I deleted a directory which has sub-directories in it, IN_DELETE_SELF event was also received along with IN_DELETE event and i was closing the fd of the directory that i was monitoring, in IN_DELETE_SELF event. So now when select was called again could not find the fd and gave me that error.
Upvotes: 1