gdbus tests: remove buggy use of GMainLoop
[glib.git] / gio / gunixvolume.c
blob1dc840b22e9d4233663c4940b136c7732f17ecfb
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
4 *
5 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
22 * Author: Alexander Larsson <alexl@redhat.com>
23 * David Zeuthen <davidz@redhat.com>
26 #include "config.h"
28 #include <string.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
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>
46 struct _GUnixVolume {
47 GObject parent;
49 GVolumeMonitor *volume_monitor;
50 GUnixMount *mount; /* owned by volume monitor */
52 char *device_path;
53 char *mount_path;
54 gboolean can_eject;
56 char *identifier;
57 char *identifier_type;
59 char *name;
60 GIcon *icon;
63 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
65 #define g_unix_volume_get_type _g_unix_volume_get_type
66 G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
67 G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
68 g_unix_volume_volume_iface_init))
70 static void
71 g_unix_volume_finalize (GObject *object)
73 GUnixVolume *volume;
75 volume = G_UNIX_VOLUME (object);
77 if (volume->volume_monitor != NULL)
78 g_object_unref (volume->volume_monitor);
80 if (volume->mount)
81 _g_unix_mount_unset_volume (volume->mount, volume);
83 g_object_unref (volume->icon);
84 g_free (volume->name);
85 g_free (volume->mount_path);
86 g_free (volume->device_path);
87 g_free (volume->identifier);
88 g_free (volume->identifier_type);
90 G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize (object);
93 static void
94 g_unix_volume_class_init (GUnixVolumeClass *klass)
96 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
98 gobject_class->finalize = g_unix_volume_finalize;
101 static void
102 g_unix_volume_init (GUnixVolume *unix_volume)
106 GUnixVolume *
107 _g_unix_volume_new (GVolumeMonitor *volume_monitor,
108 GUnixMountPoint *mountpoint)
110 GUnixVolume *volume;
112 if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
113 g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
114 g_unix_mount_point_is_loopback (mountpoint))
115 return NULL;
117 volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
118 volume->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
119 volume->mount_path = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
120 volume->device_path = g_strdup (g_unix_mount_point_get_device_path (mountpoint));
121 volume->can_eject = g_unix_mount_point_guess_can_eject (mountpoint);
123 volume->name = g_unix_mount_point_guess_name (mountpoint);
124 volume->icon = g_unix_mount_point_guess_icon (mountpoint);
127 if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
129 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
130 volume->identifier = g_strdup (volume->device_path);
132 else if (g_str_has_prefix (volume->device_path, "LABEL="))
134 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
135 volume->identifier = g_strdup (volume->device_path + 6);
137 else if (g_str_has_prefix (volume->device_path, "UUID="))
139 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
140 volume->identifier = g_strdup (volume->device_path + 5);
142 else if (g_path_is_absolute (volume->device_path))
144 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
145 volume->identifier = g_strdup (volume->device_path);
148 return volume;
151 void
152 _g_unix_volume_disconnected (GUnixVolume *volume)
154 if (volume->mount)
156 _g_unix_mount_unset_volume (volume->mount, volume);
157 volume->mount = NULL;
161 void
162 _g_unix_volume_set_mount (GUnixVolume *volume,
163 GUnixMount *mount)
165 if (volume->mount == mount)
166 return;
168 if (volume->mount)
169 _g_unix_mount_unset_volume (volume->mount, volume);
171 volume->mount = mount;
173 /* TODO: Emit changed in idle to avoid locking issues */
174 g_signal_emit_by_name (volume, "changed");
175 if (volume->volume_monitor != NULL)
176 g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
179 void
180 _g_unix_volume_unset_mount (GUnixVolume *volume,
181 GUnixMount *mount)
183 if (volume->mount == mount)
185 volume->mount = NULL;
186 /* TODO: Emit changed in idle to avoid locking issues */
187 g_signal_emit_by_name (volume, "changed");
188 if (volume->volume_monitor != NULL)
189 g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
193 static GIcon *
194 g_unix_volume_get_icon (GVolume *volume)
196 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
197 return g_object_ref (unix_volume->icon);
200 static char *
201 g_unix_volume_get_name (GVolume *volume)
203 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
204 return g_strdup (unix_volume->name);
207 static char *
208 g_unix_volume_get_uuid (GVolume *volume)
210 return NULL;
213 static gboolean
214 g_unix_volume_can_mount (GVolume *volume)
216 return TRUE;
219 static gboolean
220 g_unix_volume_can_eject (GVolume *volume)
222 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
223 return unix_volume->can_eject;
226 static gboolean
227 g_unix_volume_should_automount (GVolume *volume)
229 /* We automount all local volumes because we don't even
230 * make the internal stuff visible
232 return TRUE;
235 static GDrive *
236 g_unix_volume_get_drive (GVolume *volume)
238 return NULL;
241 static GMount *
242 g_unix_volume_get_mount (GVolume *volume)
244 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
246 if (unix_volume->mount != NULL)
247 return g_object_ref (unix_volume->mount);
249 return NULL;
253 gboolean
254 _g_unix_volume_has_mount_path (GUnixVolume *volume,
255 const char *mount_path)
257 return strcmp (volume->mount_path, mount_path) == 0;
261 typedef struct {
262 GUnixVolume *unix_volume;
263 GAsyncReadyCallback callback;
264 gpointer user_data;
265 GCancellable *cancellable;
266 int error_fd;
267 GIOChannel *error_channel;
268 GSource *error_channel_source;
269 GString *error_string;
270 } EjectMountOp;
272 static void
273 eject_mount_cb (GPid pid,
274 gint status,
275 gpointer user_data)
277 EjectMountOp *data = user_data;
278 GSimpleAsyncResult *simple;
280 if (WEXITSTATUS (status) != 0)
282 GError *error;
283 error = g_error_new_literal (G_IO_ERROR,
284 G_IO_ERROR_FAILED,
285 data->error_string->str);
286 simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
287 data->callback,
288 data->user_data,
289 error);
290 g_error_free (error);
292 else
294 simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
295 data->callback,
296 data->user_data,
297 NULL);
300 g_simple_async_result_complete (simple);
301 g_object_unref (simple);
303 if (data->error_channel_source)
305 g_source_destroy (data->error_channel_source);
306 g_source_unref (data->error_channel_source);
308 g_io_channel_unref (data->error_channel);
309 g_string_free (data->error_string, TRUE);
310 close (data->error_fd);
311 g_spawn_close_pid (pid);
312 g_free (data);
315 static gboolean
316 eject_mount_read_error (GIOChannel *channel,
317 GIOCondition condition,
318 gpointer user_data)
320 EjectMountOp *data = user_data;
321 char buf[BUFSIZ];
322 gsize bytes_read;
323 GError *error;
324 GIOStatus status;
326 error = NULL;
327 read:
328 status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
329 if (status == G_IO_STATUS_NORMAL)
331 g_string_append_len (data->error_string, buf, bytes_read);
332 if (bytes_read == sizeof (buf))
333 goto read;
335 else if (status == G_IO_STATUS_EOF)
336 g_string_append_len (data->error_string, buf, bytes_read);
337 else if (status == G_IO_STATUS_ERROR)
339 if (data->error_string->len > 0)
340 g_string_append (data->error_string, "\n");
342 g_string_append (data->error_string, error->message);
343 g_error_free (error);
345 if (data->error_channel_source)
347 g_source_unref (data->error_channel_source);
348 data->error_channel_source = NULL;
350 return FALSE;
353 return TRUE;
356 static void
357 eject_mount_do (GVolume *volume,
358 GCancellable *cancellable,
359 GAsyncReadyCallback callback,
360 gpointer user_data,
361 char **argv)
363 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
364 EjectMountOp *data;
365 GPid child_pid;
366 GSource *child_watch;
367 GError *error;
369 data = g_new0 (EjectMountOp, 1);
370 data->unix_volume = unix_volume;
371 data->callback = callback;
372 data->user_data = user_data;
373 data->cancellable = cancellable;
375 error = NULL;
376 if (!g_spawn_async_with_pipes (NULL, /* working dir */
377 argv,
378 NULL, /* envp */
379 G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
380 NULL, /* child_setup */
381 NULL, /* user_data for child_setup */
382 &child_pid,
383 NULL, /* standard_input */
384 NULL, /* standard_output */
385 &(data->error_fd),
386 &error))
388 g_assert (error != NULL);
389 goto handle_error;
392 data->error_string = g_string_new ("");
394 data->error_channel = g_io_channel_unix_new (data->error_fd);
395 g_io_channel_set_flags (data->error_channel, G_IO_FLAG_NONBLOCK, &error);
396 if (error != NULL)
397 goto handle_error;
399 data->error_channel_source = g_io_create_watch (data->error_channel, G_IO_IN);
400 g_source_set_callback (data->error_channel_source,
401 (GSourceFunc) eject_mount_read_error, data, NULL);
402 g_source_attach (data->error_channel_source, g_main_context_get_thread_default ());
404 child_watch = g_child_watch_source_new (child_pid);
405 g_source_set_callback (child_watch, (GSourceFunc) eject_mount_cb, data, NULL);
406 g_source_attach (child_watch, g_main_context_get_thread_default ());
407 g_source_unref (child_watch);
409 handle_error:
410 if (error != NULL)
412 GSimpleAsyncResult *simple;
413 simple = g_simple_async_result_new_take_error (G_OBJECT (data->unix_volume),
414 data->callback,
415 data->user_data,
416 error);
417 g_simple_async_result_complete (simple);
418 g_object_unref (simple);
420 if (data->error_string != NULL)
421 g_string_free (data->error_string, TRUE);
423 if (data->error_channel != NULL)
424 g_io_channel_unref (data->error_channel);
426 g_free (data);
431 static void
432 g_unix_volume_mount (GVolume *volume,
433 GMountMountFlags flags,
434 GMountOperation *mount_operation,
435 GCancellable *cancellable,
436 GAsyncReadyCallback callback,
437 gpointer user_data)
439 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
440 char *argv[] = { "mount", NULL, NULL };
442 if (unix_volume->mount_path != NULL)
443 argv[1] = unix_volume->mount_path;
444 else
445 argv[1] = unix_volume->device_path;
447 eject_mount_do (volume, cancellable, callback, user_data, argv);
450 static gboolean
451 g_unix_volume_mount_finish (GVolume *volume,
452 GAsyncResult *result,
453 GError **error)
455 return TRUE;
458 static void
459 g_unix_volume_eject (GVolume *volume,
460 GMountUnmountFlags flags,
461 GCancellable *cancellable,
462 GAsyncReadyCallback callback,
463 gpointer user_data)
465 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
466 char *argv[] = { "eject", NULL, NULL };
468 argv[1] = unix_volume->device_path;
470 eject_mount_do (volume, cancellable, callback, user_data, argv);
473 static gboolean
474 g_unix_volume_eject_finish (GVolume *volume,
475 GAsyncResult *result,
476 GError **error)
478 return TRUE;
481 static gchar *
482 g_unix_volume_get_identifier (GVolume *volume,
483 const gchar *kind)
485 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
487 if (unix_volume->identifier_type != NULL &&
488 strcmp (kind, unix_volume->identifier_type) == 0)
489 return g_strdup (unix_volume->identifier);
491 return NULL;
494 static gchar **
495 g_unix_volume_enumerate_identifiers (GVolume *volume)
497 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
498 gchar **res;
500 if (unix_volume->identifier_type)
502 res = g_new (gchar *, 2);
503 res[0] = g_strdup (unix_volume->identifier_type);
504 res[1] = NULL;
506 else
508 res = g_new (gchar *, 1);
509 res[0] = NULL;
512 return res;
515 static void
516 g_unix_volume_volume_iface_init (GVolumeIface *iface)
518 iface->get_name = g_unix_volume_get_name;
519 iface->get_icon = g_unix_volume_get_icon;
520 iface->get_uuid = g_unix_volume_get_uuid;
521 iface->get_drive = g_unix_volume_get_drive;
522 iface->get_mount = g_unix_volume_get_mount;
523 iface->can_mount = g_unix_volume_can_mount;
524 iface->can_eject = g_unix_volume_can_eject;
525 iface->should_automount = g_unix_volume_should_automount;
526 iface->mount_fn = g_unix_volume_mount;
527 iface->mount_finish = g_unix_volume_mount_finish;
528 iface->eject = g_unix_volume_eject;
529 iface->eject_finish = g_unix_volume_eject_finish;
530 iface->get_identifier = g_unix_volume_get_identifier;
531 iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;