/*************************************************************************
> File Name: inotify.c
> 监测一个目录下的文件变化,增加或删除提示
> Created Time: Thu 21 Sep 2017 02:41:48 PM CST
************************************************************************/ #include <stdio.h>
#include <sys/inotify.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h> int read_process_inotify_fd(int inotify_fd)
{
int res;
char event_buf[];
int event_size;
int event_pos = ;
struct inotify_event *event;
res = read(inotify_fd, event_buf, sizeof(event_buf));
if(res < sizeof(struct inotify_event))
{
if(errno == EINTR)
{
return ;
}
printf("can not get event ,%s\n",strerror(errno));
return -;
} while(res >= (int)sizeof(*event)) {
event = (struct inotify_event *)(event_buf + event_pos); //指向下一个结构体
if(event->len) {
if(event->mask & IN_CREATE) {
printf("Add device '%s' due to inotify event\n", event->name);
} else {
printf("Removing device '%s' due to inotify event\n", event->name);
}
}
event_size = sizeof(*event) + event->len;
res -= event_size;
event_pos += event_size; //跳转到下一个结构体
}
return ;
} int main(int argc ,char **argv)
{
int fd;
if(argc < )
{
printf("Usage: %s <dir>\n",argv[]);
return -;
}
// inotify_init
fd = inotify_init();
// add watch
//添加监控:argv[1]监控目录,IN_DELETE|IN_CREATE 监控 创建和删除
inotify_add_watch(fd, argv[], IN_DELETE|IN_CREATE);
// read
while()
{
read_process_inotify_fd(fd);
}
return ;
}

inotify监测实例的更多相关文章

  1. 源码包的安装、rsync同步、inotify监测

    一.源码包的安装 1.源码包的作用:yum 使用的是rpm包,rpm包安装的不能指定安装位置 源码包可以按需选择/定制,及时修复bug ,适用于各种平台 2.大致过程:源码包——>make gc ...

  2. Inotify机制的简单应用

    编程之路刚刚开始,错误难免,希望大家能够指出. 一.Inotify机制 1.简单介绍inotify:Inotify可用于检测单个文件,也可以检测整个目录.当检测的对象是一个目录的时候,目录本身和目录里 ...

  3. 028_rync和inotify实现实时备份

    一.服务节点安装inotify-tools. 确保系统后以下输出=> [root@xxxx]# ll /proc/sys/fs/inotify/ total 0 -rw-r--r-- 1 roo ...

  4. Linux内核Inotify机制学习笔记

    一.Inotify简介: Inotify是一种文件变化通知机制,Linux内核从2.6.13开始引入.它是一个内核用于通知用户空间程序文件系统变化的机制.开源社区提出用户态需要内核提供一些机制,以便用 ...

  5. Windows Azure Cloud Service (10) Role的生命周期

    <Windows Azure Platform 系列文章目录> 在上一章内容中,我们提到了Windows Azure会依次调用角色(Role)实例的OnStart()方法和Run()方法. ...

  6. Windows与Linux下文件操作监控的实现

    一.需求分析: 随着渲染业务的不断进行,数据传输渐渐成为影响业务时间最大的因素.究其原因就是因为数据传输耗费较长的时间.于是,依托于渲染业务的网盘开发逐渐成为迫切需要解决的需求.该网盘的实现和当前市场 ...

  7. kvo本质探寻

    一.概述 1.本文章内容,须参照本人的另一篇博客文章“class和object_getClass方法区别”加以理解: 2.基本使用: //给实例对象instance添加观察者,监听该实例对象的某个属性 ...

  8. TypeScript完全解读(26课时)_11.TypeScript完全解读-类型推论和兼容性

    11.TypeScript完全解读-类型推论和兼容性 在一些时候省略指令,ts会帮我们推断出省略的类型的地方适合的类型,通过学习ts的类型推论了解ts的推论规则 类型兼容性就是为了适应js灵活的特点, ...

  9. 10.5 android输入系统_Reader线程_使用EventHub读取事件和核心类及配置文件_实验_分析

    4. Reader线程_使用EventHub读取事件 使用inotify监测/dev/input下文件的创建和删除 使用epoll监测有无数据上报 细节: a.fd1 = inotify_init(& ...

随机推荐

  1. 003——数组(三)count()reset()end()prev()next()current()

    <?php /** * count 统计数组中元素的个数 */ /*$arr=array('blog.com','博客论坛',array('php课程','css课程')); echo coun ...

  2. CF 483B. Friends and Presents 数学 (二分) 难度:1

    B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input stand ...

  3. 实现斐波拉契数列的四种方式python代码

    斐波那契数列 1. 斐波拉契数列简介 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引 ...

  4. linux查看端口对应的程序及pid

    linux中查看特定端口对应的进程以及进程的pid可以使用下面指令: lsof -i:port_number 杀死进程的指令是: kill -s 9 pid

  5. HTML页面每次打开的时候都清除页面缓存

    解决办法为: (1) 用HTML标签设置HTTP头信息 <HEAD> <META    HTTP-EQUIV="Pragma" CONTENT="no- ...

  6. linux 模拟生成 CAN 设备

    /************************************************************************************** * linux 模拟生成 ...

  7. springboot整合mybatis增删改查(一):项目创建

    新建 打开 IDEA 工具,通过 File -> New -> Project->Spring Initializr 主要步骤包括: 选择 Spring Initializr 项目 ...

  8. 在Outlook中修改脱机文件(.ost)的保存位置

    方法一 少读者所在公司的邮箱客户端都在使用微软 Exchange Server 的“缓存 Exchange 模式”.Outlook会默认将脱机文件(.ost文件)保存在C盘上. 但很多读者不希望Out ...

  9. Android 基础题目

    1. BroadcastReceiver 在UI thread? BroadcastReceiver 总是在UI thread, If you register your BroadcastRecei ...

  10. Mybatis selectKey标签的keyProperty属性报错,关键字间隔不能有空格

    源代码片段: <insert id="addAdminAction" parameterType="x.x.x.RoleVo"> <selec ...