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