inotify监听文件和目录

简介: 用途:监测指定文件或目录(或不指定则为当前目录)的一切动作。

监控文件夹A,若文件夹中的B文件发生变化,则执行C命令。Linux下可以通过inotify完成该功能。


1,相关函数


inotify_init() - 创建一个inotify实例
inotify_add_watch(int fd, const char *pathname, uint32_t mask) - 加入文件或目录到inotify进行监测
inotify_rm_watch(int fd, int wd) - 移除一个watcher


2,相关结构


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 */
          };


3,Mask


适用于 inotify_add_watch mask 与 read 返回的inotify_event中mask


IN_ACCESS文件被访问


IN_ATTRIB文件属性发生变化


IN_CLOSE_WRITE以write方式打开文件并关闭


IN_CLOSE_NOWRITE以非write方式打开文件并关闭


IN_CREATE文件或目录被创建


IN_DELETE文件或目录被删除(被监测的文件夹A中B文件被删除)


IN_DELETE_SELF被监测的文件或目录被删除(被监测的文件夹A被删除)


IN_MODIFY文件被修改


IN_MOVE_SELF被监测的文件或目录移动


IN_MOVED_FROM文件移出被监测的目录


IN_MOVED_TO文件移入被监测的目录


IN_OPEN文件被打开


上述flag的集合


IN_ALL_EVENTS以上所有flag的集合


IN_MOVEIN_MOVED_TO|IN_MOVED_FROM


IN_CLOSEIN_CLOSE_WRITE|IN_CLOSE_NOWRITE


不常用的flag


IN_DONT_FOLLOW不follow符号链接 (since 2.6.15)


IN_EXCL_UNLINK当文件从监测目中unlink后,则不再报告该文件的相关event,比如监控/tmp使用 (since 2.6.36)


IN_MASK_ADD追打MASK到被监测的pathname


IN_ONESHOT只监测一次


IN_ONLYDIR只监测目录


仅由read返回


IN_IGNOREDinotify_rm_watch,文件被删除或者文件系统被umount


IN_ISDIR发生事件的是一个目录


IN_Q_OVERFLOWEvent队列溢出


IN_UNMOUNT文件系统unmount


4,例子


用途:监测指定文件或目录(或不指定则为当前目录)的一切动作。


使用:inotify [文件或目录]


#include   
#include   
#include   
#include   
#include   
#include   
#include   
#define ERROR(text) error(1, errno, "%s", text)  
struct EventMask {  
int        flag;  
const char *name;  
};  
int freadsome(void *dest, size_t remain, FILE *file)  
{  
char *offset = (char*)dest;  
while (remain) {  
int n = fread(offset, 1, remain, file);  
if (n == 0) {  
return -1;  
        }  
        remain -= n;  
        offset += n;  
    }  
return 0;  
}  
//http://www.ibm.com/developerworks/cn/linux/l-inotify/  
//http://www.jiangmiao.org/blog/2179.html  
int main(int argc, char *argv[])  
{  
const char *target;  
if (argc == 1) {  
target =".";  
    }  
else {  
        target = argv[1];  
    }  
struct EventMask event_masks[] = {  
{ IN_ACCESS,"IN_ACCESS" },    
{ IN_ATTRIB,"IN_ATTRIB" },    
{ IN_CLOSE_WRITE,"IN_CLOSE_WRITE" },    
{ IN_CLOSE_NOWRITE,"IN_CLOSE_NOWRITE" },    
{ IN_CREATE,"IN_CREATE" },    
{ IN_DELETE,"IN_DELETE" },    
{ IN_DELETE_SELF,"IN_DELETE_SELF" },    
{ IN_MODIFY,"IN_MODIFY" },    
{ IN_MOVE_SELF,"IN_MOVE_SELF" },    
{ IN_MOVED_FROM,"IN_MOVED_FROM" },    
{ IN_MOVED_TO,"IN_MOVED_TO" },    
{ IN_OPEN,"IN_OPEN" },    
{ IN_DONT_FOLLOW,"IN_DONT_FOLLOW" },    
//{ IN_EXCL_UNLINK, "IN_EXCL_UNLINK" },    
{ IN_MASK_ADD,"IN_MASK_ADD" },    
{ IN_ONESHOT,"IN_ONESHOT" },    
{ IN_ONLYDIR,"IN_ONLYDIR" },    
{ IN_IGNORED,"IN_IGNORED" },    
{ IN_ISDIR,"IN_ISDIR" },    
{ IN_Q_OVERFLOW,"IN_Q_OVERFLOW" },    
{ IN_UNMOUNT,"IN_UNMOUNT" },    
    };  
int monitor = inotify_init();  
if (-1 == monitor) {  
ERROR("monitor");  
    }  
int watcher = inotify_add_watch(monitor, target, IN_ALL_EVENTS);  
if (-1 == watcher) {  
ERROR("inotify_add_watch error");  
    }  
FILE *monitor_file = fdopen(monitor, "r");  
char last_name[1024];  
char name[1024];  
/* event:inotify_event -> name:char[event.len] */  
while (true) {  
struct inotify_event event;  
if (-1 == freadsome(&event, sizeof(event), monitor_file)) {  
ERROR("freadsome error");  
        }  
if (event.len) {  
            freadsome(name, event.len, monitor_file);  
        }  
else {  
sprintf(name,"FD: %d\n", event.wd);  
        }  
if (strcmp(name, last_name) != 0) {  
            puts(name);  
            strcpy(last_name, name);  
        }  
/* 显示event的mask的含义 */  
int i = 0;  
for (i = 0; i < sizeof(event_masks) / sizeof(struct EventMask); ++i) {  
if (event.mask & event_masks[i].flag) {  
printf("\t%s\n", event_masks[i].name);  
            }  
        }  
    }  
return 0;  
}  


下面的例子来源于inotify_tools的示例代码,但是做了一些改进。inotify_tools监视一个目录时,可以得到该目录变化的信息。但是在该目录下,创建一个子目录后,子目录中的变化,inotify_tools不能获取。所以我添加了动态监控目录的代码。


#include 
#include 
#include 
#include 
enum {
ACTION_NULL_WD,
ACTION_ADD_WD,
ACTION_DEL_WD,
};
int main(int argc, const char **argv)
{
int err = 0;
if (!inotifytools_initialize()) {
printf("inotifytools_initialize failedn");
goto error;
}
inotifytools_initialize_stats();
const char *monitor_path = ".";
if (argc > 1) {
monitor_path = argv[1];
}
printf("Monitor dir(%s)n", monitor_path);
if (!inotifytools_watch_recursively(monitor_path, IN_ALL_EVENTS)) {
printf("inotifytools_watch_recursively failedn");
goto error;
}
inotifytools_set_printf_timefmt("%F %T");
struct inotify_event * event = inotifytools_next_event(-1);
char path[256];
while (event) {
inotifytools_printf( event, "%T %w%f %en" );
if (IN_ISDIR&event->mask) {
int action = ACTION_NULL_WD;
if ((IN_DELETE|IN_DELETE_SELF|IN_MOVED_FROM)&event->mask) {
action = ACTION_DEL_WD;
snprintf(path, sizeof(path), "%s%s",
inotifytools_filename_from_wd(event->wd),
event->name);
printf("Remove path(%s) from wdn", path);
} else if (((IN_CREATE|IN_MOVED_TO)&event->mask) && (IN_ISDIR&event->mask)) {
action = ACTION_ADD_WD;
snprintf(path, sizeof(path), "%s%s",
inotifytools_filename_from_wd(event->wd),
event->name);
printf("Add path(%s) into wdn", path);
}
if (ACTION_ADD_WD == action) {
if (!inotifytools_watch_recursively(path, IN_ALL_EVENTS)) {
printf("inotifytools_watch_recursively failedn");
goto error;
}
} else if (ACTION_DEL_WD == action) {
if (!inotifytools_remove_watch_by_wd(event->wd)) {
printf("inotifytools_remove_watch_by_wd failed. event->wd(%d)n", event->wd);
goto error;
}
}
}
event = inotifytools_next_event(-1);
}
printf("Exitn");
return 0;
error:
err = inotifytools_error();
printf("Error(%d)n", err);
return -1;
}


相关文章
|
2天前
|
监控 Linux
Linux修改fs.inotify.max_user_watches(“外部文件更改同步可能很慢”和“当前的 inotify(7) 监视限制太低”)
Linux修改fs.inotify.max_user_watches(“外部文件更改同步可能很慢”和“当前的 inotify(7) 监视限制太低”)
4 0
|
1月前
|
监控 网络安全 开发工具
Linxu服务器文件双向同步-rsync+sersync
Linxu服务器文件双向同步-rsync+sersync
67 0
|
监控 Linux C++
Linux 用inotify监听文件和目录
日常应用中,常常会遇到以下场景,监控文件夹A,若文件夹中的B文件发生变化,则执行C命令。Linux下可以通过inotify完成该功能。 自从Linux kernel 2.6.13起,inotify以作为内核的一部份,同时需要glibc 2.4以上版本。
2727 0
|
监控 Linux
Linux 使用inotify-tools监听文件或目录
转载: Inotify一种强大的、细粒度的、异步文件系统监控机制,它满足各种各样的文件监控需要,可以监控文件系统的访问属性、读写属性、权限属性、删除创建、移动等操作,也就是可以监控文件发生的一切变化。
1362 0
|
Linux
Linux rsync+sersync实现文件时时同步
一.简介 sersync是基于Inotify开发的,类似于Inotify-tools的工具; sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字; rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高; 注:当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。
1165 0
|
监控 应用服务中间件 Linux
Linux inotify监听文件状态
Inotify 是一个 Linux特性,它监控文件系统操作,比如读取、写入和创建。Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。
1516 0
|
Web App开发 前端开发 JavaScript
|
监控 Linux API
linux实时文件事件监听--inotify
一、inotify简介 inotify是Linux内核2.6.13 (June 18, 2005)版本新增的一个子系统(API),它提供了一种监控文件系统(基于inode的)事件的机制,可以监控文件系统的变化如文件修改、新增、删除等,并可以将相应的事件通知给应用程序。
3201 0