Source : watch.c
파일(디렉토리) 의 create, modify, delete, move에 대해서 모니터링
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )
int main( int argc, char **argv ) {
  char target[20]; /* monitoring directory name */
  int fd;
  int wd; /* watch desc */
  char buffer[BUF_LEN];
  fd = inotify_init();
  if (fd < 0) {
    perror("inotify_init");
  }
  if (argc < 2) {
    fprintf (stderr, "Watching the current directory\n");
    strcpy (target, ".");
  } else {
    fprintf (stderr, "Watching '%s' directory\n", argv[1]);
    strcpy (target, argv[1]);
  }
  wd = inotify_add_watch(fd, "/home1/knight", IN_MODIFY | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO | IN_MOVE_SELF);
  while(1) {
    int length, i = 0;
    length = read(fd, buffer, BUF_LEN);
    if (length < 0) {
      perror("read");
    }
    while (i < length) {
      struct inotify_event *event = (struct inotify_event *) &buffer[i];
      printf ("[debug] wd=%d mask=%d cookie=%d len=%d dir=%s\n", event->wd, event->mask, event->cookie, event->len, (event->mask & IN_ISDIR)?"yes":"no");
      if (event->len) {
        if (event->mask & IN_CREATE) {
          if (event->mask & IN_ISDIR) {
            printf("The directory %s was created.\n", event->name);      
          } else {
            printf("The file %s was created.\n", event->name);
          }
        } else if (event->mask & IN_DELETE) {
          if (event->mask & IN_ISDIR) {
            printf("The directory %s was deleted.\n", event->name);      
          } else {
            printf("The file %s was deleted.\n", event->name);
          }
        } else if (event->mask & IN_MODIFY) {
          if (event->mask & IN_ISDIR) {
            printf("The directory %s was modified.\n", event->name);
          } else {
            printf("The file %s was modified.\n", event->name);
          }
        } else if (event->mask & IN_MOVED_FROM || event->mask & IN_MOVED_TO || event->mask & IN_MOVE_SELF) {
          if (event->mask & IN_ISDIR) {
            printf("The directory %s was moved.\n", event->name);
          } else {
            printf("The file %s was moved.\n", event->name);
          }
        }
      }
      i += EVENT_SIZE + event->len;
    }
  }
  /*
  inotify_rm_watch(fd, wd);
  close(fd);
  */
  return 0;
}



빌드 및 실행
[/home/knight]  gcc -o watch.out watch.c
[/home/knight] ./watch.out
Watching the current directory


터미널 하나를 띄워서 테스트해본다.

[/home/knight] mkdir xx1
[/home/knight] mkdir xx2
[/home/knight] mv xx2 xx222
[/home/knight] rm -rf xx222

watch.out을 실행시켰던 터미널에서 결과를 확인한다.


./watch.out
Watching the current directory
[debug] wd=1 mask=1073742080 cookie=0 len=16 dir=yes
The directory xx1 was created.
[debug] wd=1 mask=1073742080 cookie=0 len=16 dir=yes
The directory xx2 was created.
[debug] wd=1 mask=1073741888 cookie=33508 len=16 dir=yes
The directory xx2 was moved.
[debug] wd=1 mask=1073741952 cookie=33508 len=16 dir=yes
The directory xx222 was moved.
[debug] wd=1 mask=1073742336 cookie=0 len=16 dir=yes
The directory xx222 was deleted.


괜찮은 Referneces
http://ko.wikipedia.org/wiki/Inotify
http://www.ibm.com/developerworks/kr/library/l-ubuntu-inotify/index.html
http://linux.die.net/man/7/inotify
http://darkeside.blogspot.com/2007/12/linux-inotify-example.html
Posted by 김용환 '김용환'