Improve the g_file_make_symbolic_link docs
[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 #include "gioalias.h"
42
43
44 static gboolean ih_debug_enabled = FALSE;
45 #define IH_W if (ih_debug_enabled) g_warning 
46
47 static void ih_event_callback (ik_event_t *event, inotify_sub *sub);
48 static void ih_not_missing_callback (inotify_sub *sub);
49
50 /* We share this lock with inotify-kernel.c and inotify-missing.c
51  *
52  * inotify-kernel.c takes the lock when it reads events from
53  * the kernel and when it processes those events
54  *
55  * inotify-missing.c takes the lock when it is scanning the missing
56  * list.
57  *
58  * We take the lock in all public functions
59  */
60 G_GNUC_INTERNAL G_LOCK_DEFINE (inotify_lock);
61
62 static GFileMonitorEvent ih_mask_to_EventFlags (guint32 mask);
63
64 /**
65  * _ih_startup:
66  *
67  * Initializes the inotify backend.  This must be called before
68  * any other functions in this module.
69  *
70  * Return value: #TRUE if initialization succeeded, #FALSE otherwise
71  */
72 gboolean
73 _ih_startup (void)
74 {
75   static gboolean initialized = FALSE;
76   static gboolean result = FALSE;
77   
78   G_LOCK (inotify_lock);
79   
80   if (initialized == TRUE)
81     {
82       G_UNLOCK (inotify_lock);
83       return result;
84     }
85
86   result = _ip_startup (ih_event_callback);
87   if (!result)
88     {
89       g_warning ("Could not initialize inotify\n");
90       G_UNLOCK (inotify_lock);
91       return FALSE;
92     }
93   _im_startup (ih_not_missing_callback);
94   _id_startup ();
95
96   IH_W ("started gvfs inotify backend\n");
97   
98   initialized = TRUE;
99   
100   G_UNLOCK (inotify_lock);
101   
102   return TRUE;
103 }
104
105 /*
106  * Adds a subscription to be monitored.
107  */
108 gboolean
109 _ih_sub_add (inotify_sub *sub)
110 {
111   G_LOCK (inotify_lock);
112         
113   if (!_ip_start_watching (sub))
114     _im_add (sub);
115   
116   G_UNLOCK (inotify_lock);
117   return TRUE;
118 }
119
120 /*
121  * Cancels a subscription which was being monitored.
122  */
123 gboolean
124 _ih_sub_cancel (inotify_sub *sub)
125 {
126   G_LOCK (inotify_lock);
127
128   if (!sub->cancelled)
129     {
130       IH_W ("cancelling %s\n", sub->dirname);
131       sub->cancelled = TRUE;
132       _im_rm (sub);
133       _ip_stop_watching (sub);
134     }
135   
136   G_UNLOCK (inotify_lock);
137   
138   return TRUE;
139 }
140
141 static char *
142 _ih_fullpath_from_event (ik_event_t *event, char *dirname)
143 {
144   char *fullpath;
145
146   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 {
172   gchar *fullpath;
173   GFileMonitorEvent eflags;
174   GFile* parent;
175   GFile* child;
176   GFile* other;
177   
178   eflags = ih_mask_to_EventFlags (event->mask);
179   parent = g_file_new_for_path (sub->dirname);
180   fullpath = _ih_fullpath_from_event (event, sub->dirname);
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       char *parent_dir = _ip_get_path_for_wd (event->pair->wd);
187       fullpath = _ih_fullpath_from_event (event->pair, parent_dir);
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   g_object_unref (child);
200   g_object_unref (parent);
201   if (other)
202     g_object_unref (other);
203 }
204
205 static void
206 ih_not_missing_callback (inotify_sub *sub)
207 {
208   gchar *fullpath;
209   GFileMonitorEvent eflags;
210   guint32 mask;
211   GFile* parent;
212   GFile* child;
213   
214   parent = g_file_new_for_path (sub->dirname);
215
216   if (sub->filename)
217     {
218       fullpath = g_strdup_printf ("%s/%s", sub->dirname, sub->filename);
219       g_warning ("Missing callback called fullpath = %s\n", fullpath);
220       if (!g_file_test (fullpath, G_FILE_TEST_EXISTS))
221         {
222           g_free (fullpath);
223           return;
224         }
225       mask = IN_CREATE;
226     }
227   else
228     {
229       fullpath = g_strdup_printf ("%s", sub->dirname);
230       mask = IN_CREATE|IN_ISDIR;
231     }
232
233   eflags = ih_mask_to_EventFlags (mask);
234   child = g_file_new_for_path (fullpath);
235   g_free (fullpath);
236
237   g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
238                              child, NULL, eflags);
239
240   g_object_unref (child);
241   g_object_unref (parent);
242 }
243
244 /* Transforms a inotify event to a GVFS event. */
245 static GFileMonitorEvent
246 ih_mask_to_EventFlags (guint32 mask)
247 {
248   mask &= ~IN_ISDIR;
249   switch (mask)
250     {
251     case IN_MODIFY:
252       return G_FILE_MONITOR_EVENT_CHANGED;
253     case IN_CLOSE_WRITE:
254       return G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT;
255     case IN_ATTRIB:
256       return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
257     case IN_MOVE_SELF:
258     case IN_MOVED_FROM:
259     case IN_DELETE:
260     case IN_DELETE_SELF:
261       return G_FILE_MONITOR_EVENT_DELETED;
262     case IN_CREATE:
263     case IN_MOVED_TO:
264       return G_FILE_MONITOR_EVENT_CREATED;
265     case IN_UNMOUNT:
266       return G_FILE_MONITOR_EVENT_UNMOUNTED;
267     case IN_Q_OVERFLOW:
268     case IN_OPEN:
269     case IN_CLOSE_NOWRITE:
270     case IN_ACCESS:
271     case IN_IGNORED:
272     default:
273       return -1;
274     }
275 }