Remove a few more G_GNUC_INTERNAL users
[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
40 static gboolean ih_debug_enabled = FALSE;
41 #define IH_W if (ih_debug_enabled) g_warning 
42
43 static void ih_event_callback (ik_event_t  *event,
44                                inotify_sub *sub,
45                                gboolean     file_event);
46 static void ih_not_missing_callback (inotify_sub *sub);
47
48 /* We share this lock with inotify-kernel.c and inotify-missing.c
49  *
50  * inotify-kernel.c takes the lock when it reads events from
51  * the kernel and when it processes those events
52  *
53  * inotify-missing.c takes the lock when it is scanning the missing
54  * list.
55  *
56  * We take the lock in all public functions
57  */
58 G_LOCK_DEFINE (inotify_lock);
59
60 static GFileMonitorEvent ih_mask_to_EventFlags (guint32 mask);
61
62 /**
63  * _ih_startup:
64  *
65  * Initializes the inotify backend.  This must be called before
66  * any other functions in this module.
67  *
68  * Return value: #TRUE if initialization succeeded, #FALSE otherwise
69  */
70 gboolean
71 _ih_startup (void)
72 {
73   static gboolean initialized = FALSE;
74   static gboolean result = FALSE;
75   
76   G_LOCK (inotify_lock);
77   
78   if (initialized == TRUE)
79     {
80       G_UNLOCK (inotify_lock);
81       return result;
82     }
83
84   result = _ip_startup (ih_event_callback);
85   if (!result)
86     {
87       G_UNLOCK (inotify_lock);
88       return FALSE;
89     }
90   _im_startup (ih_not_missing_callback);
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,
139                          const char *dirname,
140                          const char *filename)
141 {
142   char *fullpath;
143
144   if (filename)
145     fullpath = g_strdup_printf ("%s/%s", dirname, filename);
146   else if (event->name)
147     fullpath = g_strdup_printf ("%s/%s", dirname, event->name);
148   else
149     fullpath = g_strdup_printf ("%s/", dirname);
150
151    return fullpath;
152 }
153
154
155 static gboolean
156 ih_event_is_paired_move (ik_event_t *event)
157 {
158   if (event->pair)
159     {
160       ik_event_t *paired = event->pair;
161       /* intofiy(7): IN_MOVE == IN_MOVED_FROM | IN_MOVED_TO */
162       return (event->mask | paired->mask) & IN_MOVE;
163     }
164
165     return FALSE;
166 }
167
168 static void
169 ih_event_callback (ik_event_t  *event, 
170                    inotify_sub *sub,
171                    gboolean     file_event)
172 {
173   gchar *fullpath;
174   GFileMonitorEvent eflags;
175   GFile* child;
176   GFile* other;
177
178   eflags = ih_mask_to_EventFlags (event->mask);
179   fullpath = _ih_fullpath_from_event (event, sub->dirname,
180                                       file_event ? sub->filename : NULL);
181   child = g_file_new_for_path (fullpath);
182   g_free (fullpath);
183
184   if (ih_event_is_paired_move (event) && sub->pair_moves)
185     {
186       const char *parent_dir = (char *) _ip_get_path_for_wd (event->pair->wd);
187       fullpath = _ih_fullpath_from_event (event->pair, parent_dir, NULL);
188       other = g_file_new_for_path (fullpath);
189       g_free (fullpath);
190       eflags = G_FILE_MONITOR_EVENT_MOVED;
191       event->pair = NULL; /* prevents the paired event to be emitted as well */
192     }
193   else
194     other = NULL;
195
196   g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
197                              child, other, eflags);
198
199   /* For paired moves or moves whose mask has been changed from IN_MOVED_TO to
200    * IN_CREATE, notify also that it's probably the last change to the file,
201    * emitting CHANGES_DONE_HINT.
202    * The first (first part of the if's guard below) is the case of a normal
203    * move within the monitored tree and in the same mounted volume.
204    * The latter (second part of the guard) is the case of a move within the
205    * same mounted volume, but from a not monitored directory.
206    *
207    * It's not needed in cases like moves across mounted volumes as the IN_CREATE
208    * will be followed by a IN_MODIFY and IN_CLOSE_WRITE events.
209    * Also not needed if sub->pair_moves is set as EVENT_MOVED will be emitted
210    * instead of EVENT_CREATED which implies no further modification will be
211    * applied to the file
212    * See: https://bugzilla.gnome.org/show_bug.cgi?id=640077
213    */
214   if ((!sub->pair_moves &&
215         event->is_second_in_pair && (event->mask & IN_MOVED_TO)) ||
216       (!ih_event_is_paired_move (event) &&
217        (event->original_mask & IN_MOVED_TO) && (event->mask & IN_CREATE)))
218     {
219       g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
220           child, NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
221     }
222
223   g_object_unref (child);
224   if (other)
225     g_object_unref (other);
226 }
227
228 static void
229 ih_not_missing_callback (inotify_sub *sub)
230 {
231   gchar *fullpath;
232   GFileMonitorEvent eflags;
233   guint32 mask;
234   GFile* child;
235
236   if (sub->filename)
237     {
238       fullpath = g_strdup_printf ("%s/%s", sub->dirname, sub->filename);
239       g_warning ("Missing callback called fullpath = %s\n", fullpath);
240       if (!g_file_test (fullpath, G_FILE_TEST_EXISTS))
241         {
242           g_free (fullpath);
243           return;
244         }
245       mask = IN_CREATE;
246     }
247   else
248     {
249       fullpath = g_strdup_printf ("%s", sub->dirname);
250       mask = IN_CREATE|IN_ISDIR;
251     }
252
253   eflags = ih_mask_to_EventFlags (mask);
254   child = g_file_new_for_path (fullpath);
255   g_free (fullpath);
256
257   g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
258                              child, NULL, eflags);
259
260   g_object_unref (child);
261 }
262
263 /* Transforms a inotify event to a GVFS event. */
264 static GFileMonitorEvent
265 ih_mask_to_EventFlags (guint32 mask)
266 {
267   mask &= ~IN_ISDIR;
268   switch (mask)
269     {
270     case IN_MODIFY:
271       return G_FILE_MONITOR_EVENT_CHANGED;
272     case IN_CLOSE_WRITE:
273       return G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT;
274     case IN_ATTRIB:
275       return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
276     case IN_MOVE_SELF:
277     case IN_MOVED_FROM:
278     case IN_DELETE:
279     case IN_DELETE_SELF:
280       return G_FILE_MONITOR_EVENT_DELETED;
281     case IN_CREATE:
282     case IN_MOVED_TO:
283       return G_FILE_MONITOR_EVENT_CREATED;
284     case IN_UNMOUNT:
285       return G_FILE_MONITOR_EVENT_UNMOUNTED;
286     case IN_Q_OVERFLOW:
287     case IN_OPEN:
288     case IN_CLOSE_NOWRITE:
289     case IN_ACCESS:
290     case IN_IGNORED:
291     default:
292       return -1;
293     }
294 }