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