1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
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, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
21 * David Zeuthen <davidz@redhat.com>
31 #include "gunixvolumemonitor.h"
32 #include "gunixmount.h"
33 #include "gunixmounts.h"
34 #include "gunixvolume.h"
35 #include "gmountprivate.h"
38 #include "gvolumemonitor.h"
39 #include "gthemedicon.h"
40 #include "gsimpleasyncresult.h"
50 GVolumeMonitor *volume_monitor;
52 GUnixVolume *volume; /* owned by volume monitor */
63 static void g_unix_mount_mount_iface_init (GMountIface *iface);
65 #define g_unix_mount_get_type _g_unix_mount_get_type
66 G_DEFINE_TYPE_WITH_CODE (GUnixMount, g_unix_mount, G_TYPE_OBJECT,
67 G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
68 g_unix_mount_mount_iface_init))
72 g_unix_mount_finalize (GObject *object)
76 mount = G_UNIX_MOUNT (object);
78 if (mount->volume_monitor != NULL)
79 g_object_unref (mount->volume_monitor);
82 _g_unix_volume_unset_mount (mount->volume, mount);
84 /* TODO: g_warn_if_fail (volume->volume == NULL); */
85 g_object_unref (mount->icon);
86 g_object_unref (mount->symbolic_icon);
88 g_free (mount->device_path);
89 g_free (mount->mount_path);
91 G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize (object);
95 g_unix_mount_class_init (GUnixMountClass *klass)
97 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
99 gobject_class->finalize = g_unix_mount_finalize;
103 g_unix_mount_init (GUnixMount *unix_mount)
108 _g_unix_mount_new (GVolumeMonitor *volume_monitor,
109 GUnixMountEntry *mount_entry,
114 /* No volume for mount: Ignore internal things */
115 if (volume == NULL && !g_unix_mount_guess_should_display (mount_entry))
118 mount = g_object_new (G_TYPE_UNIX_MOUNT, NULL);
119 mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
120 mount->device_path = g_strdup (g_unix_mount_get_device_path (mount_entry));
121 mount->mount_path = g_strdup (g_unix_mount_get_mount_path (mount_entry));
122 mount->can_eject = g_unix_mount_guess_can_eject (mount_entry);
124 mount->name = g_unix_mount_guess_name (mount_entry);
125 mount->icon = g_unix_mount_guess_icon (mount_entry);
126 mount->symbolic_icon = g_unix_mount_guess_symbolic_icon (mount_entry);
128 /* need to do this last */
129 mount->volume = volume;
131 _g_unix_volume_set_mount (volume, mount);
137 _g_unix_mount_unmounted (GUnixMount *mount)
139 if (mount->volume != NULL)
141 _g_unix_volume_unset_mount (mount->volume, mount);
142 mount->volume = NULL;
143 g_signal_emit_by_name (mount, "changed");
144 /* there's really no need to emit mount_changed on the volume monitor
145 * as we're going to be deleted.. */
150 _g_unix_mount_unset_volume (GUnixMount *mount,
153 if (mount->volume == volume)
155 mount->volume = NULL;
156 /* TODO: Emit changed in idle to avoid locking issues */
157 g_signal_emit_by_name (mount, "changed");
158 if (mount->volume_monitor != NULL)
159 g_signal_emit_by_name (mount->volume_monitor, "mount-changed", mount);
164 g_unix_mount_get_root (GMount *mount)
166 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
168 return g_file_new_for_path (unix_mount->mount_path);
172 g_unix_mount_get_icon (GMount *mount)
174 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
176 return g_object_ref (unix_mount->icon);
180 g_unix_mount_get_symbolic_icon (GMount *mount)
182 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
184 return g_object_ref (unix_mount->symbolic_icon);
188 g_unix_mount_get_uuid (GMount *mount)
194 g_unix_mount_get_name (GMount *mount)
196 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
198 return g_strdup (unix_mount->name);
202 _g_unix_mount_has_mount_path (GUnixMount *mount,
203 const char *mount_path)
205 return strcmp (mount->mount_path, mount_path) == 0;
209 g_unix_mount_get_drive (GMount *mount)
211 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
213 if (unix_mount->volume != NULL)
214 return g_volume_get_drive (G_VOLUME (unix_mount->volume));
220 g_unix_mount_get_volume (GMount *mount)
222 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
224 if (unix_mount->volume)
225 return G_VOLUME (g_object_ref (unix_mount->volume));
231 g_unix_mount_can_unmount (GMount *mount)
237 g_unix_mount_can_eject (GMount *mount)
239 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
240 return unix_mount->can_eject;
245 GUnixMount *unix_mount;
247 GIOChannel *error_channel;
248 GSource *error_channel_source;
249 GString *error_string;
254 unmount_eject_op_free (UnmountEjectOp *data)
256 if (data->error_channel_source)
258 g_source_destroy (data->error_channel_source);
259 g_source_unref (data->error_channel_source);
261 g_io_channel_unref (data->error_channel);
262 g_string_free (data->error_string, TRUE);
263 g_strfreev (data->argv);
264 close (data->error_fd);
269 eject_unmount_cb (GPid pid, gint status, gpointer user_data)
271 GTask *task = user_data;
272 UnmountEjectOp *data = g_task_get_task_data (task);
274 if (g_task_return_error_if_cancelled (task))
277 g_spawn_close_pid (pid);
279 if (WEXITSTATUS (status) != 0)
281 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
282 "%s", data->error_string->str);
285 g_task_return_boolean (task, TRUE);
287 g_object_unref (task);
291 eject_unmount_read_error (GIOChannel *channel,
292 GIOCondition condition,
295 GTask *task = user_data;
296 UnmountEjectOp *data = g_task_get_task_data (task);
302 if (g_task_return_error_if_cancelled (task))
307 status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
308 if (status == G_IO_STATUS_NORMAL)
310 g_string_append_len (data->error_string, buf, bytes_read);
311 if (bytes_read == sizeof (buf))
314 else if (status == G_IO_STATUS_EOF)
315 g_string_append_len (data->error_string, buf, bytes_read);
316 else if (status == G_IO_STATUS_ERROR)
318 if (data->error_string->len > 0)
319 g_string_append (data->error_string, "\n");
321 g_string_append (data->error_string, error->message);
322 g_error_free (error);
324 if (data->error_channel_source)
326 g_source_unref (data->error_channel_source);
327 data->error_channel_source = NULL;
336 eject_unmount_do_cb (gpointer user_data)
338 GTask *task = user_data;
339 UnmountEjectOp *data = g_task_get_task_data (task);
341 GSource *child_watch;
342 GError *error = NULL;
344 if (g_task_return_error_if_cancelled (task))
345 return G_SOURCE_REMOVE;
347 if (!g_spawn_async_with_pipes (NULL, /* working dir */
350 G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
351 NULL, /* child_setup */
352 NULL, /* user_data for child_setup */
354 NULL, /* standard_input */
355 NULL, /* standard_output */
358 g_assert (error != NULL);
362 data->error_string = g_string_new ("");
364 data->error_channel = g_io_channel_unix_new (data->error_fd);
365 g_io_channel_set_flags (data->error_channel, G_IO_FLAG_NONBLOCK, &error);
369 data->error_channel_source = g_io_create_watch (data->error_channel, G_IO_IN);
370 g_task_attach_source (task, data->error_channel_source,
371 (GSourceFunc) eject_unmount_read_error);
373 child_watch = g_child_watch_source_new (child_pid);
374 g_task_attach_source (task, data->error_channel_source,
375 (GSourceFunc) eject_unmount_cb);
376 g_source_unref (child_watch);
381 g_task_return_error (task, error);
382 g_object_unref (task);
385 return G_SOURCE_REMOVE;
389 eject_unmount_do (GMount *mount,
390 GCancellable *cancellable,
391 GAsyncReadyCallback callback,
395 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
396 UnmountEjectOp *data;
400 data = g_new0 (UnmountEjectOp, 1);
401 data->argv = g_strdupv (argv);
403 task = g_task_new (mount, cancellable, callback, user_data);
404 g_task_set_task_data (task, data, (GDestroyNotify)unmount_eject_op_free);
406 if (unix_mount->volume_monitor != NULL)
407 g_signal_emit_by_name (unix_mount->volume_monitor, "mount-pre-unmount", mount);
409 g_signal_emit_by_name (mount, "pre-unmount", 0);
411 timeout = g_timeout_source_new (500);
412 g_task_attach_source (task, timeout, (GSourceFunc) eject_unmount_do_cb);
413 g_source_unref (timeout);
417 g_unix_mount_unmount (GMount *mount,
418 GMountUnmountFlags flags,
419 GCancellable *cancellable,
420 GAsyncReadyCallback callback,
423 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
424 char *argv[] = {"umount", NULL, NULL};
426 if (unix_mount->mount_path != NULL)
427 argv[1] = unix_mount->mount_path;
429 argv[1] = unix_mount->device_path;
431 eject_unmount_do (mount, cancellable, callback, user_data, argv);
435 g_unix_mount_unmount_finish (GMount *mount,
436 GAsyncResult *result,
439 return g_task_propagate_boolean (G_TASK (result), error);
443 g_unix_mount_eject (GMount *mount,
444 GMountUnmountFlags flags,
445 GCancellable *cancellable,
446 GAsyncReadyCallback callback,
449 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
450 char *argv[] = {"eject", NULL, NULL};
452 if (unix_mount->mount_path != NULL)
453 argv[1] = unix_mount->mount_path;
455 argv[1] = unix_mount->device_path;
457 eject_unmount_do (mount, cancellable, callback, user_data, argv);
461 g_unix_mount_eject_finish (GMount *mount,
462 GAsyncResult *result,
465 return g_task_propagate_boolean (G_TASK (result), error);
469 g_unix_mount_mount_iface_init (GMountIface *iface)
471 iface->get_root = g_unix_mount_get_root;
472 iface->get_name = g_unix_mount_get_name;
473 iface->get_icon = g_unix_mount_get_icon;
474 iface->get_symbolic_icon = g_unix_mount_get_symbolic_icon;
475 iface->get_uuid = g_unix_mount_get_uuid;
476 iface->get_drive = g_unix_mount_get_drive;
477 iface->get_volume = g_unix_mount_get_volume;
478 iface->can_unmount = g_unix_mount_can_unmount;
479 iface->can_eject = g_unix_mount_can_eject;
480 iface->unmount = g_unix_mount_unmount;
481 iface->unmount_finish = g_unix_mount_unmount_finish;
482 iface->eject = g_unix_mount_eject;
483 iface->eject_finish = g_unix_mount_eject_finish;