Make this build with gcc 3.4 (#509419)
[platform/upstream/glib.git] / gio / fam / fam-helper.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Authors: Alexander Larsson <alexl@redhat.com>
21  *          John McCutchan <john@johnmccutchan.com> 
22  *          Sebastian Dröge <slomo@circular-chaos.org>
23  */
24
25 #include "config.h"
26 #include <fam.h>
27 #include <gio/gfilemonitor.h>
28
29 #include "fam-helper.h"
30
31 static FAMConnection* fam_connection = NULL;
32 static gint fam_watch_id = 0;
33 G_LOCK_DEFINE_STATIC(fam_connection);
34
35 struct _fam_sub
36 {
37   gchar *pathname;
38   gboolean directory;
39   gpointer user_data;
40   gboolean cancelled;
41   FAMRequest request;
42 };
43
44 static GFileMonitorEvent 
45 fam_event_to_file_monitor_event (FAMCodes code)
46 {
47   switch (code)
48     {
49     case FAMChanged:
50       return G_FILE_MONITOR_EVENT_CHANGED;
51       break;
52     case FAMDeleted:
53       return G_FILE_MONITOR_EVENT_DELETED;
54       break;
55     case FAMCreated:
56       return G_FILE_MONITOR_EVENT_CREATED;
57       break;
58     default:
59       return -1;
60       break;
61     }
62 }
63
64 static gboolean
65 fam_do_iter_unlocked (void)
66 {
67   while (fam_connection != NULL && FAMPending (fam_connection)) {
68     FAMEvent ev;
69     fam_sub* sub = NULL;
70     gboolean cancelled;
71     
72     if (FAMNextEvent (fam_connection, &ev) != 1) {
73       FAMClose (fam_connection);
74       g_free (fam_connection);
75       g_source_remove (fam_watch_id);
76       fam_watch_id = 0;
77       fam_connection = NULL;
78       return FALSE;
79     }
80     
81     sub = (fam_sub*)ev.userdata;
82     cancelled = sub->cancelled;
83     if (ev.code == FAMAcknowledge && cancelled)
84       {
85         _fam_sub_free (sub);
86         continue;
87       }
88     
89     if (cancelled)
90       continue;
91     
92     if (sub->directory)
93       {
94         GFileMonitor* monitor = G_FILE_MONITOR (sub->user_data);
95         GFileMonitorEvent eflags = fam_event_to_file_monitor_event (ev.code);
96         gchar* path = NULL;
97         GFile *child, *parent;
98         
99         /* unsupported event */
100         if (eflags == -1)
101           continue;
102         
103         if (ev.filename[0] == '/')
104           path = g_strdup (ev.filename);
105         else
106           path = g_strdup_printf ("%s/%s", sub->pathname, ev.filename);
107
108         child = g_file_new_for_path (path);
109         parent = g_file_get_parent (child);
110         g_file_monitor_emit_event (monitor, child, NULL, eflags);
111         g_free (path);
112         g_object_unref (child);
113         g_object_unref (parent);
114       } else {
115         GFile *child;
116         GFileMonitor* monitor = G_FILE_MONITOR (sub->user_data);
117         GFileMonitorEvent eflags = fam_event_to_file_monitor_event (ev.code);
118         gchar* path = NULL;
119       
120         if (eflags == -1)
121           continue;
122         path = g_strdup (ev.filename);
123         child = g_file_new_for_path (path);
124         g_file_monitor_emit_event (monitor, child, NULL, eflags);
125         g_free (path);
126         g_object_unref (child);
127       }
128   }
129   
130   return TRUE;
131 }
132
133 static gboolean
134 fam_callback (GIOChannel *source,
135               GIOCondition condition,
136               gpointer data)
137 {
138   gboolean res;
139   G_LOCK (fam_connection);
140   
141   res = fam_do_iter_unlocked ();
142   
143   G_UNLOCK (fam_connection);
144   return res;
145 }
146
147 gboolean
148 _fam_sub_startup (void)
149 {
150   GIOChannel *ioc;
151   
152   G_LOCK (fam_connection);
153   
154   if (fam_connection == NULL) {
155     fam_connection = g_new0 (FAMConnection, 1);
156     if (FAMOpen2 (fam_connection, "gvfs user") != 0) {
157       g_warning ("FAMOpen failed, FAMErrno=%d\n", FAMErrno);
158       g_free (fam_connection);
159       fam_connection = NULL;
160       G_UNLOCK (fam_connection);
161       return FALSE;
162     }
163 #ifdef HAVE_FAM_NO_EXISTS
164     /* This is a gamin extension that avoids sending all the Exists event for dir monitors */
165     FAMNoExists (fam_connection);
166 #endif
167     ioc = g_io_channel_unix_new (FAMCONNECTION_GETFD(fam_connection));
168     fam_watch_id = g_io_add_watch (ioc,
169                                    G_IO_IN | G_IO_HUP | G_IO_ERR,
170                                    fam_callback, fam_connection);
171     g_io_channel_unref (ioc);
172   }
173   
174   G_UNLOCK (fam_connection);
175   
176   return TRUE;
177 }
178
179 fam_sub*
180 _fam_sub_add (const gchar* pathname,
181               gboolean directory,
182               gpointer user_data)
183 {
184   fam_sub *sub;
185
186   if (!_fam_sub_startup ())
187     return NULL;
188   
189   sub = g_new0 (fam_sub, 1);
190   sub->pathname = g_strdup (pathname);
191   sub->directory = directory;
192   sub->user_data = user_data;
193   
194   G_LOCK (fam_connection);
195   /* We need to queue up incoming messages to avoid blocking on write
196    *  if there are many monitors being canceled */
197   fam_do_iter_unlocked ();
198   
199   if (fam_connection == NULL) {
200     G_UNLOCK (fam_connection);
201     return NULL;
202   }
203   
204   if (directory)
205     FAMMonitorDirectory (fam_connection, pathname, &sub->request, sub);
206   else
207     FAMMonitorFile (fam_connection, pathname, &sub->request, sub);
208   
209   G_UNLOCK (fam_connection);
210   return sub;
211 }
212
213 gboolean
214 _fam_sub_cancel (fam_sub* sub)
215 {
216   if (sub->cancelled)
217     return TRUE;
218   
219   sub->cancelled = TRUE;
220   
221   G_LOCK (fam_connection);
222   /* We need to queue up incoming messages to avoid blocking on write
223    *  if there are many monitors being canceled */
224   fam_do_iter_unlocked ();
225   
226   if (fam_connection == NULL) {
227     G_UNLOCK (fam_connection);
228     return FALSE;
229   }
230   
231   FAMCancelMonitor (fam_connection, &sub->request);
232   
233   G_UNLOCK (fam_connection);
234   
235   return TRUE;
236 }
237
238 void
239 _fam_sub_free (fam_sub* sub)
240 {
241   g_free (sub->pathname);
242   g_free (sub);
243 }
244