inotify - 监控文件系统

简介: Inotify 原文链接#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <sys/types.h>#include <sys/inotify.h>#define EVENT_SIZE ( sizeof (struct i

Inotify
原文链接

#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 )
{
  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/colin",
                         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 ) {
        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 );
        }
      }
    }
    i += EVENT_SIZE + event->len;
  }

  ( void ) inotify_rm_watch( fd, wd );
  ( void ) close( fd );

  exit( 0 );
}
目录
相关文章
|
11月前
|
Linux 网络安全
【Linux网络服务】Rsync+inotify+nfs实现数据实时备份
【Linux网络服务】Rsync+inotify+nfs实现数据实时备份
|
监控 Oracle IDE
关于Solaris中类似于的Inotify功能
当年的巨无霸一去不复返了啊。在SUN收购Mysql之后,我们还在讨论SUN已经是一个全产业链的超级大公司了。那时的SUN有语言JAVA,有操作系统Solaris,有数据库Mysql,有IDE,有硬件。
54 0
|
监控 Linux Python
Pyinotify – Linux中实时监控文件系统更改
Pyinotify 是一个简单而实用的 Python 模块,它用于通过 inotify 实时监控Linux文件系统的更改。用于在Linux中实时监控文件系统的变化。 作为系统管理员,您可以使用它来监视目标感兴趣的更改,如Web目录或应用程序数据存储目录及其他目录。
2153 0
|
网络安全 数据安全/隐私保护 测试技术
|
Linux 网络安全 数据安全/隐私保护
|
监控 开发工具