inotify - 监控文件系统
2015-12-02 00:02
190 查看
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 ); }
相关文章推荐
- JVM优化之压缩普通对象指针(CompressedOops)
- 1、tomcat目录及端口规划实践
- Optional 可选值
- tomcat部署后在web-inf下没有lib文件夹解决方案
- nginx location配置详解
- C++编程:socket设置为非阻塞方式(windows和linux)1
- centos7 安装samba
- 监控linux的脚本
- nginx转发urlencode编码
- linux 目录 授权
- Linux git 文档
- Linux磁盘管理之设备文件详解04
- Linux的slab设计
- 教你读Git——Linux内核版本管理工具
- squid 三种代理实验
- Linux内核源代码review Case
- Linux内核之——C语言“徒手”写内存!
- java.lang.NoClassDefFoundError: org/apache/log4j/Level
- Linux内核源代码解析之——欲三次握手,先构造传输控制块!
- 双击启动tomcat中的startup.bat闪退原因及解决方法