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,
153 * Gets the root directory on @mount.
156 * The returned object should be unreffed with
157 * g_object_unref() when no longer needed.
160 g_mount_get_root (GMount *mount)
164 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
166 iface = G_MOUNT_GET_IFACE (mount);
168 return (* iface->get_root) (mount);
175 * Gets the name of @mount.
177 * Returns: the name for the given @mount.
178 * The returned string should be freed with g_free()
179 * when no longer needed.
182 g_mount_get_name (GMount *mount)
186 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
188 iface = G_MOUNT_GET_IFACE (mount);
190 return (* iface->get_name) (mount);
197 * Gets the icon for @mount.
200 * The returned object should be unreffed with
201 * g_object_unref() when no longer needed.
204 g_mount_get_icon (GMount *mount)
208 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
210 iface = G_MOUNT_GET_IFACE (mount);
212 return (* iface->get_icon) (mount);
219 * Gets the UUID for the @mount. The reference is typically based on
220 * the file system UUID for the mount in question and should be
221 * considered an opaque string. Returns %NULL if there is no UUID
224 * Returns: the UUID for @mount or %NULL if no UUID can be computed.
225 * The returned string should be freed with g_free()
226 * when no longer needed.
229 g_mount_get_uuid (GMount *mount)
233 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
235 iface = G_MOUNT_GET_IFACE (mount);
237 return (* iface->get_uuid) (mount);
241 * g_mount_get_volume:
244 * Gets the volume for the @mount.
246 * Returns: a #GVolume or %NULL if @mount is not associated with a volume.
247 * The returned object should be unreffed with
248 * g_object_unref() when no longer needed.
251 g_mount_get_volume (GMount *mount)
255 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
257 iface = G_MOUNT_GET_IFACE (mount);
259 return (* iface->get_volume) (mount);
266 * Gets the drive for the @mount.
268 * This is a convenience method for getting the #GVolume and then
269 * using that object to get the #GDrive.
271 * Returns: a #GDrive or %NULL if @mount is not associated with a volume or a drive.
272 * The returned object should be unreffed with
273 * g_object_unref() when no longer needed.
276 g_mount_get_drive (GMount *mount)
280 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
282 iface = G_MOUNT_GET_IFACE (mount);
284 return (* iface->get_drive) (mount);
288 * g_mount_can_unmount:
291 * Checks if @mount can be mounted.
293 * Returns: %TRUE if the @mount can be unmounted.
296 g_mount_can_unmount (GMount *mount)
300 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
302 iface = G_MOUNT_GET_IFACE (mount);
304 return (* iface->can_unmount) (mount);
311 * Checks if @mount can be eject.
313 * Returns: %TRUE if the @mount can be ejected.
316 g_mount_can_eject (GMount *mount)
320 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
322 iface = G_MOUNT_GET_IFACE (mount);
324 return (* iface->can_eject) (mount);
330 * @flags: flags affecting the operation
331 * @cancellable: optional #GCancellable object, %NULL to ignore.
332 * @callback: a #GAsyncReadyCallback, or %NULL.
333 * @user_data: user data passed to @callback.
335 * Unmounts a mount. This is an asynchronous operation, and is
336 * finished by calling g_mount_unmount_finish() with the @mount
337 * and #GAsyncResult data returned in the @callback.
340 g_mount_unmount (GMount *mount,
341 GMountUnmountFlags flags,
342 GCancellable *cancellable,
343 GAsyncReadyCallback callback,
348 g_return_if_fail (G_IS_MOUNT (mount));
350 iface = G_MOUNT_GET_IFACE (mount);
352 if (iface->unmount == NULL)
354 g_simple_async_report_error_in_idle (G_OBJECT (mount),
356 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
357 /* Translators: This is an error
358 * message for mount objects that
359 * don't implement unmount. */
360 _("mount doesn't implement unmount"));
365 (* iface->unmount) (mount, flags, cancellable, callback, user_data);
369 * g_mount_unmount_finish:
371 * @result: a #GAsyncResult.
372 * @error: a #GError location to store the error occuring, or %NULL to
375 * Finishes unmounting a mount. If any errors occurred during the operation,
376 * @error will be set to contain the errors and %FALSE will be returned.
378 * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
381 g_mount_unmount_finish (GMount *mount,
382 GAsyncResult *result,
387 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
388 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
390 if (G_IS_SIMPLE_ASYNC_RESULT (result))
392 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
393 if (g_simple_async_result_propagate_error (simple, error))
397 iface = G_MOUNT_GET_IFACE (mount);
398 return (* iface->unmount_finish) (mount, result, error);
405 * @flags: flags affecting the unmount if required for eject
406 * @cancellable: optional #GCancellable object, %NULL to ignore.
407 * @callback: a #GAsyncReadyCallback, or %NULL.
408 * @user_data: user data passed to @callback.
410 * Ejects a mount. This is an asynchronous operation, and is
411 * finished by calling g_mount_eject_finish() with the @mount
412 * and #GAsyncResult data returned in the @callback.
415 g_mount_eject (GMount *mount,
416 GMountUnmountFlags flags,
417 GCancellable *cancellable,
418 GAsyncReadyCallback callback,
423 g_return_if_fail (G_IS_MOUNT (mount));
425 iface = G_MOUNT_GET_IFACE (mount);
427 if (iface->eject == NULL)
429 g_simple_async_report_error_in_idle (G_OBJECT (mount),
431 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
432 /* Translators: This is an error
433 * message for mount objects that
434 * don't implement eject. */
435 _("mount doesn't implement eject"));
440 (* iface->eject) (mount, flags, cancellable, callback, user_data);
444 * g_mount_eject_finish:
446 * @result: a #GAsyncResult.
447 * @error: a #GError location to store the error occuring, or %NULL to
450 * Finishes ejecting a mount. If any errors occurred during the operation,
451 * @error will be set to contain the errors and %FALSE will be returned.
453 * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
456 g_mount_eject_finish (GMount *mount,
457 GAsyncResult *result,
462 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
463 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
465 if (G_IS_SIMPLE_ASYNC_RESULT (result))
467 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
468 if (g_simple_async_result_propagate_error (simple, error))
472 iface = G_MOUNT_GET_IFACE (mount);
473 return (* iface->eject_finish) (mount, result, error);
479 * @flags: flags affecting the operation
480 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
481 * @cancellable: optional #GCancellable object, %NULL to ignore.
482 * @callback: a #GAsyncReadyCallback, or %NULL.
483 * @user_data: user data passed to @callback.
485 * Remounts a mount. This is an asynchronous operation, and is
486 * finished by calling g_mount_remount_finish() with the @mount
487 * and #GAsyncResults data returned in the @callback.
489 * Remounting is useful when some setting affecting the operation
490 * of the volume has been changed, as these may need a remount to
491 * take affect. While this is semantically equivalent with unmounting
492 * and then remounting not all backends might need to actually be
496 g_mount_remount (GMount *mount,
497 GMountMountFlags flags,
498 GMountOperation *mount_operation,
499 GCancellable *cancellable,
500 GAsyncReadyCallback callback,
505 g_return_if_fail (G_IS_MOUNT (mount));
507 iface = G_MOUNT_GET_IFACE (mount);
509 if (iface->remount == NULL)
511 g_simple_async_report_error_in_idle (G_OBJECT (mount),
513 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
514 /* Translators: This is an error
515 * message for mount objects that
516 * don't implement remount. */
517 _("mount doesn't implement remount"));
522 (* iface->remount) (mount, flags, mount_operation, cancellable, callback, user_data);
526 * g_mount_remount_finish:
528 * @result: a #GAsyncResult.
529 * @error: a #GError location to store the error occuring, or %NULL to
532 * Finishes remounting a mount. If any errors occurred during the operation,
533 * @error will be set to contain the errors and %FALSE will be returned.
535 * Returns: %TRUE if the mount was successfully remounted. %FALSE otherwise.
538 g_mount_remount_finish (GMount *mount,
539 GAsyncResult *result,
544 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
545 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
547 if (G_IS_SIMPLE_ASYNC_RESULT (result))
549 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
550 if (g_simple_async_result_propagate_error (simple, error))
554 iface = G_MOUNT_GET_IFACE (mount);
555 return (* iface->remount_finish) (mount, result, error);
559 * g_mount_guess_content_type:
561 * @force_rescan: Whether to force a rescan of the content.
562 * Otherwise a cached result will be used if available
563 * @cancellable: optional #GCancellable object, %NULL to ignore
564 * @callback: a #GAsyncReadyCallback
565 * @user_data: user data passed to @callback
567 * Tries to guess the type of content stored on @mount. Returns one or
568 * more textual identifiers of well-known content types (typically
569 * prefixed with "x-content/"), e.g. x-content/image-dcf for camera
570 * memory cards. See the <ulink url="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec">shared-mime-info</ulink>
571 * specification for more on x-content types.
573 * This is an asynchronous operation (see
574 * g_mount_guess_content_type_sync() for the synchronous version), and
575 * is finished by calling g_mount_guess_content_type_finish() with the
576 * @mount and #GAsyncResult data returned in the @callback.
581 g_mount_guess_content_type (GMount *mount,
582 gboolean force_rescan,
583 GCancellable *cancellable,
584 GAsyncReadyCallback callback,
589 g_return_if_fail (G_IS_MOUNT (mount));
591 iface = G_MOUNT_GET_IFACE (mount);
593 if (iface->guess_content_type == NULL)
595 g_simple_async_report_error_in_idle (G_OBJECT (mount),
597 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
598 /* Translators: This is an error
599 * message for mount objects that
600 * don't implement content type guessing. */
601 _("mount doesn't implement content type guessing"));
606 (* iface->guess_content_type) (mount, force_rescan, cancellable, callback, user_data);
610 * g_mount_guess_content_type_finish:
612 * @result: a #GAsyncResult
613 * @error: a #GError location to store the error occuring, or %NULL to
616 * Finishes guessing content types of @mount. If any errors occured
617 * during the operation, @error will be set to contain the errors and
618 * %FALSE will be returned. In particular, you may get an
619 * %G_IO_ERROR_NOT_SUPPORTED if the mount does not support content
622 * Returns: a %NULL-terminated array of content types or %NULL on error.
623 * Caller should free this array with g_strfreev() when done with it.
628 g_mount_guess_content_type_finish (GMount *mount,
629 GAsyncResult *result,
634 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
635 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
637 if (G_IS_SIMPLE_ASYNC_RESULT (result))
639 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
640 if (g_simple_async_result_propagate_error (simple, error))
644 iface = G_MOUNT_GET_IFACE (mount);
645 return (* iface->guess_content_type_finish) (mount, result, error);
649 * g_mount_guess_content_type_sync:
651 * @force_rescan: Whether to force a rescan of the content.
652 * Otherwise a cached result will be used if available
653 * @cancellable: optional #GCancellable object, %NULL to ignore
654 * @error: a #GError location to store the error occuring, or %NULL to
657 * Tries to guess the type of content stored on @mount. Returns one or
658 * more textual identifiers of well-known content types (typically
659 * prefixed with "x-content/"), e.g. x-content/image-dcf for camera
660 * memory cards. See the <ulink url="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec">shared-mime-info</ulink>
661 * specification for more on x-content types.
663 * This is an synchronous operation and as such may block doing IO;
664 * see g_mount_guess_content_type() for the asynchronous version.
666 * Returns: a %NULL-terminated array of content types or %NULL on error.
667 * Caller should free this array with g_strfreev() when done with it.
672 g_mount_guess_content_type_sync (GMount *mount,
673 gboolean force_rescan,
674 GCancellable *cancellable,
679 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
681 iface = G_MOUNT_GET_IFACE (mount);
683 if (iface->guess_content_type_sync == NULL)
685 g_set_error_literal (error,
686 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
687 /* Translators: This is an error
688 * message for mount objects that
689 * don't implement content type guessing. */
690 _("mount doesn't implement synchronous content type guessing"));
695 return (* iface->guess_content_type_sync) (mount, force_rescan, cancellable, error);
698 G_LOCK_DEFINE_STATIC (priv_lock);
700 /* only access this structure when holding priv_lock */
703 gint shadow_ref_count;
707 free_private (GMountPrivate *private)
711 G_UNLOCK (priv_lock);
714 /* may only be called when holding priv_lock */
715 static GMountPrivate *
716 get_private (GMount *mount)
718 GMountPrivate *private;
720 private = g_object_get_data (G_OBJECT (mount), "g-mount-private");
721 if (G_LIKELY (private != NULL))
724 private = g_new0 (GMountPrivate, 1);
725 g_object_set_data_full (G_OBJECT (mount),
728 (GDestroyNotify) free_private);
735 * g_mount_is_shadowed:
738 * Determines if @mount is shadowed. Applications or libraries should
739 * avoid displaying @mount in the user interface if it is shadowed.
741 * A mount is said to be shadowed if there exists one or more user
742 * visible objects (currently #GMount objects) with a root that is
743 * inside the root of @mount.
745 * One application of shadow mounts is when exposing a single file
746 * system that is used to address several logical volumes. In this
747 * situation, a #GVolumeMonitor implementation would create two
748 * #GVolume objects (for example, one for the camera functionality of
749 * the device and one for a SD card reader on the device) with
750 * activation URIs <literal>gphoto2://[usb:001,002]/store1/</literal>
751 * and <literal>gphoto2://[usb:001,002]/store2/</literal>. When the
752 * underlying mount (with root
753 * <literal>gphoto2://[usb:001,002]/</literal>) is mounted, said
754 * #GVolumeMonitor implementation would create two #GMount objects
755 * (each with their root matching the corresponding volume activation
756 * root) that would shadow the original mount.
758 * The proxy monitor in GVfs 2.26 and later, automatically creates and
759 * manage shadow mounts (and shadows the underlying mount) if the
760 * activation root on a #GVolume is set.
762 * Returns: %TRUE if @mount is shadowed.
767 g_mount_is_shadowed (GMount *mount)
772 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
775 priv = get_private (mount);
776 ret = (priv->shadow_ref_count > 0);
777 G_UNLOCK (priv_lock);
786 * Increments the shadow count on @mount. Usually used by
787 * #GVolumeMonitor implementations when creating a shadow mount for
788 * @mount, see g_mount_is_shadowed() for more information. The caller
789 * will need to emit the #GMount::changed signal on @mount manually.
794 g_mount_shadow (GMount *mount)
798 g_return_if_fail (G_IS_MOUNT (mount));
801 priv = get_private (mount);
802 priv->shadow_ref_count += 1;
803 G_UNLOCK (priv_lock);
810 * Decrements the shadow count on @mount. Usually used by
811 * #GVolumeMonitor implementations when destroying a shadow mount for
812 * @mount, see g_mount_is_shadowed() for more information. The caller
813 * will need to emit the #GMount::changed signal on @mount manually.
818 g_mount_unshadow (GMount *mount)
822 g_return_if_fail (G_IS_MOUNT (mount));
825 priv = get_private (mount);
826 priv->shadow_ref_count -= 1;
827 if (priv->shadow_ref_count < 0)
828 g_warning ("Shadow ref count on GMount is negative");
829 G_UNLOCK (priv_lock);
832 #define __G_MOUNT_C__
833 #include "gioaliasdef.c"