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
)
109 * @volume_monitor: a #GVolumeMonitor.
110 * @mountpoint: a #GUnixMountPoint.
112 * Returns: a #GUnixVolume for the given #GUnixMountPoint.
115 _g_unix_volume_new (GVolumeMonitor
*volume_monitor
,
116 GUnixMountPoint
*mountpoint
)
120 if (!(g_unix_mount_point_is_user_mountable (mountpoint
) ||
121 g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint
), "/vol/")) ||
122 g_unix_mount_point_is_loopback (mountpoint
))
125 volume
= g_object_new (G_TYPE_UNIX_VOLUME
, NULL
);
126 volume
->volume_monitor
= volume_monitor
!= NULL
? g_object_ref (volume_monitor
) : NULL
;
127 volume
->mount_path
= g_strdup (g_unix_mount_point_get_mount_path (mountpoint
));
128 volume
->device_path
= g_strdup (g_unix_mount_point_get_device_path (mountpoint
));
129 volume
->can_eject
= g_unix_mount_point_guess_can_eject (mountpoint
);
131 volume
->name
= g_unix_mount_point_guess_name (mountpoint
);
132 volume
->icon
= g_unix_mount_point_guess_icon (mountpoint
);
135 if (strcmp (g_unix_mount_point_get_fs_type (mountpoint
), "nfs") == 0)
137 volume
->identifier_type
= g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT
);
138 volume
->identifier
= g_strdup (volume
->device_path
);
140 else if (g_str_has_prefix (volume
->device_path
, "LABEL="))
142 volume
->identifier_type
= g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL
);
143 volume
->identifier
= g_strdup (volume
->device_path
+ 6);
145 else if (g_str_has_prefix (volume
->device_path
, "UUID="))
147 volume
->identifier_type
= g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID
);
148 volume
->identifier
= g_strdup (volume
->device_path
+ 5);
150 else if (g_path_is_absolute (volume
->device_path
))
152 volume
->identifier_type
= g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE
);
153 volume
->identifier
= g_strdup (volume
->device_path
);
160 * g_unix_volume_disconnected:
165 _g_unix_volume_disconnected (GUnixVolume
*volume
)
169 _g_unix_mount_unset_volume (volume
->mount
, volume
);
170 volume
->mount
= NULL
;
175 * g_unix_volume_set_mount:
181 _g_unix_volume_set_mount (GUnixVolume
*volume
,
184 if (volume
->mount
== mount
)
188 _g_unix_mount_unset_volume (volume
->mount
, volume
);
190 volume
->mount
= mount
;
192 /* TODO: Emit changed in idle to avoid locking issues */
193 g_signal_emit_by_name (volume
, "changed");
194 if (volume
->volume_monitor
!= NULL
)
195 g_signal_emit_by_name (volume
->volume_monitor
, "volume-changed", volume
);
199 * g_unix_volume_unset_mount:
205 _g_unix_volume_unset_mount (GUnixVolume
*volume
,
208 if (volume
->mount
== mount
)
210 volume
->mount
= NULL
;
211 /* TODO: Emit changed in idle to avoid locking issues */
212 g_signal_emit_by_name (volume
, "changed");
213 if (volume
->volume_monitor
!= NULL
)
214 g_signal_emit_by_name (volume
->volume_monitor
, "volume-changed", volume
);
219 g_unix_volume_get_icon (GVolume
*volume
)
221 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
222 return g_object_ref (unix_volume
->icon
);
226 g_unix_volume_get_name (GVolume
*volume
)
228 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
229 return g_strdup (unix_volume
->name
);
233 g_unix_volume_get_uuid (GVolume
*volume
)
239 g_unix_volume_can_mount (GVolume
*volume
)
245 g_unix_volume_can_eject (GVolume
*volume
)
247 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
248 return unix_volume
->can_eject
;
252 g_unix_volume_should_automount (GVolume
*volume
)
254 /* We automount all local volumes because we don't even
255 make the internal stuff visible */
260 g_unix_volume_get_drive (GVolume
*volume
)
266 g_unix_volume_get_mount (GVolume
*volume
)
268 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
270 if (unix_volume
->mount
!= NULL
)
271 return g_object_ref (unix_volume
->mount
);
278 _g_unix_volume_has_mount_path (GUnixVolume
*volume
,
279 const char *mount_path
)
281 return strcmp (volume
->mount_path
, mount_path
) == 0;
286 GUnixVolume
*unix_volume
;
287 GAsyncReadyCallback callback
;
289 GCancellable
*cancellable
;
291 GIOChannel
*error_channel
;
292 guint error_channel_source_id
;
293 GString
*error_string
;
297 eject_mount_cb (GPid pid
, gint status
, gpointer user_data
)
299 EjectMountOp
*data
= user_data
;
300 GSimpleAsyncResult
*simple
;
302 if (WEXITSTATUS (status
) != 0)
305 error
= g_error_new_literal (G_IO_ERROR
,
307 data
->error_string
->str
);
308 simple
= g_simple_async_result_new_from_error (G_OBJECT (data
->unix_volume
),
312 g_error_free (error
);
316 simple
= g_simple_async_result_new (G_OBJECT (data
->unix_volume
),
322 g_simple_async_result_complete (simple
);
323 g_object_unref (simple
);
325 g_source_remove (data
->error_channel_source_id
);
326 g_io_channel_unref (data
->error_channel
);
327 g_string_free (data
->error_string
, TRUE
);
328 close (data
->error_fd
);
329 g_spawn_close_pid (pid
);
334 eject_mount_read_error (GIOChannel
*channel
,
335 GIOCondition condition
,
338 EjectMountOp
*data
= user_data
;
346 status
= g_io_channel_read_chars (channel
, buf
, sizeof (buf
), &bytes_read
, &error
);
347 if (status
== G_IO_STATUS_NORMAL
)
349 g_string_append_len (data
->error_string
, buf
, bytes_read
);
350 if (bytes_read
== sizeof (buf
))
353 else if (status
== G_IO_STATUS_EOF
)
354 g_string_append_len (data
->error_string
, buf
, bytes_read
);
355 else if (status
== G_IO_STATUS_ERROR
)
357 if (data
->error_string
->len
> 0)
358 g_string_append (data
->error_string
, "\n");
360 g_string_append (data
->error_string
, error
->message
);
361 g_error_free (error
);
369 eject_mount_do (GVolume
*volume
,
370 GCancellable
*cancellable
,
371 GAsyncReadyCallback callback
,
375 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
380 data
= g_new0 (EjectMountOp
, 1);
381 data
->unix_volume
= unix_volume
;
382 data
->callback
= callback
;
383 data
->user_data
= user_data
;
384 data
->cancellable
= cancellable
;
387 if (!g_spawn_async_with_pipes (NULL
, /* working dir */
390 G_SPAWN_DO_NOT_REAP_CHILD
|G_SPAWN_SEARCH_PATH
,
391 NULL
, /* child_setup */
392 NULL
, /* user_data for child_setup */
394 NULL
, /* standard_input */
395 NULL
, /* standard_output */
398 g_assert (error
!= NULL
);
402 data
->error_string
= g_string_new ("");
404 data
->error_channel
= g_io_channel_unix_new (data
->error_fd
);
405 g_io_channel_set_flags (data
->error_channel
, G_IO_FLAG_NONBLOCK
, &error
);
409 data
->error_channel_source_id
= g_io_add_watch (data
->error_channel
, G_IO_IN
, eject_mount_read_error
, data
);
410 g_child_watch_add (child_pid
, eject_mount_cb
, data
);
414 GSimpleAsyncResult
*simple
;
415 simple
= g_simple_async_result_new_from_error (G_OBJECT (data
->unix_volume
),
419 g_simple_async_result_complete (simple
);
420 g_object_unref (simple
);
422 if (data
->error_string
!= NULL
)
423 g_string_free (data
->error_string
, TRUE
);
425 if (data
->error_channel
!= NULL
)
426 g_io_channel_unref (data
->error_channel
);
428 g_error_free (error
);
435 g_unix_volume_mount (GVolume
*volume
,
436 GMountMountFlags flags
,
437 GMountOperation
*mount_operation
,
438 GCancellable
*cancellable
,
439 GAsyncReadyCallback callback
,
442 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
443 char *argv
[] = {"mount", NULL
, NULL
};
445 if (unix_volume
->mount_path
!= NULL
)
446 argv
[1] = unix_volume
->mount_path
;
448 argv
[1] = unix_volume
->device_path
;
450 eject_mount_do (volume
, cancellable
, callback
, user_data
, argv
);
454 g_unix_volume_mount_finish (GVolume
*volume
,
455 GAsyncResult
*result
,
462 g_unix_volume_eject (GVolume
*volume
,
463 GMountUnmountFlags flags
,
464 GCancellable
*cancellable
,
465 GAsyncReadyCallback callback
,
468 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
469 char *argv
[] = {"eject", NULL
, NULL
};
471 argv
[1] = unix_volume
->device_path
;
473 eject_mount_do (volume
, cancellable
, callback
, user_data
, argv
);
477 g_unix_volume_eject_finish (GVolume
*volume
,
478 GAsyncResult
*result
,
485 g_unix_volume_get_identifier (GVolume
*volume
,
488 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
490 if (strcmp (kind
, unix_volume
->identifier_type
) == 0)
491 return g_strdup (unix_volume
->identifier
);
496 g_unix_volume_enumerate_identifiers (GVolume
*volume
)
498 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
501 if (unix_volume
->identifier_type
)
503 res
= g_new (char *, 2);
504 res
[0] = g_strdup (unix_volume
->identifier_type
);
509 res
= g_new (char *, 1);
517 g_unix_volume_volume_iface_init (GVolumeIface
*iface
)
519 iface
->get_name
= g_unix_volume_get_name
;
520 iface
->get_icon
= g_unix_volume_get_icon
;
521 iface
->get_uuid
= g_unix_volume_get_uuid
;
522 iface
->get_drive
= g_unix_volume_get_drive
;
523 iface
->get_mount
= g_unix_volume_get_mount
;
524 iface
->can_mount
= g_unix_volume_can_mount
;
525 iface
->can_eject
= g_unix_volume_can_eject
;
526 iface
->should_automount
= g_unix_volume_should_automount
;
527 iface
->mount_fn
= g_unix_volume_mount
;
528 iface
->mount_finish
= g_unix_volume_mount_finish
;
529 iface
->eject
= g_unix_volume_eject
;
530 iface
->eject_finish
= g_unix_volume_eject_finish
;
531 iface
->get_identifier
= g_unix_volume_get_identifier
;
532 iface
->enumerate_identifiers
= g_unix_volume_enumerate_identifiers
;