Line 0
Link Here
|
|
|
1 |
/*- |
2 |
* Copyright (c) 2016 - 2017 Rozhuk Ivan <rozhuk.im@gmail.com> |
3 |
* All rights reserved. |
4 |
* |
5 |
* Redistribution and use in source and binary forms, with or without |
6 |
* modification, are permitted provided that the following conditions |
7 |
* are met: |
8 |
* 1. Redistributions of source code must retain the above copyright |
9 |
* notice, this list of conditions and the following disclaimer. |
10 |
* 2. Redistributions in binary form must reproduce the above copyright |
11 |
* notice, this list of conditions and the following disclaimer in the |
12 |
* documentation and/or other materials provided with the distribution. |
13 |
* |
14 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
15 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
18 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
20 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
21 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
22 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
23 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
24 |
* SUCH DAMAGE. |
25 |
* |
26 |
* Author: Rozhuk Ivan <rozhuk.im@gmail.com> |
27 |
* |
28 |
*/ |
29 |
|
30 |
#include <sys/param.h> |
31 |
#include <sys/types.h> |
32 |
#include <sys/event.h> |
33 |
#include <sys/stat.h> |
34 |
#include <sys/mount.h> |
35 |
#include <sys/fcntl.h> /* open, fcntl */ |
36 |
|
37 |
#include <inttypes.h> |
38 |
#include <stdlib.h> /* malloc, exit */ |
39 |
#include <unistd.h> /* close, write, sysconf */ |
40 |
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */ |
41 |
#include <dirent.h> /* opendir, readdir */ |
42 |
#include <errno.h> |
43 |
#include <pthread.h> |
44 |
|
45 |
#include "kqueue_fnm.h" |
46 |
|
47 |
|
48 |
/* Preallocate items count. */ |
49 |
#ifndef FILES_ALLOC_BLK_SIZE |
50 |
# define FILES_ALLOC_BLK_SIZE 32 |
51 |
#endif |
52 |
|
53 |
|
54 |
typedef struct file_info_s { /* Directory file. */ |
55 |
int fd; /* For notify kqueue(). */ |
56 |
struct dirent de; /* d_reclen used for action. */ |
57 |
struct stat sb; |
58 |
} file_info_t, *file_info_p; |
59 |
|
60 |
|
61 |
typedef struct readdir_data_s { |
62 |
int fd; |
63 |
uint8_t *buf; |
64 |
size_t buf_size; |
65 |
size_t buf_used; |
66 |
size_t buf_pos; |
67 |
} readdir_data_t, *readdir_data_p; |
68 |
|
69 |
|
70 |
typedef struct kqueue_file_mon_data_s { |
71 |
int fd; /* For notify kqueue(). */ |
72 |
int is_dir; |
73 |
int is_local; /* Is file system local. */ |
74 |
char path[(PATH_MAX + 2)]; |
75 |
size_t path_size; |
76 |
size_t name_offset; /* Parent path size. */ |
77 |
size_t st_blksize; |
78 |
void *udata; |
79 |
kqueue_fnm_p kfnm; |
80 |
/* For dir. */ |
81 |
file_info_p files; |
82 |
volatile size_t files_count; |
83 |
size_t files_allocated; |
84 |
} kqueue_file_mon_data_t; |
85 |
|
86 |
|
87 |
typedef struct kqueue_file_nonify_monitor_s { |
88 |
int fd; /* kqueue() fd. */ |
89 |
int pfd[2]; /* pipe queue specific. */ |
90 |
uint8_t *tmpbuf; |
91 |
size_t tmpbuf_size; |
92 |
kfnm_event_handler_cb cb_func; /* Callback on dir/file change. */ |
93 |
kqueue_file_mon_settings_t s; |
94 |
pthread_t tid; |
95 |
} kqueue_fnm_t; |
96 |
|
97 |
|
98 |
typedef void (*kq_msg_cb)(kqueue_file_mon_data_p fmd); |
99 |
|
100 |
typedef struct kqueue_file_mon_msg_pkt_s { |
101 |
size_t magic; |
102 |
kq_msg_cb msg_cb; |
103 |
kqueue_file_mon_data_p fmd; |
104 |
size_t chk_sum; |
105 |
} kqueue_fnm_msg_pkt_t, *kqueue_fnm_msg_pkt_p; |
106 |
|
107 |
#define KF_MSG_PKT_MAGIC 0xffddaa00 |
108 |
|
109 |
|
110 |
#ifndef O_NOATIME |
111 |
# define O_NOATIME 0 |
112 |
#endif |
113 |
#ifndef O_EVTONLY |
114 |
# define O_EVTONLY O_RDONLY |
115 |
#endif |
116 |
#define OPEN_FILE_FLAGS (O_EVTONLY | O_NONBLOCK | O_NOFOLLOW | O_NOATIME | O_CLOEXEC) |
117 |
|
118 |
#define EVFILT_VNODE_SUB_FLAGS (NOTE_WRITE | \ |
119 |
NOTE_EXTEND | \ |
120 |
NOTE_ATTRIB | \ |
121 |
NOTE_LINK | \ |
122 |
NOTE_CLOSE_WRITE) |
123 |
|
124 |
#define EVFILT_VNODE_FLAGS_ALL (NOTE_DELETE | \ |
125 |
EVFILT_VNODE_SUB_FLAGS | \ |
126 |
NOTE_RENAME | \ |
127 |
NOTE_REVOKE) |
128 |
|
129 |
#ifndef _GENERIC_DIRSIZ |
130 |
# define _GENERIC_DIRSIZ(__de) MIN((__de)->d_reclen, sizeof(struct dirent)) |
131 |
#endif |
132 |
|
133 |
#define IS_NAME_DOTS(__name) ('.' == (__name)[0] && \ |
134 |
('\0' == (__name)[1] || \ |
135 |
('.' == (__name)[1] && '\0' == (__name)[2]))) |
136 |
#define IS_DE_NAME_EQ(__de1, __de2) (0 == mem_cmpn((__de1)->d_name, \ |
137 |
(__de1)->d_namlen, \ |
138 |
(__de2)->d_name, \ |
139 |
(__de2)->d_namlen)) |
140 |
#define zalloc(__size) calloc(1, (__size)) |
141 |
|
142 |
#if (defined(__FreeBSD_version) && __FreeBSD_version >= 1100000) || defined(HAVE_REALLOCARRAY) |
143 |
# define REALLOCARRAY(__mem, __size, __count) reallocarray((__mem), (__size), (__count)) |
144 |
#else /* For old BSD systems. */ |
145 |
# define REALLOCARRAY(__mem, __size, __count) realloc((__mem), ((__size) * (__count))) |
146 |
#endif |
147 |
|
148 |
|
149 |
void *kqueue_fnm_proccess_events_proc(void *data); |
150 |
|
151 |
static inline int |
152 |
mem_cmpn(const void *buf1, const size_t buf1_size, |
153 |
const void *buf2, const size_t buf2_size) { |
154 |
|
155 |
if (buf1_size != buf2_size) |
156 |
return (((buf1_size > buf2_size) ? 127 : -127)); |
157 |
if (0 == buf1_size || buf1 == buf2) |
158 |
return (0); |
159 |
if (NULL == buf1) |
160 |
return (-127); |
161 |
if (NULL == buf2) |
162 |
return (127); |
163 |
return (memcmp(buf1, buf2, buf1_size)); |
164 |
} |
165 |
|
166 |
static inline int |
167 |
realloc_items(void **items, const size_t item_size, |
168 |
size_t *allocated, const size_t alloc_blk_cnt, const size_t count) { |
169 |
size_t allocated_prev, allocated_new; |
170 |
uint8_t *items_new; |
171 |
|
172 |
if (NULL == items || 0 == item_size || NULL == allocated || |
173 |
0 == alloc_blk_cnt) |
174 |
return (EINVAL); |
175 |
allocated_prev = (*allocated); |
176 |
if (NULL != (*items) && |
177 |
allocated_prev > count && |
178 |
allocated_prev <= (count + alloc_blk_cnt)) |
179 |
return (0); |
180 |
allocated_new = (((count / alloc_blk_cnt) + 1) * alloc_blk_cnt); |
181 |
items_new = (uint8_t*)REALLOCARRAY((*items), item_size, allocated_new); |
182 |
if (NULL == items_new) /* Realloc fail! */ |
183 |
return (ENOMEM); |
184 |
|
185 |
if (allocated_new > allocated_prev) { /* Init new mem. */ |
186 |
memset((items_new + (allocated_prev * item_size)), 0x00, |
187 |
((allocated_new - allocated_prev) * item_size)); |
188 |
} |
189 |
(*items) = items_new; |
190 |
(*allocated) = allocated_new; |
191 |
|
192 |
return (0); |
193 |
} |
194 |
|
195 |
|
196 |
static int |
197 |
readdir_data_start(int fd, uint8_t *buf, size_t buf_size, readdir_data_p rdd) { |
198 |
|
199 |
if (-1 == fd || NULL == buf || 0 == buf_size || NULL == rdd) |
200 |
return (EINVAL); |
201 |
if (-1 == lseek(fd, 0, SEEK_SET)) |
202 |
return (errno); |
203 |
memset(rdd, 0x00, sizeof(readdir_data_t)); |
204 |
rdd->fd = fd; |
205 |
rdd->buf = buf; |
206 |
rdd->buf_size = buf_size; |
207 |
|
208 |
return (0); |
209 |
} |
210 |
|
211 |
static int |
212 |
readdir_data_next(readdir_data_p rdd, struct dirent *de) { |
213 |
int ios; |
214 |
uint8_t *ptr; |
215 |
|
216 |
if (NULL == rdd || NULL == de) |
217 |
return (EINVAL); |
218 |
|
219 |
retry: |
220 |
if (rdd->buf_used <= rdd->buf_pos) { |
221 |
ios = getdents(rdd->fd, (char*)rdd->buf, (int)rdd->buf_size); |
222 |
if (-1 == ios) |
223 |
return (errno); |
224 |
rdd->buf_used = (size_t)ios; |
225 |
rdd->buf_pos = 0; |
226 |
if (0 == ios) |
227 |
return (ESPIPE); /* EOF. */ |
228 |
} |
229 |
/* Keep data aligned. */ |
230 |
ptr = (rdd->buf + rdd->buf_pos); |
231 |
memcpy(de, ptr, (sizeof(struct dirent) - sizeof(de->d_name))); |
232 |
if (0 == de->d_reclen) |
233 |
return (ESPIPE); /* EOF. */ |
234 |
rdd->buf_pos += de->d_reclen; |
235 |
#ifdef DT_WHT |
236 |
if (DT_WHT == de->d_type) |
237 |
goto retry; |
238 |
#endif |
239 |
memcpy(de, ptr, _GENERIC_DIRSIZ(de)); |
240 |
if (IS_NAME_DOTS(de->d_name)) |
241 |
goto retry; |
242 |
return (0); |
243 |
} |
244 |
|
245 |
|
246 |
static int |
247 |
file_info_find_de_fileno(file_info_p files, size_t files_count, |
248 |
struct dirent *de, size_t *idx) { |
249 |
size_t i; |
250 |
|
251 |
if (NULL == files || NULL == de || NULL == idx) |
252 |
return (0); |
253 |
for (i = 0; i < files_count; i ++) { |
254 |
if (de->d_type != files[i].de.d_type || |
255 |
de->d_fileno != files[i].de.d_fileno) |
256 |
continue; |
257 |
(*idx) = i; |
258 |
return (1); |
259 |
} |
260 |
(*idx) = files_count; |
261 |
return (0); |
262 |
} |
263 |
|
264 |
static int |
265 |
file_info_find_de_name(file_info_p files, size_t files_count, |
266 |
const size_t skip_idx, struct dirent *de, size_t *idx) { |
267 |
size_t i; |
268 |
|
269 |
if (NULL == files || NULL == de || NULL == idx) |
270 |
return (0); |
271 |
for (i = 0; i < files_count; i ++) { |
272 |
if (i == skip_idx || |
273 |
de->d_type != files[i].de.d_type || |
274 |
0 == IS_DE_NAME_EQ(de, &files[i].de)) |
275 |
continue; |
276 |
(*idx) = i; |
277 |
return (1); |
278 |
} |
279 |
(*idx) = files_count; |
280 |
return (0); |
281 |
} |
282 |
|
283 |
|
284 |
static int |
285 |
is_fs_local(struct statfs *stfs) { |
286 |
|
287 |
if (NULL == stfs) |
288 |
return (0); |
289 |
if (0 == (MNT_LOCAL & stfs->f_flags)) |
290 |
return (0); |
291 |
/* fusefs exceptions check. */ |
292 |
if (0 != memcmp(stfs->f_fstypename, "fusefs.", 7)) |
293 |
return (1); /* Not fusefs. */ |
294 |
if (0 == memcmp(&stfs->f_fstypename[7], "sshfs", 5)) |
295 |
return (0); |
296 |
return (1); |
297 |
} |
298 |
|
299 |
|
300 |
static void |
301 |
kqueue_file_mon_data_clean(kqueue_file_mon_data_p fmd) { |
302 |
size_t i; |
303 |
|
304 |
if (NULL == fmd) |
305 |
return; |
306 |
if (-1 != fmd->fd) { |
307 |
close(fmd->fd); |
308 |
fmd->fd = -1; |
309 |
} |
310 |
if (0 != fmd->is_local) { /* Stop monitoring files/dirs. */ |
311 |
for (i = 0; i < fmd->files_count; i ++) { |
312 |
if (-1 == fmd->files[i].fd) |
313 |
continue; |
314 |
close(fmd->files[i].fd); |
315 |
} |
316 |
} |
317 |
free(fmd->files); |
318 |
fmd->files = NULL; |
319 |
fmd->files_count = 0; |
320 |
fmd->files_allocated = 0; |
321 |
} |
322 |
|
323 |
static void |
324 |
kqueue_file_mon_data_free(kqueue_file_mon_data_p fmd) { |
325 |
size_t i; |
326 |
|
327 |
if (NULL == fmd) |
328 |
return; |
329 |
if (-1 != fmd->fd) { |
330 |
close(fmd->fd); |
331 |
} |
332 |
if (0 != fmd->is_local) { /* Stop monitoring files/dirs. */ |
333 |
for (i = 0; i < fmd->files_count; i ++) { |
334 |
if (-1 == fmd->files[i].fd) |
335 |
continue; |
336 |
close(fmd->files[i].fd); |
337 |
} |
338 |
} |
339 |
free(fmd->files); |
340 |
free(fmd); |
341 |
} |
342 |
|
343 |
static kqueue_file_mon_data_p |
344 |
kqueue_file_mon_data_alloc(kqueue_fnm_p kfnm, const char *path, void *udata) { |
345 |
kqueue_file_mon_data_p fmd; |
346 |
|
347 |
if (NULL == kfnm || NULL == path) |
348 |
return (NULL); |
349 |
fmd = zalloc(sizeof(kqueue_file_mon_data_t)); |
350 |
if (NULL == fmd) |
351 |
return (NULL); |
352 |
/* Remember args. */ |
353 |
fmd->path_size = strlcpy(fmd->path, path, PATH_MAX); |
354 |
fmd->name_offset = fmd->path_size; |
355 |
fmd->udata = udata; |
356 |
fmd->kfnm = kfnm; |
357 |
|
358 |
return (fmd); |
359 |
} |
360 |
|
361 |
static int |
362 |
kqueue_file_mon_data_readdir(kqueue_file_mon_data_p fmd, size_t expected_count) { |
363 |
int error; |
364 |
struct dirent *de; |
365 |
uint8_t *tmpbuf; |
366 |
file_info_p tmfi; |
367 |
readdir_data_t rdd; |
368 |
|
369 |
if (NULL == fmd || 0 == fmd->is_dir) |
370 |
return (EINVAL); |
371 |
|
372 |
/* Get temp buf. */ |
373 |
if (fmd->st_blksize > fmd->kfnm->tmpbuf_size) { |
374 |
tmpbuf = realloc(fmd->kfnm->tmpbuf, fmd->st_blksize); |
375 |
if (NULL == tmpbuf) |
376 |
return (ENOMEM); |
377 |
fmd->kfnm->tmpbuf = tmpbuf; |
378 |
fmd->kfnm->tmpbuf_size = fmd->st_blksize; |
379 |
} |
380 |
|
381 |
free(fmd->files); |
382 |
fmd->files = NULL; |
383 |
fmd->files_count = 0; |
384 |
fmd->files_allocated = 0; |
385 |
/* Pre allocate. */ |
386 |
if (0 != realloc_items((void**)&fmd->files, |
387 |
sizeof(file_info_t), &fmd->files_allocated, |
388 |
FILES_ALLOC_BLK_SIZE, expected_count)) |
389 |
return (ENOMEM); |
390 |
|
391 |
error = readdir_data_start(fmd->fd, fmd->kfnm->tmpbuf, fmd->st_blksize, &rdd); |
392 |
if (0 != error) |
393 |
return (error); |
394 |
for (;;) { |
395 |
if (0 != realloc_items((void**)&fmd->files, |
396 |
sizeof(file_info_t), &fmd->files_allocated, |
397 |
FILES_ALLOC_BLK_SIZE, fmd->files_count)) { |
398 |
free(fmd->files); |
399 |
fmd->files = NULL; |
400 |
fmd->files_count = 0; |
401 |
fmd->files_allocated = 0; |
402 |
return (ENOMEM); |
403 |
} |
404 |
de = &fmd->files[fmd->files_count].de; /* Use short name. */ |
405 |
/* Get file name from folder. */ |
406 |
if (0 != readdir_data_next(&rdd, de)) |
407 |
break; |
408 |
/* Get file attrs. */ |
409 |
if (0 != fstatat(fmd->fd, de->d_name, |
410 |
&fmd->files[fmd->files_count].sb, |
411 |
AT_SYMLINK_NOFOLLOW)) { |
412 |
memset(&fmd->files[fmd->files_count].sb, 0x00, |
413 |
sizeof(struct stat)); |
414 |
} |
415 |
fmd->files[fmd->files_count].fd = -1; |
416 |
fmd->files_count ++; |
417 |
} |
418 |
/* Mem compact. */ |
419 |
tmfi = REALLOCARRAY(fmd->files, sizeof(file_info_t), (fmd->files_count + 1)); |
420 |
if (NULL != tmfi) { /* realloc ok. */ |
421 |
fmd->files = tmfi; |
422 |
fmd->files_allocated = (fmd->files_count + 1); |
423 |
} |
424 |
|
425 |
return (0); /* OK. */ |
426 |
} |
427 |
|
428 |
|
429 |
static void |
430 |
kqueue_file_mon_data_init(kqueue_file_mon_data_p fmd) { |
431 |
size_t i; |
432 |
struct stat sb; |
433 |
struct statfs stfs; |
434 |
struct kevent kev; |
435 |
|
436 |
if (NULL == fmd) |
437 |
return; |
438 |
fmd->fd = open(fmd->path, OPEN_FILE_FLAGS); |
439 |
if (-1 == fmd->fd) |
440 |
return; |
441 |
if (0 != fstat(fmd->fd, &sb)) |
442 |
goto err_out; |
443 |
fmd->st_blksize = (size_t)sb.st_blksize; |
444 |
|
445 |
/* Get parent folder name. */ |
446 |
if (S_ISDIR(sb.st_mode)) { |
447 |
fmd->is_dir = 1; |
448 |
/* Be sure that folder contain trailing '/'. */ |
449 |
if ('/' != fmd->path[(fmd->path_size - 1)]) { |
450 |
fmd->path[fmd->path_size] = '/'; |
451 |
fmd->path_size ++; |
452 |
fmd->path[fmd->path_size] = 0; |
453 |
} |
454 |
/* Skip last '/' for parent dir search. */ |
455 |
fmd->name_offset = (fmd->path_size - 1); |
456 |
} |
457 |
|
458 |
/* Is file system local? */ |
459 |
if (0 != fmd->is_dir && |
460 |
0 != fmd->kfnm->s.mon_local_subfiles && |
461 |
0 == fstatfs(fmd->fd, &stfs)) { |
462 |
fmd->is_local = is_fs_local(&stfs); |
463 |
} |
464 |
|
465 |
/* Find parent dir path size. */ |
466 |
while (0 < fmd->name_offset && '/' != fmd->path[(fmd->name_offset - 1)]) { |
467 |
fmd->name_offset --; |
468 |
} |
469 |
|
470 |
/* Dir special processing. */ |
471 |
if (0 != fmd->is_dir) { |
472 |
/* Read and remember dir content. */ |
473 |
if (0 != kqueue_file_mon_data_readdir(fmd, 1)) |
474 |
goto err_out; |
475 |
} |
476 |
/* Add to kqueue. */ |
477 |
EV_SET(&kev, fmd->fd, EVFILT_VNODE, (EV_ADD | EV_CLEAR), |
478 |
EVFILT_VNODE_FLAGS_ALL, 0, fmd); |
479 |
if (-1 == kevent(fmd->kfnm->fd, &kev, 1, NULL, 0, NULL)) |
480 |
goto err_out; |
481 |
if (0 != fmd->is_local) { /* Add monitor sub files/dirs, ignory errors. */ |
482 |
for (i = 0; i < fmd->files_count; i ++) { |
483 |
fmd->files[i].fd = openat(fmd->fd, |
484 |
fmd->files[i].de.d_name, OPEN_FILE_FLAGS); |
485 |
if (-1 == fmd->files[i].fd) |
486 |
continue; |
487 |
EV_SET(&kev, fmd->files[i].fd, EVFILT_VNODE, |
488 |
(EV_ADD | EV_CLEAR), |
489 |
EVFILT_VNODE_SUB_FLAGS, 0, fmd); |
490 |
kevent(fmd->kfnm->fd, &kev, 1, NULL, 0, NULL); |
491 |
} |
492 |
} |
493 |
return; /* OK. */ |
494 |
|
495 |
err_out: |
496 |
kqueue_file_mon_data_clean(fmd); |
497 |
} |
498 |
|
499 |
|
500 |
static void |
501 |
kqueue_handle_changes(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd) { |
502 |
struct stat sb; |
503 |
struct kevent kev; |
504 |
size_t i, k, files_count; |
505 |
file_info_p files; |
506 |
|
507 |
if (NULL == kfnm || NULL == fmd) |
508 |
return; |
509 |
if (0 != fstat(fmd->fd, &sb) || |
510 |
0 == sb.st_nlink) { |
511 |
kqueue_file_mon_data_clean(fmd); |
512 |
kfnm->cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED, |
513 |
fmd->path, "", NULL); |
514 |
return; |
515 |
} |
516 |
if (0 == fmd->is_dir) { |
517 |
fmd->path[fmd->name_offset] = 0; |
518 |
kfnm->cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CHANGED, fmd->path, |
519 |
(fmd->path + fmd->name_offset), NULL); |
520 |
fmd->path[fmd->name_offset] = '/'; |
521 |
return; |
522 |
} |
523 |
|
524 |
/* Dir processing. */ |
525 |
|
526 |
/* Save prev. */ |
527 |
files = fmd->files; |
528 |
files_count = fmd->files_count; |
529 |
fmd->files = NULL; |
530 |
fmd->files_count = 0; |
531 |
/* Update dir. */ |
532 |
if (0 != kqueue_file_mon_data_readdir(fmd, files_count)) { |
533 |
/* Restore prev state on fail. */ |
534 |
fmd->files = files; |
535 |
fmd->files_count = files_count; |
536 |
return; |
537 |
} |
538 |
/* Notify removed first. */ |
539 |
for (i = 0; i < files_count; i ++) { |
540 |
if (0 != file_info_find_de_fileno(fmd->files, fmd->files_count, &files[i].de, &k) || |
541 |
0 != file_info_find_de_name(fmd->files, fmd->files_count, (~(size_t)0), &files[i].de, &k)) /* Deleted. */ |
542 |
continue; |
543 |
if (-1 != files[i].fd) { |
544 |
close(files[i].fd); |
545 |
} |
546 |
kfnm->cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED, |
547 |
fmd->path, files[i].de.d_name, NULL); |
548 |
} |
549 |
/* Notify. */ |
550 |
for (i = 0; i < fmd->files_count; i ++) { |
551 |
/* Is new file/folder? */ |
552 |
if (0 == file_info_find_de_fileno(files, files_count, &fmd->files[i].de, &k) && |
553 |
0 == file_info_find_de_name(files, files_count, (~(size_t)0), &fmd->files[i].de, &k)) { /* Add new. */ |
554 |
if (0 != fmd->is_local) { /* Add monitor sub files/dirs, ignory errors. */ |
555 |
fmd->files[i].fd = openat(fmd->fd, |
556 |
fmd->files[i].de.d_name, OPEN_FILE_FLAGS); |
557 |
if (-1 != fmd->files[i].fd) { |
558 |
EV_SET(&kev, fmd->files[i].fd, |
559 |
EVFILT_VNODE, |
560 |
(EV_ADD | EV_CLEAR), |
561 |
EVFILT_VNODE_SUB_FLAGS, 0, fmd); |
562 |
kevent(kfnm->fd, &kev, 1, NULL, 0, NULL); |
563 |
} |
564 |
} |
565 |
kfnm->cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CREATED, |
566 |
fmd->path, fmd->files[i].de.d_name, NULL); |
567 |
continue; |
568 |
} |
569 |
/* Is renamed? */ |
570 |
if (0 == IS_DE_NAME_EQ(&files[k].de, &fmd->files[i].de)) { |
571 |
fmd->files[i].fd = files[k].fd; /* Keep file fd. */ |
572 |
kfnm->cb_func(kfnm, fmd, fmd->udata, KF_EVENT_RENAMED, |
573 |
fmd->path, files[k].de.d_name, fmd->files[i].de.d_name); |
574 |
continue; |
575 |
} |
576 |
/* Is modified? */ |
577 |
if (0 != memcmp(&fmd->files[i].sb, &files[k].sb, sizeof(struct stat))) { |
578 |
fmd->files[i].fd = files[k].fd; /* Keep file fd. */ |
579 |
kfnm->cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CHANGED, |
580 |
fmd->path, fmd->files[i].de.d_name, NULL); |
581 |
continue; |
582 |
} |
583 |
/* Not changed. */ |
584 |
} |
585 |
|
586 |
free(files); |
587 |
} |
588 |
|
589 |
static void |
590 |
kqueue_handle_rename(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd) { |
591 |
int up_dir_fd, found = 0; |
592 |
readdir_data_t rdd; |
593 |
struct dirent de; |
594 |
struct stat sb; |
595 |
char old_filename[(MAXNAMLEN + 2)]; |
596 |
size_t old_filename_size; |
597 |
|
598 |
if (NULL == kfnm || NULL == fmd) |
599 |
return; |
600 |
if (0 != fstat(fmd->fd, &sb) || |
601 |
0 == sb.st_nlink) { |
602 |
notify_removed: |
603 |
kqueue_file_mon_data_clean(fmd); |
604 |
kfnm->cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED, |
605 |
fmd->path, "", NULL); |
606 |
return; |
607 |
} |
608 |
/* Save old file name. */ |
609 |
old_filename_size = (fmd->path_size - fmd->name_offset - (size_t)fmd->is_dir); |
610 |
memcpy(old_filename, |
611 |
(fmd->path + fmd->name_offset), |
612 |
old_filename_size); |
613 |
old_filename[old_filename_size] = 0; |
614 |
|
615 |
/* Get parent folder name. */ |
616 |
fmd->path[fmd->name_offset] = 0; |
617 |
/* Try to open. */ |
618 |
up_dir_fd = open(fmd->path, (OPEN_FILE_FLAGS | O_DIRECTORY)); |
619 |
/* Restore '/' after parent folder. */ |
620 |
fmd->path[fmd->name_offset] = '/'; |
621 |
if (-1 == up_dir_fd || |
622 |
0 != fstat(up_dir_fd, &sb) || |
623 |
0 != readdir_data_start(up_dir_fd, fmd->kfnm->tmpbuf, (size_t)sb.st_blksize, &rdd)) { |
624 |
close(up_dir_fd); |
625 |
return; |
626 |
} |
627 |
/* Find new name by inode. */ |
628 |
while (0 == readdir_data_next(&rdd, &de)) { |
629 |
if (de.d_fileno == sb.st_ino) { |
630 |
found ++; |
631 |
break; |
632 |
} |
633 |
} |
634 |
close(up_dir_fd); |
635 |
if (0 == found) |
636 |
goto notify_removed; /* Not found. */ |
637 |
/* Update name. */ |
638 |
if (PATH_MAX <= (fmd->name_offset + de.d_namlen)) |
639 |
return; /* Too long. */ |
640 |
memcpy((fmd->path + fmd->name_offset), de.d_name, de.d_namlen); |
641 |
fmd->path_size = (fmd->name_offset + de.d_namlen); |
642 |
/* Add last '/' for dir. */ |
643 |
fmd->path[fmd->path_size] = '/'; |
644 |
fmd->path_size += (size_t)fmd->is_dir; |
645 |
fmd->path[fmd->path_size] = 0; |
646 |
/* Notify. */ |
647 |
kfnm->cb_func(kfnm, fmd, fmd->udata, KF_EVENT_RENAMED, |
648 |
fmd->path, old_filename, de.d_name); |
649 |
} |
650 |
|
651 |
|
652 |
static void |
653 |
kqueue_fnm_delay_call_process(kqueue_fnm_p kfnm, kq_msg_cb forced_msg_cb) { |
654 |
ssize_t ios; |
655 |
kqueue_fnm_msg_pkt_t msg; |
656 |
|
657 |
for (;;) { |
658 |
ios = read(kfnm->pfd[0], &msg, sizeof(msg)); |
659 |
if (0 >= ios) |
660 |
return; |
661 |
if (sizeof(msg) != ios || |
662 |
KF_MSG_PKT_MAGIC != msg.magic || |
663 |
(((size_t)msg.msg_cb) ^ ((size_t)msg.fmd)) != msg.chk_sum) |
664 |
continue; |
665 |
if (NULL != forced_msg_cb) { |
666 |
forced_msg_cb(msg.fmd); |
667 |
continue; |
668 |
} |
669 |
if (NULL == msg.msg_cb) |
670 |
continue; |
671 |
msg.msg_cb(msg.fmd); |
672 |
} |
673 |
} |
674 |
|
675 |
static int |
676 |
kqueue_fnm_delay_call(kqueue_fnm_p kfnm, kq_msg_cb msg_cb, |
677 |
kqueue_file_mon_data_p fmd) { |
678 |
kqueue_fnm_msg_pkt_t msg; |
679 |
|
680 |
if (NULL == kfnm || NULL == fmd) |
681 |
return (EINVAL); |
682 |
msg.magic = KF_MSG_PKT_MAGIC; |
683 |
msg.msg_cb = msg_cb; |
684 |
msg.fmd = fmd; |
685 |
msg.chk_sum = (((size_t)msg.msg_cb) ^ ((size_t)msg.fmd)); |
686 |
if (sizeof(msg) == write(kfnm->pfd[1], &msg, sizeof(msg))) |
687 |
return (0); |
688 |
return (errno); |
689 |
} |
690 |
|
691 |
|
692 |
void |
693 |
kqueue_fnm_free(kqueue_fnm_p kfnm) { |
694 |
|
695 |
if (NULL == kfnm) |
696 |
return; |
697 |
close(kfnm->fd); |
698 |
pthread_join(kfnm->tid, NULL); |
699 |
/* Free all in delay calls queue. */ |
700 |
kqueue_fnm_delay_call_process(kfnm, kqueue_file_mon_data_free); |
701 |
close(kfnm->pfd[0]); |
702 |
close(kfnm->pfd[1]); |
703 |
free(kfnm->tmpbuf); |
704 |
free(kfnm); |
705 |
} |
706 |
|
707 |
kqueue_fnm_p |
708 |
kqueue_fnm_create(kqueue_file_mon_settings_p s, kfnm_event_handler_cb cb_func) { |
709 |
kqueue_fnm_p kfnm; |
710 |
struct kevent kev; |
711 |
|
712 |
if (NULL == s || NULL == cb_func) |
713 |
return (NULL); |
714 |
kfnm = zalloc(sizeof(kqueue_fnm_t)); |
715 |
if (NULL == kfnm) |
716 |
return (NULL); |
717 |
kfnm->fd = kqueue(); |
718 |
if (-1 == kfnm->fd) |
719 |
goto err_out; |
720 |
if (-1 == pipe2(kfnm->pfd, O_NONBLOCK)) |
721 |
goto err_out; |
722 |
kfnm->cb_func = cb_func; |
723 |
memcpy(&kfnm->s, s, sizeof(kqueue_file_mon_settings_t)); |
724 |
|
725 |
EV_SET(&kev, kfnm->pfd[0], EVFILT_READ, EV_ADD, 0, 0, NULL); |
726 |
if (-1 == kevent(kfnm->fd, &kev, 1, NULL, 0, NULL)) |
727 |
goto err_out; |
728 |
if (0 != pthread_create(&kfnm->tid, NULL, |
729 |
kqueue_fnm_proccess_events_proc, kfnm)) |
730 |
goto err_out; |
731 |
|
732 |
return (kfnm); |
733 |
|
734 |
err_out: |
735 |
kqueue_fnm_free(kfnm); |
736 |
return (NULL); |
737 |
} |
738 |
|
739 |
kqueue_file_mon_data_p |
740 |
kqueue_fnm_add(kqueue_fnm_p kfnm, const char *path, void *udata) { |
741 |
int error; |
742 |
kqueue_file_mon_data_p fmd; |
743 |
|
744 |
if (NULL == kfnm || NULL == path) |
745 |
return (NULL); |
746 |
fmd = kqueue_file_mon_data_alloc(kfnm, path, udata); |
747 |
if (NULL == fmd) |
748 |
return (NULL); |
749 |
/* Shedule delay call to init. */ |
750 |
error = kqueue_fnm_delay_call(kfnm, kqueue_file_mon_data_init, fmd); |
751 |
if (0 != error) { /* Error, do no directly init to avoid freezes. */ |
752 |
kqueue_file_mon_data_free(fmd); |
753 |
return (NULL); |
754 |
} |
755 |
return (fmd); |
756 |
} |
757 |
|
758 |
void |
759 |
kqueue_fnm_del(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd) { |
760 |
int error; |
761 |
|
762 |
if (NULL == kfnm || NULL == fmd) |
763 |
return; |
764 |
/* Cancel notifications. */ |
765 |
close(fmd->fd); |
766 |
fmd->fd = -1; |
767 |
/* Shedule delay call to free. */ |
768 |
error = kqueue_fnm_delay_call(kfnm, kqueue_file_mon_data_free, fmd); |
769 |
if (0 == error) |
770 |
return; |
771 |
/* Error, free directly. */ |
772 |
kqueue_file_mon_data_free(fmd); |
773 |
} |
774 |
|
775 |
|
776 |
static void |
777 |
kqueue_fnm_proccess_event(kqueue_fnm_p kfnm, struct kevent *kev) { |
778 |
kqueue_file_mon_data_p fmd; |
779 |
file_info_p fi; |
780 |
size_t i; |
781 |
|
782 |
if (NULL == kfnm || NULL == kev) |
783 |
return; |
784 |
|
785 |
/* Handle delay calls. */ |
786 |
if (kev->ident == (uintptr_t)kfnm->pfd[0] && |
787 |
kev->filter == EVFILT_READ) { |
788 |
kqueue_fnm_delay_call_process(kfnm, NULL); |
789 |
return; |
790 |
} |
791 |
|
792 |
if (0 == kev->udata) |
793 |
return; /* No associated data, skip. */ |
794 |
fmd = (kqueue_file_mon_data_p)kev->udata; |
795 |
|
796 |
/* FS notifications. */ |
797 |
if (EVFILT_VNODE != kev->filter) |
798 |
return; /* Unknown event, skip. */ |
799 |
/* Subdir/file */ |
800 |
if ((int)kev->ident != fmd->fd) { |
801 |
fi = NULL; |
802 |
for (i = 0; i < fmd->files_count; i ++) { |
803 |
if ((int)kev->ident != fmd->files[i].fd) |
804 |
continue; |
805 |
fi = &fmd->files[i]; |
806 |
break; |
807 |
} |
808 |
if (NULL != fi) { |
809 |
kfnm->cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CHANGED, |
810 |
fmd->path, fi->de.d_name, NULL); |
811 |
return; |
812 |
} |
813 |
/* fd not found, rescan dir. */ |
814 |
kev->fflags = NOTE_WRITE; |
815 |
} |
816 |
/* Monitored object. */ |
817 |
/* All flags from EVFILT_VNODE_FLAGS_ALL must be handled here. */ |
818 |
if (EV_ERROR & kev->flags) { |
819 |
kev->fflags |= NOTE_REVOKE; /* Treat error as unmount. */ |
820 |
} |
821 |
if (NOTE_RENAME & kev->fflags) { |
822 |
kqueue_handle_rename(kfnm, fmd); |
823 |
} |
824 |
if ((NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_LINK | NOTE_CLOSE_WRITE) & kev->fflags) { |
825 |
kqueue_handle_changes(kfnm, fmd); |
826 |
} |
827 |
if ((NOTE_DELETE | NOTE_REVOKE) & kev->fflags) { |
828 |
kqueue_file_mon_data_clean(fmd); |
829 |
kfnm->cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED, |
830 |
fmd->path, "", NULL); |
831 |
} |
832 |
} |
833 |
|
834 |
void * |
835 |
kqueue_fnm_proccess_events_proc(void *data) { |
836 |
struct kevent kev; |
837 |
kqueue_fnm_p kfnm = data; |
838 |
|
839 |
if (NULL == kfnm) |
840 |
return (NULL); |
841 |
/* Get and proccess events, no wait. */ |
842 |
while (0 < kevent(kfnm->fd, NULL, 0, &kev, 1, NULL)) { |
843 |
kqueue_fnm_proccess_event(kfnm, &kev); |
844 |
} |
845 |
|
846 |
return (NULL); |
847 |
} |