1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Alexander Larsson <alexl@redhat.com>
21 * John McCutchan <john@johnmccutchan.com>
22 * Sebastian Dröge <slomo@circular-chaos.org>
27 #include <gio/gfilemonitor.h>
30 #include "fam-helper.h"
32 static FAMConnection* fam_connection = NULL;
33 static gint fam_watch_id = 0;
34 G_LOCK_DEFINE_STATIC(fam_connection);
45 /* This uses int as the argument type because the
46 real type differs between implementations:
47 gamin has "typedef enum FAMCodes {....} FAMCodes;"
48 fam has "enum FAMCodes { ... }".
50 static GFileMonitorEvent
51 fam_event_to_file_monitor_event (int code)
56 return G_FILE_MONITOR_EVENT_CHANGED;
59 return G_FILE_MONITOR_EVENT_DELETED;
62 return G_FILE_MONITOR_EVENT_CREATED;
71 fam_do_iter_unlocked (void)
73 while (fam_connection != NULL && FAMPending (fam_connection)) {
78 if (FAMNextEvent (fam_connection, &ev) != 1) {
79 FAMClose (fam_connection);
80 g_free (fam_connection);
81 g_source_remove (fam_watch_id);
83 fam_connection = NULL;
87 sub = (fam_sub*)ev.userdata;
88 cancelled = sub->cancelled;
89 if (ev.code == FAMAcknowledge && cancelled)
100 GFileMonitor* monitor = G_FILE_MONITOR (sub->user_data);
101 GFileMonitorEvent eflags = fam_event_to_file_monitor_event (ev.code);
103 GFile *child, *parent;
105 /* unsupported event */
109 if (ev.filename[0] == '/')
110 path = g_strdup (ev.filename);
112 path = g_strdup_printf ("%s/%s", sub->pathname, ev.filename);
114 child = g_file_new_for_path (path);
115 parent = g_file_get_parent (child);
116 g_file_monitor_emit_event (monitor, child, NULL, eflags);
118 g_object_unref (child);
119 g_object_unref (parent);
122 GFileMonitor* monitor = G_FILE_MONITOR (sub->user_data);
123 GFileMonitorEvent eflags = fam_event_to_file_monitor_event (ev.code);
128 path = g_strdup (ev.filename);
129 child = g_file_new_for_path (path);
130 g_file_monitor_emit_event (monitor, child, NULL, eflags);
132 g_object_unref (child);
140 fam_callback (GIOChannel *source,
141 GIOCondition condition,
145 G_LOCK (fam_connection);
147 res = fam_do_iter_unlocked ();
149 G_UNLOCK (fam_connection);
154 _fam_sub_startup (void)
158 G_LOCK (fam_connection);
160 if (fam_connection == NULL) {
161 fam_connection = g_new0 (FAMConnection, 1);
162 if (FAMOpen2 (fam_connection, "gvfs user") != 0) {
163 g_warning ("FAMOpen failed, FAMErrno=%d\n", FAMErrno);
164 g_free (fam_connection);
165 fam_connection = NULL;
166 G_UNLOCK (fam_connection);
169 #ifdef HAVE_FAM_NO_EXISTS
170 /* This is a gamin extension that avoids sending all the Exists event for dir monitors */
171 FAMNoExists (fam_connection);
173 ioc = g_io_channel_unix_new (FAMCONNECTION_GETFD(fam_connection));
174 fam_watch_id = g_io_add_watch (ioc,
175 G_IO_IN | G_IO_HUP | G_IO_ERR,
176 fam_callback, fam_connection);
177 g_io_channel_unref (ioc);
180 G_UNLOCK (fam_connection);
186 _fam_sub_shutdown (void)
188 G_LOCK (fam_connection);
190 if (fam_connection != NULL) {
191 FAMClose (fam_connection);
192 g_free (fam_connection);
193 g_source_remove (fam_watch_id);
195 fam_connection = NULL;
198 G_UNLOCK (fam_connection);
202 _fam_sub_add (const gchar* pathname,
208 if (!_fam_sub_startup ())
211 sub = g_new0 (fam_sub, 1);
212 sub->pathname = g_strdup (pathname);
213 sub->directory = directory;
214 sub->user_data = user_data;
216 G_LOCK (fam_connection);
217 /* We need to queue up incoming messages to avoid blocking on write
218 * if there are many monitors being canceled */
219 fam_do_iter_unlocked ();
221 if (fam_connection == NULL) {
222 G_UNLOCK (fam_connection);
227 FAMMonitorDirectory (fam_connection, pathname, &sub->request, sub);
229 FAMMonitorFile (fam_connection, pathname, &sub->request, sub);
231 G_UNLOCK (fam_connection);
236 _fam_sub_cancel (fam_sub* sub)
241 sub->cancelled = TRUE;
243 G_LOCK (fam_connection);
244 /* We need to queue up incoming messages to avoid blocking on write
245 * if there are many monitors being canceled */
246 fam_do_iter_unlocked ();
248 if (fam_connection == NULL) {
249 G_UNLOCK (fam_connection);
253 FAMCancelMonitor (fam_connection, &sub->request);
255 G_UNLOCK (fam_connection);
261 _fam_sub_free (fam_sub* sub)
263 g_free (sub->pathname);