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 G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize (object);
90 g_unix_volume_class_init (GUnixVolumeClass *klass)
92 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
94 gobject_class->finalize = g_unix_volume_finalize;
98 g_unix_volume_init (GUnixVolume *unix_volume)
104 * @volume_monitor: a #GVolumeMonitor.
105 * @mountpoint: a #GUnixMountPoint.
107 * Returns: a #GUnixVolume for the given #GUnixMountPoint.
110 _g_unix_volume_new (GVolumeMonitor *volume_monitor,
111 GUnixMountPoint *mountpoint)
115 if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
116 g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
117 g_unix_mount_point_is_loopback (mountpoint))
120 volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
121 volume->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
122 volume->mount_path = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
123 volume->device_path = g_strdup (g_unix_mount_point_get_device_path (mountpoint));
124 volume->can_eject = g_unix_mount_point_guess_can_eject (mountpoint);
126 volume->name = g_unix_mount_point_guess_name (mountpoint);
127 volume->icon = g_unix_mount_point_guess_icon (mountpoint);
130 if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
132 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
133 volume->identifier = g_strdup (volume->device_path);
135 else if (g_str_has_prefix (volume->device_path, "LABEL="))
137 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
138 volume->identifier = g_strdup (volume->device_path + 6);
140 else if (g_str_has_prefix (volume->device_path, "UUID="))
142 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
143 volume->identifier = g_strdup (volume->device_path + 5);
145 else if (g_path_is_absolute (volume->device_path))
147 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
148 volume->identifier = g_strdup (volume->device_path);
155 * g_unix_volume_disconnected:
160 _g_unix_volume_disconnected (GUnixVolume *volume)
164 _g_unix_mount_unset_volume (volume->mount, volume);
165 volume->mount = NULL;
170 * g_unix_volume_set_mount:
176 _g_unix_volume_set_mount (GUnixVolume *volume,
179 if (volume->mount == mount)
183 _g_unix_mount_unset_volume (volume->mount, volume);
185 volume->mount = mount;
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);
194 * g_unix_volume_unset_mount:
200 _g_unix_volume_unset_mount (GUnixVolume *volume,
203 if (volume->mount == mount)
205 volume->mount = NULL;
206 /* TODO: Emit changed in idle to avoid locking issues */
207 g_signal_emit_by_name (volume, "changed");
208 if (volume->volume_monitor != NULL)
209 g_signal_emit_by_name (volume->volume_monitor, "volume_changed", volume);
214 g_unix_volume_get_icon (GVolume *volume)
216 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
217 return g_object_ref (unix_volume->icon);
221 g_unix_volume_get_name (GVolume *volume)
223 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
224 return g_strdup (unix_volume->name);
228 g_unix_volume_get_uuid (GVolume *volume)
234 g_unix_volume_can_mount (GVolume *volume)
240 g_unix_volume_can_eject (GVolume *volume)
242 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
243 return unix_volume->can_eject;
247 g_unix_volume_should_automount (GVolume *volume)
249 /* We automount all local volumes because we don't even
250 make the internal stuff visible */
255 g_unix_volume_get_drive (GVolume *volume)
261 g_unix_volume_get_mount (GVolume *volume)
263 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
265 if (unix_volume->mount != NULL)
266 return g_object_ref (unix_volume->mount);
273 _g_unix_volume_has_mount_path (GUnixVolume *volume,
274 const char *mount_path)
276 return strcmp (volume->mount_path, mount_path) == 0;
281 GUnixVolume *unix_volume;
282 GAsyncReadyCallback callback;
284 GCancellable *cancellable;
286 GIOChannel *error_channel;
287 guint error_channel_source_id;
288 GString *error_string;
292 eject_mount_cb (GPid pid, gint status, gpointer user_data)
294 EjectMountOp *data = user_data;
295 GSimpleAsyncResult *simple;
297 if (WEXITSTATUS (status) != 0)
300 error = g_error_new_literal (G_IO_ERROR,
302 data->error_string->str);
303 simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
307 g_error_free (error);
311 simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
317 g_simple_async_result_complete (simple);
318 g_object_unref (simple);
320 g_source_remove (data->error_channel_source_id);
321 g_io_channel_unref (data->error_channel);
322 g_string_free (data->error_string, TRUE);
323 close (data->error_fd);
324 g_spawn_close_pid (pid);
329 eject_mount_read_error (GIOChannel *channel,
330 GIOCondition condition,
335 EjectMountOp *data = user_data;
337 g_io_channel_read_to_end (channel, &str, &str_len, NULL);
338 g_string_append (data->error_string, str);
344 eject_mount_do (GVolume *volume,
345 GCancellable *cancellable,
346 GAsyncReadyCallback callback,
350 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
355 data = g_new0 (EjectMountOp, 1);
356 data->unix_volume = unix_volume;
357 data->callback = callback;
358 data->user_data = user_data;
359 data->cancellable = cancellable;
362 if (!g_spawn_async_with_pipes (NULL, /* working dir */
365 G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
366 NULL, /* child_setup */
367 NULL, /* user_data for child_setup */
369 NULL, /* standard_input */
370 NULL, /* standard_output */
373 GSimpleAsyncResult *simple;
374 simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
378 g_simple_async_result_complete (simple);
379 g_object_unref (simple);
380 g_error_free (error);
384 data->error_string = g_string_new ("");
385 data->error_channel = g_io_channel_unix_new (data->error_fd);
386 data->error_channel_source_id = g_io_add_watch (data->error_channel, G_IO_IN, eject_mount_read_error, data);
387 g_child_watch_add (child_pid, eject_mount_cb, data);
392 g_unix_volume_mount (GVolume *volume,
393 GMountMountFlags flags,
394 GMountOperation *mount_operation,
395 GCancellable *cancellable,
396 GAsyncReadyCallback callback,
399 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
400 char *argv[] = {"mount", NULL, NULL};
402 if (unix_volume->mount_path != NULL)
403 argv[1] = unix_volume->mount_path;
405 argv[1] = unix_volume->device_path;
407 eject_mount_do (volume, cancellable, callback, user_data, argv);
411 g_unix_volume_mount_finish (GVolume *volume,
412 GAsyncResult *result,
419 g_unix_volume_eject (GVolume *volume,
420 GMountUnmountFlags flags,
421 GCancellable *cancellable,
422 GAsyncReadyCallback callback,
425 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
426 char *argv[] = {"eject", NULL, NULL};
428 argv[1] = unix_volume->device_path;
430 eject_mount_do (volume, cancellable, callback, user_data, argv);
434 g_unix_volume_eject_finish (GVolume *volume,
435 GAsyncResult *result,
442 g_unix_volume_get_identifier (GVolume *volume,
445 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
447 if (strcmp (kind, unix_volume->identifier_type) == 0)
448 return g_strdup (unix_volume->identifier);
453 g_unix_volume_enumerate_identifiers (GVolume *volume)
455 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
458 if (unix_volume->identifier_type)
460 res = g_new (char *, 2);
461 res[0] = g_strdup (unix_volume->identifier_type);
466 res = g_new (char *, 1);
474 g_unix_volume_volume_iface_init (GVolumeIface *iface)
476 iface->get_name = g_unix_volume_get_name;
477 iface->get_icon = g_unix_volume_get_icon;
478 iface->get_uuid = g_unix_volume_get_uuid;
479 iface->get_drive = g_unix_volume_get_drive;
480 iface->get_mount = g_unix_volume_get_mount;
481 iface->can_mount = g_unix_volume_can_mount;
482 iface->can_eject = g_unix_volume_can_eject;
483 iface->should_automount = g_unix_volume_should_automount;
484 iface->mount_fn = g_unix_volume_mount;
485 iface->mount_finish = g_unix_volume_mount_finish;
486 iface->eject = g_unix_volume_eject;
487 iface->eject_finish = g_unix_volume_eject_finish;
488 iface->get_identifier = g_unix_volume_get_identifier;
489 iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;