File: gam_poll_basic.c

package info (click to toggle)
gamin 0.1.9-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,632 kB
  • ctags: 1,866
  • sloc: ansic: 10,776; sh: 8,591; python: 3,706; xml: 1,303; makefile: 350; awk: 48
file content (437 lines) | stat: -rw-r--r-- 12,000 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* Gamin
 * Copyright (C) 2003 James Willcox, Corey Bowers
 * Copyright (C) 2004 Daniel Veillard
 * Copyright (C) 2005 John McCutchan
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "server_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <glib.h>
#include "fam.h"
#include "gam_error.h"
#include "gam_tree.h"
#include "gam_poll_basic.h"
#include "gam_event.h"
#include "gam_server.h"
#include "gam_protocol.h"
#include "gam_event.h"
#include "gam_excludes.h"

#define VERBOSE_POLL

static gboolean gam_poll_basic_add_subscription(GamSubscription * sub);
static gboolean gam_poll_basic_remove_subscription(GamSubscription * sub);
static gboolean gam_poll_basic_remove_all_for(GamListener * listener);
static GaminEventType gam_poll_basic_poll_file(GamNode * node);
static gboolean gam_poll_basic_scan_callback(gpointer data);

static gboolean scan_callback_running = FALSE;

gboolean
gam_poll_basic_init ()
{
	gam_poll_generic_init ();
	gam_server_install_poll_hooks (GAMIN_P_BASIC,
				       gam_poll_basic_add_subscription,
				       gam_poll_basic_remove_subscription,
				       gam_poll_basic_remove_all_for,
				       gam_poll_basic_poll_file);

	GAM_DEBUG(DEBUG_INFO, "basic poll backend initialized\n");
	return TRUE;
}

/**
 * Adds a subscription to be polled.
 *
 * @param sub a #GamSubscription to be polled
 * @returns TRUE if adding the subscription succeeded, FALSE otherwise
 */
static gboolean
gam_poll_basic_add_subscription(GamSubscription * sub)
{
	const char *path = gam_subscription_get_path (sub);
	GamNode *node = gam_tree_get_at_path (gam_poll_generic_get_tree(), path);
	int node_is_dir = FALSE;

	gam_listener_add_subscription(gam_subscription_get_listener(sub), sub);
	gam_poll_generic_update_time ();

	if (!node)
	{
		node = gam_tree_add_at_path(gam_poll_generic_get_tree(), path, gam_subscription_is_dir(sub));
	}

	gam_node_add_subscription(node, sub);
	node_is_dir = gam_node_is_dir(node);

	if (node_is_dir)
	{
		gam_poll_generic_first_scan_dir(sub, node, path);
	} else {
		GaminEventType event;

		event = gam_poll_basic_poll_file (node);
		GAM_DEBUG(DEBUG_INFO, "New file subscription: %s event %d\n", path, event);

		if ((event == 0) || (event == GAMIN_EVENT_EXISTS) ||
		    (event == GAMIN_EVENT_CHANGED) ||
		    (event == GAMIN_EVENT_CREATED))
		{
			if (gam_subscription_is_dir(sub)) {
				/* we are watching a file but requested a directory */
				gam_server_emit_one_event(path, node_is_dir, GAMIN_EVENT_DELETED, sub, 0);
			} else {
				gam_server_emit_one_event(path, node_is_dir, GAMIN_EVENT_EXISTS, sub, 0);
			}
		} else if (event != 0) {
			gam_server_emit_one_event(path, node_is_dir, GAMIN_EVENT_DELETED, sub, 0);
		}

		gam_server_emit_one_event(path, node_is_dir, GAMIN_EVENT_ENDEXISTS, sub, 0);
	}

	if (gam_node_has_pflag (node, MON_MISSING))
		gam_poll_generic_add_missing(node);

	gam_poll_generic_add (node);

	if (!scan_callback_running)
	{
	  scan_callback_running = TRUE;
	  g_timeout_add (1000, gam_poll_basic_scan_callback, NULL);
	}
	
	GAM_DEBUG(DEBUG_INFO, "Poll: added subscription for %s\n", path);
	return TRUE;
}

static gboolean
node_remove_directory_subscription(GamNode * node, GamSubscription * sub)
{
	GList *children, *l;
	gboolean remove_dir;

	GAM_DEBUG(DEBUG_INFO, "remove_directory_subscription %s\n", gam_node_get_path(node));

	gam_node_remove_subscription(node, sub);

	remove_dir = (gam_node_get_subscriptions(node) == NULL);

	children = gam_tree_get_children(gam_poll_generic_get_tree(), node);
	for (l = children; l; l = l->next) {
		GamNode *child = (GamNode *) l->data;

		if ((!gam_node_get_subscriptions(child)) && (remove_dir) &&
		    (!gam_tree_has_children(gam_poll_generic_get_tree(), child))) 
		{
			gam_poll_generic_unregister_node (child);

			gam_tree_remove(gam_poll_generic_get_tree(), child);
		} else {
			remove_dir = FALSE;
		}
	}

	g_list_free(children);

	/*
	* do not remove the directory if the parent has a directory subscription
	*/
	remove_dir = ((gam_node_get_subscriptions(node) == NULL) && (!gam_node_has_dir_subscriptions(gam_node_parent(node))));

	if (remove_dir) {
		GAM_DEBUG(DEBUG_INFO, "  => remove_dir %s\n",
		gam_node_get_path(node));
	}
	return remove_dir;
}

/**
 * Removes a subscription which was being polled.
 *
 * @param sub a #GamSubscription to remove
 * @returns TRUE if removing the subscription succeeded, FALSE otherwise
 */
static gboolean
gam_poll_basic_remove_subscription(GamSubscription * sub)
{
	const char *path = gam_subscription_get_path (sub);
	GamNode *node = gam_tree_get_at_path (gam_poll_generic_get_tree(), path);

	if (node == NULL) {
		/* free directly */
		gam_subscription_free(sub);
		return TRUE;
	}

	gam_subscription_cancel(sub);

#ifdef VERBOSE_POLL
	GAM_DEBUG(DEBUG_INFO, "Tree has %d nodes\n", gam_tree_get_size(gam_poll_generic_get_tree()));
#endif
	if (!gam_node_is_dir(node)) {
		GAM_DEBUG(DEBUG_INFO, "Removing node %s\n", gam_subscription_get_path(sub));
		gam_node_remove_subscription(node, sub);

		if (!gam_node_get_subscriptions(node)) 
		{
			GamNode *parent;
			gam_poll_generic_unregister_node (node);
			g_assert (!gam_tree_has_children(gam_poll_generic_get_tree(), node));
			parent = gam_node_parent(node);
			if ((parent != NULL) && (!gam_node_has_dir_subscriptions(parent))) {
				gam_tree_remove(gam_poll_generic_get_tree(), node);
				gam_poll_generic_prune_tree(parent);
			}
		}
	} else {
		GAM_DEBUG(DEBUG_INFO, "Removing node %s\n", gam_subscription_get_path(sub));
		if (node_remove_directory_subscription(node, sub)) {
			GamNode *parent;

			gam_poll_generic_unregister_node (node);
			parent = gam_node_parent(node);
			if (!gam_tree_has_children(gam_poll_generic_get_tree(), node)) {
				gam_tree_remove(gam_poll_generic_get_tree(), node);
			}

			gam_poll_generic_prune_tree(parent);
		}
	}

#ifdef VERBOSE_POLL
	GAM_DEBUG(DEBUG_INFO, "Tree has %d nodes\n", gam_tree_get_size(gam_poll_generic_get_tree()));
#endif

	GAM_DEBUG(DEBUG_INFO, "Poll: removed subscription for %s\n", path);

	gam_subscription_free(sub);
	return TRUE;
}

/**
 * Stop polling all subscriptions for a given #GamListener.
 *
 * @param listener a #GamListener
 * @returns TRUE if removing the subscriptions succeeded, FALSE otherwise
 */
static gboolean
gam_poll_basic_remove_all_for(GamListener * listener)
{
	GList *subs, *l = NULL;

	subs = gam_listener_get_subscriptions(listener);
	for (l = subs; l; l = l->next) {
		GamSubscription *sub = l->data;
		g_assert(sub != NULL);
		gam_poll_remove_subscription(sub);
	}

	if (subs) {
		g_list_free(subs);
		return TRUE;
	} else
		return FALSE;
}

static gboolean
gam_poll_generic_node_changed (GamNode *node, struct stat sbuf)
{
	g_assert(node);
#ifdef ST_MTIM_NSEC
	return ((node->sbuf.st_mtim.tv_sec != sbuf.st_mtim.tv_sec) ||
		(node->sbuf.st_mtim.tv_nsec != sbuf.st_mtim.tv_nsec) ||
		(node->sbuf.st_size != sbuf.st_size) ||
		(node->sbuf.st_ctim.tv_sec != sbuf.st_ctim.tv_sec) ||
		(node->sbuf.st_ctim.tv_nsec != sbuf.st_ctim.tv_nsec));
#else
	return ((node->sbuf.st_mtime != sbuf.st_mtime) ||
		(node->sbuf.st_size != sbuf.st_size) ||
		(node->sbuf.st_ctime != sbuf.st_ctime));
#endif
}

static GaminEventType
gam_poll_basic_poll_file(GamNode * node)
{
	GaminEventType event;
	struct stat sbuf;
	int stat_ret;
	const char *path = NULL;

	g_assert (node);

	path = gam_node_get_path(node);
	/* If not enough time has passed since the last time we polled this node, stop here */
	if (node->lasttime && gam_poll_generic_get_delta_time (node->lasttime) < node->poll_time)
	{
		GAM_DEBUG(DEBUG_INFO, "poll-basic: not enough time passed for %s\n", path);
		return 0;
	} else {
		GAM_DEBUG(DEBUG_INFO, "poll-basic: %d && %d < %d\n", node->lasttime, gam_poll_generic_get_delta_time (node->lasttime), node->poll_time);
	}

#ifdef VERBOSE_POLL
	GAM_DEBUG(DEBUG_INFO, "Poll: poll_file for %s called\n", path);
#endif
	memset(&sbuf, 0, sizeof(struct stat));
	if (node->lasttime == 0) 
	{
#ifdef VERBOSE_POLL
		GAM_DEBUG(DEBUG_INFO, "Poll: file is new\n");
#endif
		stat_ret = stat(node->path, &sbuf);

		if (stat_ret != 0)
			gam_node_set_pflag (node, MON_MISSING);
		else
			gam_node_set_is_dir(node, (S_ISDIR(sbuf.st_mode) != 0));

		memcpy(&(node->sbuf), &(sbuf), sizeof(struct stat));

		node->lasttime = gam_poll_generic_get_time ();

		if (stat_ret == 0)
			return 0;
		else
			return GAMIN_EVENT_DELETED;
	}

	event = 0;
	node->lasttime = gam_poll_generic_get_time ();

	stat_ret = stat(node->path, &sbuf);
	if (stat_ret != 0) {
		if ((gam_errno() == ENOENT) && (!gam_node_has_pflag(node, MON_MISSING))) {
			/* deleted */
			gam_node_set_pflags (node, MON_MISSING);

			gam_poll_generic_remove_busy(node);
			gam_poll_generic_add_missing(node);
			event = GAMIN_EVENT_DELETED;
		}
	} else if (gam_node_has_pflag (node, MON_MISSING)) {
		/* created */
		gam_node_unset_pflag (node, MON_MISSING);
		event = GAMIN_EVENT_CREATED;
		gam_poll_generic_remove_missing (node);
	} else if (gam_poll_generic_node_changed (node, sbuf)) {
		event = GAMIN_EVENT_CHANGED;
	} else {
#ifdef VERBOSE_POLL
		GAM_DEBUG(DEBUG_INFO, "Poll: poll_file %s unchanged\n", path);
#ifdef ST_MTIM_NSEC
		GAM_DEBUG(DEBUG_INFO, "%d %d : %d %d\n", node->sbuf.st_mtim.tv_sec, node->sbuf.st_mtim.tv_nsec, sbuf.st_mtim.tv_sec, sbuf.st_mtim.tv_nsec);
#else
		GAM_DEBUG(DEBUG_INFO, "%d : %d\n", node->sbuf.st_mtime, sbuf.st_mtim.tv_nsec);
#endif /* ST_MTIM_NSEC */
#endif /* VERBOSE_POLL */
	}

	/*
	* TODO: handle the case where a file/dir is removed and replaced by
	*       a dir/file
	*/
	if (stat_ret == 0)
		gam_node_set_is_dir(node, (S_ISDIR(sbuf.st_mode) != 0));

	memcpy(&(node->sbuf), &(sbuf), sizeof(struct stat));
	node->sbuf.st_mtime = sbuf.st_mtime; // VALGRIND!

	return event;
}

static gboolean
gam_poll_basic_scan_callback(gpointer data)
{
	int idx;
	gboolean did_something = FALSE;

	gam_poll_generic_update_time ();

	for (idx = 0;; idx++) 
	{
		/*
		 * do not simply walk the list as it may be modified in the callback
		 */
		GamNode *node = (GamNode *) g_list_nth_data(gam_poll_generic_get_all_list(), idx);

		if (node == NULL)
			break;

		g_assert (node);

		did_something = TRUE;
		
		if (node->is_dir) {
			gam_poll_generic_scan_directory_internal(node);
		} else {
			GaminEventType event = gam_poll_basic_poll_file (node);
			gam_node_emit_event(node, event);
		}
	}

	/*
	* do not simply walk the list as it may be modified in the callback
	*/
	for (idx = 0;; idx++)
	{
		GamNode *node = g_list_nth_data(gam_poll_generic_get_missing_list(), idx);

		if (node == NULL)
			break;

		g_assert (node);

		did_something = TRUE;
		
#ifdef VERBOSE_POLL
		GAM_DEBUG(DEBUG_INFO, "Checking missing file %s\n", node->path);
#endif
		if (node->is_dir) {
			gam_poll_generic_scan_directory_internal(node);
		} else {
			GaminEventType event = gam_poll_basic_poll_file (node);
			gam_node_emit_event(node, event);
		}

		/*
		* if the resource exists again and is not in a special monitoring
		* mode then switch back to dnotify for monitoring.
		*/
		if (!gam_node_has_pflags (node, MON_MISSING)) 
		{
			gam_poll_generic_remove_missing(node);
			gam_poll_generic_add (node);
		}
	}

	if (!did_something) {
	  scan_callback_running = FALSE;
	  return FALSE;
	}
	
	return TRUE;
}