Chinaunix首页 | 论坛 | 博客
  • 博客访问: 119692
  • 博文数量: 41
  • 博客积分: 984
  • 博客等级: 军士长
  • 技术积分: 430
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-12 09:57
文章分类

全部博文(41)

文章存档

2010年(41)

分类:

2010-06-09 11:29:42

#include
#include
#include
#include
#include

static void
_inotify_event_handler(struct inotify_event *event)

{
    printf("\n I am coming!\n");
       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 printf("什么都没做!\n");

    printf("It's end!\n");
}

int
main(int argc, char **argv)
{
  if (argc != 2) {
    printf("Usage: %s \n", argv[0]);
    return -1;
  }

  unsigned char buf[1024] = {0};
  struct inotify_event *event = {0};
  int fd = inotify_init();
  int wd = inotify_add_watch(fd, argv[1], IN_ALL_EVENTS);

  for (;;) {
    fd_set fds;
    FD_ZERO(&fds);
    FD_SET(fd, &fds);
    if (select(fd + 1, &fds, NULL, NULL, NULL) > 0) {
      int len, index = 0;
      while (((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR));
      while (index < len) {
        event = (struct inotify_event *)(buf + index);
        _inotify_event_handler(event);
        index += sizeof(struct inotify_event) + event->len;
      }
    }
  }

  inotify_rm_watch(fd, wd);

  return 0;
}
阅读(716) | 评论(0) | 转发(0) |
0

上一篇:inotify网址暂存

下一篇:创建一个守护进程

给主人留下些什么吧!~~