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/fcntl.h> /* open, fcntl */ |
35 |
|
36 |
#include <inttypes.h> |
37 |
#include <stdlib.h> /* malloc, exit */ |
38 |
#include <unistd.h> /* close, write, sysconf */ |
39 |
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */ |
40 |
#include <dirent.h> /* opendir, readdir */ |
41 |
#include <errno.h> |
42 |
|
43 |
#include "kqueue_fnm.h" |
44 |
|
45 |
|
46 |
/* Preallocate items count. */ |
47 |
#ifndef FILES_ALLOC_BLK_SIZE |
48 |
# define FILES_ALLOC_BLK_SIZE 32 |
49 |
#endif |
50 |
|
51 |
typedef struct file_info_s { /* Directory file. */ |
52 |
struct dirent de; /* d_reclen used for action. */ |
53 |
struct stat sb; |
54 |
} file_info_t, *file_info_p; |
55 |
|
56 |
|
57 |
typedef struct readdir_data_s { |
58 |
int fd; |
59 |
uint8_t *buf; |
60 |
size_t buf_size; |
61 |
size_t buf_used; |
62 |
size_t buf_pos; |
63 |
} readdir_data_t, *readdir_data_p; |
64 |
|
65 |
|
66 |
typedef struct kqueue_file_mon_data_s { |
67 |
int fd; /* fd for notify kqueue(). */ |
68 |
int is_dir; |
69 |
char path[(PATH_MAX + 2)]; |
70 |
size_t path_size; |
71 |
size_t name_offset; /* Parent path size. */ |
72 |
void *udata; |
73 |
kqueue_fnm_p kfnm; |
74 |
/* For dir. */ |
75 |
file_info_p files; |
76 |
volatile size_t files_count; |
77 |
size_t files_allocated; |
78 |
} kqueue_file_mon_data_t; |
79 |
|
80 |
|
81 |
typedef struct kqueue_file_nonify_monitor_s { |
82 |
int fd; /* kqueue() fd. */ |
83 |
int pfd[2]; /* pipe queue specific. */ |
84 |
uint8_t *tmpbuf; |
85 |
size_t tmpbuf_size; |
86 |
} kqueue_fnm_t; |
87 |
|
88 |
|
89 |
typedef void (*kq_msg_cb)(kqueue_file_mon_data_p fmd); |
90 |
|
91 |
typedef struct kqueue_file_mon_msg_pkt_s { |
92 |
size_t magic; |
93 |
kq_msg_cb msg_cb; |
94 |
kqueue_file_mon_data_p fmd; |
95 |
size_t chk_sum; |
96 |
} kqueue_fnm_msg_pkt_t, *kqueue_fnm_msg_pkt_p; |
97 |
|
98 |
#define KF_MSG_PKT_MAGIC 0xffddaa00 |
99 |
|
100 |
|
101 |
#ifndef O_NOATIME |
102 |
# define O_NOATIME 0 |
103 |
#endif |
104 |
#ifndef O_EVTONLY |
105 |
# define O_EVTONLY O_RDONLY |
106 |
#endif |
107 |
#define OPEN_FILE_FLAGS (O_EVTONLY | O_NONBLOCK | O_NOFOLLOW | O_NOATIME | O_CLOEXEC) |
108 |
|
109 |
#define EVFILT_VNODE_FLAGS_ALL (NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE) |
110 |
|
111 |
|
112 |
#ifndef _GENERIC_DIRSIZ |
113 |
# define _GENERIC_DIRSIZ(__de) MIN((__de)->d_reclen, sizeof(struct dirent)) |
114 |
#endif |
115 |
|
116 |
#define IS_NAME_DOTS(__name) ('.' == (__name)[0] && \ |
117 |
('\0' == (__name)[1] || \ |
118 |
('.' == (__name)[1] && '\0' == (__name)[2]))) |
119 |
#define IS_DE_NAME_EQ(__de1, __de2) (0 == mem_cmpn((__de1)->d_name, \ |
120 |
(__de1)->d_namlen, \ |
121 |
(__de2)->d_name, \ |
122 |
(__de2)->d_namlen)) |
123 |
#define zalloc(__size) calloc(1, (__size)) |
124 |
|
125 |
|
126 |
static inline int |
127 |
mem_cmpn(const void *buf1, const size_t buf1_size, |
128 |
const void *buf2, const size_t buf2_size) { |
129 |
|
130 |
if (buf1_size != buf2_size) |
131 |
return (((buf1_size > buf2_size) ? 127 : -127)); |
132 |
if (0 == buf1_size || buf1 == buf2) |
133 |
return (0); |
134 |
if (NULL == buf1) |
135 |
return (-127); |
136 |
if (NULL == buf2) |
137 |
return (127); |
138 |
return (memcmp(buf1, buf2, buf1_size)); |
139 |
} |
140 |
|
141 |
static inline int |
142 |
realloc_items(void **items, const size_t item_size, |
143 |
size_t *allocated, const size_t alloc_blk_cnt, const size_t count) { |
144 |
size_t allocated_prev, allocated_new; |
145 |
uint8_t *items_new; |
146 |
|
147 |
if (NULL == items || 0 == item_size || NULL == allocated || |
148 |
0 == alloc_blk_cnt) |
149 |
return (EINVAL); |
150 |
allocated_prev = (*allocated); |
151 |
if (NULL != (*items) && |
152 |
allocated_prev > count && |
153 |
allocated_prev <= (count + alloc_blk_cnt)) |
154 |
return (0); |
155 |
allocated_new = (((count / alloc_blk_cnt) + 1) * alloc_blk_cnt); |
156 |
#if defined(__FreeBSD_version) && __FreeBSD_version >= 1100000 |
157 |
items_new = (uint8_t*)reallocarray((*items), item_size, allocated_new); |
158 |
#else /* For old BSD systems. */ |
159 |
items_new = (uint8_t*)realloc((*items), (item_size * allocated_new)); |
160 |
#endif |
161 |
if (NULL == items_new) /* Realloc fail! */ |
162 |
return (ENOMEM); |
163 |
|
164 |
if (allocated_new > allocated_prev) { /* Init new mem. */ |
165 |
bzero((items_new + (allocated_prev * item_size)), |
166 |
((allocated_new - allocated_prev) * item_size)); |
167 |
} |
168 |
(*items) = items_new; |
169 |
(*allocated) = allocated_new; |
170 |
|
171 |
return (0); |
172 |
} |
173 |
|
174 |
|
175 |
static int |
176 |
readdir_data_start(int fd, uint8_t *buf, size_t buf_size, readdir_data_p rdd) { |
177 |
|
178 |
if (-1 == fd || NULL == buf || 0 == buf_size || NULL == rdd) |
179 |
return (EINVAL); |
180 |
if (-1 == lseek(fd, 0, SEEK_SET)) |
181 |
return (errno); |
182 |
bzero(rdd, sizeof(readdir_data_t)); |
183 |
rdd->fd = fd; |
184 |
rdd->buf = buf; |
185 |
rdd->buf_size = buf_size; |
186 |
|
187 |
return (0); |
188 |
} |
189 |
|
190 |
static int |
191 |
readdir_data_next(readdir_data_p rdd, struct dirent *de) { |
192 |
int ios; |
193 |
uint8_t *ptr; |
194 |
|
195 |
if (NULL == rdd || NULL == de) |
196 |
return (EINVAL); |
197 |
|
198 |
retry: |
199 |
if (rdd->buf_used <= rdd->buf_pos) { |
200 |
ios = getdents(rdd->fd, (char*)rdd->buf, rdd->buf_size); |
201 |
if (-1 == ios) |
202 |
return (errno); |
203 |
rdd->buf_used = (size_t)ios; |
204 |
rdd->buf_pos = 0; |
205 |
if (0 == ios) |
206 |
return (ESPIPE); /* EOF. */ |
207 |
} |
208 |
/* Keep data aligned. */ |
209 |
ptr = (rdd->buf + rdd->buf_pos); |
210 |
memcpy(de, ptr, (sizeof(struct dirent) - sizeof(de->d_name))); |
211 |
if (0 == de->d_reclen) |
212 |
return (ESPIPE); /* EOF. */ |
213 |
rdd->buf_pos += de->d_reclen; |
214 |
#ifdef DT_WHT |
215 |
if (DT_WHT == de->d_type) |
216 |
goto retry; |
217 |
#endif |
218 |
memcpy(de, ptr, _GENERIC_DIRSIZ(de)); |
219 |
if (IS_NAME_DOTS(de->d_name)) |
220 |
goto retry; |
221 |
return (0); |
222 |
} |
223 |
|
224 |
static void |
225 |
kqueue_file_mon_data_clean(kqueue_file_mon_data_p fmd) { |
226 |
|
227 |
if (NULL == fmd) |
228 |
return; |
229 |
close(fmd->fd); |
230 |
fmd->fd = -1; |
231 |
fmd->files_count = 0; |
232 |
free(fmd->files); |
233 |
fmd->files = NULL; |
234 |
} |
235 |
|
236 |
static void |
237 |
kqueue_file_mon_data_free(kqueue_file_mon_data_p fmd) { |
238 |
|
239 |
if (NULL == fmd) |
240 |
return; |
241 |
close(fmd->fd); |
242 |
free(fmd->files); |
243 |
free(fmd); |
244 |
} |
245 |
|
246 |
static kqueue_file_mon_data_p |
247 |
kqueue_file_mon_data_alloc(kqueue_fnm_p kfnm, const char *path, void *udata) { |
248 |
kqueue_file_mon_data_p fmd; |
249 |
|
250 |
if (NULL == kfnm || NULL == path) |
251 |
return (NULL); |
252 |
fmd = zalloc(sizeof(kqueue_file_mon_data_t)); |
253 |
if (NULL == fmd) |
254 |
return (NULL); |
255 |
/* Remember args. */ |
256 |
fmd->path_size = strlcpy(fmd->path, path, PATH_MAX); |
257 |
fmd->name_offset = fmd->path_size; |
258 |
fmd->udata = udata; |
259 |
fmd->kfnm = kfnm; |
260 |
|
261 |
return (fmd); |
262 |
} |
263 |
|
264 |
static int |
265 |
kqueue_file_mon_data_find_de_fileno(kqueue_file_mon_data_p fmd, |
266 |
struct dirent *de, size_t *idx) { |
267 |
size_t i; |
268 |
|
269 |
if (NULL == fmd || NULL == de || NULL == idx) |
270 |
return (0); |
271 |
for (i = 0; i < fmd->files_count; i ++) { |
272 |
if (de->d_fileno != fmd->files[i].de.d_fileno || |
273 |
de->d_type != fmd->files[i].de.d_type) |
274 |
continue; |
275 |
(*idx) = i; |
276 |
return (1); |
277 |
} |
278 |
(*idx) = fmd->files_count; |
279 |
return (0); |
280 |
} |
281 |
|
282 |
|
283 |
static void |
284 |
kqueue_file_mon_data_init(kqueue_file_mon_data_p fmd) { |
285 |
struct dirent *de; |
286 |
struct stat sb; |
287 |
struct kevent kev; |
288 |
struct timespec ke_timeout; |
289 |
uint8_t *tmpbuf; |
290 |
readdir_data_t rdd; |
291 |
|
292 |
if (NULL == fmd) |
293 |
return; |
294 |
fmd->fd = open(fmd->path, OPEN_FILE_FLAGS); |
295 |
if (-1 == fmd->fd) |
296 |
return; |
297 |
if (0 != fstat(fmd->fd, &sb)) |
298 |
goto err_out; |
299 |
/* Get parent folder name. */ |
300 |
if (S_ISDIR(sb.st_mode)) { |
301 |
fmd->is_dir = 1; |
302 |
/* Be sure that folder contain trailing '/'. */ |
303 |
if ('/' != fmd->path[(fmd->path_size - 1)]) { |
304 |
fmd->path[fmd->path_size] = '/'; |
305 |
fmd->path_size ++; |
306 |
fmd->path[fmd->path_size] = 0; |
307 |
} |
308 |
/* Skip last '/' for parent dir search. */ |
309 |
fmd->name_offset = (fmd->path_size - 1); |
310 |
} |
311 |
/* Find parent dir path size. */ |
312 |
while (0 < fmd->name_offset && '/' != fmd->path[(fmd->name_offset - 1)]) { |
313 |
fmd->name_offset --; |
314 |
} |
315 |
|
316 |
/* Dir special processing. */ |
317 |
if (0 != fmd->is_dir) { |
318 |
/* Read and remember dir content. */ |
319 |
/* Get temp buf. */ |
320 |
/* Do this once per dir handle. */ |
321 |
if ((size_t)sb.st_blksize > fmd->kfnm->tmpbuf_size) { |
322 |
tmpbuf = realloc(fmd->kfnm->tmpbuf, sb.st_blksize); |
323 |
if (NULL == tmpbuf) |
324 |
goto err_out; |
325 |
fmd->kfnm->tmpbuf = tmpbuf; |
326 |
fmd->kfnm->tmpbuf_size = (size_t)sb.st_blksize; |
327 |
} |
328 |
if (0 != readdir_data_start(fmd->fd, fmd->kfnm->tmpbuf, sb.st_blksize, &rdd)) |
329 |
goto err_out; |
330 |
|
331 |
for (;;) { |
332 |
if (0 != realloc_items((void**)&fmd->files, |
333 |
sizeof(file_info_t), &fmd->files_allocated, |
334 |
FILES_ALLOC_BLK_SIZE, fmd->files_count)) |
335 |
goto err_out; |
336 |
de = &fmd->files[fmd->files_count].de; /* Use short name. */ |
337 |
/* Get file name from folder. */ |
338 |
if (0 != readdir_data_next(&rdd, de)) |
339 |
break; |
340 |
/* Get file attrs. */ |
341 |
if (0 != fstatat(fmd->fd, de->d_name, |
342 |
&fmd->files[fmd->files_count].sb, |
343 |
AT_SYMLINK_NOFOLLOW)) { |
344 |
bzero(&fmd->files[fmd->files_count].sb, |
345 |
sizeof(struct stat)); |
346 |
} |
347 |
fmd->files_count ++; |
348 |
} |
349 |
} |
350 |
/* Add to kqueue. */ |
351 |
bzero(&kev, sizeof(kev)); |
352 |
kev.ident = (uintptr_t)fmd->fd; |
353 |
kev.filter = EVFILT_VNODE; |
354 |
kev.flags = (EV_ADD | EV_CLEAR); |
355 |
kev.fflags = EVFILT_VNODE_FLAGS_ALL; |
356 |
kev.udata = (void*)fmd; |
357 |
|
358 |
bzero(&ke_timeout, sizeof(ke_timeout)); |
359 |
|
360 |
if (-1 == kevent(fmd->kfnm->fd, &kev, 1, NULL, 0, &ke_timeout)) |
361 |
goto err_out; |
362 |
return; /* OK. */ |
363 |
|
364 |
err_out: |
365 |
kqueue_file_mon_data_clean(fmd); |
366 |
} |
367 |
|
368 |
|
369 |
static void |
370 |
kqueue_handle_changes(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd, |
371 |
kfnm_event_handler_cb cb_func) { |
372 |
file_info_t fi; |
373 |
size_t i, j, k; |
374 |
readdir_data_t rdd; |
375 |
|
376 |
if (NULL == kfnm || NULL == fmd || NULL == cb_func) |
377 |
return; |
378 |
if (0 != fstat(fmd->fd, &fi.sb) || |
379 |
0 == fi.sb.st_nlink) { |
380 |
kqueue_file_mon_data_clean(fmd); |
381 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED, |
382 |
fmd->path, "", NULL); |
383 |
return; |
384 |
} |
385 |
if (0 == fmd->is_dir) { |
386 |
fmd->path[fmd->name_offset] = 0; |
387 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CHANGED, fmd->path, |
388 |
(fmd->path + fmd->name_offset), NULL); |
389 |
fmd->path[fmd->name_offset] = '/'; |
390 |
return; |
391 |
} |
392 |
/* Dir processing. */ |
393 |
if (0 != readdir_data_start(fmd->fd, fmd->kfnm->tmpbuf, fi.sb.st_blksize, &rdd)) |
394 |
return; |
395 |
/* Mark all as removed. */ |
396 |
for (i = 0; i < fmd->files_count; i ++) { |
397 |
fmd->files[i].de.d_reclen = KF_EVENT_DELETED; |
398 |
} |
399 |
/* Update dir. */ |
400 |
while (0 == readdir_data_next(&rdd, &fi.de)) { |
401 |
/* Get file stat. */ |
402 |
if (0 != fstatat(fmd->fd, fi.de.d_name, &fi.sb, AT_SYMLINK_NOFOLLOW)) { |
403 |
bzero(&fi.sb, sizeof(struct stat)); /* Fail, set to zero. */ |
404 |
} |
405 |
/* Is new file/folder? */ |
406 |
if (0 == kqueue_file_mon_data_find_de_fileno(fmd, &fi.de, &i)) { /* Add new. */ |
407 |
if (0 != realloc_items((void**)&fmd->files, |
408 |
sizeof(file_info_t), &fmd->files_allocated, |
409 |
FILES_ALLOC_BLK_SIZE, fmd->files_count)) |
410 |
goto err_out; |
411 |
memcpy(&fmd->files[fmd->files_count], &fi, sizeof(file_info_t)); |
412 |
fmd->files[fmd->files_count].de.d_reclen = KF_EVENT_CREATED; |
413 |
fmd->files_count ++; |
414 |
/* Notify. */ |
415 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CREATED, |
416 |
fmd->path, fi.de.d_name, NULL); |
417 |
continue; |
418 |
} |
419 |
/* Is renamed? */ |
420 |
if (0 == IS_DE_NAME_EQ(&fi.de, &fmd->files[i].de)) { |
421 |
/* Notify. */ |
422 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_RENAMED, |
423 |
fmd->path, fmd->files[i].de.d_name, fi.de.d_name); |
424 |
/* Update. */ |
425 |
memcpy(&fmd->files[i], &fi, sizeof(file_info_t)); |
426 |
fmd->files[i].de.d_reclen = KF_EVENT_RENAMED; |
427 |
continue; |
428 |
} |
429 |
/* Is modified? */ |
430 |
if (0 != memcmp(&fmd->files[i].sb, &fi.sb, sizeof(struct stat))) { |
431 |
memcpy(&fmd->files[i].sb, &fi.sb, sizeof(struct stat)); /* Update stat. */ |
432 |
fmd->files[i].de.d_reclen = KF_EVENT_CHANGED; |
433 |
/* Notify. */ |
434 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CHANGED, |
435 |
fmd->path, fi.de.d_name, NULL); |
436 |
continue; |
437 |
} |
438 |
/* Not changed. */ |
439 |
fmd->files[i].de.d_reclen = KF_EVENT_NOT_CHANGED; |
440 |
} |
441 |
/* Remove marked as removed. */ |
442 |
for (i = 0; i < fmd->files_count; i ++) { |
443 |
if (KF_EVENT_DELETED != fmd->files[i].de.d_reclen) |
444 |
continue; |
445 |
/* Look for next non removed item + notify. */ |
446 |
for (j = (i + 1); j < fmd->files_count; j ++) { |
447 |
if (KF_EVENT_DELETED != fmd->files[j].de.d_reclen) |
448 |
break; |
449 |
} |
450 |
/* Notify. */ |
451 |
for (k = i; k < j; k ++) { |
452 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED, |
453 |
fmd->path, fmd->files[k].de.d_name, NULL); |
454 |
} |
455 |
memmove(&fmd->files[i], &fmd->files[j], |
456 |
(sizeof(file_info_t) * (fmd->files_count - j))); |
457 |
fmd->files_count -= (j - i); |
458 |
} |
459 |
|
460 |
err_out: |
461 |
return; |
462 |
} |
463 |
|
464 |
static void |
465 |
kqueue_handle_rename(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd, |
466 |
kfnm_event_handler_cb cb_func) { |
467 |
int up_dir_fd, found = 0; |
468 |
readdir_data_t rdd; |
469 |
struct dirent de; |
470 |
struct stat sb; |
471 |
char old_filename[(MAXNAMLEN + 2)]; |
472 |
size_t old_filename_size; |
473 |
|
474 |
if (NULL == kfnm || NULL == fmd || NULL == cb_func) |
475 |
return; |
476 |
if (0 != fstat(fmd->fd, &sb) || |
477 |
0 == sb.st_nlink) { |
478 |
notify_removed: |
479 |
kqueue_file_mon_data_clean(fmd); |
480 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED, |
481 |
fmd->path, "", NULL); |
482 |
return; |
483 |
} |
484 |
/* Save old file name. */ |
485 |
old_filename_size = (fmd->path_size - fmd->name_offset - fmd->is_dir); |
486 |
memcpy(old_filename, |
487 |
(fmd->path + fmd->name_offset), |
488 |
old_filename_size); |
489 |
old_filename[old_filename_size] = 0; |
490 |
|
491 |
/* Get parent folder name. */ |
492 |
fmd->path[fmd->name_offset] = 0; |
493 |
/* Try to open. */ |
494 |
up_dir_fd = open(fmd->path, (OPEN_FILE_FLAGS | O_DIRECTORY)); |
495 |
/* Restore '/' after parent folder. */ |
496 |
fmd->path[fmd->name_offset] = '/'; |
497 |
if (-1 == up_dir_fd || |
498 |
0 != fstat(up_dir_fd, &sb) || |
499 |
0 != readdir_data_start(up_dir_fd, fmd->kfnm->tmpbuf, sb.st_blksize, &rdd)) { |
500 |
close(up_dir_fd); |
501 |
return; |
502 |
} |
503 |
/* Find new name by inode. */ |
504 |
while (0 == readdir_data_next(&rdd, &de)) { |
505 |
if (de.d_fileno == sb.st_ino) { |
506 |
found ++; |
507 |
break; |
508 |
} |
509 |
} |
510 |
close(up_dir_fd); |
511 |
if (0 == found) |
512 |
goto notify_removed; /* Not found. */ |
513 |
/* Update name. */ |
514 |
if (PATH_MAX <= (fmd->name_offset + de.d_namlen)) |
515 |
return; /* Too long. */ |
516 |
memcpy((fmd->path + fmd->name_offset), de.d_name, de.d_namlen); |
517 |
fmd->path_size = (fmd->name_offset + de.d_namlen); |
518 |
/* Add last '/' for dir. */ |
519 |
fmd->path[fmd->path_size] = '/'; |
520 |
fmd->path_size += fmd->is_dir; |
521 |
fmd->path[fmd->path_size] = 0; |
522 |
/* Notify. */ |
523 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_RENAMED, |
524 |
fmd->path, old_filename, de.d_name); |
525 |
} |
526 |
|
527 |
|
528 |
static void |
529 |
kqueue_fnm_delay_call_process(kqueue_fnm_p kfnm, kq_msg_cb forced_msg_cb) { |
530 |
ssize_t ios; |
531 |
kqueue_fnm_msg_pkt_t msg; |
532 |
|
533 |
for (;;) { |
534 |
ios = read(kfnm->pfd[0], &msg, sizeof(msg)); |
535 |
if (0 >= ios) |
536 |
return; |
537 |
if (sizeof(msg) != ios || |
538 |
KF_MSG_PKT_MAGIC != msg.magic || |
539 |
(((size_t)msg.msg_cb) ^ ((size_t)msg.fmd)) != msg.chk_sum) |
540 |
continue; |
541 |
if (NULL != forced_msg_cb) { |
542 |
forced_msg_cb(msg.fmd); |
543 |
continue; |
544 |
} |
545 |
if (NULL == msg.msg_cb) |
546 |
continue; |
547 |
msg.msg_cb(msg.fmd); |
548 |
} |
549 |
} |
550 |
|
551 |
static int |
552 |
kqueue_fnm_delay_call(kqueue_fnm_p kfnm, kq_msg_cb msg_cb, |
553 |
kqueue_file_mon_data_p fmd) { |
554 |
kqueue_fnm_msg_pkt_t msg; |
555 |
|
556 |
if (NULL == kfnm || NULL == fmd) |
557 |
return (EINVAL); |
558 |
msg.magic = KF_MSG_PKT_MAGIC; |
559 |
msg.msg_cb = msg_cb; |
560 |
msg.fmd = fmd; |
561 |
msg.chk_sum = (((size_t)msg.msg_cb) ^ ((size_t)msg.fmd)); |
562 |
if (sizeof(msg) == write(kfnm->pfd[1], &msg, sizeof(msg))) |
563 |
return (0); |
564 |
return (errno); |
565 |
} |
566 |
|
567 |
|
568 |
void |
569 |
kqueue_fnm_free(kqueue_fnm_p kfnm) { |
570 |
|
571 |
if (NULL == kfnm) |
572 |
return; |
573 |
close(kfnm->fd); |
574 |
/* Free all in delay calls queue. */ |
575 |
kqueue_fnm_delay_call_process(kfnm, kqueue_file_mon_data_free); |
576 |
close(kfnm->pfd[0]); |
577 |
close(kfnm->pfd[1]); |
578 |
free(kfnm->tmpbuf); |
579 |
free(kfnm); |
580 |
} |
581 |
|
582 |
kqueue_fnm_p |
583 |
kqueue_fnm_create(void) { |
584 |
kqueue_fnm_p kfnm; |
585 |
struct kevent kev; |
586 |
struct timespec ke_timeout; |
587 |
|
588 |
kfnm = zalloc(sizeof(kqueue_fnm_t)); |
589 |
if (NULL == kfnm) |
590 |
return (NULL); |
591 |
kfnm->fd = kqueue(); |
592 |
if (-1 == kfnm->fd) |
593 |
goto err_out; |
594 |
if (-1 == pipe2(kfnm->pfd, O_NONBLOCK)) |
595 |
goto err_out; |
596 |
|
597 |
bzero(&kev, sizeof(kev)); |
598 |
kev.ident = kfnm->pfd[0]; |
599 |
kev.filter = EVFILT_READ; |
600 |
kev.flags = (EV_ADD | EV_ENABLE); |
601 |
|
602 |
bzero(&ke_timeout, sizeof(ke_timeout)); |
603 |
|
604 |
if (-1 == kevent(kfnm->fd, &kev, 1, NULL, 0, &ke_timeout)) |
605 |
goto err_out; |
606 |
return (kfnm); |
607 |
|
608 |
err_out: |
609 |
kqueue_fnm_free(kfnm); |
610 |
return (NULL); |
611 |
} |
612 |
|
613 |
kqueue_file_mon_data_p |
614 |
kqueue_fnm_add(kqueue_fnm_p kfnm, const char *path, void *udata) { |
615 |
int error; |
616 |
kqueue_file_mon_data_p fmd; |
617 |
|
618 |
if (NULL == kfnm || NULL == path) |
619 |
return (NULL); |
620 |
fmd = kqueue_file_mon_data_alloc(kfnm, path, udata); |
621 |
if (NULL == fmd) |
622 |
return (NULL); |
623 |
/* Shedule delay call to init. */ |
624 |
error = kqueue_fnm_delay_call(kfnm, kqueue_file_mon_data_init, fmd); |
625 |
if (0 != error) { /* Error, do no directly init to avoid freezes. */ |
626 |
kqueue_file_mon_data_free(fmd); |
627 |
return (NULL); |
628 |
} |
629 |
return (fmd); |
630 |
} |
631 |
|
632 |
void |
633 |
kqueue_fnm_del(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd) { |
634 |
int error; |
635 |
|
636 |
if (NULL == kfnm || NULL == fmd) |
637 |
return; |
638 |
/* Cancel notifications. */ |
639 |
close(fmd->fd); |
640 |
fmd->fd = -1; |
641 |
/* Shedule delay call to free. */ |
642 |
error = kqueue_fnm_delay_call(kfnm, kqueue_file_mon_data_free, fmd); |
643 |
if (0 == error) |
644 |
return; |
645 |
/* Error, free directly. */ |
646 |
kqueue_file_mon_data_free(fmd); |
647 |
} |
648 |
|
649 |
|
650 |
int |
651 |
kqueue_fnm_get_ev_recv_fd(kqueue_fnm_p kfnm) { |
652 |
|
653 |
if (NULL == kfnm) |
654 |
return (-1); |
655 |
return (kfnm->fd); |
656 |
} |
657 |
|
658 |
void |
659 |
kqueue_fnm_proccess_events(kqueue_fnm_p kfnm, kfnm_event_handler_cb cb_func) { |
660 |
struct kevent kev; |
661 |
struct timespec ke_timeout; |
662 |
kqueue_file_mon_data_p fmd; |
663 |
|
664 |
if (NULL == kfnm || NULL == cb_func) |
665 |
return; |
666 |
/* Get and proccess events. */ |
667 |
bzero(&ke_timeout, sizeof(ke_timeout)); |
668 |
while (0 < kevent(kfnm->fd, NULL, 0, &kev, 1, &ke_timeout)) { |
669 |
if (kev.ident == (uintptr_t)kfnm->pfd[0] && |
670 |
kev.filter == EVFILT_READ) { /* Handle delay calls. */ |
671 |
kqueue_fnm_delay_call_process(kfnm, NULL); |
672 |
continue; |
673 |
} |
674 |
if (EVFILT_VNODE != kev.filter || |
675 |
0 == kev.udata) |
676 |
continue; /* Unknown event or no associated data, skip. */ |
677 |
fmd = (kqueue_file_mon_data_p)kev.udata; |
678 |
|
679 |
if (EV_ERROR & kev.flags) { |
680 |
kev.flags |= NOTE_REVOKE; /* Treat error as unmount. */ |
681 |
} |
682 |
if (NOTE_RENAME & kev.fflags) { |
683 |
kqueue_handle_rename(kfnm, fmd, cb_func); |
684 |
} |
685 |
if ((NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB) & kev.fflags) { |
686 |
kqueue_handle_changes(kfnm, fmd, cb_func); |
687 |
} |
688 |
if ((NOTE_DELETE | NOTE_REVOKE) & kev.fflags) { |
689 |
kqueue_file_mon_data_clean(fmd); |
690 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED, |
691 |
fmd->path, "", NULL); |
692 |
} |
693 |
} |
694 |
} |