File: inotify-helper.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 (211 lines) | stat: -rw-r--r-- 4,875 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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* inotify-helper.c - Gnome VFS Monitor based on inotify.

   Copyright (C) 2005 John McCutchan

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Authors: 
		 John McCutchan <john@johnmccutchan.com>
*/

#include "config.h"
#include <errno.h>
#include <time.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/inotify.h>
#include "inotify-helper.h"
#include "inotify-missing.h"
#include "inotify-path.h"
#include "inotify-diag.h"

static gboolean		ih_debug_enabled = FALSE;
#define IH_W if (ih_debug_enabled) g_warning 

static void ih_event_callback (ik_event_t *event, ih_sub_t *sub);
static void ih_found_callback (ih_sub_t *sub);

/* We share this lock with inotify-kernel.c and inotify-missing.c
 *
 * inotify-kernel.c takes the lock when it reads events from
 * the kernel and when it processes those events
 *
 * inotify-missing.c takes the lock when it is scanning the missing
 * list.
 * 
 * We take the lock in all public functions 
 */
G_LOCK_DEFINE (inotify_lock);
static GList *sub_list = NULL;
static gboolean initialized = FALSE;
static event_callback_t user_ecb = NULL;
static found_callback_t user_fcb = NULL;

/**
 * Initializes the inotify backend.  This must be called before
 * any other functions in this module.
 *
 * @returns TRUE if initialization succeeded, FALSE otherwise
 */
gboolean
ih_startup (event_callback_t ecb,
	    found_callback_t fcb)
{
	static gboolean result = FALSE;

	G_LOCK(inotify_lock);
	
	if (initialized == TRUE) {
		G_UNLOCK(inotify_lock);
		return result;
	}

	result = ip_startup (ih_event_callback);
	if (!result) {
		g_warning( "Could not initialize inotify\n");
		G_UNLOCK(inotify_lock);
		return FALSE;
	}
	initialized = TRUE;
	user_ecb = ecb;
	user_fcb = fcb;
	im_startup (ih_found_callback);
	id_startup ();

	IH_W ("started gnome-vfs inotify backend\n");

	G_UNLOCK(inotify_lock);
	return TRUE;
}

gboolean
ih_running (void)
{
	return initialized;
}

/**
 * Adds a subscription to be monitored.
 */
gboolean
ih_sub_add (ih_sub_t * sub)
{
	G_LOCK(inotify_lock);
	
	g_assert (g_list_find (sub_list, sub) == NULL);

	// make sure that sub isn't on sub_list first.
	if (!ip_start_watching (sub))
	{
		im_add (sub);
	}

	sub_list = g_list_prepend (sub_list, sub);

	G_UNLOCK(inotify_lock);
	return TRUE;
}

/**
 * Cancels a subscription which was being monitored.
 * inotify_lock must be held when calling.
 */
static gboolean
ih_sub_cancel (ih_sub_t * sub)
{
	if (!sub->cancelled)
	{
		IH_W("cancelling %s\n", sub->pathname);
		g_assert (g_list_find (sub_list, sub) != NULL);
		sub->cancelled = TRUE;
		im_rm (sub);
		ip_stop_watching (sub);
		sub_list = g_list_remove (sub_list, sub);
	}

	return TRUE;
}

static void
ih_sub_foreach_worker (void *callerdata, gboolean (*f)(ih_sub_t *sub, void *callerdata), gboolean free)
{
	GList *l = NULL;
	GList *next = NULL;

	G_LOCK(inotify_lock);

	for (l = sub_list; l; l = next)
	{
		ih_sub_t *sub = l->data;
		next = l->next;
		
		if (f(sub, callerdata))
		{
			ih_sub_cancel (sub); /* Removes sub from sub_list */
			if (free)
				ih_sub_free (sub);
		}
	}

	G_UNLOCK(inotify_lock);
}

void
ih_sub_foreach (void *callerdata, gboolean (*f)(ih_sub_t *sub, void *callerdata))
{
	ih_sub_foreach_worker (callerdata, f, FALSE);
}

void
ih_sub_foreach_free (void *callerdata, gboolean (*f)(ih_sub_t *sub, void *callerdata))
{
	ih_sub_foreach_worker (callerdata, f, TRUE);
}

static void ih_event_callback (ik_event_t *event, ih_sub_t *sub)
{
	gchar *fullpath;
	if (event->name)
	{
		fullpath = g_strdup_printf ("%s/%s", sub->dirname, event->name);
	} else {
		fullpath = g_strdup_printf ("%s/", sub->dirname);
	}

	user_ecb (fullpath, event->mask, sub->usersubdata);
	g_free(fullpath);
}

static void ih_found_callback (ih_sub_t *sub)
{
	gchar *fullpath;

	if (sub->filename)
	{
		fullpath = g_strdup_printf ("%s/%s", sub->dirname, sub->filename);
		if (!g_file_test (fullpath, G_FILE_TEST_EXISTS)) {
			g_free (fullpath);
			return;
		}
	} else {
		fullpath = g_strdup_printf ("%s/", sub->dirname);
	}

	user_fcb (fullpath, sub->usersubdata);
	g_free(fullpath);
}