前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Inotify机制

Inotify机制

作者头像
DragonKingZhu
发布2020-03-24 16:44:13
1.3K0
发布2020-03-24 16:44:13
举报

描述

Inotify API用于检测文件系统变化的机制。Inotify可用于检测单个文件,也可以检测整个目录。当检测的对象是一个目录的时候,目录本身和目录里的内容都会成为检测的对象。

此种机制的出现的目的是当内核空间发生某种事件之后,可以立即通知到用户空间。方便用户做出具体的操作。

Inotify API

  • inotify_init(void)

用于创建一个inotify的实例,然后返回inotify事件队列的文件描述符。 同样内核也提供了inotify_init1(int flags)接口函数,当flag等于0的时候,该函数等价于inotify_init(void)函数。

  • inotify_add_watch(int fd, const char* pathname, uint32_t mask)

该函数用于添加“watch list”,也就是检测列表。 可以是一个新的watch,也可以是一个已经存在的watch。其中fd就是inotify_init的返回值,pathname是要检测目录或者文件的路径,mask就是要检测的事件类型。该函数成功返回的是一个unique的watch描述符。

  • inotify_rm_watch(int fd, int wd)

用于从watch list种移除检测的对象。

数据结构

内核使用struct inotify_event代表一个文件事件。当检测的文件对象发生变化时,使用read系统调用就会返回一个或者多个inotify_event的文件事件对象。

代码语言:javascript
复制
struct inotify_event {
   int      wd;       /* Watch descriptor */
   uint32_t mask;     /* Mask of events */
   uint32_t cookie;   /* Unique cookie associating related
						 events (for rename(2)) */
   uint32_t len;      /* Size of name field */
   char     name[];   /* Optional null-terminated name */
};

.wd: 就是检测的对象的watch descriptor

.mask: 检测事件的mask

.cookie: 和rename事件相关。

.len: name字段的长度。

.name: 检测对象的name。

可以看到name字段的长度是0,也就是变长的。因为检测的对象的name不定,使用变长可以方便记录检测对象的name。

有关检测的事件类型分为好几种,如下:

代码语言:javascript
复制
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 (*).

注释写的很清楚,不再一一解释了。

实例分析

代码语言:javascript
复制
#include <sys/inotify.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

/*
struct inotify_event {
   int      wd;       // Watch descriptor 
   uint32_t mask;     // Mask of events 
   uint32_t cookie;   // Unique cookie associating related  events (for rename(2))
   uint32_t len;      // Size of name field 
   char     name[];   // Optional null-terminated name 
};

*/

int watch_inotify_events(int fd)
{
	char event_buf[512];
	int ret;
	int event_pos = 0;
	int event_size = 0;
	struct inotify_event *event;
	
	/*读事件是否发生,没有发生就会阻塞*/
	ret = read(fd, event_buf, sizeof(event_buf));
	
	/*如果read的返回值,小于inotify_event大小出现错误*/
	if(ret < (int)sizeof(struct inotify_event))
	{
		printf("counld not get event!\n");
		return -1;
	}
	
	/*因为read的返回值存在一个或者多个inotify_event对象,需要一个一个取出来处理*/
	while( ret >= (int)sizeof(struct inotify_event) )
	{
		event = (struct inotify_event*)(event_buf + event_pos);
		if(event->len)
		{
			if(event->mask & IN_CREATE)
			{
				printf("create file: %s\n",event->name);
			}
			else
			{
				printf("delete file: %s\n",event->name);
			}
		}
		
		/*event_size就是一个事件的真正大小*/
		event_size = sizeof(struct inotify_event) + event->len;
		ret -= event_size;
		event_pos += event_size;
	}
	
	return 0;
}

int main(int argc, char** argv)
{
	int InotifyFd;
	int ret;
	
	if (argc != 2)
	{
		printf("Usage: %s <dir>\n", argv[0]);
		return -1;
	}
	
	/*inotify初始化*/
	InotifyFd = inotify_init();
	if( InotifyFd == -1)
	{
		printf("inotify_init error!\n");
		return -1;
	}
	
	/*添加watch对象*/
	ret = inotify_add_watch(InotifyFd, argv[1], IN_CREATE |  IN_DELETE);
	
	/*处理事件*/
	watch_inotify_events(InotifyFd);

    /*删除inotify的watch对象*/
	if ( inotify_rm_watch(InotifyFd, ret) == -1) 
	{
		printf("notify_rm_watch error!\n");
		return -1;
	}
	
	/*关闭inotify描述符*/
	close(InotifyFd);
	
	return 0;
}

1. 编译代码

代码语言:javascript
复制
gcc inotify.c -o inotify

2. 在tmp目录下创建test目录

代码语言:javascript
复制
mkdir /tmp/test

3. 检测/tmp/test目录,使用inotify机制

代码语言:javascript
复制
./inotify /tmp/test &

4. 在/tmp/test下创建1.txt文件

代码语言:javascript
复制
test$ touch /tmp/test/1.txt
create file: 1.txt
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 描述
  • Inotify API
  • 数据结构
  • 实例分析
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档