Add g_drive_get_identifier and g_drive_enumerate_identifiers
[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 GDrive *
248 g_unix_volume_get_drive (GVolume *volume)
249 {
250   return NULL;
251 }
252
253 static GMount *
254 g_unix_volume_get_mount (GVolume *volume)
255 {
256   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
257
258   if (unix_volume->mount != NULL)
259     return g_object_ref (unix_volume->mount);
260
261   return NULL;
262 }
263
264
265 gboolean
266 _g_unix_volume_has_mount_path (GUnixVolume *volume,
267                                          const char  *mount_path)
268 {
269   return strcmp (volume->mount_path, mount_path) == 0;
270 }
271
272
273 typedef struct {
274   GUnixVolume *unix_volume;
275   GAsyncReadyCallback callback;
276   gpointer user_data;
277   GCancellable *cancellable;
278   int error_fd;
279   GIOChannel *error_channel;
280   guint error_channel_source_id;
281   GString *error_string;
282 } EjectMountOp;
283
284 static void 
285 eject_mount_cb (GPid pid, gint status, gpointer user_data)
286 {
287   EjectMountOp *data = user_data;
288   GSimpleAsyncResult *simple;
289   
290   if (WEXITSTATUS (status) != 0)
291     {
292       GError *error;
293       error = g_error_new_literal (G_IO_ERROR, 
294                                    G_IO_ERROR_FAILED,
295                                    data->error_string->str);
296       simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
297                                                      data->callback,
298                                                      data->user_data,
299                                                      error);
300       g_error_free (error);
301     }
302   else
303     {
304       simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
305                                           data->callback,
306                                           data->user_data,
307                                           NULL);
308     }
309
310   g_simple_async_result_complete (simple);
311   g_object_unref (simple);
312
313   g_source_remove (data->error_channel_source_id);
314   g_io_channel_unref (data->error_channel);
315   g_string_free (data->error_string, TRUE);
316   close (data->error_fd);
317   g_spawn_close_pid (pid);
318   g_free (data);
319 }
320
321 static gboolean
322 eject_mount_read_error (GIOChannel *channel,
323                   GIOCondition condition,
324                   gpointer user_data)
325 {
326   char *str;
327   gsize str_len;
328   EjectMountOp *data = user_data;
329
330   g_io_channel_read_to_end (channel, &str, &str_len, NULL);
331   g_string_append (data->error_string, str);
332   g_free (str);
333   return TRUE;
334 }
335
336 static void
337 eject_mount_do (GVolume             *volume,
338                 GCancellable        *cancellable,
339                 GAsyncReadyCallback  callback,
340                 gpointer             user_data,
341                 char               **argv)
342 {
343   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
344   EjectMountOp *data;
345   GPid child_pid;
346   GError *error;
347   
348   data = g_new0 (EjectMountOp, 1);
349   data->unix_volume = unix_volume;
350   data->callback = callback;
351   data->user_data = user_data;
352   data->cancellable = cancellable;
353   
354   error = NULL;
355   if (!g_spawn_async_with_pipes (NULL,         /* working dir */
356                                  argv,
357                                  NULL,         /* envp */
358                                  G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
359                                  NULL,         /* child_setup */
360                                  NULL,         /* user_data for child_setup */
361                                  &child_pid,
362                                  NULL,           /* standard_input */
363                                  NULL,           /* standard_output */
364                                  &(data->error_fd),
365                                  &error)) {
366     GSimpleAsyncResult *simple;
367     simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
368                                                    data->callback,
369                                                    data->user_data,
370                                                    error);
371     g_simple_async_result_complete (simple);
372     g_object_unref (simple);
373     g_error_free (error);
374     g_free (data);
375     return;
376   }
377   data->error_string = g_string_new ("");
378   data->error_channel = g_io_channel_unix_new (data->error_fd);
379   data->error_channel_source_id = g_io_add_watch (data->error_channel, G_IO_IN, eject_mount_read_error, data);
380   g_child_watch_add (child_pid, eject_mount_cb, data);
381 }
382
383
384 static void
385 g_unix_volume_mount (GVolume    *volume,
386                      GMountOperation     *mount_operation,
387                      GCancellable        *cancellable,
388                      GAsyncReadyCallback  callback,
389                      gpointer             user_data)
390 {
391   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
392   char *argv[] = {"mount", NULL, NULL};
393
394   if (unix_volume->mount_path != NULL)
395     argv[1] = unix_volume->mount_path;
396   else
397     argv[1] = unix_volume->device_path;
398
399   eject_mount_do (volume, cancellable, callback, user_data, argv);
400 }
401
402 static gboolean
403 g_unix_volume_mount_finish (GVolume        *volume,
404                            GAsyncResult  *result,
405                            GError       **error)
406 {
407   return TRUE;
408 }
409
410 static void
411 g_unix_volume_eject (GVolume    *volume,
412                      GMountUnmountFlags   flags,
413                      GCancellable        *cancellable,
414                      GAsyncReadyCallback  callback,
415                      gpointer             user_data)
416 {
417   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
418   char *argv[] = {"eject", NULL, NULL};
419
420   argv[1] = unix_volume->device_path;
421
422   eject_mount_do (volume, cancellable, callback, user_data, argv);
423 }
424
425 static gboolean
426 g_unix_volume_eject_finish (GVolume        *volume,
427                             GAsyncResult  *result,
428                             GError       **error)
429 {
430   return TRUE;
431 }
432
433 static char *
434 g_unix_volume_get_identifier (GVolume              *volume,
435                               const char          *kind)
436 {
437   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
438
439   if (strcmp (kind, unix_volume->identifier_type) == 0)
440     return g_strdup (unix_volume->identifier);
441   return NULL;
442 }
443
444 static char **
445 g_unix_volume_enumerate_identifiers (GVolume *volume)
446 {
447   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
448   char **res;
449
450   if (unix_volume->identifier_type)
451     {
452       res = g_new (char *, 2);
453       res[0] = g_strdup (unix_volume->identifier_type);
454       res[1] = NULL;
455     }
456   else
457     {
458       res = g_new (char *, 1);
459       res[0] = NULL;
460     }
461
462   return res;
463 }
464
465
466 static void
467 g_unix_volume_volume_iface_init (GVolumeIface *iface)
468 {
469   iface->get_name = g_unix_volume_get_name;
470   iface->get_icon = g_unix_volume_get_icon;
471   iface->get_uuid = g_unix_volume_get_uuid;
472   iface->get_drive = g_unix_volume_get_drive;
473   iface->get_mount = g_unix_volume_get_mount;
474   iface->can_mount = g_unix_volume_can_mount;
475   iface->can_eject = g_unix_volume_can_eject;
476   iface->mount_fn = g_unix_volume_mount;
477   iface->mount_finish = g_unix_volume_mount_finish;
478   iface->eject = g_unix_volume_eject;
479   iface->eject_finish = g_unix_volume_eject_finish;
480   iface->get_identifier = g_unix_volume_get_identifier;
481   iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;
482 }