(英文部分为转的。代码是个人代码)

1 What’s inotify 

The inotify API provides a mechanism for monitoring file system events. Inotify can be used to monitor individual files, or to monitor directories. When a directory is
monitored, inotify will return events for the directory itself, and for files inside the directory.

2 How to use inotify

2.1 system calls used with this API

The following system calls are used with this API: inotify_init(orinotify_init1),inotify_add_watch, inotify_rm_watch, read,
and close.

2.1.1 inotify_init

It creates an inotify instance and returns a file descriptor referring to the inotify instance. The more recent inotify_init1 is like inotify_init,
but provides some extra functionality.

 
2.1.2 inotify_add_watch
It manipulates the "watch list" associated with an inotify instance. Each item ("watch") in the watch list specifies the pathname of a file or directory, along with some set of events that the kernel should
monitor for the file referred to by that pathname.
 
inotify_add_watch either creates a new watch item, or modifies an existing watch. Each watch has a unique "watch descriptor", an integer returned by inotify_add_watch when
the watch is created.
 
2.1.3 inotify_rm_watch
It removes an item from an inotify watch list.
When all file descriptors referring to an inotify instance have been closed, the underlying object and its resources are freed for reuse by the kernel; all associated watches are automatically freed.
 
2.1.4 read
To determine what events have occurred, an application reads from the inotify file descriptor.
If no events have so far occurred, then, assuming a blocking file descriptor, read will block until at least one event occurs (unless interrupted
by a signal, in which case the call fails with the error EINTR; see signal(7)).
 
Each successful read returns a buffer containing one or more of the following structures:
struct inotify_event {
    int      wd;       /* Watch descriptor */
    uint32_t mask;     /* Mask of events */
    uint32_t cookie;   /* Unique cookie associating related events (for rename ) */
    uint32_t len;      /* Size of name field */
    char     name[];   /* Optional null-terminated name */
};
 
2.2 Inotify events
The inotify_add_watch mask argument and the mask field of the inotify_event structure returned
when reading an inotify file descriptor are both bit masks identifying inotify events. The following bits can be specified in mask when calling inotify_add_watch and
may be returned in the mask field returned by read:
 
IN_ACCESS                             File was accessed (read) (*).
IN_ATTRIB                              Metadata changed, e.g., permissions, timestamps, extended attributes, link count (since Linux 2.6.25), UID, GID, etc. (*).
IN_CLOSE_WRITE                  File opened for writing was closed (*).
IN_CLOSE_NOWRITE            File not opened for writing was closed (*).
IN_CREATE                             File/directory created in watched directory (*).
IN_DELETE                             File/directory deleted from watched directory (*).
IN_DELETE_SELF                   Watched file/directory was itself deleted.
IN_MODIFY                            File was modified (*).
IN_MOVE_SELF                      Watched file/directory was itself moved.
IN_MOVED_FROM                 File moved out of watched directory (*).
IN_MOVED_TO                       File moved into watched directory (*).
IN_OPEN                                 File was opened (*).
……
 
2.3 The flow for the systme calls used with inotify
 

3 代码演示样例
#include <sys/inotify.h>

static aeEventLoop *loop;
/* 全局的notify watch item */
static const char *wds[10]; void sync_file_thread(void*args)
{
int inotify_fd, wd;
int poll_num;
const char *dir, *file;
dir = "/data/wcl/redis_proxy";
file = "binlog_1"; loop =aeCreateEventLoop(1024);
/* 创建inotify instance */
inotify_fd = inotify_init1(IN_NONBLOCK);
if (inotify_fd == -1)
{
perror("Unable to create inotify instance\n");
exit(-1);
}
printf("inotify_fd [%d]\n",inotify_fd); /* 监控文件夹以下的新建文件事件 */
wd = inotify_add_watch(inotify_fd, dir, IN_CREATE);
wds[wd] = dir; /* 监控文件夹以下文件的改动事件 */
wd = inotify_add_watch(inotify_fd, file, IN_MODIFY);
wds[wd] = file; /* 监听inotify的可读事件, 有可读事件, 则表示监控的文件系统有事件产生 */
if (aeCreateFileEvent(loop,inotify_fd,AE_READABLE,handle_inotify_events,NULL))
{
exit(0);
}
aeMain(loop);
aeDeleteEventLoop(loop);
} void handle_inotify_events(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask)
{
char buf[4096], *ptr;
ssize_t len;
struct inotify_event *event; len = read(fd, buf, sizeof(buf));
if (len == -1 && errno != EAGAIN)
{
perror("read error");
exit(-1);
} if (len <= 0)
{
return;
} for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len)
{
event = (struct inotify_event *)ptr; if(event->mask & IN_CREATE)
{
/* new binlog files created */
if(event->len)
{
printf("New File created: %s\n", event->name);
}
} if(event->mask & IN_MODIFY)
{
/* existing files are modified */
printf("File is modified: %s\n", wds[event->wd]);
}
} }

这里我仅仅监控了创建和改动的两个事件,然后依据文件名称字和一些其它的相应机制进行对文件推断改动等。



基于redis ae实现 Linux中的文件系统监控机制(inotify)的更多相关文章

  1. Linux中常用的监控性能的命令(sar、mpstat,vmstat, iostat,)详解

    Linux中常用的监控性能的命令有: sar:能查看CPU的平均信息,还能查看指定CPU的信息.与mpstat相比,sar能查看CPU历史信息 mpstat:能查看所有CPU的平均信息,还能查看指定C ...

  2. inotify 工具 是一种强大的、细粒度的、异步文件系统监控机制

    前言:Inotify是一种强大的.细粒度的.异步文件系统监控机制,它满足各种各样的文件监控需要,可以监控文件系统的访问属性.读写属性.权限属性.删除创建.移动等操作,也就是可以监控文件发生的一切变化. ...

  3. LInux中的文件系统1

    2017-03-08 10:37:55 一.虚拟文件系统VFS 文件系统用于将位于磁盘上的文件按照某种方式组织进内存,并给上层应用程序提供统一的访问接口.Linux支持多种文件系统EXT2/3,NTF ...

  4. Linux 中直接 I/O 机制的介绍

    https://www.ibm.com/developerworks/cn/linux/l-cn-directio/ 对于传统的操作系统来说,普通的 I/O 操作一般会被内核缓存,这种 I/O 被称作 ...

  5. I/O 机制的介绍(Linux 中直接 I/O 机制的介绍)

    IO连接的建立方式 1.缓存IO.流式IO: 2.映射IO.块式IO: 3.直接IO. IO的方式: 同步.异步.定时刷新: MMAP与内核空间 mmap使用共享用户空间与内核空间实现: 直接 I/O ...

  6. Linux中文件/文件系统的压缩、打包和备份总结(基于rhel7)

    文件/文件系统的压缩.打包 Linux有哪些压缩工具可供选择 按压缩比:xz>bzip2>gzip,按压缩时长:gzip>bzip2>xz,另外还有zip可以选择. gzip只 ...

  7. 基于redis AE异步网络架构

    最近的研究已redis源代码,redis高效率是令人钦佩. 在我们的linux那个机器,cpu型号, Intel(R) Pentium(R) CPU G630 @ 2.70GHz  Intel(R) ...

  8. Linux中ext2文件系统的结构

    1.ext2产生的历史 最早的Linux内核是从MINIX系统过渡发展而来的.Linux最早的文件系统就是MINIX文件系统.MINIX文件系统几乎到处都是bug,采用的是16bit偏移量,最大容量为 ...

  9. linux中proc文件系统 -- ldd3读书笔记

    1./proc 文件系统概述 /proc 文件系统是由软件创建,被内核用来向外界报告信息的一个文件系统./proc 下面的每一个文件都和一个内核函数相关联,当文件的被读取时,与之对应的内核函数用于产生 ...

随机推荐

  1. [HTML 5] Styling with ARIA

    See if you can do a better job styling this button using ARIA states. One huge benefit to styling wi ...

  2. Hibernate单向关联N-N

    单向N-N关联必须使用连接表. Company实体: package com.ydoing.hibernate5; import java.util.HashSet; import java.util ...

  3. 切换JDK版本quick

    最近遇到一个小问题,同时做两个项目,jdk版本一个是5,一个是6,我也去网上找了找方法,但是感觉不是特别好用,最后自己通过一些环境变量设置的技巧和一些批处理命令来使得这件事情只需要双击,输入一个数字回 ...

  4. 如何安装windows系统

    前言:装系统有两种方式,一种是下载系统镜像文件后解压ios文件到除c盘以外其他盘都可(如原系统是win10系统,则可以直接右键加载,而不必解压),然后运行.exe文件就可以自动安装了.这种方法在新款电 ...

  5. Memcache相关面试题

    1)memcached的cache机制是怎样的? 懒惰算法 +最近最少使用原则 2)memcached如何实现冗余机制? 冗余:就是有好多好多不经常使用的. 可以不用实现冗余机制,如果非要实现.那就搞 ...

  6. Java对象、Json、Xml转换工具Jackson使用

    在Java项目中將一个对象转换成一段Json格式的字符串是非常常见的,能够实现这种需求的工具包也比较多,例如Gson.JSON-lib.Jackson等等.本文主要介绍Jackson的使用,Jacks ...

  7. DirectUI界面编程(三)从XML文件中加载界面

    Duilib支持xml界面布局,使得界面设计与逻辑处理相分离,本节介绍如何从xml文件中加载界面元素. 我们需要以下几个步骤: 创建并初始化CPaintManagerUI对象. 创建CDialogBu ...

  8. Windows2003 安装MVC4 环境的步骤

    一.作为部署服务器的安装步骤 1.服务器上安装SP2 和 IIS6 2.安装.Net Framework3.5 SP1(完整安装包,包含2.0 2.0SP1,237MB那个安装包) 3.安装.Net ...

  9. this self指针

    this 和 self指针 为函数提供了运行上下问:为函数提供了当前对象的其实地址,方便函数的对对象的访问.

  10. PhotoZoom的工具栏 图片放大不失真

    使用PhotoZoom能够对数码图片无损放大,备受设计师和业内人员的青睐,它的出现时一场技术的革新,新颖的技术,简单的界面,优化的算法,使得它可以对图片进行放大而没有锯齿,不会失真.本文为您一起来认识 ...