SHARE
TWEET

Untitled

a guest Jul 21st, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <sys/types.h>
  6. #include <sys/inotify.h>
  7. #include <limits.h>
  8.  
  9. #define MAX_EVENTS  1024                                // MAX NUMBER OF PROCESS AT ONE ROUTINE
  10. #define LEN_NAME    32                                  // FILE NAME LENGTH LIMIT
  11. #define EVENT_SIZE  (sizeof(struct inotify_event))      // SIZE OF ONE EVENT
  12. #define BUF_LEN     (MAX_EVENTS*(EVENT_SIZE+LEN_NAME))  // BUFFER TO STORE THE DATA OF EVENTS
  13.  
  14.  
  15. int main(int argc, char **argv) {
  16.     int length, i = 0, wd;
  17.     int fd;
  18.     char buffer[BUF_LEN];
  19.  
  20.     // INITIALIZE INOTIFY
  21.     fd = inotify_init();
  22.     if(fd < 0)
  23.         perror("INOTIFY INITIALIZATION FAIL");
  24.  
  25.     // WATCH FOR STARTING DIRECTORY
  26.     wd = inotify_add_watch(fd, argv[1], IN_CREATE | IN_MODIFY | IN_DELETE);
  27.  
  28.     if(wd == -1)
  29.         printf("FAIL TO ADD WATCH TO %s\n", argv[1]);
  30.     else
  31.         printf("WATCHING DIRECTORY %s\n", argv[1]);
  32.  
  33.     // LOOP
  34.     while(1) {
  35.         i = 0;
  36.         length = read(fd, buffer, BUF_LEN);
  37.  
  38.         if(length < 0)
  39.             perror("read");
  40.  
  41.         while(i < length) {
  42.             struct inotify_event *event = (struct inotify_event *)&buffer[i];
  43.             if(event->len) {
  44.                 if(event->mask & IN_CREATE) {
  45.                     if(event->mask & IN_ISDIR)
  46.                         printf("DIRECTORY %s WAS CREATED\n", event->name);
  47.                     else
  48.                         printf("FILE %s WAS CREATED WITH WD %d\n", event->name, event->wd);
  49.                 }
  50.                 if(event->mask & IN_MODIFY) {
  51.                     if(event->mask & IN_ISDIR)
  52.                         printf("DIRECTORY %s WAS MODIFIED\n", event->name);
  53.                     else
  54.                         printf("FILE %s WAS MODIFIED WITH WD %d\n", event->name, event->wd);
  55.                 }
  56.                 if(event->mask & IN_DELETE) {
  57.                     if(event->mask & IN_ISDIR)
  58.                         printf("DIRECTORY %s WAS DELETED\n", event->name);
  59.                     else
  60.                         printf("FILE %s WAS DELETED WITH WD %d\n", event->name, event->wd);
  61.                 }
  62.                 i += EVENT_SIZE + event->len;
  63.             }
  64.         }
  65.     }
  66.  
  67.     // CLEANING UP
  68.     inotify_rm_watch(fd, wd);
  69.     close(fd);
  70.  
  71.     return 0;
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top