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 "gunixvolume.h"
34 #include "gunixmount.h"
35 #include "gthemedicon.h"
36 #include "gvolumemonitor.h"
37 #include "gsimpleasyncresult.h"
45 GVolumeMonitor *volume_monitor;
46 GUnixMount *mount; /* owned by volume monitor */
53 char *identifier_type;
59 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
61 #define g_unix_volume_get_type _g_unix_volume_get_type
62 G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
63 G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
64 g_unix_volume_volume_iface_init))
67 g_unix_volume_finalize (GObject *object)
71 volume = G_UNIX_VOLUME (object);
73 if (volume->volume_monitor != NULL)
74 g_object_unref (volume->volume_monitor);
77 _g_unix_mount_unset_volume (volume->mount, volume);
79 g_object_unref (volume->icon);
80 g_free (volume->name);
81 g_free (volume->mount_path);
82 g_free (volume->device_path);
83 g_free (volume->identifier);
84 g_free (volume->identifier_type);
86 if (G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize)
87 (*G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize) (object);
91 g_unix_volume_class_init (GUnixVolumeClass *klass)
93 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
95 gobject_class->finalize = g_unix_volume_finalize;
99 g_unix_volume_init (GUnixVolume *unix_volume)
105 * @volume_monitor: a #GVolumeMonitor.
106 * @mountpoint: a #GUnixMountPoint.
108 * Returns: a #GUnixVolume for the given #GUnixMountPoint.
111 _g_unix_volume_new (GVolumeMonitor *volume_monitor,
112 GUnixMountPoint *mountpoint)
116 if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
117 g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
118 g_unix_mount_point_is_loopback (mountpoint))
121 volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
122 volume->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
123 volume->mount_path = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
124 volume->device_path = g_strdup (g_unix_mount_point_get_device_path (mountpoint));
125 volume->can_eject = g_unix_mount_point_guess_can_eject (mountpoint);
127 volume->name = g_unix_mount_point_guess_name (mountpoint);
128 volume->icon = g_unix_mount_point_guess_icon (mountpoint);
131 if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
133 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
134 volume->identifier = g_strdup (volume->device_path);
136 else if (g_str_has_prefix (volume->device_path, "LABEL="))
138 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
139 volume->identifier = g_strdup (volume->device_path + 6);
141 else if (g_str_has_prefix (volume->device_path, "UUID="))
143 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
144 volume->identifier = g_strdup (volume->device_path + 5);
146 else if (g_path_is_absolute (volume->device_path))
148 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
149 volume->identifier = g_strdup (volume->device_path);
156 * g_unix_volume_disconnected:
161 _g_unix_volume_disconnected (GUnixVolume *volume)
165 _g_unix_mount_unset_volume (volume->mount, volume);
166 volume->mount = NULL;
171 * g_unix_volume_set_mount:
177 _g_unix_volume_set_mount (GUnixVolume *volume,
180 if (volume->mount == mount)
184 _g_unix_mount_unset_volume (volume->mount, volume);
186 volume->mount = mount;
188 /* TODO: Emit changed in idle to avoid locking issues */
189 g_signal_emit_by_name (volume, "changed");
190 if (volume->volume_monitor != NULL)
191 g_signal_emit_by_name (volume->volume_monitor, "volume_changed", volume);
195 * g_unix_volume_unset_mount:
201 _g_unix_volume_unset_mount (GUnixVolume *volume,
204 if (volume->mount == mount)
206 volume->mount = NULL;
207 /* TODO: Emit changed in idle to avoid locking issues */
208 g_signal_emit_by_name (volume, "changed");
209 if (volume->volume_monitor != NULL)
210 g_signal_emit_by_name (volume->volume_monitor, "volume_changed", volume);
215 g_unix_volume_get_icon (GVolume *volume)
217 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
218 return g_object_ref (unix_volume->icon);
222 g_unix_volume_get_name (GVolume *volume)
224 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
225 return g_strdup (unix_volume->name);
229 g_unix_volume_get_uuid (GVolume *volume)
235 g_unix_volume_can_mount (GVolume *volume)
241 g_unix_volume_can_eject (GVolume *volume)
243 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
244 return unix_volume->can_eject;
248 g_unix_volume_get_drive (GVolume *volume)
254 g_unix_volume_get_mount (GVolume *volume)
256 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
258 if (unix_volume->mount != NULL)
259 return g_object_ref (unix_volume->mount);
266 _g_unix_volume_has_mount_path (GUnixVolume *volume,
267 const char *mount_path)
269 return strcmp (volume->mount_path, mount_path) == 0;
274 GUnixVolume *unix_volume;
275 GAsyncReadyCallback callback;
277 GCancellable *cancellable;
279 GIOChannel *error_channel;
280 guint error_channel_source_id;
281 GString *error_string;
285 eject_mount_cb (GPid pid, gint status, gpointer user_data)
287 EjectMountOp *data = user_data;
288 GSimpleAsyncResult *simple;
290 if (WEXITSTATUS (status) != 0)
293 error = g_error_new_literal (G_IO_ERROR,
295 data->error_string->str);
296 simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
300 g_error_free (error);
304 simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
310 g_simple_async_result_complete (simple);
311 g_object_unref (simple);
313 g_source_remove (data->error_channel_source_id);
314 g_io_channel_unref (data->error_channel);
315 g_string_free (data->error_string, TRUE);
316 close (data->error_fd);
317 g_spawn_close_pid (pid);
322 eject_mount_read_error (GIOChannel *channel,
323 GIOCondition condition,
328 EjectMountOp *data = user_data;
330 g_io_channel_read_to_end (channel, &str, &str_len, NULL);
331 g_string_append (data->error_string, str);
337 eject_mount_do (GVolume *volume,
338 GCancellable *cancellable,
339 GAsyncReadyCallback callback,
343 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
348 data = g_new0 (EjectMountOp, 1);
349 data->unix_volume = unix_volume;
350 data->callback = callback;
351 data->user_data = user_data;
352 data->cancellable = cancellable;
355 if (!g_spawn_async_with_pipes (NULL, /* working dir */
358 G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
359 NULL, /* child_setup */
360 NULL, /* user_data for child_setup */
362 NULL, /* standard_input */
363 NULL, /* standard_output */
366 GSimpleAsyncResult *simple;
367 simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
371 g_simple_async_result_complete (simple);
372 g_object_unref (simple);
373 g_error_free (error);
377 data->error_string = g_string_new ("");
378 data->error_channel = g_io_channel_unix_new (data->error_fd);
379 data->error_channel_source_id = g_io_add_watch (data->error_channel, G_IO_IN, eject_mount_read_error, data);
380 g_child_watch_add (child_pid, eject_mount_cb, data);
385 g_unix_volume_mount (GVolume *volume,
386 GMountOperation *mount_operation,
387 GCancellable *cancellable,
388 GAsyncReadyCallback callback,
391 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
392 char *argv[] = {"mount", NULL, NULL};
394 if (unix_volume->mount_path != NULL)
395 argv[1] = unix_volume->mount_path;
397 argv[1] = unix_volume->device_path;
399 eject_mount_do (volume, cancellable, callback, user_data, argv);
403 g_unix_volume_mount_finish (GVolume *volume,
404 GAsyncResult *result,
411 g_unix_volume_eject (GVolume *volume,
412 GMountUnmountFlags flags,
413 GCancellable *cancellable,
414 GAsyncReadyCallback callback,
417 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
418 char *argv[] = {"eject", NULL, NULL};
420 argv[1] = unix_volume->device_path;
422 eject_mount_do (volume, cancellable, callback, user_data, argv);
426 g_unix_volume_eject_finish (GVolume *volume,
427 GAsyncResult *result,
434 g_unix_volume_get_identifier (GVolume *volume,
437 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
439 if (strcmp (kind, unix_volume->identifier_type) == 0)
440 return g_strdup (unix_volume->identifier);
445 g_unix_volume_enumerate_identifiers (GVolume *volume)
447 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
450 if (unix_volume->identifier_type)
452 res = g_new (char *, 2);
453 res[0] = g_strdup (unix_volume->identifier_type);
458 res = g_new (char *, 1);
467 g_unix_volume_volume_iface_init (GVolumeIface *iface)
469 iface->get_name = g_unix_volume_get_name;
470 iface->get_icon = g_unix_volume_get_icon;
471 iface->get_uuid = g_unix_volume_get_uuid;
472 iface->get_drive = g_unix_volume_get_drive;
473 iface->get_mount = g_unix_volume_get_mount;
474 iface->can_mount = g_unix_volume_can_mount;
475 iface->can_eject = g_unix_volume_can_eject;
476 iface->mount_fn = g_unix_volume_mount;
477 iface->mount_finish = g_unix_volume_mount_finish;
478 iface->eject = g_unix_volume_eject;
479 iface->eject_finish = g_unix_volume_eject_finish;
480 iface->get_identifier = g_unix_volume_get_identifier;
481 iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;