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



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

    相应头文件是:#include

    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. */  
    };  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    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 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

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

  • 相关阅读:
    手动快速批量修改文件名
    汽车汽配行业经销商协同系统:实现汽配经销商销售管理全流程数字化
    [软件工程] CAP理论和BASE理论
    Go——一、Go语言安装及介绍
    Vue2高级-全局事件总线、TodoList案例(改良版)
    96 前缀树Trie
    linux下doc转docx
    【 OpenGauss源码学习 —— 列存储(CStore)(四)】
    Stream()流常用方法
    spring boot 下载resources下的静态文件为流格式
  • 原文地址:https://blog.csdn.net/chisuisi5702/article/details/126283851