cha19.监控文件事件

19.1

监控某个目录及其所有子目录的,创建,删除改名

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// Created by root on 5/22/23.
//
#define _XOPEN_SOURCE 600
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <sys/inotify.h>
#include <stddef.h>
#include <sys/stat.h>
#include <ftw.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <alloca.h>
#include <stdbool.h>
// 对root及其子目录下所有文件的创建、删除改名操作监控,并支持监控新建的子目录

struct listNode {
int wd;
char *name;
struct listNode *next;
} *head;

int len() {
int l = 0;
struct listNode *next = head->next;
while (next) {
l++;
next = next->next;
}
return l;
}

struct listNode *newListNode(int wd, const char *name, struct listNode *next) {
struct listNode *node = malloc(sizeof(struct listNode));
node->wd = wd;
node->name = strdup(name);
node->next = next;
return node;
}
struct listNode *searchWD(const char *name) {
struct listNode *next = head->next;
while (next) {
if(strcmp(next->name, name) == 0) {
return next;
}
next = next->next;
}
return NULL;
}
struct listNode *search(int wd) {
struct listNode *next = head->next;
while (next) {
if(next->wd == wd) {
return next;
}
next = next->next;
}
return NULL;
}

bool delete(int wd) {
struct listNode *next = head;
while (next->next) {
if(next->next->wd == wd) {
struct listNode *del = next->next;
next->next = next->next->next;
free(del);
return true;
}
next = next->next;
}
return false;
}

const uint32_t watch_mask = IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MOVED_TO|IN_MOVED_FROM;
int fd = -1;
size_t read_event(void *ievent) {
size_t numRead = read(fd, ievent, 10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
if(numRead == -1) {
fprintf(stderr, "read1: %s\n", strerror(errno));
return -1;
}
printf("readNum = %lu\n", numRead);
return numRead;
}

int addwatch(const char *path) {
int wd;
if((wd = inotify_add_watch(fd, path, watch_mask)) != -1) {
struct listNode *e = NULL;
if((e = search(wd)) == NULL) {
head->next = newListNode(wd, path, head->next);
} else {
free(e->name);
e->name = strdup(path);
}
fprintf(stderr, "watching: %s\n", path);
return 0;
} else {
fprintf(stderr, "fail to watch: %s, %s\n", path, strerror(errno));
return -1;
}
}

int nftw_read(const char *path, const struct stat *sbuf, int type, struct FTW *ftwb) {
switch (sbuf->st_mode & S_IFMT) {
case S_IFDIR:
break;
default:
return 0;
}
addwatch(path);
return 0;
}

void update_monitor(struct inotify_event *ievent) {

struct listNode *node = search(ievent->wd);
char *new_path = malloc(strlen(node->name) + strlen(ievent->name) + 1 + 1);
sprintf(new_path, "%s/%s", node->name, ievent->name);
struct stat stat1;
if(stat(new_path, &stat1) != -1) {
if((stat1.st_mode & S_IFMT) == S_IFDIR) {
// addwatch(new_path);
if(nftw(new_path, nftw_read, 10, FTW_PHYS) == -1) {
fprintf(stderr, "fail to traverse: %s, %s\n", new_path, strerror(errno));
}
}
}
free(new_path);
}
int nftw_del(const char *path, const struct stat *sbuf, int type, struct FTW *ftwb) {
switch (sbuf->st_mode & S_IFMT) {
case S_IFDIR:
break;
default:
return 0;
}
// addwatch(path);
int wd = searchWD(path)->wd;
inotify_rm_watch(fd,wd);
delete(wd);
return 0;
}
void rm_monitor(struct inotify_event *ievent, bool recursive) {
if(recursive) {
struct listNode *node = search(ievent->wd);
char *new_path = malloc(strlen(node->name) + strlen(ievent->name) + 1 + 1);
sprintf(new_path, "%s/%s", node->name, ievent->name);
if (nftw(new_path, nftw_del, 10, FTW_PHYS) == -1) {
fprintf(stderr, "fail to traverse: %s, %s\n", new_path, strerror(errno));
}
free(new_path);
} else {
// int wd = searchWD(new_path)->wd;
inotify_rm_watch(fd,ievent->wd);
delete(ievent->wd);
}
}

void process_event(struct inotify_event *ievent) {
// IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MOVE

printf("mask = %x\n", ievent->mask);
if(ievent->mask & IN_CREATE) {
printf("Monitor: File Creation: %s/%s\n", search(ievent->wd)->name, ievent->name);
update_monitor(ievent);
}
if(ievent->mask & IN_DELETE) {
printf("Monitor: File Deletion: %s, wd = %s\n", ievent->name, search(ievent->wd)->name);
}
if(ievent->mask & IN_DELETE_SELF) {
printf("Monitor: File Deletion: %s, stop monitoring, wd = %s\n", search(ievent->wd)->name, search(ievent->wd)->name);
rm_monitor(ievent, false);
}
if(ievent->mask & IN_MOVED_FROM) {
printf("Monitor: File Move in, from: %s/%s\n", search(ievent->wd)->name, ievent->name);
rm_monitor(ievent, true);

}
if(ievent->mask & IN_MOVED_TO) {
printf("Monitor: File Move out, to: %s/%s\n", search(ievent->wd)->name, ievent->name);
update_monitor(ievent);
}
}

int main(int argc, char *argv[]) {
char *monitor_root = (argc > 1) ? argv[1] : ".";
fd = inotify_init();
if(fd == -1) {
fprintf(stderr, "fail to init inotify: %s\n", strerror(errno));
return 1;
}

head = newListNode(0, "", NULL);
if(head == NULL) {
fprintf(stderr, "fail to malloc head, %s\n", strerror(errno));
return 2;
}

if(nftw(monitor_root, nftw_read, 10, FTW_PHYS) == -1) {
fprintf(stderr, "fail to traverse: %s, %s\n", monitor_root, strerror(errno));
}

void *ievent = malloc(10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
for(;len()>0;) {
size_t numread = -1;
if((numread = read_event(ievent)) == -1) {
fprintf(stderr, "read fail, sleep\n");
usleep(500000);
continue;
}
for(void *p = ievent; p < ievent + numread;) {
struct inotify_event* e = (struct inotify_event *)p;
p += e->len + sizeof(struct inotify_event);
process_event(e);
}
}
free(ievent);
}

todo:

如果read了半个event怎么办

作者

Meow Meow Liu

发布于

2023-05-22

更新于

2024-04-23

许可协议

评论