Linux-c inotify监控目录和文件使用


在Linux相关项目中,遇到过这样子的需求,要监控某个目录下文件的改动,比如新建,删除等,这时候inotify就派上用场了。

相应头文件是:#include <sys/inotify.h>

inotify相关API

  • int inotify_init(void)
    初始化一个inotify实例,返回值是一个文件描述符,在inotify_add_watch函数中将会用到。
  • int inotify_add_watch(int fd,const char *pathname,uint32_t mask)
    由名字可知,增加一个监听,将要监听的文件或者目录添加到inotify中,其中fd是inotify初始化时候返回的文件描述符,pathname是要监听的文件的路径,mask是要监听的事件(事件有多种,比如创建,删除等),其返回值是一个inotify标识,用于判断返回的事件是属于哪一个被监听的文件的,还有移除监听文件的时候通过这个标识指定。
  • int inotify_rm_watch(int fd,uint32_t wd)
    从监听列表中删除一个监听文件/目录,fd是初始化时候返回的文件描述符,wd是监听的文件的标识,由inotify_add_watch函数返回。
  • int inotify_rm_watch(int fd, int wd)
    移除一个文件/目录监控
读取事件

读取事件是调用系统read()函数,其中参数为inotify初始化返回的文件描述符,而读取事件会返回一个inotify_event结构体,如下:

struct inotify_event  
{  
    int wd; /* Watch descriptor. */  
    unit32_t mask; /* Watch mask */  
    unit32_t cookie; /* Cookie to synchronize two events. */  
    unit32_t len; /* Length (including NULLs) of name. */  
    char name[]; /* Name. */  
};  

wd:inotify标识符(就是inotify_add_watch的返回值)
mask:事件的掩码
cookie:文件被修改时才用到
name:就是发生改变的文件/目录的名字
len:就是name的长度

关闭inotify监听

如同文件操作一样,inotify在最后也要调用close()函数关闭监听,参数为初始化时候返回的文件描述符。

事件类型

  • IN_ACCESS:文件被访问
  • IN_ATTRIB:文件属性改变
  • IN_CLOSE_WRITE:关闭打开写的文件
  • IN_CLOSE_NOWRITE:关闭不是打开写的文件
  • IN_CREATE:用于目录,监控的目录中创建目录或者文件时会发生
  • IN_DELETE:用于目录,监控的目录中删除目录或者文件时会发生
  • IN_DELETE_SELF:监控的目录或文件自身被删除
  • IN_MODIFY:文件被修改,会用到上述结构体中的cookie
  • IN_MOVE_SELF:监控的文件或目录自身被移动
  • IN_MOVED_FROM:从监控的目录中移出文件
  • IN_MOVED_TO:往监控的目录中移入文件
  • IN_OPEN:文件/目录被打开
  • IN_ALL_EVENTS: 包含上述所有事件,在inotify_add_watch函数中调用
Sample

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>

#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )

int main(void)
{
	int length, i = 0;
	int fd;
	int wd;
	char buffer[EVENT_BUF_LEN];

	//inotify初始化
	fd = inotify_init();
	if (fd < 0) 
	{
		perror("inotify_init");
		return -1;
	}

	//监听/mnt和/home目录
	wd = inotify_add_watch(fd, "/mnt", IN_CREATE | IN_DELETE);
    printf("1.wd = %d\n",wd);
	wd = inotify_add_watch(fd, "/home", IN_CREATE | IN_DELETE); 
    printf("2.wd = %d\n",wd);
	
	while(1)
	{
		i = 0;
		length = read(fd, buffer, EVENT_BUF_LEN);
		if (length < 0)
		{
			perror("read");
			continue;
		}

		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("New directory wd = %d .\n", event->wd);
						printf("New directory %s created.\n", event->name);
					} 
					else 
					{
                        printf("New file wd = %d .\n", event->wd);
						printf("New file %s created.\n", event->name);
					}
				} 
				else if (event->mask & IN_DELETE) 
				{
					if (event->mask & IN_ISDIR) 
					{
                        printf("Directory wd = %d deleted.\n", event->wd);
						printf("Directory %s deleted.\n", event->name);
					} 
					else 
					{
                        printf("File wd = %d deleted.\n", event->wd);
						printf("File %s deleted.\n", event->name);
					}
				}
			}
			i += EVENT_SIZE + event->len;
		}

	}
	
	inotify_rm_watch(fd, wd);
	close(fd);
	
	return 0;
}

注意在事件发生时候,inotify.event.name的问题,可以参考这篇博文:
https://ixyzero.com/blog/archives/3513.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux C/C中,可以使用inotify API来监控文件系统的事件。inotify API提供了一种异步的、高效的文件系统事件监控机制,可以监控文件目录的创建、删除、修改、移动等操作。 使用inotify API需要先创建一个inotify实例,并使用inotify_add_watch函数添加要监控文件目录。当文件系统中发生对应的事件时,inotify通知应用程序。 下面是一个简单的例子,演示如何使用inotify API监控文件变化: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.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, "/tmp", IN_MODIFY | IN_CREATE | IN_DELETE ); while ( 1 ) { i = 0; 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 ); } ``` 上面的代码中,先使用inotify_init函数创建一个inotify实例,然后使用inotify_add_watch函数添加要监控目录。在while循环中,使用read函数读取inotify实例中的事件,然后根据事件类型进行相应的处理。最后使用inotify_rm_watch函数移除监控项,关闭inotify实例。 需要注意的是,inotify API只能用于Linux系统,其他操作系统需要使用不同的机制来监控文件系统事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值