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