File: gam_node.c

package info (click to toggle)
gamin 0.1.10-5
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 3,784 kB
  • ctags: 1,921
  • sloc: ansic: 10,675; sh: 8,915; python: 3,706; xml: 1,303; makefile: 319; awk: 48
file content (380 lines) | stat: -rw-r--r-- 8,036 bytes parent folder | download | duplicates (3)
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
/* Gamin
 * Copyright (C) 2003 James Willcox, Corey Bowers
 *
 * 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 <string.h>
#include <glib.h>
#include "gam_event.h"
#include "gam_node.h"
#include "gam_error.h"

/**
 * Create a new node
 *
 * @param path the path the node will represent
 * @param sub an initial GamSubscription for the node, could be NULL
 * @param is_dir whether the node is a directory or not
 * @returns the new node
 */
GamNode *
gam_node_new(const char *path, GamSubscription * sub, gboolean is_dir)
{
    GamNode *node;
    node = g_new0(GamNode, 1);

    node->path = g_strdup(path);
    if (sub)
        node->subs = g_list_prepend(NULL, sub);
    else
        node->subs = NULL;

    node->is_dir = is_dir;
    node->flags = 0;
	node->checks = 0;

    node->poll_time = gam_fs_get_poll_timeout (path);
    node->mon_type = gam_fs_get_mon_type (path);
	GAM_DEBUG(DEBUG_INFO, "g_n_n: node for %s using %s with poll timeout of %d\n", path, node->mon_type == GFS_MT_KERNEL ? "kernel" : "poll", node->poll_time);
    return node;
}

/**
 * Frees a node
 *
 * @param node the node
 */
void
gam_node_free(GamNode * node)
{
    g_assert(node != NULL);
    g_assert(node->subs == NULL);

    g_free(node->path);
    g_free(node);
}

/**
 * Retrieves the parent of a given node
 *
 * @param node the node
 * @returns the parent, or NULL if node has no parent
 */
GamNode *
gam_node_parent(GamNode * node)
{
    GamNode *ret = NULL;

    g_assert(node);

    if (node->node && node->node->parent)
        ret = (GamNode *) node->node->parent->data;

    return ret;
}

/**
 * Returns whether a node is a directory or not
 *
 * @param node the node
 * @returns TRUE if the node is a directory, FALSE otherwise
 */
gboolean
gam_node_is_dir(GamNode * node)
{
    g_assert(node);
    return(node->is_dir);
}

/**
 * Sets whether a node is a directory
 *
 * @param node the node
 * @param is_dir whether the node is a directory
 */
void
gam_node_set_is_dir(GamNode * node, gboolean is_dir)
{
    g_assert(node);
    node->is_dir = is_dir;
}

/**
 * Returns the path associated with a node
 *
 * @param node the node
 * @returns the path.  The caller must keep a reference to node until
 * it has finished with the string.  If it must keep it longer, it
 * should makes its own copy.  The returned string must not be freed.
 */
const char *
gam_node_get_path(GamNode * node)
{
    g_assert(node);
    return node->path;
}

/**
 * Returns the list of subscriptions to a node
 *
 * @param node the node
 * @returns the list of #GamSubscription to the node
 */
GList *
gam_node_get_subscriptions(GamNode * node)
{
    g_assert(node);
    return node->subs;
}

/**
 * Returns whether a node has any directory subscriptions
 *
 * @param node the node
 * @returns TRUE if the node has directory subscriptions, FALSE otherwise
 */
gboolean
gam_node_has_dir_subscriptions(GamNode * node)
{
    GList *s;

    g_assert (node);
    if (!(node->is_dir))
        return(FALSE);
    for (s = node->subs;s != NULL;s = s->next) {
        if (gam_subscription_is_dir((GamSubscription *) s->data))
	    return(TRUE);
    }
    return(FALSE);
}

/**
 * Adds a subscription to a node
 *
 * @param node the node
 * @param sub the subscription
 * @returns TRUE on success, FALSE otherwise
 */
gboolean
gam_node_add_subscription(GamNode * node, GamSubscription * sub)
{
    g_assert(node);
    g_assert(sub);
    g_assert(!g_list_find(node->subs, sub));

    node->subs = g_list_prepend(node->subs, sub);

    return TRUE;
}

/**
 * Removes a subscription from a node
 *
 * @param node the node
 * @param sub the subscription to remove
 * @returns TRUE if the subscription was removed, FALSE otherwise
 */
gboolean
gam_node_remove_subscription(GamNode * node, GamSubscription * sub)
{
    g_assert(node);
    g_assert(g_list_find (node->subs, sub));

    node->subs = g_list_remove_all(node->subs, sub);

    return TRUE;
}

/**
 * Sets the GNode associated with a node.  Should only be used by MdTree
 *
 * @param node the node
 * @param gnode the GNode
 */
void
gam_node_set_node(GamNode * node, GNode * gnode)
{
    g_assert(node);
    node->node = gnode;
}

/**
 * Gets the GNode associated with a node.  Should only be used by MdTree
 *
 * @param node the node
 * @returns the GNode
 */
GNode *
gam_node_get_node(GamNode * node)
{
    g_assert(node);
    return node->node;
}

/**
 * Set a flag on a node
 *
 * @param node the node
 * @param flag the flag to set
 */
void
gam_node_set_flag(GamNode * node, int flag)
{
    g_assert(node);
    /* Make sure we set exactly one flag.  */
    g_assert((flag & (flag - 1)) == 0);
    node->flags |= flag;
}

/**
 * Clears a flag from a node
 *
 * @param node the node
 * @param flag the flag
 */
void
gam_node_unset_flag(GamNode * node, int flag)
{
    g_assert(node);
    /* Make sure we clear exactly one flag.  */
    g_assert((flag & (flag - 1)) == 0);
    node->flags &= ~flag;
}

/**
 * Checks whether a flag is set on a node
 *
 * @param node the node
 * @param flag the flag
 * @returns TRUE if the flag is set, FALSE otherwise
 */
gboolean
gam_node_has_flag(GamNode * node, int flag)
{
    g_assert(node);
    /* Check exactly one flag.  */
    g_assert((flag & (flag - 1)) == 0);
    return node->flags & flag;
}

/**
 * Set a pflag on a node
 *
 * @param node the node
 * @param flag the flag to set
 */
void
gam_node_set_pflag(GamNode * node, int flag)
{
    g_assert(node);
    /* Make sure we set exactly one flag.  */
    g_assert((flag & (flag - 1)) == 0);
    node->pflags |= flag;
}

/**
 * Clears a pflag from a node
 *
 * @param node the node
 * @param flag the flag
 */
void
gam_node_unset_pflag(GamNode * node, int flag)
{
    g_assert(node);
    /* Make sure we clear exactly one flag.  */
    g_assert((flag & (flag - 1)) == 0);
    node->pflags &= ~flag;
}

/**
 * Checks whether a pflag is set on a node
 *
 * @param node the node
 * @param flag the flag
 * @returns TRUE if the flag is set, FALSE otherwise
 */
gboolean
gam_node_has_pflag(GamNode * node, int flag)
{
    g_assert(node);
    /* Check exactly one flag.  */
    g_assert((flag & (flag - 1)) == 0);
    return node->pflags & flag;
}

/**
 * Set the pflags on a node
 *
 * @param node the node
 * @param flags the flags
 */
void
gam_node_set_pflags(GamNode * node, int flags)
{
    g_assert(node);
    node->pflags = flags;
}


/**
 * Checks whether some pflags are set on a node
 *
 * @param node the node
 * @param flags the flags
 * @returns TRUE if the flag is set, FALSE otherwise
 */
gboolean
gam_node_has_pflags(GamNode * node, int flags)
{
    g_assert(node);
    return node->pflags & flags;
}

void
gam_node_emit_event (GamNode *node, GaminEventType event)
{
	GList *l;
	GamNode *parent;
	GList *subs;
	int is_dir_node = gam_node_is_dir(node);

#ifdef VERBOSE_POLL
	GAM_DEBUG(DEBUG_INFO, "Poll: emit events %d for %s\n", event, gam_node_get_path(node));
#endif
	subs = gam_node_get_subscriptions(node);

	if (subs)
		subs = g_list_copy(subs);

	parent = gam_node_parent(node);
	if (parent) {
		GList *parent_subs = gam_node_get_subscriptions(parent);

		for (l = parent_subs; l; l = l->next) {
			if (!g_list_find(subs, l->data))
				subs = g_list_prepend(subs, l->data);
		}
	}

	gam_server_emit_event(gam_node_get_path(node), is_dir_node, event, subs, 0);

	g_list_free(subs);
}

/** @} */