Updated FSF's address
[platform/upstream/glib.git] / gio / gunixmount.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GIO - GLib Input, Output and Streaming Library
4  * 
5  * Copyright (C) 2006-2007 Red Hat, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This 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  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  *         David Zeuthen <davidz@redhat.com>
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29
30 #include <glib.h>
31 #include "gunixvolumemonitor.h"
32 #include "gunixmount.h"
33 #include "gunixmounts.h"
34 #include "gunixvolume.h"
35 #include "gmountprivate.h"
36 #include "gmount.h"
37 #include "gfile.h"
38 #include "gvolumemonitor.h"
39 #include "gthemedicon.h"
40 #include "gsimpleasyncresult.h"
41 #include "gioerror.h"
42 #include "glibintl.h"
43 /* for BUFSIZ */
44 #include <stdio.h>
45
46
47 struct _GUnixMount {
48   GObject parent;
49
50   GVolumeMonitor   *volume_monitor;
51
52   GUnixVolume      *volume; /* owned by volume monitor */
53
54   char *name;
55   GIcon *icon;
56   GIcon *symbolic_icon;
57   char *device_path;
58   char *mount_path;
59
60   gboolean can_eject;
61 };
62
63 static void g_unix_mount_mount_iface_init (GMountIface *iface);
64
65 #define g_unix_mount_get_type _g_unix_mount_get_type
66 G_DEFINE_TYPE_WITH_CODE (GUnixMount, g_unix_mount, G_TYPE_OBJECT,
67                          G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
68                                                 g_unix_mount_mount_iface_init))
69
70
71 static void
72 g_unix_mount_finalize (GObject *object)
73 {
74   GUnixMount *mount;
75   
76   mount = G_UNIX_MOUNT (object);
77
78   if (mount->volume_monitor != NULL)
79     g_object_unref (mount->volume_monitor);
80
81   if (mount->volume)
82     _g_unix_volume_unset_mount (mount->volume, mount);
83     
84   /* TODO: g_warn_if_fail (volume->volume == NULL); */
85   g_object_unref (mount->icon);
86   g_object_unref (mount->symbolic_icon);
87   g_free (mount->name);
88   g_free (mount->device_path);
89   g_free (mount->mount_path);
90
91   G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize (object);
92 }
93
94 static void
95 g_unix_mount_class_init (GUnixMountClass *klass)
96 {
97   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
98
99   gobject_class->finalize = g_unix_mount_finalize;
100 }
101
102 static void
103 g_unix_mount_init (GUnixMount *unix_mount)
104 {
105 }
106
107 GUnixMount *
108 _g_unix_mount_new (GVolumeMonitor  *volume_monitor,
109                    GUnixMountEntry *mount_entry,
110                    GUnixVolume     *volume)
111 {
112   GUnixMount *mount;
113   
114   /* No volume for mount: Ignore internal things */
115   if (volume == NULL && !g_unix_mount_guess_should_display (mount_entry))
116     return NULL;
117
118   mount = g_object_new (G_TYPE_UNIX_MOUNT, NULL);
119   mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
120   mount->device_path = g_strdup (g_unix_mount_get_device_path (mount_entry));
121   mount->mount_path = g_strdup (g_unix_mount_get_mount_path (mount_entry));
122   mount->can_eject = g_unix_mount_guess_can_eject (mount_entry);
123
124   mount->name = g_unix_mount_guess_name (mount_entry);
125   mount->icon = g_unix_mount_guess_icon (mount_entry);
126   mount->symbolic_icon = g_unix_mount_guess_symbolic_icon (mount_entry);
127
128   /* need to do this last */
129   mount->volume = volume;
130   if (volume != NULL)
131     _g_unix_volume_set_mount (volume, mount);
132
133   return mount;
134 }
135
136 void
137 _g_unix_mount_unmounted (GUnixMount *mount)
138 {
139   if (mount->volume != NULL)
140     {
141       _g_unix_volume_unset_mount (mount->volume, mount);
142       mount->volume = NULL;
143       g_signal_emit_by_name (mount, "changed");
144       /* there's really no need to emit mount_changed on the volume monitor 
145        * as we're going to be deleted.. */
146     }
147 }
148
149 void
150 _g_unix_mount_unset_volume (GUnixMount *mount,
151                             GUnixVolume  *volume)
152 {
153   if (mount->volume == volume)
154     {
155       mount->volume = NULL;
156       /* TODO: Emit changed in idle to avoid locking issues */
157       g_signal_emit_by_name (mount, "changed");
158       if (mount->volume_monitor != NULL)
159         g_signal_emit_by_name (mount->volume_monitor, "mount-changed", mount);
160     }
161 }
162
163 static GFile *
164 g_unix_mount_get_root (GMount *mount)
165 {
166   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
167
168   return g_file_new_for_path (unix_mount->mount_path);
169 }
170
171 static GIcon *
172 g_unix_mount_get_icon (GMount *mount)
173 {
174   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
175
176   return g_object_ref (unix_mount->icon);
177 }
178
179 static GIcon *
180 g_unix_mount_get_symbolic_icon (GMount *mount)
181 {
182   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
183
184   return g_object_ref (unix_mount->symbolic_icon);
185 }
186
187 static char *
188 g_unix_mount_get_uuid (GMount *mount)
189 {
190   return NULL;
191 }
192
193 static char *
194 g_unix_mount_get_name (GMount *mount)
195 {
196   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
197   
198   return g_strdup (unix_mount->name);
199 }
200
201 gboolean
202 _g_unix_mount_has_mount_path (GUnixMount *mount,
203                               const char  *mount_path)
204 {
205   return strcmp (mount->mount_path, mount_path) == 0;
206 }
207
208 static GDrive *
209 g_unix_mount_get_drive (GMount *mount)
210 {
211   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
212
213   if (unix_mount->volume != NULL)
214     return g_volume_get_drive (G_VOLUME (unix_mount->volume));
215
216   return NULL;
217 }
218
219 static GVolume *
220 g_unix_mount_get_volume (GMount *mount)
221 {
222   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
223
224   if (unix_mount->volume)
225     return G_VOLUME (g_object_ref (unix_mount->volume));
226   
227   return NULL;
228 }
229
230 static gboolean
231 g_unix_mount_can_unmount (GMount *mount)
232 {
233   return TRUE;
234 }
235
236 static gboolean
237 g_unix_mount_can_eject (GMount *mount)
238 {
239   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
240   return unix_mount->can_eject;
241 }
242
243
244 typedef struct {
245   GUnixMount *unix_mount;
246   int error_fd;
247   GIOChannel *error_channel;
248   GSource *error_channel_source;
249   GString *error_string;
250   gchar **argv;
251 } UnmountEjectOp;
252
253 static void
254 unmount_eject_op_free (UnmountEjectOp *data)
255 {
256   if (data->error_channel_source)
257     {
258       g_source_destroy (data->error_channel_source);
259       g_source_unref (data->error_channel_source);
260     }
261   g_io_channel_unref (data->error_channel);
262   g_string_free (data->error_string, TRUE);
263   g_strfreev (data->argv);
264   close (data->error_fd);
265   g_free (data);
266 }
267
268 static void 
269 eject_unmount_cb (GPid pid, gint status, gpointer user_data)
270 {
271   GTask *task = user_data;
272   UnmountEjectOp *data = g_task_get_task_data (task);
273   
274   if (g_task_return_error_if_cancelled (task))
275     return;
276
277   g_spawn_close_pid (pid);
278
279   if (WEXITSTATUS (status) != 0)
280     {
281       g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
282                                "%s", data->error_string->str);
283     }
284   else
285     g_task_return_boolean (task, TRUE);
286
287   g_object_unref (task);
288 }
289
290 static gboolean
291 eject_unmount_read_error (GIOChannel *channel,
292                           GIOCondition condition,
293                           gpointer user_data)
294 {
295   GTask *task = user_data;
296   UnmountEjectOp *data = g_task_get_task_data (task);
297   char buf[BUFSIZ];
298   gsize bytes_read;
299   GError *error;
300   GIOStatus status;
301
302   if (g_task_return_error_if_cancelled (task))
303     return FALSE;
304
305   error = NULL;
306 read:
307   status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
308   if (status == G_IO_STATUS_NORMAL)
309    {
310      g_string_append_len (data->error_string, buf, bytes_read);
311      if (bytes_read == sizeof (buf))
312         goto read;
313    }
314   else if (status == G_IO_STATUS_EOF)
315     g_string_append_len (data->error_string, buf, bytes_read);
316   else if (status == G_IO_STATUS_ERROR)
317     {
318       if (data->error_string->len > 0)
319         g_string_append (data->error_string, "\n");
320
321       g_string_append (data->error_string, error->message);
322       g_error_free (error);
323
324       if (data->error_channel_source)
325         {
326           g_source_unref (data->error_channel_source);
327           data->error_channel_source = NULL;
328         }
329       return FALSE;
330     }
331
332   return TRUE;
333 }
334
335 static gboolean
336 eject_unmount_do_cb (gpointer user_data)
337 {
338   GTask *task = user_data;
339   UnmountEjectOp *data = g_task_get_task_data (task);
340   GPid child_pid;
341   GSource *child_watch;
342   GError *error = NULL;
343
344   if (g_task_return_error_if_cancelled (task))
345     return G_SOURCE_REMOVE;
346
347   if (!g_spawn_async_with_pipes (NULL,         /* working dir */
348                                  data->argv,
349                                  NULL,         /* envp */
350                                  G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
351                                  NULL,         /* child_setup */
352                                  NULL,         /* user_data for child_setup */
353                                  &child_pid,
354                                  NULL,           /* standard_input */
355                                  NULL,           /* standard_output */
356                                  &(data->error_fd),
357                                  &error)) {
358     g_assert (error != NULL);
359     goto handle_error;
360   }
361
362   data->error_string = g_string_new ("");
363
364   data->error_channel = g_io_channel_unix_new (data->error_fd);
365   g_io_channel_set_flags (data->error_channel, G_IO_FLAG_NONBLOCK, &error);
366   if (error != NULL)
367     goto handle_error;
368
369   data->error_channel_source = g_io_create_watch (data->error_channel, G_IO_IN);
370   g_task_attach_source (task, data->error_channel_source,
371                         (GSourceFunc) eject_unmount_read_error);
372
373   child_watch = g_child_watch_source_new (child_pid);
374   g_task_attach_source (task, data->error_channel_source,
375                         (GSourceFunc) eject_unmount_cb);
376   g_source_unref (child_watch);
377
378 handle_error:
379   if (error != NULL)
380     {
381       g_task_return_error (task, error);
382       g_object_unref (task);
383     }
384
385   return G_SOURCE_REMOVE;
386 }
387
388 static void
389 eject_unmount_do (GMount              *mount,
390                   GCancellable        *cancellable,
391                   GAsyncReadyCallback  callback,
392                   gpointer             user_data,
393                   char               **argv)
394 {
395   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
396   UnmountEjectOp *data;
397   GTask *task;
398   GSource *timeout;
399
400   data = g_new0 (UnmountEjectOp, 1);
401   data->argv = g_strdupv (argv);
402
403   task = g_task_new (mount, cancellable, callback, user_data);
404   g_task_set_task_data (task, data, (GDestroyNotify)unmount_eject_op_free);
405
406   if (unix_mount->volume_monitor != NULL)
407     g_signal_emit_by_name (unix_mount->volume_monitor, "mount-pre-unmount", mount);
408
409   g_signal_emit_by_name (mount, "pre-unmount", 0);
410
411   timeout = g_timeout_source_new (500);
412   g_task_attach_source (task, timeout, (GSourceFunc) eject_unmount_do_cb);
413   g_source_unref (timeout);
414 }
415
416 static void
417 g_unix_mount_unmount (GMount             *mount,
418                       GMountUnmountFlags flags,
419                       GCancellable        *cancellable,
420                       GAsyncReadyCallback  callback,
421                       gpointer             user_data)
422 {
423   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
424   char *argv[] = {"umount", NULL, NULL};
425
426   if (unix_mount->mount_path != NULL)
427     argv[1] = unix_mount->mount_path;
428   else
429     argv[1] = unix_mount->device_path;
430
431   eject_unmount_do (mount, cancellable, callback, user_data, argv);
432 }
433
434 static gboolean
435 g_unix_mount_unmount_finish (GMount       *mount,
436                              GAsyncResult  *result,
437                              GError       **error)
438 {
439   return g_task_propagate_boolean (G_TASK (result), error);
440 }
441
442 static void
443 g_unix_mount_eject (GMount             *mount,
444                     GMountUnmountFlags flags,
445                     GCancellable        *cancellable,
446                     GAsyncReadyCallback  callback,
447                     gpointer             user_data)
448 {
449   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
450   char *argv[] = {"eject", NULL, NULL};
451
452   if (unix_mount->mount_path != NULL)
453     argv[1] = unix_mount->mount_path;
454   else
455     argv[1] = unix_mount->device_path;
456
457   eject_unmount_do (mount, cancellable, callback, user_data, argv);
458 }
459
460 static gboolean
461 g_unix_mount_eject_finish (GMount       *mount,
462                            GAsyncResult  *result,
463                            GError       **error)
464 {
465   return g_task_propagate_boolean (G_TASK (result), error);
466 }
467
468 static void
469 g_unix_mount_mount_iface_init (GMountIface *iface)
470 {
471   iface->get_root = g_unix_mount_get_root;
472   iface->get_name = g_unix_mount_get_name;
473   iface->get_icon = g_unix_mount_get_icon;
474   iface->get_symbolic_icon = g_unix_mount_get_symbolic_icon;
475   iface->get_uuid = g_unix_mount_get_uuid;
476   iface->get_drive = g_unix_mount_get_drive;
477   iface->get_volume = g_unix_mount_get_volume;
478   iface->can_unmount = g_unix_mount_can_unmount;
479   iface->can_eject = g_unix_mount_can_eject;
480   iface->unmount = g_unix_mount_unmount;
481   iface->unmount_finish = g_unix_mount_unmount_finish;
482   iface->eject = g_unix_mount_eject;
483   iface->eject_finish = g_unix_mount_eject_finish;
484 }