Inotify file in C

I am trying to run the inotify example in C..but does not work. I want to track changes in a file (tmp.cfg file), but it does not work. I don’t know if I run it correctly, because I understand how to control directories, but not one file Here is an example:

#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #include <sys/inotify.h> #include <unistd.h> #define EVENT_SIZE ( sizeof (struct inotify_event) ) #define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) int main( int argc, char **argv ) { int length, i = 0; int fd; int wd; char buffer[BUF_LEN]; fd = inotify_init(); if ( fd < 0 ) { perror( "inotify_init" ); } wd = inotify_add_watch( fd, "/home/name/tmp.cfg", IN_MODIFY | IN_CREATE | IN_DELETE ); length = read( fd, buffer, BUF_LEN ); if ( length < 0 ) { perror( "read" ); } while ( i < length ) { struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ]; if ( event->mask & IN_CREATE ) { printf( "The file %s was created.\n", event->name ); } else if ( event->mask & IN_DELETE ) { printf( "The file %s was deleted.\n", event->name ); } else if ( event->mask & IN_MODIFY ) { printf( "The file %s was modified.\n", event->name ); } i += EVENT_SIZE + event->len; } ( void ) inotify_rm_watch( fd, wd ); ( void ) close( fd ); return 0; } 

As soon as I run it, if I write something in a file and then save it, nothing will happen. I tried debugging it ... the problem seems to be that (event-> mask and IN_MODIFY), since it does not recognize it as a modification

+6
source share
3 answers

You have 2 questions. Firstly, as far as I can tell, inotify really does not work with files - this requires a directory name.

Secondly, you missed if (event->len) { inside the while loop.

This code works for me to create, delete, and modify files in the current directory:

 #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #include <sys/inotify.h> #include <unistd.h> #define EVENT_SIZE (sizeof(struct inotify_event)) #define BUF_LEN (1024 * (EVENT_SIZE + 16)) int main(int argc, char **argv) { int length, i = 0; int fd; int wd; char buffer[BUF_LEN]; fd = inotify_init(); if (fd < 0) { perror("inotify_init"); } wd = inotify_add_watch(fd, ".", IN_MODIFY | IN_CREATE | IN_DELETE); length = read(fd, buffer, BUF_LEN); if (length < 0) { perror("read"); } while (i < length) { struct inotify_event *event = (struct inotify_event *) &buffer[i]; if (event->len) { if (event->mask & IN_CREATE) { printf("The file %s was created.\n", event->name); } else if (event->mask & IN_DELETE) { printf("The file %s was deleted.\n", event->name); } else if (event->mask & IN_MODIFY) { printf("The file %s was modified.\n", event->name); } } i += EVENT_SIZE + event->len; } (void) inotify_rm_watch(fd, wd); (void) close(fd); return 0; } 
+2
source

I think you are not using your username, which is your home directory, and you are not checking the return of inotify_add_watch , which probably does not work:

 "/home/name/tmp.cfg" 

Edit: ok, the second problem, you shouldn't type name because

The name field is present only when the event is returned for the file inside the observed directory;

Edit2: the third problem, the file must exist before the program starts, because you add the clock to the file, I suggest you check the error with inotify_add_watch

+1
source

When viewing a file, if the file is controlled by an editor, which you can do to edit it and create a change, most likely it will perform some operations, as a result of which the original file that you asked for will be deleted. Consequently, notifications stop if you look at only one file.

+1
source

All Articles