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