1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2008 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>
31 #include "gmountprivate.h"
32 #include "gasyncresult.h"
33 #include "gsimpleasyncresult.h"
41 * @short_description: Mount management
43 * @see also: GVolume, GUnixMount
45 * The #GMount interface represents user-visible mounts. Note, when
46 * porting from GnomeVFS, #GMount is the moral equivalent of #GnomeVFSVolume.
48 * #GMount is a "mounted" filesystem that you can access. Mounted is in
49 * quotes because it's not the same as a unix mount, it might be a gvfs
50 * mount, but you can still access the files on it if you use GIO. Might or
51 * might not be related to a volume object.
53 * Unmounting a #GMount instance is an asynchronous operation. For
54 * more information about asynchronous operations, see #GAsyncReady
55 * and #GSimpleAsyncReady. To unmount a #GMount instance, first call
56 * g_mount_unmount() with (at least) the #GMount instance and a
57 * #GAsyncReadyCallback. The callback will be fired when the
58 * operation has resolved (either with success or failure), and a
59 * #GAsyncReady structure will be passed to the callback. That
60 * callback should then call g_mount_unmount_finish() with the #GMount
61 * and the #GAsyncReady data to see if the operation was completed
62 * successfully. If an @error is present when g_mount_unmount_finish()
63 * is called, then it will be filled with any error information.
66 static void g_mount_base_init (gpointer g_class);
67 static void g_mount_class_init (gpointer g_class,
71 g_mount_get_type (void)
73 static volatile gsize g_define_type_id__volatile = 0;
75 if (g_once_init_enter (&g_define_type_id__volatile))
77 const GTypeInfo mount_info =
79 sizeof (GMountIface), /* class_size */
80 g_mount_base_init, /* base_init */
81 NULL, /* base_finalize */
83 NULL, /* class_finalize */
84 NULL, /* class_data */
89 GType g_define_type_id =
90 g_type_register_static (G_TYPE_INTERFACE, I_("GMount"),
93 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
95 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
98 return g_define_type_id__volatile;
102 g_mount_class_init (gpointer g_class,
108 g_mount_base_init (gpointer g_class)
110 static gboolean initialized = FALSE;
116 * @mount: the object on which the signal is emitted
118 * Emitted when the mount has been changed.
120 g_signal_new (I_("changed"),
123 G_STRUCT_OFFSET (GMountIface, changed),
125 g_cclosure_marshal_VOID__VOID,
130 * @mount: the object on which the signal is emitted
132 * This signal is emitted when the #GMount have been
133 * unmounted. If the recipient is holding references to the
134 * object they should release them so the object can be
137 g_signal_new (I_("unmounted"),
140 G_STRUCT_OFFSET (GMountIface, unmounted),
142 g_cclosure_marshal_VOID__VOID,
145 * GMount::pre-unmount:
146 * @mount: the object on which the signal is emitted
148 * This signal is emitted when the #GMount is about to be
153 g_signal_new (I_("pre-unmount"),
156 G_STRUCT_OFFSET (GMountIface, pre_unmount),
158 g_cclosure_marshal_VOID__VOID,
169 * Gets the root directory on @mount.
172 * The returned object should be unreffed with
173 * g_object_unref() when no longer needed.
176 g_mount_get_root (GMount *mount)
180 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
182 iface = G_MOUNT_GET_IFACE (mount);
184 return (* iface->get_root) (mount);
191 * Gets the name of @mount.
193 * Returns: the name for the given @mount.
194 * The returned string should be freed with g_free()
195 * when no longer needed.
198 g_mount_get_name (GMount *mount)
202 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
204 iface = G_MOUNT_GET_IFACE (mount);
206 return (* iface->get_name) (mount);
213 * Gets the icon for @mount.
216 * The returned object should be unreffed with
217 * g_object_unref() when no longer needed.
220 g_mount_get_icon (GMount *mount)
224 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
226 iface = G_MOUNT_GET_IFACE (mount);
228 return (* iface->get_icon) (mount);
235 * Gets the UUID for the @mount. The reference is typically based on
236 * the file system UUID for the mount in question and should be
237 * considered an opaque string. Returns %NULL if there is no UUID
240 * Returns: the UUID for @mount or %NULL if no UUID can be computed.
241 * The returned string should be freed with g_free()
242 * when no longer needed.
245 g_mount_get_uuid (GMount *mount)
249 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
251 iface = G_MOUNT_GET_IFACE (mount);
253 return (* iface->get_uuid) (mount);
257 * g_mount_get_volume:
260 * Gets the volume for the @mount.
262 * Returns: a #GVolume or %NULL if @mount is not associated with a volume.
263 * The returned object should be unreffed with
264 * g_object_unref() when no longer needed.
267 g_mount_get_volume (GMount *mount)
271 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
273 iface = G_MOUNT_GET_IFACE (mount);
275 return (* iface->get_volume) (mount);
282 * Gets the drive for the @mount.
284 * This is a convenience method for getting the #GVolume and then
285 * using that object to get the #GDrive.
287 * Returns: a #GDrive or %NULL if @mount is not associated with a volume or a drive.
288 * The returned object should be unreffed with
289 * g_object_unref() when no longer needed.
292 g_mount_get_drive (GMount *mount)
296 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
298 iface = G_MOUNT_GET_IFACE (mount);
300 return (* iface->get_drive) (mount);
304 * g_mount_can_unmount:
307 * Checks if @mount can be mounted.
309 * Returns: %TRUE if the @mount can be unmounted.
312 g_mount_can_unmount (GMount *mount)
316 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
318 iface = G_MOUNT_GET_IFACE (mount);
320 return (* iface->can_unmount) (mount);
327 * Checks if @mount can be eject.
329 * Returns: %TRUE if the @mount can be ejected.
332 g_mount_can_eject (GMount *mount)
336 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
338 iface = G_MOUNT_GET_IFACE (mount);
340 return (* iface->can_eject) (mount);
346 * @flags: flags affecting the operation
347 * @cancellable: optional #GCancellable object, %NULL to ignore.
348 * @callback: a #GAsyncReadyCallback, or %NULL.
349 * @user_data: user data passed to @callback.
351 * Unmounts a mount. This is an asynchronous operation, and is
352 * finished by calling g_mount_unmount_finish() with the @mount
353 * and #GAsyncResult data returned in the @callback.
356 g_mount_unmount (GMount *mount,
357 GMountUnmountFlags flags,
358 GCancellable *cancellable,
359 GAsyncReadyCallback callback,
364 g_return_if_fail (G_IS_MOUNT (mount));
366 iface = G_MOUNT_GET_IFACE (mount);
368 if (iface->unmount == NULL)
370 g_simple_async_report_error_in_idle (G_OBJECT (mount),
372 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
373 /* Translators: This is an error
374 * message for mount objects that
375 * don't implement unmount. */
376 _("mount doesn't implement unmount"));
381 (* iface->unmount) (mount, flags, cancellable, callback, user_data);
385 * g_mount_unmount_finish:
387 * @result: a #GAsyncResult.
388 * @error: a #GError location to store the error occuring, or %NULL to
391 * Finishes unmounting a mount. If any errors occurred during the operation,
392 * @error will be set to contain the errors and %FALSE will be returned.
394 * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
397 g_mount_unmount_finish (GMount *mount,
398 GAsyncResult *result,
403 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
404 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
406 if (G_IS_SIMPLE_ASYNC_RESULT (result))
408 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
409 if (g_simple_async_result_propagate_error (simple, error))
413 iface = G_MOUNT_GET_IFACE (mount);
414 return (* iface->unmount_finish) (mount, result, error);
421 * @flags: flags affecting the unmount if required for eject
422 * @cancellable: optional #GCancellable object, %NULL to ignore.
423 * @callback: a #GAsyncReadyCallback, or %NULL.
424 * @user_data: user data passed to @callback.
426 * Ejects a mount. This is an asynchronous operation, and is
427 * finished by calling g_mount_eject_finish() with the @mount
428 * and #GAsyncResult data returned in the @callback.
431 g_mount_eject (GMount *mount,
432 GMountUnmountFlags flags,
433 GCancellable *cancellable,
434 GAsyncReadyCallback callback,
439 g_return_if_fail (G_IS_MOUNT (mount));
441 iface = G_MOUNT_GET_IFACE (mount);
443 if (iface->eject == NULL)
445 g_simple_async_report_error_in_idle (G_OBJECT (mount),
447 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
448 /* Translators: This is an error
449 * message for mount objects that
450 * don't implement eject. */
451 _("mount doesn't implement eject"));
456 (* iface->eject) (mount, flags, cancellable, callback, user_data);
460 * g_mount_eject_finish:
462 * @result: a #GAsyncResult.
463 * @error: a #GError location to store the error occuring, or %NULL to
466 * Finishes ejecting a mount. If any errors occurred during the operation,
467 * @error will be set to contain the errors and %FALSE will be returned.
469 * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
472 g_mount_eject_finish (GMount *mount,
473 GAsyncResult *result,
478 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
479 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
481 if (G_IS_SIMPLE_ASYNC_RESULT (result))
483 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
484 if (g_simple_async_result_propagate_error (simple, error))
488 iface = G_MOUNT_GET_IFACE (mount);
489 return (* iface->eject_finish) (mount, result, error);
495 * @flags: flags affecting the operation
496 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
497 * @cancellable: optional #GCancellable object, %NULL to ignore.
498 * @callback: a #GAsyncReadyCallback, or %NULL.
499 * @user_data: user data passed to @callback.
501 * Remounts a mount. This is an asynchronous operation, and is
502 * finished by calling g_mount_remount_finish() with the @mount
503 * and #GAsyncResults data returned in the @callback.
505 * Remounting is useful when some setting affecting the operation
506 * of the volume has been changed, as these may need a remount to
507 * take affect. While this is semantically equivalent with unmounting
508 * and then remounting not all backends might need to actually be
512 g_mount_remount (GMount *mount,
513 GMountMountFlags flags,
514 GMountOperation *mount_operation,
515 GCancellable *cancellable,
516 GAsyncReadyCallback callback,
521 g_return_if_fail (G_IS_MOUNT (mount));
523 iface = G_MOUNT_GET_IFACE (mount);
525 if (iface->remount == NULL)
527 g_simple_async_report_error_in_idle (G_OBJECT (mount),
529 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
530 /* Translators: This is an error
531 * message for mount objects that
532 * don't implement remount. */
533 _("mount doesn't implement remount"));
538 (* iface->remount) (mount, flags, mount_operation, cancellable, callback, user_data);
542 * g_mount_remount_finish:
544 * @result: a #GAsyncResult.
545 * @error: a #GError location to store the error occuring, or %NULL to
548 * Finishes remounting a mount. If any errors occurred during the operation,
549 * @error will be set to contain the errors and %FALSE will be returned.
551 * Returns: %TRUE if the mount was successfully remounted. %FALSE otherwise.
554 g_mount_remount_finish (GMount *mount,
555 GAsyncResult *result,
560 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
561 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
563 if (G_IS_SIMPLE_ASYNC_RESULT (result))
565 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
566 if (g_simple_async_result_propagate_error (simple, error))
570 iface = G_MOUNT_GET_IFACE (mount);
571 return (* iface->remount_finish) (mount, result, error);
575 * g_mount_guess_content_type:
577 * @force_rescan: Whether to force a rescan of the content.
578 * Otherwise a cached result will be used if available
579 * @cancellable: optional #GCancellable object, %NULL to ignore
580 * @callback: a #GAsyncReadyCallback
581 * @user_data: user data passed to @callback
583 * Tries to guess the type of content stored on @mount. Returns one or
584 * more textual identifiers of well-known content types (typically
585 * prefixed with "x-content/"), e.g. x-content/image-dcf for camera
586 * memory cards. See the <ulink url="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec">shared-mime-info</ulink>
587 * specification for more on x-content types.
589 * This is an asynchronous operation (see
590 * g_mount_guess_content_type_sync() for the synchronous version), and
591 * is finished by calling g_mount_guess_content_type_finish() with the
592 * @mount and #GAsyncResult data returned in the @callback.
597 g_mount_guess_content_type (GMount *mount,
598 gboolean force_rescan,
599 GCancellable *cancellable,
600 GAsyncReadyCallback callback,
605 g_return_if_fail (G_IS_MOUNT (mount));
607 iface = G_MOUNT_GET_IFACE (mount);
609 if (iface->guess_content_type == NULL)
611 g_simple_async_report_error_in_idle (G_OBJECT (mount),
613 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
614 /* Translators: This is an error
615 * message for mount objects that
616 * don't implement content type guessing. */
617 _("mount doesn't implement content type guessing"));
622 (* iface->guess_content_type) (mount, force_rescan, cancellable, callback, user_data);
626 * g_mount_guess_content_type_finish:
628 * @result: a #GAsyncResult
629 * @error: a #GError location to store the error occuring, or %NULL to
632 * Finishes guessing content types of @mount. If any errors occured
633 * during the operation, @error will be set to contain the errors and
634 * %FALSE will be returned. In particular, you may get an
635 * %G_IO_ERROR_NOT_SUPPORTED if the mount does not support content
638 * Returns: a %NULL-terminated array of content types or %NULL on error.
639 * Caller should free this array with g_strfreev() when done with it.
644 g_mount_guess_content_type_finish (GMount *mount,
645 GAsyncResult *result,
650 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
651 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
653 if (G_IS_SIMPLE_ASYNC_RESULT (result))
655 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
656 if (g_simple_async_result_propagate_error (simple, error))
660 iface = G_MOUNT_GET_IFACE (mount);
661 return (* iface->guess_content_type_finish) (mount, result, error);
665 * g_mount_guess_content_type_sync:
667 * @force_rescan: Whether to force a rescan of the content.
668 * Otherwise a cached result will be used if available
669 * @cancellable: optional #GCancellable object, %NULL to ignore
670 * @error: a #GError location to store the error occuring, or %NULL to
673 * Tries to guess the type of content stored on @mount. Returns one or
674 * more textual identifiers of well-known content types (typically
675 * prefixed with "x-content/"), e.g. x-content/image-dcf for camera
676 * memory cards. See the <ulink url="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec">shared-mime-info</ulink>
677 * specification for more on x-content types.
679 * This is an synchronous operation and as such may block doing IO;
680 * see g_mount_guess_content_type() for the asynchronous version.
682 * Returns: a %NULL-terminated array of content types or %NULL on error.
683 * Caller should free this array with g_strfreev() when done with it.
688 g_mount_guess_content_type_sync (GMount *mount,
689 gboolean force_rescan,
690 GCancellable *cancellable,
695 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
697 iface = G_MOUNT_GET_IFACE (mount);
699 if (iface->guess_content_type_sync == NULL)
701 g_set_error_literal (error,
702 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
703 /* Translators: This is an error
704 * message for mount objects that
705 * don't implement content type guessing. */
706 _("mount doesn't implement synchronous content type guessing"));
711 return (* iface->guess_content_type_sync) (mount, force_rescan, cancellable, error);
714 G_LOCK_DEFINE_STATIC (priv_lock);
716 /* only access this structure when holding priv_lock */
719 gint shadow_ref_count;
723 free_private (GMountPrivate *private)
727 G_UNLOCK (priv_lock);
730 /* may only be called when holding priv_lock */
731 static GMountPrivate *
732 get_private (GMount *mount)
734 GMountPrivate *private;
736 private = g_object_get_data (G_OBJECT (mount), "g-mount-private");
737 if (G_LIKELY (private != NULL))
740 private = g_new0 (GMountPrivate, 1);
741 g_object_set_data_full (G_OBJECT (mount),
744 (GDestroyNotify) free_private);
751 * g_mount_is_shadowed:
754 * Determines if @mount is shadowed. Applications or libraries should
755 * avoid displaying @mount in the user interface if it is shadowed.
757 * A mount is said to be shadowed if there exists one or more user
758 * visible objects (currently #GMount objects) with a root that is
759 * inside the root of @mount.
761 * One application of shadow mounts is when exposing a single file
762 * system that is used to address several logical volumes. In this
763 * situation, a #GVolumeMonitor implementation would create two
764 * #GVolume objects (for example, one for the camera functionality of
765 * the device and one for a SD card reader on the device) with
766 * activation URIs <literal>gphoto2://[usb:001,002]/store1/</literal>
767 * and <literal>gphoto2://[usb:001,002]/store2/</literal>. When the
768 * underlying mount (with root
769 * <literal>gphoto2://[usb:001,002]/</literal>) is mounted, said
770 * #GVolumeMonitor implementation would create two #GMount objects
771 * (each with their root matching the corresponding volume activation
772 * root) that would shadow the original mount.
774 * The proxy monitor in GVfs 2.26 and later, automatically creates and
775 * manage shadow mounts (and shadows the underlying mount) if the
776 * activation root on a #GVolume is set.
778 * Returns: %TRUE if @mount is shadowed.
783 g_mount_is_shadowed (GMount *mount)
788 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
791 priv = get_private (mount);
792 ret = (priv->shadow_ref_count > 0);
793 G_UNLOCK (priv_lock);
802 * Increments the shadow count on @mount. Usually used by
803 * #GVolumeMonitor implementations when creating a shadow mount for
804 * @mount, see g_mount_is_shadowed() for more information. The caller
805 * will need to emit the #GMount::changed signal on @mount manually.
810 g_mount_shadow (GMount *mount)
814 g_return_if_fail (G_IS_MOUNT (mount));
817 priv = get_private (mount);
818 priv->shadow_ref_count += 1;
819 G_UNLOCK (priv_lock);
826 * Decrements the shadow count on @mount. Usually used by
827 * #GVolumeMonitor implementations when destroying a shadow mount for
828 * @mount, see g_mount_is_shadowed() for more information. The caller
829 * will need to emit the #GMount::changed signal on @mount manually.
834 g_mount_unshadow (GMount *mount)
838 g_return_if_fail (G_IS_MOUNT (mount));
841 priv = get_private (mount);
842 priv->shadow_ref_count -= 1;
843 if (priv->shadow_ref_count < 0)
844 g_warning ("Shadow ref count on GMount is negative");
845 G_UNLOCK (priv_lock);
848 #define __G_MOUNT_C__
849 #include "gioaliasdef.c"