Advertisement
Guest User

Fixed

a guest
Mar 5th, 2013
1,212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <sys/types.h>
  5. #include <sys/inotify.h>
  6. #include <unistd.h>
  7.  
  8. #define EVENT_SIZE  ( sizeof (struct inotify_event) )
  9. #define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )
  10.  
  11. const char * file_path = "/path/to/file";
  12.  
  13. int main( int argc, char **argv )
  14. {
  15.   int length, i;
  16.   int fd;
  17.   int wd;
  18.   char buffer[BUF_LEN];
  19.  
  20.   fd = inotify_init();
  21.  
  22.   if ( fd < 0 ) {
  23.     perror( "inotify_init" );
  24.   }
  25.  
  26.   wd = inotify_add_watch(fd,
  27.              file_path,
  28.              IN_ALL_EVENTS);
  29.  
  30.   while(1)
  31.   {
  32.       length = read( fd, buffer, BUF_LEN );
  33.  
  34.       if ( length < 0 ) {
  35.           perror( "read" );
  36.       }
  37.           i = 0;
  38.       while ( i < length ) {
  39.           struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
  40.           printf("inotify event\n");
  41.  
  42.           if ( event->len ) {
  43.               if ( event->mask & IN_CREATE ) {
  44.                   if ( event->mask & IN_ISDIR ) {
  45.                       printf( "The directory %s was created.\n", event->name );
  46.                   }
  47.                   else {
  48.                       printf( "The file %s was created.\n", event->name );
  49.                   }
  50.               }
  51.               else if ( event->mask & IN_DELETE ) {
  52.                   if ( event->mask & IN_ISDIR ) {
  53.                       printf( "The directory %s was deleted.\n", event->name );
  54.                   }
  55.                   else {
  56.                       printf( "The file %s was deleted.\n", event->name );
  57.                   }
  58.               }
  59.               else if ( event->mask & IN_MODIFY ) {
  60.                   if ( event->mask & IN_ISDIR ) {
  61.                       printf( "The directory %s was modified.\n", event->name );
  62.                   }
  63.                   else {
  64.                       printf( "The file %s was modified.\n", event->name );
  65.                   }
  66.               }
  67.           }
  68.           i += EVENT_SIZE + event->len;
  69.       }
  70.   }
  71.  
  72.   ( void ) inotify_rm_watch( fd, wd );
  73.   ( void ) close( fd );
  74.  
  75.   exit( 0 );
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement