gio/: fully remove gioalias hacks
[platform/upstream/glib.git] / gio / inotify / inotify-helper.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
2
3 /* inotify-helper.c - GVFS Monitor based on inotify.
4
5    Copyright (C) 2007 John McCutchan
6
7    The Gnome Library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11
12    The Gnome Library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public
18    License along with the Gnome Library; see the file COPYING.LIB.  If not,
19    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.
21
22    Authors: 
23                  John McCutchan <john@johnmccutchan.com>
24 */
25
26 #include "config.h"
27 #include <errno.h>
28 #include <time.h>
29 #include <string.h>
30 #include <sys/ioctl.h>
31 /* Just include the local header to stop all the pain */
32 #include <sys/inotify.h>
33 #include <gio/glocalfile.h>
34 #include <gio/gfilemonitor.h>
35 #include <gio/gfile.h>
36 #include "inotify-helper.h"
37 #include "inotify-missing.h"
38 #include "inotify-path.h"
39 #include "inotify-diag.h"
40
41 static gboolean ih_debug_enabled = FALSE;
42 #define IH_W if (ih_debug_enabled) g_warning 
43
44 static void ih_event_callback (ik_event_t *event, inotify_sub *sub);
45 static void ih_not_missing_callback (inotify_sub *sub);
46
47 /* We share this lock with inotify-kernel.c and inotify-missing.c
48  *
49  * inotify-kernel.c takes the lock when it reads events from
50  * the kernel and when it processes those events
51  *
52  * inotify-missing.c takes the lock when it is scanning the missing
53  * list.
54  *
55  * We take the lock in all public functions
56  */
57 G_GNUC_INTERNAL G_LOCK_DEFINE (inotify_lock);
58
59 static GFileMonitorEvent ih_mask_to_EventFlags (guint32 mask);
60
61 /**
62  * _ih_startup:
63  *
64  * Initializes the inotify backend.  This must be called before
65  * any other functions in this module.
66  *
67  * Return value: #TRUE if initialization succeeded, #FALSE otherwise
68  */
69 gboolean
70 _ih_startup (void)
71 {
72   static gboolean initialized = FALSE;
73   static gboolean result = FALSE;
74   
75   G_LOCK (inotify_lock);
76   
77   if (initialized == TRUE)
78     {
79       G_UNLOCK (inotify_lock);
80       return result;
81     }
82
83   result = _ip_startup (ih_event_callback);
84   if (!result)
85     {
86       G_UNLOCK (inotify_lock);
87       return FALSE;
88     }
89   _im_startup (ih_not_missing_callback);
90   _id_startup ();
91
92   IH_W ("started gvfs inotify backend\n");
93   
94   initialized = TRUE;
95   
96   G_UNLOCK (inotify_lock);
97   
98   return TRUE;
99 }
100
101 /*
102  * Adds a subscription to be monitored.
103  */
104 gboolean
105 _ih_sub_add (inotify_sub *sub)
106 {
107   G_LOCK (inotify_lock);
108         
109   if (!_ip_start_watching (sub))
110     _im_add (sub);
111   
112   G_UNLOCK (inotify_lock);
113   return TRUE;
114 }
115
116 /*
117  * Cancels a subscription which was being monitored.
118  */
119 gboolean
120 _ih_sub_cancel (inotify_sub *sub)
121 {
122   G_LOCK (inotify_lock);
123
124   if (!sub->cancelled)
125     {
126       IH_W ("cancelling %s\n", sub->dirname);
127       sub->cancelled = TRUE;
128       _im_rm (sub);
129       _ip_stop_watching (sub);
130     }
131   
132   G_UNLOCK (inotify_lock);
133   
134   return TRUE;
135 }
136
137 static char *
138 _ih_fullpath_from_event (ik_event_t *event, char *dirname)
139 {
140   char *fullpath;
141
142   if (event->name)
143     fullpath = g_strdup_printf ("%s/%s", dirname, event->name);
144   else
145     fullpath = g_strdup_printf ("%s/", dirname);
146
147    return fullpath;
148 }
149
150
151 static gboolean
152 ih_event_is_paired_move (ik_event_t *event)
153 {
154   if (event->pair)
155     {
156       ik_event_t *paired = event->pair;
157       /* intofiy(7): IN_MOVE == IN_MOVED_FROM | IN_MOVED_TO */
158       return (event->mask | paired->mask) & IN_MOVE;
159     }
160
161     return FALSE;
162 }
163
164 static void
165 ih_event_callback (ik_event_t  *event, 
166                    inotify_sub *sub)
167 {
168   gchar *fullpath;
169   GFileMonitorEvent eflags;
170   GFile* parent;
171   GFile* child;
172   GFile* other;
173   
174   eflags = ih_mask_to_EventFlags (event->mask);
175   parent = g_file_new_for_path (sub->dirname);
176   fullpath = _ih_fullpath_from_event (event, sub->dirname);
177   child = g_file_new_for_path (fullpath);
178   g_free (fullpath);
179
180   if (ih_event_is_paired_move (event) && sub->pair_moves)
181     {
182       char *parent_dir = (char *) _ip_get_path_for_wd (event->pair->wd);
183       fullpath = _ih_fullpath_from_event (event->pair, parent_dir);
184       other = g_file_new_for_path (fullpath);
185       g_free (fullpath);
186       eflags = G_FILE_MONITOR_EVENT_MOVED;
187       event->pair = NULL; /* prevents the paired event to be emitted as well */
188     }
189   else
190     other = NULL;
191
192   g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
193                              child, other, eflags);
194
195   g_object_unref (child);
196   g_object_unref (parent);
197   if (other)
198     g_object_unref (other);
199 }
200
201 static void
202 ih_not_missing_callback (inotify_sub *sub)
203 {
204   gchar *fullpath;
205   GFileMonitorEvent eflags;
206   guint32 mask;
207   GFile* parent;
208   GFile* child;
209   
210   parent = g_file_new_for_path (sub->dirname);
211
212   if (sub->filename)
213     {
214       fullpath = g_strdup_printf ("%s/%s", sub->dirname, sub->filename);
215       g_warning ("Missing callback called fullpath = %s\n", fullpath);
216       if (!g_file_test (fullpath, G_FILE_TEST_EXISTS))
217         {
218           g_free (fullpath);
219           return;
220         }
221       mask = IN_CREATE;
222     }
223   else
224     {
225       fullpath = g_strdup_printf ("%s", sub->dirname);
226       mask = IN_CREATE|IN_ISDIR;
227     }
228
229   eflags = ih_mask_to_EventFlags (mask);
230   child = g_file_new_for_path (fullpath);
231   g_free (fullpath);
232
233   g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
234                              child, NULL, eflags);
235
236   g_object_unref (child);
237   g_object_unref (parent);
238 }
239
240 /* Transforms a inotify event to a GVFS event. */
241 static GFileMonitorEvent
242 ih_mask_to_EventFlags (guint32 mask)
243 {
244   mask &= ~IN_ISDIR;
245   switch (mask)
246     {
247     case IN_MODIFY:
248       return G_FILE_MONITOR_EVENT_CHANGED;
249     case IN_CLOSE_WRITE:
250       return G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT;
251     case IN_ATTRIB:
252       return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
253     case IN_MOVE_SELF:
254     case IN_MOVED_FROM:
255     case IN_DELETE:
256     case IN_DELETE_SELF:
257       return G_FILE_MONITOR_EVENT_DELETED;
258     case IN_CREATE:
259     case IN_MOVED_TO:
260       return G_FILE_MONITOR_EVENT_CREATED;
261     case IN_UNMOUNT:
262       return G_FILE_MONITOR_EVENT_UNMOUNTED;
263     case IN_Q_OVERFLOW:
264     case IN_OPEN:
265     case IN_CLOSE_NOWRITE:
266     case IN_ACCESS:
267     case IN_IGNORED:
268     default:
269       return -1;
270     }
271 }