mohangraj
mohangraj

Reputation: 11034

Inotify event in C

Program:

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

int main()
{
    int fd = inotify_init();
    int wd1 = inotify_add_watch(fd, "/home/guest/a", IN_MODIFY);
    struct inotify_event *event = (struct inotify_event*) malloc(sizeof(struct inotify_event));
    read(fd, event, 1000);
    if (event->mask & IN_MODIFY) {
        printf("File '%s' is modified\n", event->name);
    }
}

Output:

$ ./a.out 
File '' is modified
$

I expected that the above program will notify with the filename if file a is modified. But it notify without filename. So, how to get the filename if the file is modified using inotify.

Upvotes: 4

Views: 919

Answers (1)

John Zwinck
John Zwinck

Reputation: 249093

The documentation says:

The name field is present only when an event is returned for a file inside a watched directory; it identifies the file pathname relative to the watched directory. This pathname is null-terminated, and may include further null bytes ('\0') to align subsequent reads to a suitable address boundary.

So your problem is that you're expecting inotify to "echo" the name back to you, but that is not how it works.

Upvotes: 4

Related Questions