Add GMountUnmountFlags to all unmount and eject calls. Add
[platform/upstream/glib.git] / gio / gunixvolume.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, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * Author: Alexander Larsson <alexl@redhat.com>
23  *         David Zeuthen <davidz@redhat.com>
24  */
25
26 #include <config.h>
27
28 #include <string.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31
32 #include <glib.h>
33 #include "gunixvolume.h"
34 #include "gunixmount.h"
35 #include "gthemedicon.h"
36 #include "gvolumemonitor.h"
37 #include "gsimpleasyncresult.h"
38 #include "glibintl.h"
39
40 #include "gioalias.h"
41
42 struct _GUnixVolume {
43   GObject parent;
44
45   GVolumeMonitor *volume_monitor;
46   GUnixMount     *mount; /* owned by volume monitor */
47   
48   char *device_path;
49   char *mount_path;
50   gboolean can_eject;
51
52   char *name;
53   GIcon *icon;
54 };
55
56 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
57
58 #define g_unix_volume_get_type _g_unix_volume_get_type
59 G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
60                          G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
61                                                 g_unix_volume_volume_iface_init))
62
63 static void
64 g_unix_volume_finalize (GObject *object)
65 {
66   GUnixVolume *volume;
67   
68   volume = G_UNIX_VOLUME (object);
69
70   if (volume->volume_monitor != NULL)
71     g_object_unref (volume->volume_monitor);
72
73   if (volume->mount)
74     _g_unix_mount_unset_volume (volume->mount, volume);
75   
76   g_object_unref (volume->icon);
77   g_free (volume->name);
78   g_free (volume->mount_path);
79   g_free (volume->device_path);
80
81   if (G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize)
82     (*G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize) (object);
83 }
84
85 static void
86 g_unix_volume_class_init (GUnixVolumeClass *klass)
87 {
88   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89
90   gobject_class->finalize = g_unix_volume_finalize;
91 }
92
93 static void
94 g_unix_volume_init (GUnixVolume *unix_volume)
95 {
96 }
97
98 /**
99  * g_unix_volume_new:
100  * @volume_monitor: a #GVolumeMonitor.
101  * @mountpoint: a #GUnixMountPoint.
102  * 
103  * Returns: a #GUnixVolume for the given #GUnixMountPoint.
104  **/
105 GUnixVolume *
106 _g_unix_volume_new (GVolumeMonitor  *volume_monitor,
107                     GUnixMountPoint *mountpoint)
108 {
109   GUnixVolume *volume;
110   
111   if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
112         g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
113       g_unix_mount_point_is_loopback (mountpoint))
114     return NULL;
115   
116   volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
117   volume->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
118   volume->mount_path = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
119   volume->device_path = g_strdup (g_unix_mount_point_get_device_path (mountpoint));
120   volume->can_eject = g_unix_mount_point_guess_can_eject (mountpoint);
121
122   volume->name = g_unix_mount_point_guess_name (mountpoint);
123   volume->icon = g_unix_mount_point_guess_icon (mountpoint);
124   return volume;
125 }
126
127 /**
128  * g_unix_volume_disconnected:
129  * @volume:
130  * 
131  **/
132 void
133 _g_unix_volume_disconnected (GUnixVolume *volume)
134 {
135   if (volume->mount)
136     {
137       _g_unix_mount_unset_volume (volume->mount, volume);
138       volume->mount = NULL;
139     }
140 }
141
142 /**
143  * g_unix_volume_set_mount:
144  * @volume:
145  * @mount:
146  *  
147  **/
148 void
149 _g_unix_volume_set_mount (GUnixVolume  *volume,
150                           GUnixMount *mount)
151 {
152   if (volume->mount == mount)
153     return;
154   
155   if (volume->mount)
156     _g_unix_mount_unset_volume (volume->mount, volume);
157   
158   volume->mount = mount;
159   
160   /* TODO: Emit changed in idle to avoid locking issues */
161   g_signal_emit_by_name (volume, "changed");
162   if (volume->volume_monitor != NULL)
163     g_signal_emit_by_name (volume->volume_monitor, "volume_changed", volume);
164 }
165
166 /**
167  * g_unix_volume_unset_mount:
168  * @volume:
169  * @mount:
170  *
171  **/
172 void
173 _g_unix_volume_unset_mount (GUnixVolume  *volume,
174                             GUnixMount *mount)
175 {
176   if (volume->mount == mount)
177     {
178       volume->mount = NULL;
179       /* TODO: Emit changed in idle to avoid locking issues */
180       g_signal_emit_by_name (volume, "changed");
181       if (volume->volume_monitor != NULL)
182         g_signal_emit_by_name (volume->volume_monitor, "volume_changed", volume);
183     }
184 }
185
186 static GIcon *
187 g_unix_volume_get_icon (GVolume *volume)
188 {
189   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
190   return g_object_ref (unix_volume->icon);
191 }
192
193 static char *
194 g_unix_volume_get_name (GVolume *volume)
195 {
196   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
197   return g_strdup (unix_volume->name);
198 }
199
200 static char *
201 g_unix_volume_get_uuid (GVolume *volume)
202 {
203   return NULL;
204 }
205
206 static gboolean
207 g_unix_volume_can_mount (GVolume *volume)
208 {
209   return TRUE;
210 }
211
212 static gboolean
213 g_unix_volume_can_eject (GVolume *volume)
214 {
215   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
216   return unix_volume->can_eject;
217 }
218
219 static GDrive *
220 g_unix_volume_get_drive (GVolume *volume)
221 {
222   return NULL;
223 }
224
225 static GMount *
226 g_unix_volume_get_mount (GVolume *volume)
227 {
228   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
229
230   if (unix_volume->mount != NULL)
231     return g_object_ref (unix_volume->mount);
232
233   return NULL;
234 }
235
236
237 gboolean
238 _g_unix_volume_has_mount_path (GUnixVolume *volume,
239                                          const char  *mount_path)
240 {
241   return strcmp (volume->mount_path, mount_path) == 0;
242 }
243
244
245 typedef struct {
246   GUnixVolume *unix_volume;
247   GAsyncReadyCallback callback;
248   gpointer user_data;
249   GCancellable *cancellable;
250   int error_fd;
251   GIOChannel *error_channel;
252   guint error_channel_source_id;
253   GString *error_string;
254 } EjectMountOp;
255
256 static void 
257 eject_mount_cb (GPid pid, gint status, gpointer user_data)
258 {
259   EjectMountOp *data = user_data;
260   GSimpleAsyncResult *simple;
261   
262   if (WEXITSTATUS (status) != 0)
263     {
264       GError *error;
265       error = g_error_new_literal (G_IO_ERROR, 
266                                    G_IO_ERROR_FAILED,
267                                    data->error_string->str);
268       simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
269                                                      data->callback,
270                                                      data->user_data,
271                                                      error);
272       g_error_free (error);
273     }
274   else
275     {
276       simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
277                                           data->callback,
278                                           data->user_data,
279                                           NULL);
280     }
281
282   g_simple_async_result_complete (simple);
283   g_object_unref (simple);
284
285   g_source_remove (data->error_channel_source_id);
286   g_io_channel_unref (data->error_channel);
287   g_string_free (data->error_string, TRUE);
288   close (data->error_fd);
289   g_spawn_close_pid (pid);
290   g_free (data);
291 }
292
293 static gboolean
294 eject_mount_read_error (GIOChannel *channel,
295                   GIOCondition condition,
296                   gpointer user_data)
297 {
298   char *str;
299   gsize str_len;
300   EjectMountOp *data = user_data;
301
302   g_io_channel_read_to_end (channel, &str, &str_len, NULL);
303   g_string_append (data->error_string, str);
304   g_free (str);
305   return TRUE;
306 }
307
308 static void
309 eject_mount_do (GVolume             *volume,
310                 GCancellable        *cancellable,
311                 GAsyncReadyCallback  callback,
312                 gpointer             user_data,
313                 char               **argv)
314 {
315   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
316   EjectMountOp *data;
317   GPid child_pid;
318   GError *error;
319   
320   data = g_new0 (EjectMountOp, 1);
321   data->unix_volume = unix_volume;
322   data->callback = callback;
323   data->user_data = user_data;
324   data->cancellable = cancellable;
325   
326   error = NULL;
327   if (!g_spawn_async_with_pipes (NULL,         /* working dir */
328                                  argv,
329                                  NULL,         /* envp */
330                                  G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
331                                  NULL,         /* child_setup */
332                                  NULL,         /* user_data for child_setup */
333                                  &child_pid,
334                                  NULL,           /* standard_input */
335                                  NULL,           /* standard_output */
336                                  &(data->error_fd),
337                                  &error)) {
338     GSimpleAsyncResult *simple;
339     simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
340                                                    data->callback,
341                                                    data->user_data,
342                                                    error);
343     g_simple_async_result_complete (simple);
344     g_object_unref (simple);
345     g_error_free (error);
346     g_free (data);
347     return;
348   }
349   data->error_string = g_string_new ("");
350   data->error_channel = g_io_channel_unix_new (data->error_fd);
351   data->error_channel_source_id = g_io_add_watch (data->error_channel, G_IO_IN, eject_mount_read_error, data);
352   g_child_watch_add (child_pid, eject_mount_cb, data);
353 }
354
355
356 static void
357 g_unix_volume_mount (GVolume    *volume,
358                      GMountOperation     *mount_operation,
359                      GCancellable        *cancellable,
360                      GAsyncReadyCallback  callback,
361                      gpointer             user_data)
362 {
363   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
364   char *argv[] = {"mount", NULL, NULL};
365
366   if (unix_volume->mount_path != NULL)
367     argv[1] = unix_volume->mount_path;
368   else
369     argv[1] = unix_volume->device_path;
370
371   eject_mount_do (volume, cancellable, callback, user_data, argv);
372 }
373
374 static gboolean
375 g_unix_volume_mount_finish (GVolume        *volume,
376                            GAsyncResult  *result,
377                            GError       **error)
378 {
379   return TRUE;
380 }
381
382 static void
383 g_unix_volume_eject (GVolume    *volume,
384                      GMountUnmountFlags   flags,
385                      GCancellable        *cancellable,
386                      GAsyncReadyCallback  callback,
387                      gpointer             user_data)
388 {
389   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
390   char *argv[] = {"eject", NULL, NULL};
391
392   argv[1] = unix_volume->device_path;
393
394   eject_mount_do (volume, cancellable, callback, user_data, argv);
395 }
396
397 static gboolean
398 g_unix_volume_eject_finish (GVolume        *volume,
399                             GAsyncResult  *result,
400                             GError       **error)
401 {
402   return TRUE;
403 }
404
405 static void
406 g_unix_volume_volume_iface_init (GVolumeIface *iface)
407 {
408   iface->get_name = g_unix_volume_get_name;
409   iface->get_icon = g_unix_volume_get_icon;
410   iface->get_uuid = g_unix_volume_get_uuid;
411   iface->get_drive = g_unix_volume_get_drive;
412   iface->get_mount = g_unix_volume_get_mount;
413   iface->can_mount = g_unix_volume_can_mount;
414   iface->can_eject = g_unix_volume_can_eject;
415   iface->mount_fn = g_unix_volume_mount;
416   iface->mount_finish = g_unix_volume_mount_finish;
417   iface->eject = g_unix_volume_eject;
418   iface->eject_finish = g_unix_volume_eject_finish;
419 }