Removed unnecessary file
[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 "local_inotify.h"
33 #if 0
34 #ifdef HAVE_SYS_INOTIFY_H
35 /* We don't actually include the libc header, because there has been
36  * problems with libc versions that was built without inotify support.
37  * Instead we use the local version.
38  */
39 #include "local_inotify.h"
40 #elif defined (HAVE_LINUX_INOTIFY_H)
41 #include <linux/inotify.h>
42 #endif
43 #endif
44 #include <gio/glocalfile.h>
45 #include <gio/gfilemonitor.h>
46 #include <gio/gdirectorymonitor.h>
47 #include "inotify-helper.h"
48 #include "inotify-missing.h"
49 #include "inotify-path.h"
50 #include "inotify-diag.h"
51
52 #include "gioalias.h"
53
54
55 static gboolean ih_debug_enabled = FALSE;
56 #define IH_W if (ih_debug_enabled) g_warning 
57
58 static void ih_event_callback (ik_event_t *event, inotify_sub *sub);
59 static void ih_not_missing_callback (inotify_sub *sub);
60
61 /* We share this lock with inotify-kernel.c and inotify-missing.c
62  *
63  * inotify-kernel.c takes the lock when it reads events from
64  * the kernel and when it processes those events
65  *
66  * inotify-missing.c takes the lock when it is scanning the missing
67  * list.
68  *
69  * We take the lock in all public functions
70  */
71 G_LOCK_DEFINE (inotify_lock);
72
73 static GFileMonitorEvent ih_mask_to_EventFlags (guint32 mask);
74
75 /**
76  * _ih_startup:
77  *
78  * Initializes the inotify backend.  This must be called before
79  * any other functions in this module.
80  *
81  * Return value: #TRUE if initialization succeeded, #FALSE otherwise
82  */
83 gboolean
84 _ih_startup (void)
85 {
86   static gboolean initialized = FALSE;
87   static gboolean result = FALSE;
88   
89   G_LOCK (inotify_lock);
90   
91   if (initialized == TRUE)
92     {
93       G_UNLOCK (inotify_lock);
94       return result;
95     }
96
97   result = _ip_startup (ih_event_callback);
98   if (!result)
99     {
100       g_warning ("Could not initialize inotify\n");
101       G_UNLOCK (inotify_lock);
102       return FALSE;
103     }
104   _im_startup (ih_not_missing_callback);
105   _id_startup ();
106
107   IH_W ("started gvfs inotify backend\n");
108   
109   initialized = TRUE;
110   
111   G_UNLOCK (inotify_lock);
112   
113   return TRUE;
114 }
115
116 /**
117  * Adds a subscription to be monitored.
118  */
119 gboolean
120 _ih_sub_add (inotify_sub * sub)
121 {
122   G_LOCK (inotify_lock);
123         
124   if (!_ip_start_watching (sub))
125     _im_add (sub);
126   
127   G_UNLOCK (inotify_lock);
128   return TRUE;
129 }
130
131 /**
132  * Cancels a subscription which was being monitored.
133  */
134 gboolean
135 _ih_sub_cancel (inotify_sub * sub)
136 {
137   G_LOCK (inotify_lock);
138
139   if (!sub->cancelled)
140     {
141       IH_W ("cancelling %s\n", sub->dirname);
142       sub->cancelled = TRUE;
143       _im_rm (sub);
144       _ip_stop_watching (sub);
145     }
146   
147   G_UNLOCK (inotify_lock);
148   
149   return TRUE;
150 }
151
152
153 static void
154 ih_event_callback (ik_event_t *event, inotify_sub *sub)
155 {
156   gchar *fullpath;
157   GFileMonitorEvent eflags;
158   GFile* parent;
159   GFile* child;
160   
161   eflags = ih_mask_to_EventFlags (event->mask);
162   parent = g_file_new_for_path (sub->dirname);
163   if (event->name)
164     fullpath = g_strdup_printf ("%s/%s", sub->dirname, event->name);
165   else
166     fullpath = g_strdup_printf ("%s/", sub->dirname);
167   
168   child = g_file_new_for_path (fullpath);
169   g_free (fullpath);
170
171   if (G_IS_DIRECTORY_MONITOR (sub->user_data))
172     {
173       GDirectoryMonitor* monitor = G_DIRECTORY_MONITOR (sub->user_data);
174       g_directory_monitor_emit_event (monitor, 
175                                       child, NULL, eflags);
176     }
177   else if (G_IS_FILE_MONITOR (sub->user_data))
178     {
179       GFileMonitor* monitor = G_FILE_MONITOR (sub->user_data);
180       g_file_monitor_emit_event (monitor,
181                                  child, NULL, eflags);
182     }
183
184   g_object_unref (child);
185   g_object_unref (parent);
186 }
187
188 static void
189 ih_not_missing_callback (inotify_sub *sub)
190 {
191   gchar *fullpath;
192   GFileMonitorEvent eflags;
193   guint32 mask;
194   GFile* parent;
195   GFile* child;
196   
197   parent = g_file_new_for_path (sub->dirname);
198
199   if (sub->filename)
200     {
201       fullpath = g_strdup_printf ("%s/%s", sub->dirname, sub->filename);
202       g_warning ("Missing callback called fullpath = %s\n", fullpath);
203       if (!g_file_test (fullpath, G_FILE_TEST_EXISTS))
204         {
205           g_free (fullpath);
206           return;
207         }
208       mask = IN_CREATE;
209     }
210   else
211     {
212       fullpath = g_strdup_printf ("%s", sub->dirname);
213       mask = IN_CREATE|IN_ISDIR;
214     }
215
216   eflags = ih_mask_to_EventFlags (mask);
217   child = g_file_new_for_path (fullpath);
218   g_free (fullpath);
219
220   if (G_IS_DIRECTORY_MONITOR (sub->user_data))
221     {
222       GDirectoryMonitor* monitor = G_DIRECTORY_MONITOR (sub->user_data);
223       g_directory_monitor_emit_event (monitor, child, NULL, eflags);
224     }
225   else if (G_IS_FILE_MONITOR (sub->user_data))
226     {
227       GFileMonitor* monitor = G_FILE_MONITOR (sub->user_data);
228       g_file_monitor_emit_event (monitor,
229                                  child, NULL, eflags);
230     }
231
232   g_object_unref (child);
233   g_object_unref (parent);
234 }
235
236 /* Transforms a inotify event to a GVFS event. */
237 static GFileMonitorEvent
238 ih_mask_to_EventFlags (guint32 mask)
239 {
240   mask &= ~IN_ISDIR;
241   switch (mask)
242     {
243     case IN_MODIFY:
244       return G_FILE_MONITOR_EVENT_CHANGED;
245     case IN_CLOSE_WRITE:
246       return G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT;
247     case IN_ATTRIB:
248       return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
249     case IN_MOVE_SELF:
250     case IN_MOVED_FROM:
251     case IN_DELETE:
252     case IN_DELETE_SELF:
253       return G_FILE_MONITOR_EVENT_DELETED;
254     case IN_CREATE:
255     case IN_MOVED_TO:
256       return G_FILE_MONITOR_EVENT_CREATED;
257     case IN_UNMOUNT:
258       return G_FILE_MONITOR_EVENT_UNMOUNTED;
259     case IN_Q_OVERFLOW:
260     case IN_OPEN:
261     case IN_CLOSE_NOWRITE:
262     case IN_ACCESS:
263     case IN_IGNORED:
264     default:
265       return -1;
266     }
267 }