View | Details | Raw Unified | Return to bug 214338 | Differences between
and this patch

Collapse All | Expand All

(-)devel/glib20/Makefile (+17 lines)
Lines 110-115 Link Here
110
	@${REINPLACE_CMD} -e 's|inotify_support=yes|inotify_support=no| ; \
110
	@${REINPLACE_CMD} -e 's|inotify_support=yes|inotify_support=no| ; \
111
		s|-Werror|| ; \
111
		s|-Werror|| ; \
112
		s|#define HAVE_SYS_INOTIFY_H 1||' ${WRKSRC}/configure
112
		s|#define HAVE_SYS_INOTIFY_H 1||' ${WRKSRC}/configure
113
	@${CP} ${FILESDIR}/kqueue_fnm.c.in ${WRKSRC}/gio/kqueue/kqueue_fnm.c
114
	@${CP} ${FILESDIR}/kqueue_fnm.h.in ${WRKSRC}/gio/kqueue/kqueue_fnm.h
115
	@${RM} ${WRKSRC}/gio/kqueue/dep-list.c
116
	@${RM} ${WRKSRC}/gio/kqueue/dep-list.h
117
	@${RM} ${WRKSRC}/gio/kqueue/gkqueuefilemonitor.h
118
	@${RM} ${WRKSRC}/gio/kqueue/kqueue-exclusions.c
119
	@${RM} ${WRKSRC}/gio/kqueue/kqueue-exclusions.h
120
	@${RM} ${WRKSRC}/gio/kqueue/kqueue-helper.c
121
	@${RM} ${WRKSRC}/gio/kqueue/kqueue-helper.h
122
	@${RM} ${WRKSRC}/gio/kqueue/kqueue-missing.c
123
	@${RM} ${WRKSRC}/gio/kqueue/kqueue-missing.h
124
	@${RM} ${WRKSRC}/gio/kqueue/kqueue-sub.c
125
	@${RM} ${WRKSRC}/gio/kqueue/kqueue-sub.h
126
	@${RM} ${WRKSRC}/gio/kqueue/kqueue-thread.c
127
	@${RM} ${WRKSRC}/gio/kqueue/kqueue-thread.h
128
	@${RM} ${WRKSRC}/gio/kqueue/kqueue-utils.c
129
	@${RM} ${WRKSRC}/gio/kqueue/kqueue-utils.h
113
130
114
post-install:
131
post-install:
115
	@${MKDIR} ${STAGEDIR}${PREFIX}/share/GConf/gsettings
132
	@${MKDIR} ${STAGEDIR}${PREFIX}/share/GConf/gsettings
(-)devel/glib20/files/kqueue_fnm.c.in (+710 lines)
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	8
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
119
#define IS_NAME_DOTS(__name)	('.' == (__name)[0] &&			\
120
				 ('\0' == (__name)[1] || 		\
121
				  ('.' == (__name)[1] && '\0' == (__name)[2])))
122
#define IS_DE_NAME_EQ(__de1, __de2)  (0 == mem_cmpn((__de1)->d_name,	\
123
						    (__de1)->d_namlen,	\
124
						    (__de2)->d_name,	\
125
						    (__de2)->d_namlen))
126
#define zalloc(__size)		calloc(1, (__size))
127
128
129
static inline int
130
mem_cmpn(const void *buf1, const size_t buf1_size,
131
    const void *buf2, const size_t buf2_size) {
132
133
	if (buf1_size != buf2_size)
134
		return (((buf1_size > buf2_size) ? 127 : -127));
135
	if (0 == buf1_size || buf1 == buf2)
136
		return (0);
137
	if (NULL == buf1)
138
		return (-127);
139
	if (NULL == buf2)
140
		return (127);
141
	return (memcmp(buf1, buf2, buf1_size));
142
}
143
144
/* For old BSD systems. */
145
#if !defined(__FreeBSD_version) || __FreeBSD_version < 1100000
146
static inline void *
147
reallocarray(void *buf, const size_t nmemb, const size_t size) {
148
	size_t nmemb_size;
149
150
	nmemb_size = (nmemb * size);
151
	if (0 == nmemb_size) {
152
		if (0 != nmemb &&
153
		    0 != size) { /* Overflow. */
154
			errno = ENOMEM;
155
			return (NULL);
156
		}
157
		nmemb_size ++;
158
	} else if (((nmemb | size) & (SIZE_T_MAX << (sizeof(size_t) * 4))) &&
159
	    (nmemb_size / size) != nmemb) { /* size_t overflow. */
160
		errno = ENOMEM;
161
		return (NULL);
162
	}
163
	return (realloc(buf, nmemb_size));
164
}
165
#endif
166
167
static inline int
168
realloc_items(void **items, const size_t item_size,
169
    size_t *allocated, const size_t alloc_blk_cnt, const size_t count) {
170
	size_t allocated_prev, allocated_new;
171
	uint8_t *items_new;
172
173
	if (NULL == items || NULL == allocated || 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
	if (allocated_new > allocated_prev) { /* Init new mem. */
185
		bzero((items_new + (allocated_prev * item_size)),
186
		    ((allocated_new - allocated_prev) * item_size));
187
	}
188
	(*items) = items_new;
189
	(*allocated) = allocated_new;
190
191
	return (0);
192
}
193
194
195
static int
196
readdir_data_start(int fd, uint8_t *buf, size_t buf_size, readdir_data_p rddd) {
197
198
	if (-1 == fd || NULL == buf || 0 == buf_size || NULL == rddd)
199
		return (EINVAL);
200
	if (-1 == lseek(fd, 0, SEEK_SET))
201
		return (errno);
202
	bzero(rddd, sizeof(readdir_data_t));
203
	rddd->fd = fd;
204
	rddd->buf = buf;
205
	rddd->buf_size = buf_size;
206
207
	return (0);
208
}
209
210
static int
211
readdir_data_next(readdir_data_p rddd, struct dirent *de) {
212
	int ios;
213
	struct dirent *de_cur;
214
215
	if (NULL == rddd || NULL == de)
216
		return (EINVAL);
217
218
retry:
219
	if (rddd->buf_used <= rddd->buf_pos) {
220
		ios = getdirentries(rddd->fd, (char*)rddd->buf,
221
		    rddd->buf_size, &rddd->basep);
222
		if (-1 == ios)
223
			return (errno);
224
		rddd->buf_used = (size_t)ios;
225
		rddd->buf_pos = 0;
226
	}
227
	de_cur = ((struct dirent*)(rddd->buf + rddd->buf_pos));
228
	if (rddd->buf_used <= rddd->buf_pos ||
229
	    0 == de_cur->d_reclen)
230
		return (ESPIPE); /* EOF. */
231
	rddd->buf_pos += de_cur->d_reclen;
232
	if (DT_WHT == de_cur->d_type ||
233
	    IS_NAME_DOTS(de_cur->d_name))
234
		goto retry;
235
	memcpy(de, de_cur, de_cur->d_reclen);
236
237
	return (0);
238
}
239
240
static void
241
kqueue_file_mon_data_clean(kqueue_file_mon_data_p fmd) {
242
243
	if (NULL == fmd)
244
		return;
245
	close(fmd->fd);
246
	fmd->fd = -1;
247
	fmd->files_count = 0;
248
	realloc_items((void**)&fmd->files, sizeof(file_info_t),
249
	    &fmd->files_allocated, FILES_ALLOC_BLK_SIZE, fmd->files_count);
250
}
251
252
static void
253
kqueue_file_mon_data_free(kqueue_file_mon_data_p fmd) {
254
255
	if (NULL == fmd)
256
		return;
257
	close(fmd->fd);
258
	free(fmd->files);
259
	free(fmd);
260
}
261
262
static kqueue_file_mon_data_p
263
kqueue_file_mon_data_alloc(kqueue_fnm_p kfnm, const char *path, void *udata) {
264
	kqueue_file_mon_data_p fmd;
265
266
	if (NULL == kfnm || NULL == path)
267
		return (NULL);
268
	fmd = zalloc(sizeof(kqueue_file_mon_data_t));
269
	if (NULL == fmd)
270
		return (NULL);
271
	/* Remember args. */
272
	fmd->path_size = strlcpy(fmd->path, path, sizeof(fmd->path));
273
	fmd->name_offset = fmd->path_size;
274
	fmd->udata = udata;
275
	fmd->kfnm = kfnm;
276
277
	return (fmd);
278
}
279
280
static int
281
kqueue_file_mon_data_find_de_fileno(kqueue_file_mon_data_p fmd,
282
    struct dirent *de, size_t *idx) {
283
	size_t i;
284
285
	if (NULL == fmd || NULL == de || NULL == idx)
286
		return (0);
287
	for (i = 0; i < fmd->files_count; i ++) {
288
		if (de->d_fileno != fmd->files[i].de.d_fileno ||
289
		    de->d_type != fmd->files[i].de.d_type)
290
			continue;
291
		(*idx) = i;
292
		return (1);
293
	}
294
	(*idx) = fmd->files_count;
295
	return (0);
296
}
297
298
299
static void
300
kqueue_file_mon_data_init(kqueue_file_mon_data_p fmd) {
301
	struct dirent *de;
302
	struct stat sb;
303
	struct kevent kev;
304
	struct timespec ke_timeout;
305
	uint8_t *tmpbuf;
306
	readdir_data_t rddd;
307
308
	if (NULL == fmd)
309
		return;
310
	fmd->fd = open(fmd->path, OPEN_FILE_FLAGS);
311
	if (-1 == fmd->fd)
312
		return;
313
	if (0 != fstat(fmd->fd, &sb))
314
		goto err_out;
315
	/* Get parent folder name. */
316
	if (S_ISDIR(sb.st_mode)) {
317
		fmd->is_dir = 1;
318
		/* Be sure that folder contain trailing '/'. */
319
		if ('/' != fmd->path[(fmd->path_size - 1)]) {
320
			fmd->path[fmd->path_size] = '/';
321
			fmd->path[(fmd->path_size + 1)] = 0;
322
			fmd->path_size ++;
323
		}
324
		/* Skip last '/' for parent dir search. */
325
		fmd->name_offset = (fmd->path_size - 1);
326
	}
327
	/* Find parent dir path size. */
328
	while (0 < fmd->name_offset && '/' != fmd->path[(fmd->name_offset - 1)]) {
329
		fmd->name_offset --;
330
	}
331
332
	/* Dir special processing. */
333
	if (0 != fmd->is_dir) {
334
		/* Read and remember dir content. */
335
		/* Get temp buf. */
336
		/* Do this once per dir handle. */
337
		if ((size_t)sb.st_blksize > fmd->kfnm->tmpbuf_size) {
338
			tmpbuf = realloc(fmd->kfnm->tmpbuf, sb.st_blksize);
339
			if (NULL == tmpbuf)
340
				goto err_out;
341
			fmd->kfnm->tmpbuf = tmpbuf;
342
			fmd->kfnm->tmpbuf_size = (size_t)sb.st_blksize;
343
		} else {
344
			tmpbuf = fmd->kfnm->tmpbuf;
345
		}
346
		if (0 != readdir_data_start(fmd->fd, tmpbuf, sb.st_blksize, &rddd))
347
			goto err_out;
348
349
		for (;;) {
350
			if (0 != realloc_items((void**)&fmd->files,
351
			    sizeof(file_info_t), &fmd->files_allocated,
352
			    FILES_ALLOC_BLK_SIZE, fmd->files_count))
353
				goto err_out;
354
			de = &fmd->files[fmd->files_count].de; /* Use short name. */
355
			/* Get file name from folder. */
356
			if (0 != readdir_data_next(&rddd, de))
357
				break;
358
			/* Get file attrs. */
359
			if (0 != fstatat(fmd->fd, de->d_name,
360
			    &fmd->files[fmd->files_count].sb,
361
			    AT_SYMLINK_NOFOLLOW)) {
362
				bzero(&fmd->files[fmd->files_count].sb,
363
				    sizeof(struct stat));
364
			}
365
			fmd->files_count ++;
366
		}
367
	}
368
	/* Add to kqueue. */
369
	kev.ident = (uintptr_t)fmd->fd;
370
	kev.filter = EVFILT_VNODE;
371
	kev.flags = (EV_ADD | EV_CLEAR);
372
	kev.fflags = EVFILT_VNODE_FLAGS_ALL;
373
	kev.udata = (void*)fmd;
374
	bzero(&ke_timeout, sizeof(ke_timeout));
375
	if (-1 == kevent(fmd->kfnm->fd, &kev, 1, NULL, 0, &ke_timeout))
376
		goto err_out;
377
	return; /* OK. */
378
379
err_out:
380
	kqueue_file_mon_data_clean(fmd);
381
}
382
383
384
static void
385
kqueue_handle_changes(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd,
386
    kfnm_event_handler_cb cb_func) {
387
	file_info_t fi;
388
	size_t i, j, k;
389
	readdir_data_t rddd;
390
391
	if (NULL == kfnm || NULL == fmd || NULL == cb_func)
392
		return;
393
	if (0 != fstat(fmd->fd, &fi.sb) ||
394
	    0 == fi.sb.st_nlink) {
395
		kqueue_file_mon_data_clean(fmd);
396
		cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED,
397
		    fmd->path, "", NULL);
398
		return;
399
	}
400
	if (0 == fmd->is_dir) {
401
		fmd->path[fmd->name_offset] = 0;
402
		cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CHANGED, fmd->path,
403
		    (fmd->path + fmd->name_offset), NULL);
404
		fmd->path[fmd->name_offset] = '/';
405
		return;
406
	}
407
	/* Dir processing. */
408
	if (0 != readdir_data_start(fmd->fd, fmd->kfnm->tmpbuf,
409
	    fi.sb.st_blksize, &rddd))
410
		return;
411
	/* Mark all as removed. */
412
	for (i = 0; i < fmd->files_count; i ++) {
413
		fmd->files[i].de.d_reclen = KF_EVENT_DELETED;
414
	}
415
	/* Update dir. */
416
	while (0 == readdir_data_next(&rddd, &fi.de)) {
417
		/* Get file stat. */
418
		if (0 != fstatat(fmd->fd, fi.de.d_name, &fi.sb, AT_SYMLINK_NOFOLLOW)) {
419
			bzero(&fi.sb, sizeof(struct stat)); /* Fail, set to zero. */
420
		}
421
		/* Is new file/folder? */
422
		if (0 == kqueue_file_mon_data_find_de_fileno(fmd, &fi.de, &i)) { /* Add new. */
423
			if (0 != realloc_items((void**)&fmd->files, sizeof(file_info_t),
424
			    &fmd->files_allocated, FILES_ALLOC_BLK_SIZE, fmd->files_count))
425
				goto err_out;
426
			memcpy(&fmd->files[fmd->files_count], &fi, sizeof(file_info_t));
427
			fmd->files[fmd->files_count].de.d_reclen = KF_EVENT_CREATED;
428
			fmd->files_count ++;
429
			/* Notify. */
430
			cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CREATED,
431
			    fmd->path, fi.de.d_name, NULL);
432
			continue;
433
		}
434
		/* Is renamed? */
435
		if (0 == IS_DE_NAME_EQ(&fi.de, &fmd->files[i].de)) {
436
			/* Notify. */
437
			cb_func(kfnm, fmd, fmd->udata, KF_EVENT_RENAMED,
438
			    fmd->path, fmd->files[i].de.d_name, fi.de.d_name);
439
			/* Update. */
440
			memcpy(&fmd->files[i], &fi, sizeof(file_info_t));
441
			fmd->files[i].de.d_reclen = KF_EVENT_RENAMED;
442
			continue;
443
		}
444
		/* Is modified? */
445
		if (0 != memcmp(&fmd->files[i].sb, &fi.sb, sizeof(struct stat))) {
446
			memcpy(&fmd->files[i].sb, &fi.sb, sizeof(struct stat)); /* Update stat. */
447
			fmd->files[i].de.d_reclen = KF_EVENT_CHANGED;
448
			/* Notify. */
449
			cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CHANGED,
450
			    fmd->path, fi.de.d_name, NULL);
451
			continue;
452
		}
453
		/* Not changed. */
454
		fmd->files[i].de.d_reclen = KF_EVENT_NOT_CHANGED;
455
	}
456
	/* Remove marked as removed. */
457
	for (i = 0; i < fmd->files_count; i ++) {
458
		if (KF_EVENT_DELETED != fmd->files[i].de.d_reclen)
459
			continue;
460
		/* Look for next non removed item + notify. */
461
		for (j = (i + 1); j < fmd->files_count; j ++) {
462
			if (KF_EVENT_DELETED != fmd->files[j].de.d_reclen)
463
				break;
464
		}
465
		/* Notify. */
466
		for (k = i; k < j; k ++) {
467
			cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED,
468
			    fmd->path, fmd->files[k].de.d_name, NULL);
469
		}
470
		memmove(&fmd->files[i], &fmd->files[j],
471
		    (sizeof(file_info_t) * (fmd->files_count - j)));
472
		fmd->files_count -= (j - i);
473
	}
474
475
err_out:
476
	return;
477
}
478
479
static void
480
kqueue_handle_rename(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd,
481
    kfnm_event_handler_cb cb_func) {
482
	int up_dir_fd, found = 0;
483
	readdir_data_t rddd;
484
	struct dirent de;
485
	struct stat sb;
486
	char old_filename[(MAXNAMLEN + 2)];
487
	size_t old_filename_size;
488
489
	if (NULL == kfnm || NULL == fmd || NULL == cb_func)
490
		return;
491
	if (0 != fstat(fmd->fd, &sb) ||
492
	    0 == sb.st_nlink) {
493
notify_removed:
494
		kqueue_file_mon_data_clean(fmd);
495
		cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED,
496
		    fmd->path, "", NULL);
497
		return;
498
	}
499
	/* Save old file name. */
500
	old_filename_size = (fmd->path_size - fmd->name_offset - fmd->is_dir);
501
	memcpy(old_filename,
502
	    (fmd->path + fmd->name_offset),
503
	    old_filename_size);
504
	old_filename[old_filename_size] = 0;
505
506
	/* Get parent folder name. */
507
	fmd->path[fmd->name_offset] = 0;
508
	/* Try to open. */
509
	up_dir_fd = open(fmd->path, (OPEN_FILE_FLAGS | O_DIRECTORY));
510
	/* Restore '/' after parent folder. */
511
	fmd->path[fmd->name_offset] = '/';
512
	if (-1 == up_dir_fd ||
513
	    0 != fstat(up_dir_fd, &sb) ||
514
	    0 != readdir_data_start(up_dir_fd, fmd->kfnm->tmpbuf, sb.st_blksize, &rddd)) {
515
		close(up_dir_fd);
516
		return;
517
	}
518
	/* Find new name by inode. */
519
	while (0 == readdir_data_next(&rddd, &de)) {
520
		if (de.d_fileno == sb.st_ino) {
521
			found ++;
522
			break;
523
		}
524
	}
525
	close(up_dir_fd);
526
	if (0 == found)
527
		goto notify_removed; /* Not found. */
528
	/* Update name. */
529
	if ((sizeof(fmd->path) - 2) <= (fmd->name_offset + de.d_namlen))
530
		return; /* Too long. */
531
	memcpy((fmd->path + fmd->name_offset), de.d_name, de.d_namlen);
532
	fmd->path_size = (fmd->name_offset + de.d_namlen);
533
	/* Add last '/' for dir. */
534
	fmd->path[fmd->path_size] = '/';
535
	fmd->path_size += fmd->is_dir;
536
	fmd->path[fmd->path_size] = 0;
537
	/* Notify. */
538
	cb_func(kfnm, fmd, fmd->udata, KF_EVENT_RENAMED,
539
	    fmd->path, old_filename, de.d_name);
540
}
541
542
543
static void
544
kqueue_fnm_delay_call_process(kqueue_fnm_p kfnm, kq_msg_cb forced_msg_cb) {
545
	ssize_t rd;
546
	kqueue_fnm_msg_pkt_t msg;
547
548
	for (;;) {
549
		rd = read(kfnm->pfd[0], &msg, sizeof(msg));
550
		if (0 >= rd)
551
			return;
552
		if (0 == KF_MSG_PKT_IS_VALID(&msg))
553
			continue;
554
		if (NULL != forced_msg_cb) {
555
			forced_msg_cb(msg.fmd);
556
			continue;
557
		}
558
		if (NULL == msg.msg_cb)
559
			continue;
560
		msg.msg_cb(msg.fmd);
561
	}
562
}
563
564
static int
565
kqueue_fnm_delay_call(kqueue_fnm_p kfnm, kq_msg_cb msg_cb,
566
    kqueue_file_mon_data_p fmd) {
567
	kqueue_fnm_msg_pkt_t msg;
568
569
	if (NULL == kfnm || NULL == fmd)
570
		return (EINVAL);
571
	msg.magic = KF_MSG_PKT_MAGIC;
572
	msg.msg_cb = msg_cb;
573
	msg.fmd = fmd;
574
	KF_MSG_PKT_CHK_SUM_SET(&msg);
575
	if (sizeof(msg) == write(kfnm->pfd[1], &msg, sizeof(msg)))
576
		return (0);
577
	return (errno);
578
}
579
580
581
void
582
kqueue_fnm_free(kqueue_fnm_p kfnm) {
583
584
	if (NULL == kfnm)
585
		return;
586
	close(kfnm->fd);
587
	kfnm->fd = -1;
588
	/* Free all in delay calls queue. */
589
	kqueue_fnm_delay_call_process(kfnm, kqueue_file_mon_data_free);
590
	close(kfnm->pfd[0]);
591
	close(kfnm->pfd[1]);
592
	kfnm->pfd[0] = -1;
593
	kfnm->pfd[1] = -1;
594
	kfnm->tmpbuf_size = 0;
595
	free(kfnm->tmpbuf);
596
	free(kfnm);
597
}
598
599
kqueue_fnm_p
600
kqueue_fnm_create(void) {
601
	kqueue_fnm_p kfnm;
602
	struct kevent kev;
603
	struct timespec ke_timeout;
604
605
	kfnm = zalloc(sizeof(kqueue_fnm_t));
606
	if (NULL == kfnm)
607
		return (NULL);
608
	kfnm->fd = kqueue();
609
	if (-1 == kfnm->fd)
610
		goto err_out;
611
	if (-1 == pipe2(kfnm->pfd, O_NONBLOCK))
612
		goto err_out;
613
	kev.ident = kfnm->pfd[0];
614
	kev.filter = EVFILT_READ;
615
	kev.flags = (EV_ADD | EV_ENABLE);
616
	kev.fflags = 0;
617
	kev.udata = NULL;
618
	bzero(&ke_timeout, sizeof(ke_timeout));
619
	if (-1 == kevent(kfnm->fd, &kev, 1, NULL, 0, &ke_timeout))
620
		goto err_out;
621
	return (kfnm);
622
623
err_out:
624
	kqueue_fnm_free(kfnm);
625
	return (NULL);
626
}
627
628
kqueue_file_mon_data_p
629
kqueue_fnm_add(kqueue_fnm_p kfnm, const char *path, void *udata) {
630
	int error;
631
	kqueue_file_mon_data_p fmd;
632
	
633
	if (NULL == kfnm || NULL == path)
634
		return (NULL);
635
	fmd = (void*)kqueue_file_mon_data_alloc(kfnm, path, udata);
636
	if (NULL == fmd)
637
		return (NULL);
638
	/* Shedule delay call to init. */
639
	error = kqueue_fnm_delay_call(kfnm, kqueue_file_mon_data_init, fmd);
640
	if (0 != error) { /* Error, init directly. */
641
		//kqueue_file_mon_data_init(fmd);
642
		kqueue_file_mon_data_free(fmd);
643
		return (NULL);
644
	}
645
	return (fmd);
646
}
647
648
void
649
kqueue_fnm_del(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd) {
650
	int error;
651
652
	if (NULL == kfnm || NULL == fmd)
653
		return;
654
	/* Cancel notifications. */
655
	close(fmd->fd);
656
	fmd->fd = -1;
657
	/* Shedule delay call to free. */
658
	error = kqueue_fnm_delay_call(kfnm, kqueue_file_mon_data_free, fmd);
659
	if (0 == error)
660
		return;
661
	/* Error, free directly. */
662
	kqueue_file_mon_data_free(fmd);
663
}
664
665
666
int
667
kqueue_fnm_get_ev_recv_fd(kqueue_fnm_p kfnm) {
668
669
	if (NULL == kfnm)
670
		return (-1);
671
	return (kfnm->fd);
672
}
673
674
void
675
kqueue_fnm_proccess_events(kqueue_fnm_p kfnm, kfnm_event_handler_cb cb_func) {
676
	struct kevent kev;
677
	struct timespec ke_timeout;
678
	kqueue_file_mon_data_p fmd;
679
680
	if (NULL == kfnm || NULL == cb_func)
681
		return;
682
	/* Get and proccess events. */
683
	bzero(&ke_timeout, sizeof(ke_timeout));
684
	while (0 < kevent(kfnm->fd, NULL, 0, &kev, 1, &ke_timeout)) {
685
		if (kev.ident == (uintptr_t)kfnm->pfd[0] &&
686
		    kev.filter == EVFILT_READ) { /* Handle delay calls. */
687
			kqueue_fnm_delay_call_process(kfnm, NULL);
688
			continue;
689
		}
690
		if (EVFILT_VNODE != kev.filter ||
691
		    0 == kev.udata)
692
			continue; /* Unknown event or no associated data, skip. */
693
		fmd = (kqueue_file_mon_data_p)kev.udata;
694
695
		if (EV_ERROR & kev.flags) {
696
			kev.flags |= NOTE_REVOKE; /* Treat error as unmount. */
697
		}
698
		if (NOTE_RENAME & kev.fflags) {
699
			kqueue_handle_rename(kfnm, fmd, cb_func);
700
		}
701
		if ((NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB) & kev.fflags) {
702
			kqueue_handle_changes(kfnm, fmd, cb_func);
703
		}
704
		if ((NOTE_DELETE | NOTE_REVOKE) & kev.fflags) {
705
			kqueue_file_mon_data_clean(fmd);
706
			cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED,
707
			    fmd->path, "", NULL);
708
		}
709
	}
710
}
(-)devel/glib20/files/kqueue_fnm.h.in (+68 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (c) 2016 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
31
 
32
#ifndef __KQUEUE_FILE_NOTIFY_MONITOR_H__
33
#define __KQUEUE_FILE_NOTIFY_MONITOR_H__
34
35
#include <sys/param.h>
36
#include <sys/types.h>
37
#include <inttypes.h>
38
39
40
typedef struct kqueue_file_nonify_monitor_s	*kqueue_fnm_p;
41
typedef struct kqueue_file_mon_data_s		*kqueue_file_mon_data_p;
42
43
typedef void (*kfnm_event_handler_cb)(kqueue_fnm_p kfnm,
44
			       kqueue_file_mon_data_p fmd, void *udata,
45
			       uint32_t event,
46
			       const char *base,
47
			       const char *filename,
48
			       const char *new_filename);
49
#define KF_EVENT_NOT_CHANGED	0 /* Internal use. */
50
#define KF_EVENT_CREATED	1
51
#define KF_EVENT_DELETED	2
52
#define KF_EVENT_RENAMED	3
53
#define KF_EVENT_CHANGED	4
54
55
56
kqueue_fnm_p	kqueue_fnm_create(void);
57
void		kqueue_fnm_free(kqueue_fnm_p kfnm);
58
59
kqueue_file_mon_data_p	kqueue_fnm_add(kqueue_fnm_p kfnm,
60
			    const char *path, void *udata);
61
void		kqueue_fnm_del(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd);
62
63
int		kqueue_fnm_get_ev_recv_fd(kqueue_fnm_p kfnm);
64
void		kqueue_fnm_proccess_events(kqueue_fnm_p kfnm,
65
		    kfnm_event_handler_cb cb_func);
66
67
68
#endif /* __KQUEUE_FILE_NOTIFY_MONITOR_H__ */
(-)devel/glib20/files/patch-gio_kqueue_Makefile.am (+26 lines)
Line 0 Link Here
1
--- gio/kqueue/Makefile.am.orig	2015-10-14 14:41:16.000000000 +0300
2
+++ gio/kqueue/Makefile.am	2016-11-06 05:08:37.646089000 +0300
3
@@ -4,21 +4,8 @@
4
 
5
 libkqueue_la_SOURCES = \
6
        gkqueuefilemonitor.c \
7
-       gkqueuefilemonitor.h \
8
-       kqueue-helper.c \
9
-       kqueue-helper.h \
10
-       kqueue-thread.c \
11
-       kqueue-thread.h \
12
-       kqueue-sub.c \
13
-       kqueue-sub.h \
14
-       kqueue-missing.c \
15
-       kqueue-missing.h \
16
-       kqueue-utils.c \
17
-       kqueue-utils.h \
18
-       kqueue-exclusions.c \
19
-       kqueue-exclusions.h \
20
-       dep-list.c \
21
-       dep-list.h \
22
+       kqueue_fnm.c \
23
+       kqueue_fnm.h \
24
        $(NULL)
25
 
26
 libkqueue_la_CFLAGS = \
(-)devel/glib20/files/patch-gio_kqueue_gkqueuefilemonitor.c (+372 lines)
Line 0 Link Here
1
--- gio/kqueue/gkqueuefilemonitor.c.orig	2015-10-14 14:41:16.000000000 +0300
2
+++ gio/kqueue/gkqueuefilemonitor.c	2016-11-06 05:01:49.712277000 +0300
3
@@ -1,198 +1,203 @@
4
-/*******************************************************************************
5
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
6
-
7
-  Permission is hereby granted, free of charge, to any person obtaining a copy
8
-  of this software and associated documentation files (the "Software"), to deal
9
-  in the Software without restriction, including without limitation the rights
10
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
-  copies of the Software, and to permit persons to whom the Software is
12
-  furnished to do so, subject to the following conditions:
13
-
14
-  The above copyright notice and this permission notice shall be included in
15
-  all copies or substantial portions of the Software.
16
-
17
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
-  THE SOFTWARE.
24
-*******************************************************************************/
25
+/*-
26
+ * Copyright (c) 2016 Rozhuk Ivan <rozhuk.im@gmail.com>
27
+ * All rights reserved.
28
+ *
29
+ * Redistribution and use in source and binary forms, with or without
30
+ * modification, are permitted provided that the following conditions
31
+ * are met:
32
+ * 1. Redistributions of source code must retain the above copyright
33
+ *    notice, this list of conditions and the following disclaimer.
34
+ * 2. Redistributions in binary form must reproduce the above copyright
35
+ *    notice, this list of conditions and the following disclaimer in the
36
+ *    documentation and/or other materials provided with the distribution.
37
+ *
38
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48
+ * SUCH DAMAGE.
49
+ *
50
+ * Author: Rozhuk Ivan <rozhuk.im@gmail.com>
51
+ *
52
+ */
53
 
54
 #include "config.h"
55
 
56
-#include "gkqueuefilemonitor.h"
57
-#include "kqueue-helper.h"
58
-#include "kqueue-exclusions.h"
59
-#include <gio/gpollfilemonitor.h>
60
-#include <gio/gfile.h>
61
+#include <glib-object.h>
62
+#include <string.h>
63
+#include <gio/gfilemonitor.h>
64
+#include <gio/glocalfilemonitor.h>
65
 #include <gio/giomodule.h>
66
+#include "glib-private.h"
67
+#include <glib-unix.h>
68
+#include "kqueue_fnm.h"
69
 
70
 
71
-struct _GKqueueFileMonitor
72
-{
73
-  GLocalFileMonitor parent_instance;
74
+static GMutex			kqueue_lock;
75
+static GSource			*kqueue_source = NULL;
76
+static volatile kqueue_fnm_p	kqueue_fnm = NULL;
77
 
78
-  kqueue_sub *sub;
79
+#define G_TYPE_KQUEUE_FILE_MONITOR      (g_kqueue_file_monitor_get_type())
80
+#define G_KQUEUE_FILE_MONITOR(inst)     (G_TYPE_CHECK_INSTANCE_CAST((inst), \
81
+					 G_TYPE_KQUEUE_FILE_MONITOR, GKqueueFileMonitor))
82
 
83
-  GFileMonitor *fallback;
84
-  GFile *fbfile;
85
-};
86
+typedef GLocalFileMonitorClass	GKqueueFileMonitorClass;
87
 
88
-static gboolean g_kqueue_file_monitor_cancel (GFileMonitor* monitor);
89
+typedef struct {
90
+	GLocalFileMonitor	parent_instance;
91
+	kqueue_file_mon_data_p	fmd;
92
+} GKqueueFileMonitor;
93
 
94
+GType g_kqueue_file_monitor_get_type(void);
95
 G_DEFINE_TYPE_WITH_CODE (GKqueueFileMonitor, g_kqueue_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
96
-       g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
97
+       g_io_extension_point_implement(G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
98
                g_define_type_id,
99
                "kqueue",
100
-               20))
101
+               10))
102
 
103
 
104
 static void
105
-_fallback_callback (GFileMonitor      *unused,
106
-                    GFile             *first,
107
-                    GFile             *second,
108
-                    GFileMonitorEvent  event,
109
-                    gpointer           udata)
110
-{
111
-  GKqueueFileMonitor *kq_mon = G_KQUEUE_FILE_MONITOR (udata);
112
-  GFileMonitor *mon = G_FILE_MONITOR (kq_mon);
113
-  g_assert (kq_mon != NULL);
114
-  g_assert (mon != NULL);
115
-  (void) unused;
116
-
117
-  if (event == G_FILE_MONITOR_EVENT_CHANGED)
118
-    {
119
-      GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (kq_mon);
120
-
121
-      _kh_dir_diff (kq_mon->sub, local_monitor->source);
122
-    }
123
-  else
124
-    g_file_monitor_emit_event (mon, first, second, event);
125
-}
126
-
127
-
128
-static void
129
-g_kqueue_file_monitor_finalize (GObject *object)
130
-{
131
-  GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (object);
132
-
133
-  if (kqueue_monitor->sub)
134
-    {
135
-      _kh_cancel_sub (kqueue_monitor->sub);
136
-      _kh_sub_free (kqueue_monitor->sub);
137
-      kqueue_monitor->sub = NULL;
138
-    }
139
-
140
-  if (kqueue_monitor->fallback)
141
-    g_object_unref (kqueue_monitor->fallback);
142
-
143
-  if (kqueue_monitor->fbfile)
144
-    g_object_unref (kqueue_monitor->fbfile);
145
-
146
-  if (G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize)
147
-    (*G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize) (object);
148
-}
149
-
150
-static void
151
-g_kqueue_file_monitor_start (GLocalFileMonitor *local_monitor,
152
-                             const gchar *dirname,
153
-                             const gchar *basename,
154
-                             const gchar *filename,
155
-                             GFileMonitorSource *source)
156
-{
157
-  GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (local_monitor);
158
-  GObject *obj;
159
-  GKqueueFileMonitorClass *klass;
160
-  GObjectClass *parent_class;
161
-  kqueue_sub *sub = NULL;
162
-  gboolean ret_kh_startup = FALSE;
163
-  const gchar *path = NULL; 
164
-
165
-
166
-  ret_kh_startup = _kh_startup ();
167
-  g_assert (ret_kh_startup);
168
-
169
-  path = filename;
170
-  if (!path)
171
-    path = dirname;
172
-
173
-  /* For a directory monitor, create a subscription object anyway.
174
-   * It will be used for directory diff calculation routines. 
175
-   * Wait, directory diff in a GKqueueFileMonitor?
176
-   * Yes, it is. When a file monitor is started on an non-existent
177
-   * file, GIO uses a GKqueueFileMonitor object for that. If a directory
178
-   * will be created under that path, GKqueueFileMonitor will have to
179
-   * handle the directory notifications. */
180
-
181
-  sub = _kh_sub_new (path, TRUE, source);
182
-
183
-  /* FIXME: what to do about errors here? we can't return NULL or another
184
-   * kind of error and an assertion is probably too hard (same issue as in
185
-   * the inotify backend) */
186
-  g_assert (sub != NULL);
187
-  kqueue_monitor->sub = sub;
188
-
189
-  if (!_ke_is_excluded (path))
190
-    _kh_add_sub (sub);
191
-  else
192
-    {
193
-      GFile *file = g_file_new_for_path (path);
194
-      kqueue_monitor->fbfile = file;
195
-      kqueue_monitor->fallback = _g_poll_file_monitor_new (file);
196
-      g_signal_connect (kqueue_monitor->fallback,
197
-                        "changed",
198
-                        G_CALLBACK (_fallback_callback),
199
-                        kqueue_monitor);
200
-    }
201
+kqueue_event_handler(kqueue_fnm_p kfnm,
202
+    kqueue_file_mon_data_p fmd, void *udata, uint32_t event,
203
+    const char *base, const char *filename, const char *new_filename) {
204
+	static const uint32_t kfnm_to_glib_map[] = {
205
+		0,				/* KF_EVENT_NOT_CHANGED */
206
+		G_FILE_MONITOR_EVENT_CREATED,	/* KF_EVENT_CREATED */
207
+		G_FILE_MONITOR_EVENT_DELETED,	/* KF_EVENT_DELETED */
208
+		G_FILE_MONITOR_EVENT_RENAMED,	/* KF_EVENT_RENAMED */
209
+		G_FILE_MONITOR_EVENT_CHANGED	/* KF_EVENT_CHANGED */
210
+	};
211
+
212
+	if (NULL == kfnm || NULL == filename ||
213
+	    KF_EVENT_CREATED > event ||
214
+	    KF_EVENT_CHANGED < event)
215
+		return;
216
+	g_file_monitor_source_handle_event(udata,
217
+	    kfnm_to_glib_map[event],
218
+	    filename, new_filename, NULL,
219
+	    g_source_get_time(kqueue_source));
220
+}
221
+
222
+static gboolean
223
+g_kqueue_file_monitor_callback(gint fd, GIOCondition condition, gpointer user_data) {
224
+
225
+	/* Only one thread process events. */
226
+	g_mutex_lock(&kqueue_lock);
227
+	kqueue_fnm_proccess_events(kqueue_fnm, kqueue_event_handler);
228
+	g_mutex_unlock(&kqueue_lock);
229
+
230
+	return (TRUE);
231
+}
232
+
233
+static gboolean
234
+g_kqueue_file_monitor_is_supported(void) {
235
+
236
+	if (NULL != kqueue_fnm)
237
+		return (TRUE);
238
+	/* Init only once. */
239
+	g_mutex_lock(&kqueue_lock);
240
+	if (NULL != kqueue_fnm) {
241
+		g_mutex_unlock(&kqueue_lock);
242
+		return (TRUE); /* Initialized while wait lock. */
243
+	}
244
+	kqueue_fnm = kqueue_fnm_create();
245
+	if (NULL == kqueue_fnm) {
246
+		g_mutex_unlock(&kqueue_lock);
247
+		return (FALSE); /* Init fail. */
248
+	}
249
+	kqueue_source = g_unix_fd_source_new(kqueue_fnm_get_ev_recv_fd(kqueue_fnm), G_IO_IN);
250
+	g_source_set_callback(kqueue_source, (GSourceFunc)g_kqueue_file_monitor_callback, NULL, NULL);
251
+	g_source_attach(kqueue_source, GLIB_PRIVATE_CALL(g_get_worker_context)());
252
+	g_mutex_unlock(&kqueue_lock);
253
+
254
+	return (TRUE);
255
 }
256
 
257
 static gboolean
258
-g_kqueue_file_monitor_is_supported (void)
259
-{
260
-  return _kh_startup ();
261
+g_kqueue_file_monitor_cancel(GFileMonitor *monitor) {
262
+	GKqueueFileMonitor *gffm = G_KQUEUE_FILE_MONITOR(monitor);
263
+
264
+	kqueue_fnm_del(kqueue_fnm, gffm->fmd);
265
+	gffm->fmd = NULL;
266
+
267
+	return (TRUE);
268
 }
269
 
270
 static void
271
-g_kqueue_file_monitor_class_init (GKqueueFileMonitorClass *klass)
272
-{
273
-  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
274
-  GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
275
-  GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass);
276
+g_kqueue_file_monitor_finalize(GObject *object) {
277
+	GKqueueFileMonitor *gffm = G_KQUEUE_FILE_MONITOR(object);
278
 
279
-  gobject_class->finalize = g_kqueue_file_monitor_finalize;
280
-  file_monitor_class->cancel = g_kqueue_file_monitor_cancel;
281
+	kqueue_fnm_del(kqueue_fnm, gffm->fmd);
282
+	gffm->fmd = NULL;
283
+}
284
 
285
-  local_file_monitor_class->is_supported = g_kqueue_file_monitor_is_supported;
286
-  local_file_monitor_class->start = g_kqueue_file_monitor_start;
287
-  local_file_monitor_class->mount_notify = TRUE; /* TODO: ??? */
288
+static void
289
+g_kqueue_file_monitor_start(GLocalFileMonitor *local_monitor,
290
+    const gchar *dirname, const gchar *basename,
291
+    const gchar *filename, GFileMonitorSource *source) {
292
+	GKqueueFileMonitor *gffm = G_KQUEUE_FILE_MONITOR (local_monitor);
293
+
294
+	g_assert(NULL != kqueue_fnm);
295
+	g_source_ref((GSource*)source);
296
+
297
+	if (NULL == filename) {
298
+		filename = dirname;
299
+	}
300
+	gffm->fmd = kqueue_fnm_add(kqueue_fnm, filename, source);
301
 }
302
 
303
 static void
304
-g_kqueue_file_monitor_init (GKqueueFileMonitor *monitor)
305
-{
306
+g_kqueue_file_monitor_init(GKqueueFileMonitor *monitor) {
307
+	
308
 }
309
 
310
-static gboolean
311
-g_kqueue_file_monitor_cancel (GFileMonitor *monitor)
312
-{
313
-  GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (monitor);
314
-
315
-  if (kqueue_monitor->sub)
316
-    {
317
-      _kh_cancel_sub (kqueue_monitor->sub);
318
-      _kh_sub_free (kqueue_monitor->sub);
319
-      kqueue_monitor->sub = NULL;
320
-    }
321
-  else if (kqueue_monitor->fallback)
322
-    {
323
-      g_signal_handlers_disconnect_by_func (kqueue_monitor->fallback, _fallback_callback, kqueue_monitor);
324
-      g_file_monitor_cancel (kqueue_monitor->fallback);
325
-    }
326
+static void
327
+g_kqueue_file_monitor_class_init(GKqueueFileMonitorClass *class) {
328
+	GObjectClass *gobject_class = G_OBJECT_CLASS(class);
329
+	GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS(class);
330
+
331
+	class->is_supported = g_kqueue_file_monitor_is_supported;
332
+	class->start = g_kqueue_file_monitor_start;
333
+	class->mount_notify = TRUE; /* TODO: ??? */
334
+	file_monitor_class->cancel = g_kqueue_file_monitor_cancel;
335
+	gobject_class->finalize = g_kqueue_file_monitor_finalize;
336
+}
337
+
338
+static void
339
+g_kqueue_file_monitor_class_finalize(GKqueueFileMonitorClass *class) {
340
+	
341
+}
342
+
343
+void
344
+g_io_module_load(GIOModule *module) {
345
+
346
+	g_type_module_use(G_TYPE_MODULE(module));
347
+
348
+	g_io_extension_point_implement(G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
349
+	    G_TYPE_KQUEUE_FILE_MONITOR, "kqueue", 10);
350
+	g_io_extension_point_implement(G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME,
351
+	    G_TYPE_KQUEUE_FILE_MONITOR, "kqueue", 10);
352
+}
353
+
354
+void
355
+g_io_module_unload(GIOModule *module) {
356
+	
357
+	g_assert_not_reached();
358
+}
359
 
360
-  if (G_FILE_MONITOR_CLASS (g_kqueue_file_monitor_parent_class)->cancel)
361
-    (*G_FILE_MONITOR_CLASS (g_kqueue_file_monitor_parent_class)->cancel) (monitor);
362
+char **
363
+g_io_module_query(void) {
364
+	char *eps[] = {
365
+		G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
366
+		G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME,
367
+		NULL
368
+	};
369
 
370
-  return TRUE;
371
+	return (g_strdupv(eps));
372
 }

Return to bug 214338