LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   inotify problems (https://www.linuxquestions.org/questions/programming-9/inotify-problems-616336/)

quixote08 01-25-2008 01:47 PM

inotify problems
 
Greetings,

I have been playing with a piece of code based mostly on Robert Love's article in the Linux journal and the code (which follows) is simply not doing what it is supposed to be doing. Basically, every time select unblocks after an event, the event mask is the same, even though the events are all different. I am quite certain I have missed something very basic but for the life of me I cannot see it. Here is the code:

#include <stdio.h>
#include <sys/inotify.h>
#include <sys/select.h>

void print_mask(int );

int main (void)
{
int fd, wd, ret;
struct timeval time;
struct inotify_event *event;
fd_set rfds;


// time out after 10 seconds
time.tv_sec = 5;
time.tv_usec = 0;

fd = inotify_init();
if (fd < 0)
perror ("inotify_init");

wd = inotify_add_watch (fd, "/data/temp", IN_MODIFY|IN_CREATE|IN_DELETE);
if (wd < 0)
perror ("inotify_add_watch");

FD_ZERO (&rfds);
FD_SET (fd, &rfds);

ret = select (fd + 1, &rfds, NULL, NULL, NULL);
if (ret < 0)
perror ("select");
else if (!ret)
printf ("timed out\n");
else if (FD_ISSET (fd, &rfds))
{
if (event->mask & IN_CREATE)
{
printf ("File Created!\n");
print_mask (event->mask);
}
else if (event->mask & IN_DELETE)
{
printf ("File Deleted!\n");
printf("0x%08x\n", event->mask);
}
}

ret = inotify_rm_watch (fd, wd);
if (ret)
perror ("inotify_rm_watch");
if (close(fd))
perror ("close");
return 0;
}

void print_mask(int mask)
{
if (mask & IN_ACCESS)
printf("ACCESS ");
if (mask & IN_MODIFY)
printf("MODIFY ");
if (mask & IN_ATTRIB)
printf("ATTRIB ");
if (mask & IN_CLOSE)
printf("CLOSE ");
if (mask & IN_OPEN)
printf("OPEN ");
if (mask & IN_MOVED_FROM)
printf("MOVE_FROM ");
if (mask & IN_MOVED_TO)
printf("MOVE_TO ");
if (mask & IN_DELETE)
printf("DELETE ");
if (mask & IN_CREATE)
printf("CREATE ");
if (mask & IN_DELETE_SELF)
printf("DELETE_SELF ");
if (mask & IN_UNMOUNT)
printf("UNMOUNT ");
if (mask & IN_Q_OVERFLOW)
printf("Q_OVERFLOW ");
if (mask & IN_IGNORED)
printf("IGNORED " );

if (mask & IN_ISDIR)
printf("(dir) ");
else
printf("(file) ");

printf("0x%08x\n", mask);
}


Any help will be most appreciated. Thanks.

Guttorm 01-26-2008 05:17 AM

Hi

Please use code tags when you post code - it's much easier to read.

You are thinking of http://www.linuxjournal.com/article/8478 right? In it, there is a section where the event is read - which you seem to miss in your code. Select is used to check if there is data available to read, since the read function will wait till there is some. When you have found out there is data to be read you need to read it. Reading events is a bit complicated as the event structure doesn't have a fixed length. But there is a section in the article explaining how you do it.

Also, in your code, you specify IN_MODIFY|IN_CREATE|IN_DELETE meaning that's the only event you will recieve. If you want all, you can use IN_ALL_EVENTS instead.

quixote08 01-26-2008 09:53 AM

Hi Guttorm,

Yes I read that part. I think I misunderstood because I thought what he meant was we could use select and that way not get blocked on a read. Later on I did realized that even was not initialized, which is pretty basic error. In any case I sorted it all out now.

I thought I had inserted the code tags but perhaps not; I rarely post on th eforums so the little details escape me. But I will keep it in mind for next time.

Thanks for your suggestions.


All times are GMT -5. The time now is 05:00 AM.