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"
48 GVolumeMonitor *volume_monitor;
49 GUnixMount *mount; /* owned by volume monitor */
56 char *identifier_type;
62 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
64 #define g_unix_volume_get_type _g_unix_volume_get_type
65 G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
66 G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
67 g_unix_volume_volume_iface_init))
70 g_unix_volume_finalize (GObject *object)
74 volume = G_UNIX_VOLUME (object);
76 if (volume->volume_monitor != NULL)
77 g_object_unref (volume->volume_monitor);
80 _g_unix_mount_unset_volume (volume->mount, volume);
82 g_object_unref (volume->icon);
83 g_free (volume->name);
84 g_free (volume->mount_path);
85 g_free (volume->device_path);
86 g_free (volume->identifier);
87 g_free (volume->identifier_type);
89 G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize (object);
93 g_unix_volume_class_init (GUnixVolumeClass *klass)
95 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
97 gobject_class->finalize = g_unix_volume_finalize;
101 g_unix_volume_init (GUnixVolume *unix_volume)
107 * @volume_monitor: a #GVolumeMonitor.
108 * @mountpoint: a #GUnixMountPoint.
110 * Returns: a #GUnixVolume for the given #GUnixMountPoint.
113 _g_unix_volume_new (GVolumeMonitor *volume_monitor,
114 GUnixMountPoint *mountpoint)
118 if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
119 g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
120 g_unix_mount_point_is_loopback (mountpoint))
123 volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
124 volume->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
125 volume->mount_path = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
126 volume->device_path = g_strdup (g_unix_mount_point_get_device_path (mountpoint));
127 volume->can_eject = g_unix_mount_point_guess_can_eject (mountpoint);
129 volume->name = g_unix_mount_point_guess_name (mountpoint);
130 volume->icon = g_unix_mount_point_guess_icon (mountpoint);
133 if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
135 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
136 volume->identifier = g_strdup (volume->device_path);
138 else if (g_str_has_prefix (volume->device_path, "LABEL="))
140 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
141 volume->identifier = g_strdup (volume->device_path + 6);
143 else if (g_str_has_prefix (volume->device_path, "UUID="))
145 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
146 volume->identifier = g_strdup (volume->device_path + 5);
148 else if (g_path_is_absolute (volume->device_path))
150 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
151 volume->identifier = g_strdup (volume->device_path);
158 * g_unix_volume_disconnected:
163 _g_unix_volume_disconnected (GUnixVolume *volume)
167 _g_unix_mount_unset_volume (volume->mount, volume);
168 volume->mount = NULL;
173 * g_unix_volume_set_mount:
179 _g_unix_volume_set_mount (GUnixVolume *volume,
182 if (volume->mount == mount)
186 _g_unix_mount_unset_volume (volume->mount, volume);
188 volume->mount = mount;
190 /* TODO: Emit changed in idle to avoid locking issues */
191 g_signal_emit_by_name (volume, "changed");
192 if (volume->volume_monitor != NULL)
193 g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
197 * g_unix_volume_unset_mount:
203 _g_unix_volume_unset_mount (GUnixVolume *volume,
206 if (volume->mount == mount)
208 volume->mount = NULL;
209 /* TODO: Emit changed in idle to avoid locking issues */
210 g_signal_emit_by_name (volume, "changed");
211 if (volume->volume_monitor != NULL)
212 g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
217 g_unix_volume_get_icon (GVolume *volume)
219 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
220 return g_object_ref (unix_volume->icon);
224 g_unix_volume_get_name (GVolume *volume)
226 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
227 return g_strdup (unix_volume->name);
231 g_unix_volume_get_uuid (GVolume *volume)
237 g_unix_volume_can_mount (GVolume *volume)
243 g_unix_volume_can_eject (GVolume *volume)
245 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
246 return unix_volume->can_eject;
250 g_unix_volume_should_automount (GVolume *volume)
252 /* We automount all local volumes because we don't even
253 make the internal stuff visible */
258 g_unix_volume_get_drive (GVolume *volume)
264 g_unix_volume_get_mount (GVolume *volume)
266 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
268 if (unix_volume->mount != NULL)
269 return g_object_ref (unix_volume->mount);
276 _g_unix_volume_has_mount_path (GUnixVolume *volume,
277 const char *mount_path)
279 return strcmp (volume->mount_path, mount_path) == 0;
284 GUnixVolume *unix_volume;
285 GAsyncReadyCallback callback;
287 GCancellable *cancellable;
289 GIOChannel *error_channel;
290 guint error_channel_source_id;
291 GString *error_string;
295 eject_mount_cb (GPid pid, gint status, gpointer user_data)
297 EjectMountOp *data = user_data;
298 GSimpleAsyncResult *simple;
300 if (WEXITSTATUS (status) != 0)
303 error = g_error_new_literal (G_IO_ERROR,
305 data->error_string->str);
306 simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
310 g_error_free (error);
314 simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
320 g_simple_async_result_complete (simple);
321 g_object_unref (simple);
323 g_source_remove (data->error_channel_source_id);
324 g_io_channel_unref (data->error_channel);
325 g_string_free (data->error_string, TRUE);
326 close (data->error_fd);
327 g_spawn_close_pid (pid);
332 eject_mount_read_error (GIOChannel *channel,
333 GIOCondition condition,
338 EjectMountOp *data = user_data;
340 g_io_channel_read_to_end (channel, &str, &str_len, NULL);
341 g_string_append (data->error_string, str);
347 eject_mount_do (GVolume *volume,
348 GCancellable *cancellable,
349 GAsyncReadyCallback callback,
353 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
358 data = g_new0 (EjectMountOp, 1);
359 data->unix_volume = unix_volume;
360 data->callback = callback;
361 data->user_data = user_data;
362 data->cancellable = cancellable;
365 if (!g_spawn_async_with_pipes (NULL, /* working dir */
368 G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
369 NULL, /* child_setup */
370 NULL, /* user_data for child_setup */
372 NULL, /* standard_input */
373 NULL, /* standard_output */
376 GSimpleAsyncResult *simple;
377 simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
381 g_simple_async_result_complete (simple);
382 g_object_unref (simple);
383 g_error_free (error);
387 data->error_string = g_string_new ("");
388 data->error_channel = g_io_channel_unix_new (data->error_fd);
389 data->error_channel_source_id = g_io_add_watch (data->error_channel, G_IO_IN, eject_mount_read_error, data);
390 g_child_watch_add (child_pid, eject_mount_cb, data);
395 g_unix_volume_mount (GVolume *volume,
396 GMountMountFlags flags,
397 GMountOperation *mount_operation,
398 GCancellable *cancellable,
399 GAsyncReadyCallback callback,
402 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
403 char *argv[] = {"mount", NULL, NULL};
405 if (unix_volume->mount_path != NULL)
406 argv[1] = unix_volume->mount_path;
408 argv[1] = unix_volume->device_path;
410 eject_mount_do (volume, cancellable, callback, user_data, argv);
414 g_unix_volume_mount_finish (GVolume *volume,
415 GAsyncResult *result,
422 g_unix_volume_eject (GVolume *volume,
423 GMountUnmountFlags flags,
424 GCancellable *cancellable,
425 GAsyncReadyCallback callback,
428 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
429 char *argv[] = {"eject", NULL, NULL};
431 argv[1] = unix_volume->device_path;
433 eject_mount_do (volume, cancellable, callback, user_data, argv);
437 g_unix_volume_eject_finish (GVolume *volume,
438 GAsyncResult *result,
445 g_unix_volume_get_identifier (GVolume *volume,
448 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
450 if (strcmp (kind, unix_volume->identifier_type) == 0)
451 return g_strdup (unix_volume->identifier);
456 g_unix_volume_enumerate_identifiers (GVolume *volume)
458 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
461 if (unix_volume->identifier_type)
463 res = g_new (char *, 2);
464 res[0] = g_strdup (unix_volume->identifier_type);
469 res = g_new (char *, 1);
477 g_unix_volume_volume_iface_init (GVolumeIface *iface)
479 iface->get_name = g_unix_volume_get_name;
480 iface->get_icon = g_unix_volume_get_icon;
481 iface->get_uuid = g_unix_volume_get_uuid;
482 iface->get_drive = g_unix_volume_get_drive;
483 iface->get_mount = g_unix_volume_get_mount;
484 iface->can_mount = g_unix_volume_can_mount;
485 iface->can_eject = g_unix_volume_can_eject;
486 iface->should_automount = g_unix_volume_should_automount;
487 iface->mount_fn = g_unix_volume_mount;
488 iface->mount_finish = g_unix_volume_mount_finish;
489 iface->eject = g_unix_volume_eject;
490 iface->eject_finish = g_unix_volume_eject_finish;
491 iface->get_identifier = g_unix_volume_get_identifier;
492 iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;