下你所需,载你所想!
汇集开发技术源码资料

linux系统监控文件变化状态

:825B :1 :2022-10-08 14:22:00

部分简介

linux系统监控文件变化状态如果开发者对于本文件有需要的可以参考。
#include
#include
#include
#include
#include
#define EVENT_NUM 12
char *event_str[EVENT_NUM] =
{
"IN_ACCESS",
"IN_MODIFY",
"IN_ATTRIB",
"IN_CLOSE_WRITE",
"IN_CLOSE_NOWRITE",
"IN_OPEN",
"IN_MOVED_FROM",
"IN_MOVED_TO",
"IN_CREATE",
"IN_DELETE",
"IN_DELETE_SELF",
"IN_MOVE_SELF"
};
int main(int argc, char *argv[])
{
int fd;
int wd;
int len;
int nread;
char buf[BUFSIZ];
struct inotify_event *event;
int i;
if(argc < 2)
{
fprintf(stderr, "%s path\n", argv[0]);
return -1;
}
fd = inotify_init();
if( fd < 0 )
{
fprintf(stderr, "inotify_init failed\n");
return -1;
}
wd = inotify_add_watch(fd, argv[1], IN_ALL_EVENTS);
if(wd < 0)
{
fprintf(stderr, "inotify_add_watch %s failed\n", argv[1]);
return -1;
}
buf[sizeof(buf) - 1] = 0;
while( (len = read(fd, buf, sizeof(buf) - 1)) > 0 )
{
nread = 0;
while( len > 0 )
{
event = (struct inotify_event *)&buf[nread];
for(i=0; i {
if((event->mask >> i) & 1)
{
if(event->len > 0)
fprintf(stdout, "%s --- %s\n", event->name, event_str[i]);
else
fprintf(stdout, "%s --- %s\n", " ", event_str[i]);
}
}
nread = nread sizeof(struct inotify_event) event->len;
len = len - sizeof(struct inotify_event) - event->len;
}
}
return 0;
}

热门推荐

相关文章