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_should_automount (GVolume *volume)
250 /* We automount all local volumes because we don't even
251 make the internal stuff visible */
256 g_unix_volume_get_drive (GVolume *volume)
262 g_unix_volume_get_mount (GVolume *volume)
264 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
266 if (unix_volume->mount != NULL)
267 return g_object_ref (unix_volume->mount);
274 _g_unix_volume_has_mount_path (GUnixVolume *volume,
275 const char *mount_path)
277 return strcmp (volume->mount_path, mount_path) == 0;
282 GUnixVolume *unix_volume;
283 GAsyncReadyCallback callback;
285 GCancellable *cancellable;
287 GIOChannel *error_channel;
288 guint error_channel_source_id;
289 GString *error_string;
293 eject_mount_cb (GPid pid, gint status, gpointer user_data)
295 EjectMountOp *data = user_data;
296 GSimpleAsyncResult *simple;
298 if (WEXITSTATUS (status) != 0)
301 error = g_error_new_literal (G_IO_ERROR,
303 data->error_string->str);
304 simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
308 g_error_free (error);
312 simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
318 g_simple_async_result_complete (simple);
319 g_object_unref (simple);
321 g_source_remove (data->error_channel_source_id);
322 g_io_channel_unref (data->error_channel);
323 g_string_free (data->error_string, TRUE);
324 close (data->error_fd);
325 g_spawn_close_pid (pid);
330 eject_mount_read_error (GIOChannel *channel,
331 GIOCondition condition,
336 EjectMountOp *data = user_data;
338 g_io_channel_read_to_end (channel, &str, &str_len, NULL);
339 g_string_append (data->error_string, str);
345 eject_mount_do (GVolume *volume,
346 GCancellable *cancellable,
347 GAsyncReadyCallback callback,
351 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
356 data = g_new0 (EjectMountOp, 1);
357 data->unix_volume = unix_volume;
358 data->callback = callback;
359 data->user_data = user_data;
360 data->cancellable = cancellable;
363 if (!g_spawn_async_with_pipes (NULL, /* working dir */
366 G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
367 NULL, /* child_setup */
368 NULL, /* user_data for child_setup */
370 NULL, /* standard_input */
371 NULL, /* standard_output */
374 GSimpleAsyncResult *simple;
375 simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
379 g_simple_async_result_complete (simple);
380 g_object_unref (simple);
381 g_error_free (error);
385 data->error_string = g_string_new ("");
386 data->error_channel = g_io_channel_unix_new (data->error_fd);
387 data->error_channel_source_id = g_io_add_watch (data->error_channel, G_IO_IN, eject_mount_read_error, data);
388 g_child_watch_add (child_pid, eject_mount_cb, data);
393 g_unix_volume_mount (GVolume *volume,
394 GMountMountFlags flags,
395 GMountOperation *mount_operation,
396 GCancellable *cancellable,
397 GAsyncReadyCallback callback,
400 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
401 char *argv[] = {"mount", NULL, NULL};
403 if (unix_volume->mount_path != NULL)
404 argv[1] = unix_volume->mount_path;
406 argv[1] = unix_volume->device_path;
408 eject_mount_do (volume, cancellable, callback, user_data, argv);
412 g_unix_volume_mount_finish (GVolume *volume,
413 GAsyncResult *result,
420 g_unix_volume_eject (GVolume *volume,
421 GMountUnmountFlags flags,
422 GCancellable *cancellable,
423 GAsyncReadyCallback callback,
426 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
427 char *argv[] = {"eject", NULL, NULL};
429 argv[1] = unix_volume->device_path;
431 eject_mount_do (volume, cancellable, callback, user_data, argv);
435 g_unix_volume_eject_finish (GVolume *volume,
436 GAsyncResult *result,
443 g_unix_volume_get_identifier (GVolume *volume,
446 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
448 if (strcmp (kind, unix_volume->identifier_type) == 0)
449 return g_strdup (unix_volume->identifier);
454 g_unix_volume_enumerate_identifiers (GVolume *volume)
456 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
459 if (unix_volume->identifier_type)
461 res = g_new (char *, 2);
462 res[0] = g_strdup (unix_volume->identifier_type);
467 res = g_new (char *, 1);
475 g_unix_volume_volume_iface_init (GVolumeIface *iface)
477 iface->get_name = g_unix_volume_get_name;
478 iface->get_icon = g_unix_volume_get_icon;
479 iface->get_uuid = g_unix_volume_get_uuid;
480 iface->get_drive = g_unix_volume_get_drive;
481 iface->get_mount = g_unix_volume_get_mount;
482 iface->can_mount = g_unix_volume_can_mount;
483 iface->can_eject = g_unix_volume_can_eject;
484 iface->should_automount = g_unix_volume_should_automount;
485 iface->mount_fn = g_unix_volume_mount;
486 iface->mount_finish = g_unix_volume_mount_finish;
487 iface->eject = g_unix_volume_eject;
488 iface->eject_finish = g_unix_volume_eject_finish;
489 iface->get_identifier = g_unix_volume_get_identifier;
490 iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;