Add g_volume_should_automount. Docs needed.
[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 *identifier;
53   char *identifier_type;
54   
55   char *name;
56   GIcon *icon;
57 };
58
59 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
60
61 #define g_unix_volume_get_type _g_unix_volume_get_type
62 G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
63                          G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
64                                                 g_unix_volume_volume_iface_init))
65
66 static void
67 g_unix_volume_finalize (GObject *object)
68 {
69   GUnixVolume *volume;
70   
71   volume = G_UNIX_VOLUME (object);
72
73   if (volume->volume_monitor != NULL)
74     g_object_unref (volume->volume_monitor);
75
76   if (volume->mount)
77     _g_unix_mount_unset_volume (volume->mount, volume);
78   
79   g_object_unref (volume->icon);
80   g_free (volume->name);
81   g_free (volume->mount_path);
82   g_free (volume->device_path);
83   g_free (volume->identifier);
84   g_free (volume->identifier_type);
85
86   if (G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize)
87     (*G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize) (object);
88 }
89
90 static void
91 g_unix_volume_class_init (GUnixVolumeClass *klass)
92 {
93   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
94
95   gobject_class->finalize = g_unix_volume_finalize;
96 }
97
98 static void
99 g_unix_volume_init (GUnixVolume *unix_volume)
100 {
101 }
102
103 /**
104  * g_unix_volume_new:
105  * @volume_monitor: a #GVolumeMonitor.
106  * @mountpoint: a #GUnixMountPoint.
107  * 
108  * Returns: a #GUnixVolume for the given #GUnixMountPoint.
109  **/
110 GUnixVolume *
111 _g_unix_volume_new (GVolumeMonitor  *volume_monitor,
112                     GUnixMountPoint *mountpoint)
113 {
114   GUnixVolume *volume;
115   
116   if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
117         g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
118       g_unix_mount_point_is_loopback (mountpoint))
119     return NULL;
120   
121   volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
122   volume->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
123   volume->mount_path = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
124   volume->device_path = g_strdup (g_unix_mount_point_get_device_path (mountpoint));
125   volume->can_eject = g_unix_mount_point_guess_can_eject (mountpoint);
126
127   volume->name = g_unix_mount_point_guess_name (mountpoint);
128   volume->icon = g_unix_mount_point_guess_icon (mountpoint);
129
130
131   if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
132     {
133       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
134       volume->identifier = g_strdup (volume->device_path);
135     }
136   else if (g_str_has_prefix (volume->device_path, "LABEL="))
137     {
138       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
139       volume->identifier = g_strdup (volume->device_path + 6);
140     }
141   else if (g_str_has_prefix (volume->device_path, "UUID="))
142     {
143       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
144       volume->identifier = g_strdup (volume->device_path + 5);
145     }
146   else if (g_path_is_absolute (volume->device_path))
147     {
148       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
149       volume->identifier = g_strdup (volume->device_path);
150     }
151   
152   return volume;
153 }
154
155 /**
156  * g_unix_volume_disconnected:
157  * @volume:
158  * 
159  **/
160 void
161 _g_unix_volume_disconnected (GUnixVolume *volume)
162 {
163   if (volume->mount)
164     {
165       _g_unix_mount_unset_volume (volume->mount, volume);
166       volume->mount = NULL;
167     }
168 }
169
170 /**
171  * g_unix_volume_set_mount:
172  * @volume:
173  * @mount:
174  *  
175  **/
176 void
177 _g_unix_volume_set_mount (GUnixVolume  *volume,
178                           GUnixMount *mount)
179 {
180   if (volume->mount == mount)
181     return;
182   
183   if (volume->mount)
184     _g_unix_mount_unset_volume (volume->mount, volume);
185   
186   volume->mount = mount;
187   
188   /* TODO: Emit changed in idle to avoid locking issues */
189   g_signal_emit_by_name (volume, "changed");
190   if (volume->volume_monitor != NULL)
191     g_signal_emit_by_name (volume->volume_monitor, "volume_changed", volume);
192 }
193
194 /**
195  * g_unix_volume_unset_mount:
196  * @volume:
197  * @mount:
198  *
199  **/
200 void
201 _g_unix_volume_unset_mount (GUnixVolume  *volume,
202                             GUnixMount *mount)
203 {
204   if (volume->mount == mount)
205     {
206       volume->mount = NULL;
207       /* TODO: Emit changed in idle to avoid locking issues */
208       g_signal_emit_by_name (volume, "changed");
209       if (volume->volume_monitor != NULL)
210         g_signal_emit_by_name (volume->volume_monitor, "volume_changed", volume);
211     }
212 }
213
214 static GIcon *
215 g_unix_volume_get_icon (GVolume *volume)
216 {
217   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
218   return g_object_ref (unix_volume->icon);
219 }
220
221 static char *
222 g_unix_volume_get_name (GVolume *volume)
223 {
224   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
225   return g_strdup (unix_volume->name);
226 }
227
228 static char *
229 g_unix_volume_get_uuid (GVolume *volume)
230 {
231   return NULL;
232 }
233
234 static gboolean
235 g_unix_volume_can_mount (GVolume *volume)
236 {
237   return TRUE;
238 }
239
240 static gboolean
241 g_unix_volume_can_eject (GVolume *volume)
242 {
243   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
244   return unix_volume->can_eject;
245 }
246
247 static gboolean
248 g_unix_volume_should_automount (GVolume *volume)
249 {
250   /* We automount all local volumes because we don't even
251      make the internal stuff visible */
252   return TRUE;
253 }
254
255 static GDrive *
256 g_unix_volume_get_drive (GVolume *volume)
257 {
258   return NULL;
259 }
260
261 static GMount *
262 g_unix_volume_get_mount (GVolume *volume)
263 {
264   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
265
266   if (unix_volume->mount != NULL)
267     return g_object_ref (unix_volume->mount);
268
269   return NULL;
270 }
271
272
273 gboolean
274 _g_unix_volume_has_mount_path (GUnixVolume *volume,
275                                          const char  *mount_path)
276 {
277   return strcmp (volume->mount_path, mount_path) == 0;
278 }
279
280
281 typedef struct {
282   GUnixVolume *unix_volume;
283   GAsyncReadyCallback callback;
284   gpointer user_data;
285   GCancellable *cancellable;
286   int error_fd;
287   GIOChannel *error_channel;
288   guint error_channel_source_id;
289   GString *error_string;
290 } EjectMountOp;
291
292 static void 
293 eject_mount_cb (GPid pid, gint status, gpointer user_data)
294 {
295   EjectMountOp *data = user_data;
296   GSimpleAsyncResult *simple;
297   
298   if (WEXITSTATUS (status) != 0)
299     {
300       GError *error;
301       error = g_error_new_literal (G_IO_ERROR, 
302                                    G_IO_ERROR_FAILED,
303                                    data->error_string->str);
304       simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
305                                                      data->callback,
306                                                      data->user_data,
307                                                      error);
308       g_error_free (error);
309     }
310   else
311     {
312       simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
313                                           data->callback,
314                                           data->user_data,
315                                           NULL);
316     }
317
318   g_simple_async_result_complete (simple);
319   g_object_unref (simple);
320
321   g_source_remove (data->error_channel_source_id);
322   g_io_channel_unref (data->error_channel);
323   g_string_free (data->error_string, TRUE);
324   close (data->error_fd);
325   g_spawn_close_pid (pid);
326   g_free (data);
327 }
328
329 static gboolean
330 eject_mount_read_error (GIOChannel *channel,
331                   GIOCondition condition,
332                   gpointer user_data)
333 {
334   char *str;
335   gsize str_len;
336   EjectMountOp *data = user_data;
337
338   g_io_channel_read_to_end (channel, &str, &str_len, NULL);
339   g_string_append (data->error_string, str);
340   g_free (str);
341   return TRUE;
342 }
343
344 static void
345 eject_mount_do (GVolume             *volume,
346                 GCancellable        *cancellable,
347                 GAsyncReadyCallback  callback,
348                 gpointer             user_data,
349                 char               **argv)
350 {
351   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
352   EjectMountOp *data;
353   GPid child_pid;
354   GError *error;
355   
356   data = g_new0 (EjectMountOp, 1);
357   data->unix_volume = unix_volume;
358   data->callback = callback;
359   data->user_data = user_data;
360   data->cancellable = cancellable;
361   
362   error = NULL;
363   if (!g_spawn_async_with_pipes (NULL,         /* working dir */
364                                  argv,
365                                  NULL,         /* envp */
366                                  G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
367                                  NULL,         /* child_setup */
368                                  NULL,         /* user_data for child_setup */
369                                  &child_pid,
370                                  NULL,           /* standard_input */
371                                  NULL,           /* standard_output */
372                                  &(data->error_fd),
373                                  &error)) {
374     GSimpleAsyncResult *simple;
375     simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
376                                                    data->callback,
377                                                    data->user_data,
378                                                    error);
379     g_simple_async_result_complete (simple);
380     g_object_unref (simple);
381     g_error_free (error);
382     g_free (data);
383     return;
384   }
385   data->error_string = g_string_new ("");
386   data->error_channel = g_io_channel_unix_new (data->error_fd);
387   data->error_channel_source_id = g_io_add_watch (data->error_channel, G_IO_IN, eject_mount_read_error, data);
388   g_child_watch_add (child_pid, eject_mount_cb, data);
389 }
390
391
392 static void
393 g_unix_volume_mount (GVolume    *volume,
394                      GMountOperation     *mount_operation,
395                      GCancellable        *cancellable,
396                      GAsyncReadyCallback  callback,
397                      gpointer             user_data)
398 {
399   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
400   char *argv[] = {"mount", NULL, NULL};
401
402   if (unix_volume->mount_path != NULL)
403     argv[1] = unix_volume->mount_path;
404   else
405     argv[1] = unix_volume->device_path;
406
407   eject_mount_do (volume, cancellable, callback, user_data, argv);
408 }
409
410 static gboolean
411 g_unix_volume_mount_finish (GVolume        *volume,
412                            GAsyncResult  *result,
413                            GError       **error)
414 {
415   return TRUE;
416 }
417
418 static void
419 g_unix_volume_eject (GVolume    *volume,
420                      GMountUnmountFlags   flags,
421                      GCancellable        *cancellable,
422                      GAsyncReadyCallback  callback,
423                      gpointer             user_data)
424 {
425   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
426   char *argv[] = {"eject", NULL, NULL};
427
428   argv[1] = unix_volume->device_path;
429
430   eject_mount_do (volume, cancellable, callback, user_data, argv);
431 }
432
433 static gboolean
434 g_unix_volume_eject_finish (GVolume        *volume,
435                             GAsyncResult  *result,
436                             GError       **error)
437 {
438   return TRUE;
439 }
440
441 static char *
442 g_unix_volume_get_identifier (GVolume              *volume,
443                               const char          *kind)
444 {
445   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
446
447   if (strcmp (kind, unix_volume->identifier_type) == 0)
448     return g_strdup (unix_volume->identifier);
449   return NULL;
450 }
451
452 static char **
453 g_unix_volume_enumerate_identifiers (GVolume *volume)
454 {
455   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
456   char **res;
457
458   if (unix_volume->identifier_type)
459     {
460       res = g_new (char *, 2);
461       res[0] = g_strdup (unix_volume->identifier_type);
462       res[1] = NULL;
463     }
464   else
465     {
466       res = g_new (char *, 1);
467       res[0] = NULL;
468     }
469
470   return res;
471 }
472
473 static void
474 g_unix_volume_volume_iface_init (GVolumeIface *iface)
475 {
476   iface->get_name = g_unix_volume_get_name;
477   iface->get_icon = g_unix_volume_get_icon;
478   iface->get_uuid = g_unix_volume_get_uuid;
479   iface->get_drive = g_unix_volume_get_drive;
480   iface->get_mount = g_unix_volume_get_mount;
481   iface->can_mount = g_unix_volume_can_mount;
482   iface->can_eject = g_unix_volume_can_eject;
483   iface->should_automount = g_unix_volume_should_automount;
484   iface->mount_fn = g_unix_volume_mount;
485   iface->mount_finish = g_unix_volume_mount_finish;
486   iface->eject = g_unix_volume_eject;
487   iface->eject_finish = g_unix_volume_eject_finish;
488   iface->get_identifier = g_unix_volume_get_identifier;
489   iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;
490 }