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 "gunixmounts.h"
36 #include "gthemedicon.h"
38 #include "gvolumemonitor.h"
39 #include "gsimpleasyncresult.h"
50 GVolumeMonitor *volume_monitor;
51 GUnixMount *mount; /* owned by volume monitor */
58 char *identifier_type;
64 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
66 #define g_unix_volume_get_type _g_unix_volume_get_type
67 G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
68 G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
69 g_unix_volume_volume_iface_init))
72 g_unix_volume_finalize (GObject *object)
76 volume = G_UNIX_VOLUME (object);
78 if (volume->volume_monitor != NULL)
79 g_object_unref (volume->volume_monitor);
82 _g_unix_mount_unset_volume (volume->mount, volume);
84 g_object_unref (volume->icon);
85 g_free (volume->name);
86 g_free (volume->mount_path);
87 g_free (volume->device_path);
88 g_free (volume->identifier);
89 g_free (volume->identifier_type);
91 G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize (object);
95 g_unix_volume_class_init (GUnixVolumeClass *klass)
97 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
99 gobject_class->finalize = g_unix_volume_finalize;
103 g_unix_volume_init (GUnixVolume *unix_volume)
108 _g_unix_volume_new (GVolumeMonitor *volume_monitor,
109 GUnixMountPoint *mountpoint)
113 if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
114 g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
115 g_unix_mount_point_is_loopback (mountpoint))
118 volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
119 volume->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
120 volume->mount_path = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
121 volume->device_path = g_strdup (g_unix_mount_point_get_device_path (mountpoint));
122 volume->can_eject = g_unix_mount_point_guess_can_eject (mountpoint);
124 volume->name = g_unix_mount_point_guess_name (mountpoint);
125 volume->icon = g_unix_mount_point_guess_icon (mountpoint);
128 if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
130 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
131 volume->identifier = g_strdup (volume->device_path);
133 else if (g_str_has_prefix (volume->device_path, "LABEL="))
135 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
136 volume->identifier = g_strdup (volume->device_path + 6);
138 else if (g_str_has_prefix (volume->device_path, "UUID="))
140 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
141 volume->identifier = g_strdup (volume->device_path + 5);
143 else if (g_path_is_absolute (volume->device_path))
145 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
146 volume->identifier = g_strdup (volume->device_path);
153 _g_unix_volume_disconnected (GUnixVolume *volume)
157 _g_unix_mount_unset_volume (volume->mount, volume);
158 volume->mount = NULL;
163 _g_unix_volume_set_mount (GUnixVolume *volume,
166 if (volume->mount == mount)
170 _g_unix_mount_unset_volume (volume->mount, volume);
172 volume->mount = mount;
174 /* TODO: Emit changed in idle to avoid locking issues */
175 g_signal_emit_by_name (volume, "changed");
176 if (volume->volume_monitor != NULL)
177 g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
181 _g_unix_volume_unset_mount (GUnixVolume *volume,
184 if (volume->mount == mount)
186 volume->mount = NULL;
187 /* TODO: Emit changed in idle to avoid locking issues */
188 g_signal_emit_by_name (volume, "changed");
189 if (volume->volume_monitor != NULL)
190 g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
195 g_unix_volume_get_icon (GVolume *volume)
197 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
198 return g_object_ref (unix_volume->icon);
202 g_unix_volume_get_name (GVolume *volume)
204 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
205 return g_strdup (unix_volume->name);
209 g_unix_volume_get_uuid (GVolume *volume)
215 g_unix_volume_can_mount (GVolume *volume)
221 g_unix_volume_can_eject (GVolume *volume)
223 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
224 return unix_volume->can_eject;
228 g_unix_volume_should_automount (GVolume *volume)
230 /* We automount all local volumes because we don't even
231 * make the internal stuff visible
237 g_unix_volume_get_drive (GVolume *volume)
243 g_unix_volume_get_mount (GVolume *volume)
245 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
247 if (unix_volume->mount != NULL)
248 return g_object_ref (unix_volume->mount);
255 _g_unix_volume_has_mount_path (GUnixVolume *volume,
256 const char *mount_path)
258 return strcmp (volume->mount_path, mount_path) == 0;
263 GUnixVolume *unix_volume;
264 GAsyncReadyCallback callback;
266 GCancellable *cancellable;
268 GIOChannel *error_channel;
269 GSource *error_channel_source;
270 GString *error_string;
274 eject_mount_cb (GPid pid,
278 EjectMountOp *data = user_data;
279 GSimpleAsyncResult *simple;
281 if (WEXITSTATUS (status) != 0)
284 error = g_error_new_literal (G_IO_ERROR,
286 data->error_string->str);
287 simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
291 g_error_free (error);
295 simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
301 g_simple_async_result_complete (simple);
302 g_object_unref (simple);
304 if (data->error_channel_source)
306 g_source_destroy (data->error_channel_source);
307 g_source_unref (data->error_channel_source);
309 g_io_channel_unref (data->error_channel);
310 g_string_free (data->error_string, TRUE);
311 close (data->error_fd);
312 g_spawn_close_pid (pid);
317 eject_mount_read_error (GIOChannel *channel,
318 GIOCondition condition,
321 EjectMountOp *data = user_data;
329 status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
330 if (status == G_IO_STATUS_NORMAL)
332 g_string_append_len (data->error_string, buf, bytes_read);
333 if (bytes_read == sizeof (buf))
336 else if (status == G_IO_STATUS_EOF)
337 g_string_append_len (data->error_string, buf, bytes_read);
338 else if (status == G_IO_STATUS_ERROR)
340 if (data->error_string->len > 0)
341 g_string_append (data->error_string, "\n");
343 g_string_append (data->error_string, error->message);
344 g_error_free (error);
346 if (data->error_channel_source)
348 g_source_unref (data->error_channel_source);
349 data->error_channel_source = NULL;
358 eject_mount_do (GVolume *volume,
359 GCancellable *cancellable,
360 GAsyncReadyCallback callback,
364 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
367 GSource *child_watch;
370 data = g_new0 (EjectMountOp, 1);
371 data->unix_volume = unix_volume;
372 data->callback = callback;
373 data->user_data = user_data;
374 data->cancellable = cancellable;
377 if (!g_spawn_async_with_pipes (NULL, /* working dir */
380 G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
381 NULL, /* child_setup */
382 NULL, /* user_data for child_setup */
384 NULL, /* standard_input */
385 NULL, /* standard_output */
389 g_assert (error != NULL);
393 data->error_string = g_string_new ("");
395 data->error_channel = g_io_channel_unix_new (data->error_fd);
396 g_io_channel_set_flags (data->error_channel, G_IO_FLAG_NONBLOCK, &error);
400 data->error_channel_source = g_io_create_watch (data->error_channel, G_IO_IN);
401 g_source_set_callback (data->error_channel_source,
402 (GSourceFunc) eject_mount_read_error, data, NULL);
403 g_source_attach (data->error_channel_source, g_main_context_get_thread_default ());
405 child_watch = g_child_watch_source_new (child_pid);
406 g_source_set_callback (child_watch, (GSourceFunc) eject_mount_cb, data, NULL);
407 g_source_attach (child_watch, g_main_context_get_thread_default ());
408 g_source_unref (child_watch);
413 GSimpleAsyncResult *simple;
414 simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
418 g_simple_async_result_complete (simple);
419 g_object_unref (simple);
421 if (data->error_string != NULL)
422 g_string_free (data->error_string, TRUE);
424 if (data->error_channel != NULL)
425 g_io_channel_unref (data->error_channel);
427 g_error_free (error);
434 g_unix_volume_mount (GVolume *volume,
435 GMountMountFlags flags,
436 GMountOperation *mount_operation,
437 GCancellable *cancellable,
438 GAsyncReadyCallback callback,
441 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
442 char *argv[] = { "mount", NULL, NULL };
444 if (unix_volume->mount_path != NULL)
445 argv[1] = unix_volume->mount_path;
447 argv[1] = unix_volume->device_path;
449 eject_mount_do (volume, cancellable, callback, user_data, argv);
453 g_unix_volume_mount_finish (GVolume *volume,
454 GAsyncResult *result,
461 g_unix_volume_eject (GVolume *volume,
462 GMountUnmountFlags flags,
463 GCancellable *cancellable,
464 GAsyncReadyCallback callback,
467 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
468 char *argv[] = { "eject", NULL, NULL };
470 argv[1] = unix_volume->device_path;
472 eject_mount_do (volume, cancellable, callback, user_data, argv);
476 g_unix_volume_eject_finish (GVolume *volume,
477 GAsyncResult *result,
484 g_unix_volume_get_identifier (GVolume *volume,
487 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
489 if (unix_volume->identifier_type != NULL &&
490 strcmp (kind, unix_volume->identifier_type) == 0)
491 return g_strdup (unix_volume->identifier);
497 g_unix_volume_enumerate_identifiers (GVolume *volume)
499 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
502 if (unix_volume->identifier_type)
504 res = g_new (gchar *, 2);
505 res[0] = g_strdup (unix_volume->identifier_type);
510 res = g_new (gchar *, 1);
518 g_unix_volume_volume_iface_init (GVolumeIface *iface)
520 iface->get_name = g_unix_volume_get_name;
521 iface->get_icon = g_unix_volume_get_icon;
522 iface->get_uuid = g_unix_volume_get_uuid;
523 iface->get_drive = g_unix_volume_get_drive;
524 iface->get_mount = g_unix_volume_get_mount;
525 iface->can_mount = g_unix_volume_can_mount;
526 iface->can_eject = g_unix_volume_can_eject;
527 iface->should_automount = g_unix_volume_should_automount;
528 iface->mount_fn = g_unix_volume_mount;
529 iface->mount_finish = g_unix_volume_mount_finish;
530 iface->eject = g_unix_volume_eject;
531 iface->eject_finish = g_unix_volume_eject_finish;
532 iface->get_identifier = g_unix_volume_get_identifier;
533 iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;