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