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, 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>
33 #include "gunixvolumemonitor.h"
34 #include "gunixmount.h"
35 #include "gunixvolume.h"
36 #include "gmountprivate.h"
37 #include "gvolumemonitor.h"
38 #include "gthemedicon.h"
39 #include "gsimpleasyncresult.h"
47 GVolumeMonitor *volume_monitor;
49 GUnixVolume *volume; /* owned by volume monitor */
57 static void g_unix_mount_mount_iface_init (GMountIface *iface);
59 #define g_unix_mount_get_type _g_unix_mount_get_type
60 G_DEFINE_TYPE_WITH_CODE (GUnixMount, g_unix_mount, G_TYPE_OBJECT,
61 G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
62 g_unix_mount_mount_iface_init))
66 g_unix_mount_finalize (GObject *object)
70 mount = G_UNIX_MOUNT (object);
72 if (mount->volume_monitor != NULL)
73 g_object_unref (mount->volume_monitor);
76 _g_unix_volume_unset_mount (mount->volume, mount);
78 //TODO: g_warn_if_fail (volume->volume == NULL);
79 g_object_unref (mount->icon);
81 g_free (mount->device_path);
82 g_free (mount->mount_path);
84 if (G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize)
85 (*G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize) (object);
89 g_unix_mount_class_init (GUnixMountClass *klass)
91 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
93 gobject_class->finalize = g_unix_mount_finalize;
97 g_unix_mount_init (GUnixMount *unix_mount)
102 _g_unix_mount_new (GVolumeMonitor *volume_monitor,
103 GUnixMountEntry *mount_entry,
108 /* No volume for mount: Ignore internal things */
109 if (volume == NULL && g_unix_mount_is_system_internal (mount_entry))
112 mount = g_object_new (G_TYPE_UNIX_MOUNT, NULL);
113 mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
114 mount->device_path = g_strdup (g_unix_mount_get_device_path (mount_entry));
115 mount->mount_path = g_strdup (g_unix_mount_get_mount_path (mount_entry));
116 mount->name = g_unix_mount_guess_name (mount_entry);
117 mount->icon = g_unix_mount_guess_icon (mount_entry);
119 /* need to do this last */
120 mount->volume = volume;
122 _g_unix_volume_set_mount (volume, mount);
128 _g_unix_mount_unmounted (GUnixMount *mount)
130 if (mount->volume != NULL)
132 _g_unix_volume_unset_mount (mount->volume, mount);
133 mount->volume = NULL;
134 g_signal_emit_by_name (mount, "changed");
135 /* there's really no need to emit mount_changed on the volume monitor
136 * as we're going to be deleted.. */
141 _g_unix_mount_unset_volume (GUnixMount *mount,
144 if (mount->volume == volume)
146 mount->volume = NULL;
147 /* TODO: Emit changed in idle to avoid locking issues */
148 g_signal_emit_by_name (mount, "changed");
149 if (mount->volume_monitor != NULL)
150 g_signal_emit_by_name (mount->volume_monitor, "mount_changed", mount);
155 g_unix_mount_get_root (GMount *mount)
157 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
159 return g_file_new_for_path (unix_mount->mount_path);
163 g_unix_mount_get_icon (GMount *mount)
165 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
167 return g_object_ref (unix_mount->icon);
171 g_unix_mount_get_name (GMount *mount)
173 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
175 return g_strdup (unix_mount->name);
179 _g_unix_mount_has_mount_path (GUnixMount *mount,
180 const char *mount_path)
182 return strcmp (mount->mount_path, mount_path) == 0;
186 g_unix_mount_get_drive (GMount *mount)
188 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
190 if (unix_mount->volume != NULL)
191 return g_volume_get_drive (G_VOLUME (unix_mount->volume));
197 g_unix_mount_get_volume (GMount *mount)
199 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
201 if (unix_mount->volume)
202 return G_VOLUME (g_object_ref (unix_mount->volume));
208 g_unix_mount_can_unmount (GMount *mount)
215 GUnixMount *unix_mount;
216 GAsyncReadyCallback callback;
218 GCancellable *cancellable;
220 GIOChannel *error_channel;
221 guint error_channel_source_id;
222 GString *error_string;
226 unmount_cb (GPid pid, gint status, gpointer user_data)
228 UnmountOp *data = user_data;
229 GSimpleAsyncResult *simple;
231 if (WEXITSTATUS (status) != 0)
234 error = g_error_new_literal (G_IO_ERROR,
236 data->error_string->str);
237 simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_mount),
241 g_error_free (error);
245 simple = g_simple_async_result_new (G_OBJECT (data->unix_mount),
251 g_simple_async_result_complete (simple);
252 g_object_unref (simple);
254 g_source_remove (data->error_channel_source_id);
255 g_io_channel_unref (data->error_channel);
256 g_string_free (data->error_string, TRUE);
257 close (data->error_fd);
258 g_spawn_close_pid (pid);
263 unmount_read_error (GIOChannel *channel,
264 GIOCondition condition,
269 UnmountOp *data = user_data;
271 g_io_channel_read_to_end (channel, &str, &str_len, NULL);
272 g_string_append (data->error_string, str);
278 g_unix_mount_unmount (GMount *mount,
279 GCancellable *cancellable,
280 GAsyncReadyCallback callback,
283 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
287 char *argv[] = {"umount", NULL, NULL};
289 if (unix_mount->mount_path != NULL)
290 argv[1] = unix_mount->mount_path;
292 argv[1] = unix_mount->device_path;
294 data = g_new0 (UnmountOp, 1);
295 data->unix_mount = unix_mount;
296 data->callback = callback;
297 data->user_data = user_data;
298 data->cancellable = cancellable;
301 if (!g_spawn_async_with_pipes (NULL, /* working dir */
304 G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
305 NULL, /* child_setup */
306 NULL, /* user_data for child_setup */
308 NULL, /* standard_input */
309 NULL, /* standard_output */
312 GSimpleAsyncResult *simple;
313 simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_mount),
317 g_simple_async_result_complete (simple);
318 g_object_unref (simple);
319 g_error_free (error);
323 data->error_string = g_string_new ("");
324 data->error_channel = g_io_channel_unix_new (data->error_fd);
325 data->error_channel_source_id = g_io_add_watch (data->error_channel, G_IO_IN, unmount_read_error, data);
326 g_child_watch_add (child_pid, unmount_cb, data);
330 g_unix_mount_unmount_finish (GMount *mount,
331 GAsyncResult *result,
338 g_unix_mount_mount_iface_init (GMountIface *iface)
340 iface->get_root = g_unix_mount_get_root;
341 iface->get_name = g_unix_mount_get_name;
342 iface->get_icon = g_unix_mount_get_icon;
343 iface->get_drive = g_unix_mount_get_drive;
344 iface->get_volume = g_unix_mount_get_volume;
345 iface->can_unmount = g_unix_mount_can_unmount;
346 iface->unmount = g_unix_mount_unmount;
347 iface->unmount_finish = g_unix_mount_unmount_finish;