Find the answer to your Linux question:
Results 1 to 2 of 2
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 ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    2

    inotify woes

    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.

  2. #2
    Just Joined!
    Join Date
    Jan 2008
    Posts
    2

    Never mind...

    Suffering from brain freeze. "event" was obviously not initialized. Got it all working now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...