Simplify glib/glib/tests setup
[glib.git] / gio / gunixmount.c
blob3f79714ea386e8fd1e5e97464edca9ad8674afd2
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 "gunixvolumemonitor.h"
34 #include "gunixmount.h"
35 #include "gunixmounts.h"
36 #include "gunixvolume.h"
37 #include "gmountprivate.h"
38 #include "gmount.h"
39 #include "gfile.h"
40 #include "gvolumemonitor.h"
41 #include "gthemedicon.h"
42 #include "gsimpleasyncresult.h"
43 #include "gioerror.h"
44 #include "glibintl.h"
45 /* for BUFSIZ */
46 #include <stdio.h>
49 struct _GUnixMount {
50 GObject parent;
52 GVolumeMonitor *volume_monitor;
54 GUnixVolume *volume; /* owned by volume monitor */
56 char *name;
57 GIcon *icon;
58 GIcon *symbolic_icon;
59 char *device_path;
60 char *mount_path;
62 gboolean can_eject;
65 static void g_unix_mount_mount_iface_init (GMountIface *iface);
67 #define g_unix_mount_get_type _g_unix_mount_get_type
68 G_DEFINE_TYPE_WITH_CODE (GUnixMount, g_unix_mount, G_TYPE_OBJECT,
69 G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
70 g_unix_mount_mount_iface_init))
73 static void
74 g_unix_mount_finalize (GObject *object)
76 GUnixMount *mount;
78 mount = G_UNIX_MOUNT (object);
80 if (mount->volume_monitor != NULL)
81 g_object_unref (mount->volume_monitor);
83 if (mount->volume)
84 _g_unix_volume_unset_mount (mount->volume, mount);
86 /* TODO: g_warn_if_fail (volume->volume == NULL); */
87 g_object_unref (mount->icon);
88 g_object_unref (mount->symbolic_icon);
89 g_free (mount->name);
90 g_free (mount->device_path);
91 g_free (mount->mount_path);
93 G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize (object);
96 static void
97 g_unix_mount_class_init (GUnixMountClass *klass)
99 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
101 gobject_class->finalize = g_unix_mount_finalize;
104 static void
105 g_unix_mount_init (GUnixMount *unix_mount)
109 GUnixMount *
110 _g_unix_mount_new (GVolumeMonitor *volume_monitor,
111 GUnixMountEntry *mount_entry,
112 GUnixVolume *volume)
114 GUnixMount *mount;
116 /* No volume for mount: Ignore internal things */
117 if (volume == NULL && !g_unix_mount_guess_should_display (mount_entry))
118 return NULL;
120 mount = g_object_new (G_TYPE_UNIX_MOUNT, NULL);
121 mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
122 mount->device_path = g_strdup (g_unix_mount_get_device_path (mount_entry));
123 mount->mount_path = g_strdup (g_unix_mount_get_mount_path (mount_entry));
124 mount->can_eject = g_unix_mount_guess_can_eject (mount_entry);
126 mount->name = g_unix_mount_guess_name (mount_entry);
127 mount->icon = g_unix_mount_guess_icon (mount_entry);
128 mount->symbolic_icon = g_unix_mount_guess_symbolic_icon (mount_entry);
130 /* need to do this last */
131 mount->volume = volume;
132 if (volume != NULL)
133 _g_unix_volume_set_mount (volume, mount);
135 return mount;
138 void
139 _g_unix_mount_unmounted (GUnixMount *mount)
141 if (mount->volume != NULL)
143 _g_unix_volume_unset_mount (mount->volume, mount);
144 mount->volume = NULL;
145 g_signal_emit_by_name (mount, "changed");
146 /* there's really no need to emit mount_changed on the volume monitor
147 * as we're going to be deleted.. */
151 void
152 _g_unix_mount_unset_volume (GUnixMount *mount,
153 GUnixVolume *volume)
155 if (mount->volume == volume)
157 mount->volume = NULL;
158 /* TODO: Emit changed in idle to avoid locking issues */
159 g_signal_emit_by_name (mount, "changed");
160 if (mount->volume_monitor != NULL)
161 g_signal_emit_by_name (mount->volume_monitor, "mount-changed", mount);
165 static GFile *
166 g_unix_mount_get_root (GMount *mount)
168 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
170 return g_file_new_for_path (unix_mount->mount_path);
173 static GIcon *
174 g_unix_mount_get_icon (GMount *mount)
176 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
178 return g_object_ref (unix_mount->icon);
181 static GIcon *
182 g_unix_mount_get_symbolic_icon (GMount *mount)
184 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
186 return g_object_ref (unix_mount->symbolic_icon);
189 static char *
190 g_unix_mount_get_uuid (GMount *mount)
192 return NULL;
195 static char *
196 g_unix_mount_get_name (GMount *mount)
198 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
200 return g_strdup (unix_mount->name);
203 gboolean
204 _g_unix_mount_has_mount_path (GUnixMount *mount,
205 const char *mount_path)
207 return strcmp (mount->mount_path, mount_path) == 0;
210 static GDrive *
211 g_unix_mount_get_drive (GMount *mount)
213 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
215 if (unix_mount->volume != NULL)
216 return g_volume_get_drive (G_VOLUME (unix_mount->volume));
218 return NULL;
221 static GVolume *
222 g_unix_mount_get_volume (GMount *mount)
224 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
226 if (unix_mount->volume)
227 return G_VOLUME (g_object_ref (unix_mount->volume));
229 return NULL;
232 static gboolean
233 g_unix_mount_can_unmount (GMount *mount)
235 return TRUE;
238 static gboolean
239 g_unix_mount_can_eject (GMount *mount)
241 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
242 return unix_mount->can_eject;
246 typedef struct {
247 GUnixMount *unix_mount;
248 int error_fd;
249 GIOChannel *error_channel;
250 GSource *error_channel_source;
251 GString *error_string;
252 gchar **argv;
253 } UnmountEjectOp;
255 static void
256 unmount_eject_op_free (UnmountEjectOp *data)
258 if (data->error_channel_source)
260 g_source_destroy (data->error_channel_source);
261 g_source_unref (data->error_channel_source);
263 g_io_channel_unref (data->error_channel);
264 g_string_free (data->error_string, TRUE);
265 g_strfreev (data->argv);
266 close (data->error_fd);
267 g_free (data);
270 static void
271 eject_unmount_cb (GPid pid, gint status, gpointer user_data)
273 GTask *task = user_data;
274 UnmountEjectOp *data = g_task_get_task_data (task);
276 if (g_task_return_error_if_cancelled (task))
277 return;
279 g_spawn_close_pid (pid);
281 if (WEXITSTATUS (status) != 0)
283 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
284 "%s", data->error_string->str);
286 else
287 g_task_return_boolean (task, TRUE);
289 g_object_unref (task);
292 static gboolean
293 eject_unmount_read_error (GIOChannel *channel,
294 GIOCondition condition,
295 gpointer user_data)
297 GTask *task = user_data;
298 UnmountEjectOp *data = g_task_get_task_data (task);
299 char buf[BUFSIZ];
300 gsize bytes_read;
301 GError *error;
302 GIOStatus status;
304 if (g_task_return_error_if_cancelled (task))
305 return FALSE;
307 error = NULL;
308 read:
309 status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
310 if (status == G_IO_STATUS_NORMAL)
312 g_string_append_len (data->error_string, buf, bytes_read);
313 if (bytes_read == sizeof (buf))
314 goto read;
316 else if (status == G_IO_STATUS_EOF)
317 g_string_append_len (data->error_string, buf, bytes_read);
318 else if (status == G_IO_STATUS_ERROR)
320 if (data->error_string->len > 0)
321 g_string_append (data->error_string, "\n");
323 g_string_append (data->error_string, error->message);
324 g_error_free (error);
326 if (data->error_channel_source)
328 g_source_unref (data->error_channel_source);
329 data->error_channel_source = NULL;
331 return FALSE;
334 return TRUE;
337 static gboolean
338 eject_unmount_do_cb (gpointer user_data)
340 GTask *task = user_data;
341 UnmountEjectOp *data = g_task_get_task_data (task);
342 GPid child_pid;
343 GSource *child_watch;
344 GError *error = NULL;
346 if (g_task_return_error_if_cancelled (task))
347 return G_SOURCE_REMOVE;
349 if (!g_spawn_async_with_pipes (NULL, /* working dir */
350 data->argv,
351 NULL, /* envp */
352 G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
353 NULL, /* child_setup */
354 NULL, /* user_data for child_setup */
355 &child_pid,
356 NULL, /* standard_input */
357 NULL, /* standard_output */
358 &(data->error_fd),
359 &error)) {
360 g_assert (error != NULL);
361 goto handle_error;
364 data->error_string = g_string_new ("");
366 data->error_channel = g_io_channel_unix_new (data->error_fd);
367 g_io_channel_set_flags (data->error_channel, G_IO_FLAG_NONBLOCK, &error);
368 if (error != NULL)
369 goto handle_error;
371 data->error_channel_source = g_io_create_watch (data->error_channel, G_IO_IN);
372 g_task_attach_source (task, data->error_channel_source,
373 (GSourceFunc) eject_unmount_read_error);
375 child_watch = g_child_watch_source_new (child_pid);
376 g_task_attach_source (task, data->error_channel_source,
377 (GSourceFunc) eject_unmount_cb);
378 g_source_unref (child_watch);
380 handle_error:
381 if (error != NULL)
383 g_task_return_error (task, error);
384 g_object_unref (task);
387 return G_SOURCE_REMOVE;
390 static void
391 eject_unmount_do (GMount *mount,
392 GCancellable *cancellable,
393 GAsyncReadyCallback callback,
394 gpointer user_data,
395 char **argv)
397 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
398 UnmountEjectOp *data;
399 GTask *task;
400 GSource *timeout;
402 data = g_new0 (UnmountEjectOp, 1);
403 data->argv = g_strdupv (argv);
405 task = g_task_new (mount, cancellable, callback, user_data);
406 g_task_set_task_data (task, data, (GDestroyNotify)unmount_eject_op_free);
408 if (unix_mount->volume_monitor != NULL)
409 g_signal_emit_by_name (unix_mount->volume_monitor, "mount-pre-unmount", mount);
411 g_signal_emit_by_name (mount, "pre-unmount", 0);
413 timeout = g_timeout_source_new (500);
414 g_task_attach_source (task, timeout, (GSourceFunc) eject_unmount_do_cb);
415 g_source_unref (timeout);
418 static void
419 g_unix_mount_unmount (GMount *mount,
420 GMountUnmountFlags flags,
421 GCancellable *cancellable,
422 GAsyncReadyCallback callback,
423 gpointer user_data)
425 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
426 char *argv[] = {"umount", NULL, NULL};
428 if (unix_mount->mount_path != NULL)
429 argv[1] = unix_mount->mount_path;
430 else
431 argv[1] = unix_mount->device_path;
433 eject_unmount_do (mount, cancellable, callback, user_data, argv);
436 static gboolean
437 g_unix_mount_unmount_finish (GMount *mount,
438 GAsyncResult *result,
439 GError **error)
441 return g_task_propagate_boolean (G_TASK (result), error);
444 static void
445 g_unix_mount_eject (GMount *mount,
446 GMountUnmountFlags flags,
447 GCancellable *cancellable,
448 GAsyncReadyCallback callback,
449 gpointer user_data)
451 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
452 char *argv[] = {"eject", NULL, NULL};
454 if (unix_mount->mount_path != NULL)
455 argv[1] = unix_mount->mount_path;
456 else
457 argv[1] = unix_mount->device_path;
459 eject_unmount_do (mount, cancellable, callback, user_data, argv);
462 static gboolean
463 g_unix_mount_eject_finish (GMount *mount,
464 GAsyncResult *result,
465 GError **error)
467 return g_task_propagate_boolean (G_TASK (result), error);
470 static void
471 g_unix_mount_mount_iface_init (GMountIface *iface)
473 iface->get_root = g_unix_mount_get_root;
474 iface->get_name = g_unix_mount_get_name;
475 iface->get_icon = g_unix_mount_get_icon;
476 iface->get_symbolic_icon = g_unix_mount_get_symbolic_icon;
477 iface->get_uuid = g_unix_mount_get_uuid;
478 iface->get_drive = g_unix_mount_get_drive;
479 iface->get_volume = g_unix_mount_get_volume;
480 iface->can_unmount = g_unix_mount_can_unmount;
481 iface->can_eject = g_unix_mount_can_eject;
482 iface->unmount = g_unix_mount_unmount;
483 iface->unmount_finish = g_unix_mount_unmount_finish;
484 iface->eject = g_unix_mount_eject;
485 iface->eject_finish = g_unix_mount_eject_finish;