File: gam_subscription.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 (355 lines) | stat: -rw-r--r-- 8,104 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
/* Marmot
 * 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 <sys/types.h>
#include <string.h>
#include <glib.h>
#include "gam_event.h"
#include "gam_listener.h"
#include "gam_subscription.h"
#include "gam_protocol.h"
#include "gam_event.h"
#include "gam_error.h"

//#define GAM_SUB_VERBOSE

struct _GamSubscription {
    char *path;
    int events;
    int reqno;
    int pathlen;
    int options;

    gboolean is_dir;
    gboolean cancelled;

    GamListener *listener;
};


/**
 * @defgroup GamSubscription GamSubscription
 * @ingroup Daemon
 * @brief GamSubscription API.
 *
 * A #GamSubscription represents a single monitoring request (or "subscription").
 *
 * @{
 */

/**
 * Creates a new GamSubscription
 *
 * @param path the path to be monitored
 * @param events the events that are accepted
 * @param is_dir whether the subscription is for a directory or not
 * @returns the new GamSubscription
 */
GamSubscription *
gam_subscription_new(const char *path,
                     int events,
                     int reqno,
                     gboolean is_dir,
		     int options)
{
    GamSubscription *sub;

    sub = g_new0(GamSubscription, 1);
    sub->path = g_strdup(path);
    sub->events = events;
    sub->reqno = reqno;
    sub->pathlen = strlen(path);

    /* everyone accepts this */
    gam_subscription_set_event(sub, GAMIN_EVENT_EXISTS | GAMIN_EVENT_ENDEXISTS);

    sub->is_dir = is_dir;
    sub->options = options;

#ifdef GAM_SUB_VERBOSE
    GAM_DEBUG(DEBUG_INFO, "Created subscription for %s\n", path);
#endif
    return sub;
}

/**
 * Frees a GamSubscription
 *
 * @param sub the GamSubscription
 */
void
gam_subscription_free(GamSubscription * sub)
{
    if (sub == NULL)
        return;
#ifdef GAM_SUB_VERBOSE
    GAM_DEBUG(DEBUG_INFO, "Freeing subscription for %s\n", sub->path);
#endif

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

/**
 * Tells if a GamSubscription is for a directory or not
 *
 * @param sub the GamSubscription
 * @returns TRUE if the subscription is for a directory, FALSE otherwise
 */
gboolean
gam_subscription_is_dir(GamSubscription * sub)
{
    if (sub == NULL)
        return(FALSE);
    return sub->is_dir;
}

/**
 * Provide the path len for a GamSubscription
 *
 * @param sub the GamSubscription
 * @returns the path len for the subscription
 */
int
gam_subscription_pathlen(GamSubscription * sub)
{
    if (sub == NULL)
        return(-1);
    return sub->pathlen;
}

/**
 * Gets the path for a GamSubscription
 *
 * @param sub the GamSubscription
 * @returns The path being monitored.  It should not be freed.
 */
const char *
gam_subscription_get_path(GamSubscription * sub)
{
    if (sub == NULL)
        return(NULL);
    return sub->path;
}

/**
 * Gets the request number for a GamSubscription
 *
 * @param sub the GamSubscription
 * @returns The request number
 */
int
gam_subscription_get_reqno(GamSubscription * sub)
{
    if (sub == NULL)
        return(-1);
    return sub->reqno;
}

/**
 * Gets the GamListener which owns this GamSubscription
 *
 * @param sub the GamSubscription
 * @returns the GamListener, or NULL
 */
GamListener *
gam_subscription_get_listener(GamSubscription * sub)
{
    if (sub == NULL)
        return(NULL);
    return sub->listener;
}

/**
 * Sets the GamListener which is owned by this GamSubscription
 *
 * @param sub the GamSubscription
 * @param listener the GamListener
 */
void
gam_subscription_set_listener(GamSubscription * sub,
                              GamListener * listener)
{
    if (sub == NULL)
        return;

	GAM_DEBUG(DEBUG_INFO, "%s listening for %s\n", gam_listener_get_pidname (listener), sub->path);
    sub->listener = listener;
}

/**
 * Set the events this GamSubscription is interested in
 *
 * @param sub the GamSubscription
 * @param event an ORed combination of the events desired
 */
void
gam_subscription_set_event(GamSubscription * sub, int event)
{
    if (sub == NULL)
        return;
    sub->events |= event;
}

/**
 * Removes an event from the set of acceptable events
 *
 * @param sub the GamSubscription
 * @param event the event to remove
 */
void
gam_subscription_unset_event(GamSubscription * sub, int event)
{
    if (sub == NULL)
        return;
    sub->events &= ~event;
}

/**
 *  
 * @param sub the GamSubscription
 * @param event the event to test for
 * @returns Whether or not this subscription accepts a given event
 */
gboolean
gam_subscription_has_event(GamSubscription * sub, int event)
{
    if (sub == NULL)
        return(FALSE);
    return((sub->events & event) != 0);
}

/**
 *  
 * @param sub the GamSubscription
 * @option option
 * @returns Whether or not this subscription has that option.
 */
gboolean
gam_subscription_has_option(GamSubscription * sub, int option)
{
    if (sub == NULL)
        return(FALSE);
    return((sub->options & option) != 0);
}

/**
 * Mark this GamSubscription as cancelled
 *
 * @param sub the GamSubscription
 */
void
gam_subscription_cancel(GamSubscription * sub)
{
    if (sub == NULL)
        return;
	GAM_DEBUG(DEBUG_INFO, "%s not listening for %s\n", gam_listener_get_pidname (sub->listener), sub->path);
    sub->cancelled = TRUE;
}

/**
 * Checks if the GamSubscription is cancelled or not
 *
 * @param sub the GamSubscription
 * @returns TRUE if the GamSubscription is cancelled, FALSE otherwise
 */
gboolean
gam_subscription_is_cancelled(GamSubscription * sub)
{
    if (sub == NULL)
        return(TRUE);
    return sub->cancelled == TRUE;
}

/**
 * gam_subscription_wants_event:
 * @sub: the GamSubscription
 * @name: file name (just the base name, not the complete path)
 * @is_dir_node: is the target a directory
 * @event: the event
 * @force: force the event as much as possible
 *
 * Checks if a given path/event combination is accepted by this GamSubscription
 *
 * Returns TRUE if the combination is accepted, FALSE otherwise
 */
gboolean
gam_subscription_wants_event(GamSubscription * sub,
                             const char *name, int is_dir_node, 
			     GaminEventType event, int force)
{
    int same_path = 0;

    if ((sub == NULL) || (name == NULL) || (event == 0))
        return(FALSE);
    if (sub->cancelled)
        return FALSE;

    if ((sub->options & GAM_OPT_NOEXISTS) &&
        ((event == GAMIN_EVENT_EXISTS) ||
	 (event == GAMIN_EVENT_ENDEXISTS)))
	return FALSE;

    /* only directory listening cares for other files */
    same_path = !strcmp(name, sub->path);
    if ((sub->is_dir == 0) && (!same_path))
        return(FALSE);

    if (!gam_subscription_has_event(sub, event)) {
        return FALSE;
    }

    if (force)
        return TRUE;
    if ((sub->is_dir) && (is_dir_node) && (same_path)) {
        if ((event == GAMIN_EVENT_EXISTS) ||
	    (event == GAMIN_EVENT_CHANGED) ||
	    (event == GAMIN_EVENT_ENDEXISTS))
	    return FALSE;
    }

    return TRUE;
}

/**
 * gam_subscription_debug:
 * @sub: the subscription
 *
 * Provide debug output for that subscription node/state
 */
void
gam_subscription_debug(GamSubscription *sub) {
#ifdef GAM_DEBUG_ENABLED
    if (sub == NULL) {
	GAM_DEBUG(DEBUG_INFO, "    Subscription is NULL\n");
        return;
    }
    GAM_DEBUG(DEBUG_INFO,
              "    Subscription %d reqno %d events %d dir %d: %s\n",
              sub->reqno, sub->events, sub->events, sub->is_dir, sub->path);
#endif
}

void
gam_subscription_shutdown ()
{
}

/** @} */