build: Include host_machine.cpu_family() in tapset directory (Meson)
[glib.git] / gio / gmount.c
blob0169ea54b8739cfb5596b73058c96510e6ed941f
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
4 *
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.1 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, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
21 * David Zeuthen <davidz@redhat.com>
24 #include "config.h"
26 #include <string.h>
28 #include "gmount.h"
29 #include "gmountprivate.h"
30 #include "gthemedicon.h"
31 #include "gasyncresult.h"
32 #include "gtask.h"
33 #include "gioerror.h"
34 #include "glibintl.h"
37 /**
38 * SECTION:gmount
39 * @short_description: Mount management
40 * @include: gio/gio.h
41 * @see_also: GVolume, GUnixMountEntry, GUnixMountPoint
43 * The #GMount interface represents user-visible mounts. Note, when
44 * porting from GnomeVFS, #GMount is the moral equivalent of #GnomeVFSVolume.
46 * #GMount is a "mounted" filesystem that you can access. Mounted is in
47 * quotes because it's not the same as a unix mount, it might be a gvfs
48 * mount, but you can still access the files on it if you use GIO. Might or
49 * might not be related to a volume object.
51 * Unmounting a #GMount instance is an asynchronous operation. For
52 * more information about asynchronous operations, see #GAsyncResult
53 * and #GTask. To unmount a #GMount instance, first call
54 * g_mount_unmount_with_operation() with (at least) the #GMount instance and a
55 * #GAsyncReadyCallback. The callback will be fired when the
56 * operation has resolved (either with success or failure), and a
57 * #GAsyncResult structure will be passed to the callback. That
58 * callback should then call g_mount_unmount_with_operation_finish() with the #GMount
59 * and the #GAsyncResult data to see if the operation was completed
60 * successfully. If an @error is present when g_mount_unmount_with_operation_finish()
61 * is called, then it will be filled with any error information.
62 **/
64 typedef GMountIface GMountInterface;
65 G_DEFINE_INTERFACE (GMount, g_mount, G_TYPE_OBJECT)
67 static void
68 g_mount_default_init (GMountInterface *iface)
70 /**
71 * GMount::changed:
72 * @mount: the object on which the signal is emitted
74 * Emitted when the mount has been changed.
75 **/
76 g_signal_new (I_("changed"),
77 G_TYPE_MOUNT,
78 G_SIGNAL_RUN_LAST,
79 G_STRUCT_OFFSET (GMountIface, changed),
80 NULL, NULL,
81 g_cclosure_marshal_VOID__VOID,
82 G_TYPE_NONE, 0);
84 /**
85 * GMount::unmounted:
86 * @mount: the object on which the signal is emitted
88 * This signal is emitted when the #GMount have been
89 * unmounted. If the recipient is holding references to the
90 * object they should release them so the object can be
91 * finalized.
92 **/
93 g_signal_new (I_("unmounted"),
94 G_TYPE_MOUNT,
95 G_SIGNAL_RUN_LAST,
96 G_STRUCT_OFFSET (GMountIface, unmounted),
97 NULL, NULL,
98 g_cclosure_marshal_VOID__VOID,
99 G_TYPE_NONE, 0);
101 * GMount::pre-unmount:
102 * @mount: the object on which the signal is emitted
104 * This signal may be emitted when the #GMount is about to be
105 * unmounted.
107 * This signal depends on the backend and is only emitted if
108 * GIO was used to unmount.
110 * Since: 2.22
112 g_signal_new (I_("pre-unmount"),
113 G_TYPE_MOUNT,
114 G_SIGNAL_RUN_LAST,
115 G_STRUCT_OFFSET (GMountIface, pre_unmount),
116 NULL, NULL,
117 g_cclosure_marshal_VOID__VOID,
118 G_TYPE_NONE, 0);
122 * g_mount_get_root:
123 * @mount: a #GMount.
125 * Gets the root directory on @mount.
127 * Returns: (transfer full): a #GFile.
128 * The returned object should be unreffed with
129 * g_object_unref() when no longer needed.
131 GFile *
132 g_mount_get_root (GMount *mount)
134 GMountIface *iface;
136 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
138 iface = G_MOUNT_GET_IFACE (mount);
140 return (* iface->get_root) (mount);
144 * g_mount_get_default_location:
145 * @mount: a #GMount.
147 * Gets the default location of @mount. The default location of the given
148 * @mount is a path that reflects the main entry point for the user (e.g.
149 * the home directory, or the root of the volume).
151 * Returns: (transfer full): a #GFile.
152 * The returned object should be unreffed with
153 * g_object_unref() when no longer needed.
155 GFile *
156 g_mount_get_default_location (GMount *mount)
158 GMountIface *iface;
159 GFile *file;
161 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
163 iface = G_MOUNT_GET_IFACE (mount);
165 /* Fallback to get_root when default_location () is not available */
166 if (iface->get_default_location)
167 file = (* iface->get_default_location) (mount);
168 else
169 file = (* iface->get_root) (mount);
171 return file;
175 * g_mount_get_name:
176 * @mount: a #GMount.
178 * Gets the name of @mount.
180 * Returns: the name for the given @mount.
181 * The returned string should be freed with g_free()
182 * when no longer needed.
184 char *
185 g_mount_get_name (GMount *mount)
187 GMountIface *iface;
189 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
191 iface = G_MOUNT_GET_IFACE (mount);
193 return (* iface->get_name) (mount);
197 * g_mount_get_icon:
198 * @mount: a #GMount.
200 * Gets the icon for @mount.
202 * Returns: (transfer full): a #GIcon.
203 * The returned object should be unreffed with
204 * g_object_unref() when no longer needed.
206 GIcon *
207 g_mount_get_icon (GMount *mount)
209 GMountIface *iface;
211 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
213 iface = G_MOUNT_GET_IFACE (mount);
215 return (* iface->get_icon) (mount);
220 * g_mount_get_symbolic_icon:
221 * @mount: a #GMount.
223 * Gets the symbolic icon for @mount.
225 * Returns: (transfer full): a #GIcon.
226 * The returned object should be unreffed with
227 * g_object_unref() when no longer needed.
229 * Since: 2.34
231 GIcon *
232 g_mount_get_symbolic_icon (GMount *mount)
234 GMountIface *iface;
235 GIcon *ret;
237 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
239 iface = G_MOUNT_GET_IFACE (mount);
241 if (iface->get_symbolic_icon != NULL)
242 ret = iface->get_symbolic_icon (mount);
243 else
244 ret = g_themed_icon_new_with_default_fallbacks ("folder-remote-symbolic");
246 return ret;
250 * g_mount_get_uuid:
251 * @mount: a #GMount.
253 * Gets the UUID for the @mount. The reference is typically based on
254 * the file system UUID for the mount in question and should be
255 * considered an opaque string. Returns %NULL if there is no UUID
256 * available.
258 * Returns: the UUID for @mount or %NULL if no UUID can be computed.
259 * The returned string should be freed with g_free()
260 * when no longer needed.
262 char *
263 g_mount_get_uuid (GMount *mount)
265 GMountIface *iface;
267 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
269 iface = G_MOUNT_GET_IFACE (mount);
271 return (* iface->get_uuid) (mount);
275 * g_mount_get_volume:
276 * @mount: a #GMount.
278 * Gets the volume for the @mount.
280 * Returns: (transfer full): a #GVolume or %NULL if @mount is not associated with a volume.
281 * The returned object should be unreffed with
282 * g_object_unref() when no longer needed.
284 GVolume *
285 g_mount_get_volume (GMount *mount)
287 GMountIface *iface;
289 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
291 iface = G_MOUNT_GET_IFACE (mount);
293 return (* iface->get_volume) (mount);
297 * g_mount_get_drive:
298 * @mount: a #GMount.
300 * Gets the drive for the @mount.
302 * This is a convenience method for getting the #GVolume and then
303 * using that object to get the #GDrive.
305 * Returns: (transfer full): a #GDrive or %NULL if @mount is not associated with a volume or a drive.
306 * The returned object should be unreffed with
307 * g_object_unref() when no longer needed.
309 GDrive *
310 g_mount_get_drive (GMount *mount)
312 GMountIface *iface;
314 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
316 iface = G_MOUNT_GET_IFACE (mount);
318 return (* iface->get_drive) (mount);
322 * g_mount_can_unmount:
323 * @mount: a #GMount.
325 * Checks if @mount can be unmounted.
327 * Returns: %TRUE if the @mount can be unmounted.
329 gboolean
330 g_mount_can_unmount (GMount *mount)
332 GMountIface *iface;
334 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
336 iface = G_MOUNT_GET_IFACE (mount);
338 return (* iface->can_unmount) (mount);
342 * g_mount_can_eject:
343 * @mount: a #GMount.
345 * Checks if @mount can be ejected.
347 * Returns: %TRUE if the @mount can be ejected.
349 gboolean
350 g_mount_can_eject (GMount *mount)
352 GMountIface *iface;
354 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
356 iface = G_MOUNT_GET_IFACE (mount);
358 return (* iface->can_eject) (mount);
362 * g_mount_unmount:
363 * @mount: a #GMount.
364 * @flags: flags affecting the operation
365 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
366 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
367 * @user_data: user data passed to @callback.
369 * Unmounts a mount. This is an asynchronous operation, and is
370 * finished by calling g_mount_unmount_finish() with the @mount
371 * and #GAsyncResult data returned in the @callback.
373 * Deprecated: 2.22: Use g_mount_unmount_with_operation() instead.
375 void
376 g_mount_unmount (GMount *mount,
377 GMountUnmountFlags flags,
378 GCancellable *cancellable,
379 GAsyncReadyCallback callback,
380 gpointer user_data)
382 GMountIface *iface;
384 g_return_if_fail (G_IS_MOUNT (mount));
386 iface = G_MOUNT_GET_IFACE (mount);
388 if (iface->unmount == NULL)
390 g_task_report_new_error (mount, callback, user_data,
391 g_mount_unmount_with_operation,
392 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
393 /* Translators: This is an error
394 * message for mount objects that
395 * don't implement unmount. */
396 _("mount doesn’t implement “unmount”"));
397 return;
400 (* iface->unmount) (mount, flags, cancellable, callback, user_data);
404 * g_mount_unmount_finish:
405 * @mount: a #GMount.
406 * @result: a #GAsyncResult.
407 * @error: a #GError location to store the error occurring, or %NULL to
408 * ignore.
410 * Finishes unmounting a mount. If any errors occurred during the operation,
411 * @error will be set to contain the errors and %FALSE will be returned.
413 * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
415 * Deprecated: 2.22: Use g_mount_unmount_with_operation_finish() instead.
417 gboolean
418 g_mount_unmount_finish (GMount *mount,
419 GAsyncResult *result,
420 GError **error)
422 GMountIface *iface;
424 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
425 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
427 if (g_async_result_legacy_propagate_error (result, error))
428 return FALSE;
429 else if (g_async_result_is_tagged (result, g_mount_unmount_with_operation))
430 return g_task_propagate_boolean (G_TASK (result), error);
432 iface = G_MOUNT_GET_IFACE (mount);
433 return (* iface->unmount_finish) (mount, result, error);
438 * g_mount_eject:
439 * @mount: a #GMount.
440 * @flags: flags affecting the unmount if required for eject
441 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
442 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
443 * @user_data: user data passed to @callback.
445 * Ejects a mount. This is an asynchronous operation, and is
446 * finished by calling g_mount_eject_finish() with the @mount
447 * and #GAsyncResult data returned in the @callback.
449 * Deprecated: 2.22: Use g_mount_eject_with_operation() instead.
451 void
452 g_mount_eject (GMount *mount,
453 GMountUnmountFlags flags,
454 GCancellable *cancellable,
455 GAsyncReadyCallback callback,
456 gpointer user_data)
458 GMountIface *iface;
460 g_return_if_fail (G_IS_MOUNT (mount));
462 iface = G_MOUNT_GET_IFACE (mount);
464 if (iface->eject == NULL)
466 g_task_report_new_error (mount, callback, user_data,
467 g_mount_eject_with_operation,
468 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
469 /* Translators: This is an error
470 * message for mount objects that
471 * don't implement eject. */
472 _("mount doesn’t implement “eject”"));
473 return;
476 (* iface->eject) (mount, flags, cancellable, callback, user_data);
480 * g_mount_eject_finish:
481 * @mount: a #GMount.
482 * @result: a #GAsyncResult.
483 * @error: a #GError location to store the error occurring, or %NULL to
484 * ignore.
486 * Finishes ejecting a mount. If any errors occurred during the operation,
487 * @error will be set to contain the errors and %FALSE will be returned.
489 * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
491 * Deprecated: 2.22: Use g_mount_eject_with_operation_finish() instead.
493 gboolean
494 g_mount_eject_finish (GMount *mount,
495 GAsyncResult *result,
496 GError **error)
498 GMountIface *iface;
500 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
501 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
503 if (g_async_result_legacy_propagate_error (result, error))
504 return FALSE;
505 else if (g_async_result_is_tagged (result, g_mount_eject_with_operation))
506 return g_task_propagate_boolean (G_TASK (result), error);
508 iface = G_MOUNT_GET_IFACE (mount);
509 return (* iface->eject_finish) (mount, result, error);
513 * g_mount_unmount_with_operation:
514 * @mount: a #GMount.
515 * @flags: flags affecting the operation
516 * @mount_operation: (nullable): a #GMountOperation or %NULL to avoid
517 * user interaction.
518 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
519 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
520 * @user_data: user data passed to @callback.
522 * Unmounts a mount. This is an asynchronous operation, and is
523 * finished by calling g_mount_unmount_with_operation_finish() with the @mount
524 * and #GAsyncResult data returned in the @callback.
526 * Since: 2.22
528 void
529 g_mount_unmount_with_operation (GMount *mount,
530 GMountUnmountFlags flags,
531 GMountOperation *mount_operation,
532 GCancellable *cancellable,
533 GAsyncReadyCallback callback,
534 gpointer user_data)
536 GMountIface *iface;
538 g_return_if_fail (G_IS_MOUNT (mount));
540 iface = G_MOUNT_GET_IFACE (mount);
542 if (iface->unmount == NULL && iface->unmount_with_operation == NULL)
544 g_task_report_new_error (mount, callback, user_data,
545 g_mount_unmount_with_operation,
546 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
547 /* Translators: This is an error
548 * message for mount objects that
549 * don't implement any of unmount or unmount_with_operation. */
550 _("mount doesn’t implement “unmount” or “unmount_with_operation”"));
551 return;
554 if (iface->unmount_with_operation != NULL)
555 (* iface->unmount_with_operation) (mount, flags, mount_operation, cancellable, callback, user_data);
556 else
557 (* iface->unmount) (mount, flags, cancellable, callback, user_data);
561 * g_mount_unmount_with_operation_finish:
562 * @mount: a #GMount.
563 * @result: a #GAsyncResult.
564 * @error: a #GError location to store the error occurring, or %NULL to
565 * ignore.
567 * Finishes unmounting a mount. If any errors occurred during the operation,
568 * @error will be set to contain the errors and %FALSE will be returned.
570 * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
572 * Since: 2.22
574 gboolean
575 g_mount_unmount_with_operation_finish (GMount *mount,
576 GAsyncResult *result,
577 GError **error)
579 GMountIface *iface;
581 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
582 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
584 if (g_async_result_legacy_propagate_error (result, error))
585 return FALSE;
586 else if (g_async_result_is_tagged (result, g_mount_unmount_with_operation))
587 return g_task_propagate_boolean (G_TASK (result), error);
589 iface = G_MOUNT_GET_IFACE (mount);
590 if (iface->unmount_with_operation_finish != NULL)
591 return (* iface->unmount_with_operation_finish) (mount, result, error);
592 else
593 return (* iface->unmount_finish) (mount, result, error);
598 * g_mount_eject_with_operation:
599 * @mount: a #GMount.
600 * @flags: flags affecting the unmount if required for eject
601 * @mount_operation: (nullable): a #GMountOperation or %NULL to avoid
602 * user interaction.
603 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
604 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
605 * @user_data: user data passed to @callback.
607 * Ejects a mount. This is an asynchronous operation, and is
608 * finished by calling g_mount_eject_with_operation_finish() with the @mount
609 * and #GAsyncResult data returned in the @callback.
611 * Since: 2.22
613 void
614 g_mount_eject_with_operation (GMount *mount,
615 GMountUnmountFlags flags,
616 GMountOperation *mount_operation,
617 GCancellable *cancellable,
618 GAsyncReadyCallback callback,
619 gpointer user_data)
621 GMountIface *iface;
623 g_return_if_fail (G_IS_MOUNT (mount));
625 iface = G_MOUNT_GET_IFACE (mount);
627 if (iface->eject == NULL && iface->eject_with_operation == NULL)
629 g_task_report_new_error (mount, callback, user_data,
630 g_mount_eject_with_operation,
631 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
632 /* Translators: This is an error
633 * message for mount objects that
634 * don't implement any of eject or eject_with_operation. */
635 _("mount doesn’t implement “eject” or “eject_with_operation”"));
636 return;
639 if (iface->eject_with_operation != NULL)
640 (* iface->eject_with_operation) (mount, flags, mount_operation, cancellable, callback, user_data);
641 else
642 (* iface->eject) (mount, flags, cancellable, callback, user_data);
646 * g_mount_eject_with_operation_finish:
647 * @mount: a #GMount.
648 * @result: a #GAsyncResult.
649 * @error: a #GError location to store the error occurring, or %NULL to
650 * ignore.
652 * Finishes ejecting a mount. If any errors occurred during the operation,
653 * @error will be set to contain the errors and %FALSE will be returned.
655 * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
657 * Since: 2.22
659 gboolean
660 g_mount_eject_with_operation_finish (GMount *mount,
661 GAsyncResult *result,
662 GError **error)
664 GMountIface *iface;
666 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
667 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
669 if (g_async_result_legacy_propagate_error (result, error))
670 return FALSE;
671 else if (g_async_result_is_tagged (result, g_mount_eject_with_operation))
672 return g_task_propagate_boolean (G_TASK (result), error);
674 iface = G_MOUNT_GET_IFACE (mount);
675 if (iface->eject_with_operation_finish != NULL)
676 return (* iface->eject_with_operation_finish) (mount, result, error);
677 else
678 return (* iface->eject_finish) (mount, result, error);
682 * g_mount_remount:
683 * @mount: a #GMount.
684 * @flags: flags affecting the operation
685 * @mount_operation: (nullable): a #GMountOperation or %NULL to avoid
686 * user interaction.
687 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
688 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
689 * @user_data: user data passed to @callback.
691 * Remounts a mount. This is an asynchronous operation, and is
692 * finished by calling g_mount_remount_finish() with the @mount
693 * and #GAsyncResults data returned in the @callback.
695 * Remounting is useful when some setting affecting the operation
696 * of the volume has been changed, as these may need a remount to
697 * take affect. While this is semantically equivalent with unmounting
698 * and then remounting not all backends might need to actually be
699 * unmounted.
701 void
702 g_mount_remount (GMount *mount,
703 GMountMountFlags flags,
704 GMountOperation *mount_operation,
705 GCancellable *cancellable,
706 GAsyncReadyCallback callback,
707 gpointer user_data)
709 GMountIface *iface;
711 g_return_if_fail (G_IS_MOUNT (mount));
713 iface = G_MOUNT_GET_IFACE (mount);
715 if (iface->remount == NULL)
717 g_task_report_new_error (mount, callback, user_data,
718 g_mount_remount,
719 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
720 /* Translators: This is an error
721 * message for mount objects that
722 * don't implement remount. */
723 _("mount doesn’t implement “remount”"));
724 return;
727 (* iface->remount) (mount, flags, mount_operation, cancellable, callback, user_data);
731 * g_mount_remount_finish:
732 * @mount: a #GMount.
733 * @result: a #GAsyncResult.
734 * @error: a #GError location to store the error occurring, or %NULL to
735 * ignore.
737 * Finishes remounting a mount. If any errors occurred during the operation,
738 * @error will be set to contain the errors and %FALSE will be returned.
740 * Returns: %TRUE if the mount was successfully remounted. %FALSE otherwise.
742 gboolean
743 g_mount_remount_finish (GMount *mount,
744 GAsyncResult *result,
745 GError **error)
747 GMountIface *iface;
749 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
750 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
752 if (g_async_result_legacy_propagate_error (result, error))
753 return FALSE;
754 else if (g_async_result_is_tagged (result, g_mount_remount))
755 return g_task_propagate_boolean (G_TASK (result), error);
757 iface = G_MOUNT_GET_IFACE (mount);
758 return (* iface->remount_finish) (mount, result, error);
762 * g_mount_guess_content_type:
763 * @mount: a #GMount
764 * @force_rescan: Whether to force a rescan of the content.
765 * Otherwise a cached result will be used if available
766 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
767 * @callback: a #GAsyncReadyCallback
768 * @user_data: user data passed to @callback
770 * Tries to guess the type of content stored on @mount. Returns one or
771 * more textual identifiers of well-known content types (typically
772 * prefixed with "x-content/"), e.g. x-content/image-dcf for camera
773 * memory cards. See the
774 * [shared-mime-info](http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec)
775 * specification for more on x-content types.
777 * This is an asynchronous operation (see
778 * g_mount_guess_content_type_sync() for the synchronous version), and
779 * is finished by calling g_mount_guess_content_type_finish() with the
780 * @mount and #GAsyncResult data returned in the @callback.
782 * Since: 2.18
784 void
785 g_mount_guess_content_type (GMount *mount,
786 gboolean force_rescan,
787 GCancellable *cancellable,
788 GAsyncReadyCallback callback,
789 gpointer user_data)
791 GMountIface *iface;
793 g_return_if_fail (G_IS_MOUNT (mount));
795 iface = G_MOUNT_GET_IFACE (mount);
797 if (iface->guess_content_type == NULL)
799 g_task_report_new_error (mount, callback, user_data,
800 g_mount_guess_content_type,
801 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
802 /* Translators: This is an error
803 * message for mount objects that
804 * don't implement content type guessing. */
805 _("mount doesn’t implement content type guessing"));
806 return;
809 (* iface->guess_content_type) (mount, force_rescan, cancellable, callback, user_data);
813 * g_mount_guess_content_type_finish:
814 * @mount: a #GMount
815 * @result: a #GAsyncResult
816 * @error: a #GError location to store the error occurring, or %NULL to
817 * ignore
819 * Finishes guessing content types of @mount. If any errors occurred
820 * during the operation, @error will be set to contain the errors and
821 * %FALSE will be returned. In particular, you may get an
822 * %G_IO_ERROR_NOT_SUPPORTED if the mount does not support content
823 * guessing.
825 * Returns: (transfer full) (element-type utf8): a %NULL-terminated array of content types or %NULL on error.
826 * Caller should free this array with g_strfreev() when done with it.
828 * Since: 2.18
830 gchar **
831 g_mount_guess_content_type_finish (GMount *mount,
832 GAsyncResult *result,
833 GError **error)
835 GMountIface *iface;
837 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
838 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
840 if (g_async_result_legacy_propagate_error (result, error))
841 return NULL;
842 else if (g_async_result_is_tagged (result, g_mount_guess_content_type))
843 return g_task_propagate_pointer (G_TASK (result), error);
845 iface = G_MOUNT_GET_IFACE (mount);
846 return (* iface->guess_content_type_finish) (mount, result, error);
850 * g_mount_guess_content_type_sync:
851 * @mount: a #GMount
852 * @force_rescan: Whether to force a rescan of the content.
853 * Otherwise a cached result will be used if available
854 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
855 * @error: a #GError location to store the error occurring, or %NULL to
856 * ignore
858 * Tries to guess the type of content stored on @mount. Returns one or
859 * more textual identifiers of well-known content types (typically
860 * prefixed with "x-content/"), e.g. x-content/image-dcf for camera
861 * memory cards. See the
862 * [shared-mime-info](http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec)
863 * specification for more on x-content types.
865 * This is an synchronous operation and as such may block doing IO;
866 * see g_mount_guess_content_type() for the asynchronous version.
868 * Returns: (transfer full) (element-type utf8): a %NULL-terminated array of content types or %NULL on error.
869 * Caller should free this array with g_strfreev() when done with it.
871 * Since: 2.18
873 char **
874 g_mount_guess_content_type_sync (GMount *mount,
875 gboolean force_rescan,
876 GCancellable *cancellable,
877 GError **error)
879 GMountIface *iface;
881 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
883 iface = G_MOUNT_GET_IFACE (mount);
885 if (iface->guess_content_type_sync == NULL)
887 g_set_error_literal (error,
888 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
889 /* Translators: This is an error
890 * message for mount objects that
891 * don't implement content type guessing. */
892 _("mount doesn’t implement synchronous content type guessing"));
894 return NULL;
897 return (* iface->guess_content_type_sync) (mount, force_rescan, cancellable, error);
900 G_LOCK_DEFINE_STATIC (priv_lock);
902 /* only access this structure when holding priv_lock */
903 typedef struct
905 gint shadow_ref_count;
906 } GMountPrivate;
908 static void
909 free_private (GMountPrivate *private)
911 G_LOCK (priv_lock);
912 g_free (private);
913 G_UNLOCK (priv_lock);
916 /* may only be called when holding priv_lock */
917 static GMountPrivate *
918 get_private (GMount *mount)
920 GMountPrivate *private;
922 private = g_object_get_data (G_OBJECT (mount), "g-mount-private");
923 if (G_LIKELY (private != NULL))
924 goto out;
926 private = g_new0 (GMountPrivate, 1);
927 g_object_set_data_full (G_OBJECT (mount),
928 "g-mount-private",
929 private,
930 (GDestroyNotify) free_private);
932 out:
933 return private;
937 * g_mount_is_shadowed:
938 * @mount: A #GMount.
940 * Determines if @mount is shadowed. Applications or libraries should
941 * avoid displaying @mount in the user interface if it is shadowed.
943 * A mount is said to be shadowed if there exists one or more user
944 * visible objects (currently #GMount objects) with a root that is
945 * inside the root of @mount.
947 * One application of shadow mounts is when exposing a single file
948 * system that is used to address several logical volumes. In this
949 * situation, a #GVolumeMonitor implementation would create two
950 * #GVolume objects (for example, one for the camera functionality of
951 * the device and one for a SD card reader on the device) with
952 * activation URIs `gphoto2://[usb:001,002]/store1/`
953 * and `gphoto2://[usb:001,002]/store2/`. When the
954 * underlying mount (with root
955 * `gphoto2://[usb:001,002]/`) is mounted, said
956 * #GVolumeMonitor implementation would create two #GMount objects
957 * (each with their root matching the corresponding volume activation
958 * root) that would shadow the original mount.
960 * The proxy monitor in GVfs 2.26 and later, automatically creates and
961 * manage shadow mounts (and shadows the underlying mount) if the
962 * activation root on a #GVolume is set.
964 * Returns: %TRUE if @mount is shadowed.
966 * Since: 2.20
968 gboolean
969 g_mount_is_shadowed (GMount *mount)
971 GMountPrivate *priv;
972 gboolean ret;
974 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
976 G_LOCK (priv_lock);
977 priv = get_private (mount);
978 ret = (priv->shadow_ref_count > 0);
979 G_UNLOCK (priv_lock);
981 return ret;
985 * g_mount_shadow:
986 * @mount: A #GMount.
988 * Increments the shadow count on @mount. Usually used by
989 * #GVolumeMonitor implementations when creating a shadow mount for
990 * @mount, see g_mount_is_shadowed() for more information. The caller
991 * will need to emit the #GMount::changed signal on @mount manually.
993 * Since: 2.20
995 void
996 g_mount_shadow (GMount *mount)
998 GMountPrivate *priv;
1000 g_return_if_fail (G_IS_MOUNT (mount));
1002 G_LOCK (priv_lock);
1003 priv = get_private (mount);
1004 priv->shadow_ref_count += 1;
1005 G_UNLOCK (priv_lock);
1009 * g_mount_unshadow:
1010 * @mount: A #GMount.
1012 * Decrements the shadow count on @mount. Usually used by
1013 * #GVolumeMonitor implementations when destroying a shadow mount for
1014 * @mount, see g_mount_is_shadowed() for more information. The caller
1015 * will need to emit the #GMount::changed signal on @mount manually.
1017 * Since: 2.20
1019 void
1020 g_mount_unshadow (GMount *mount)
1022 GMountPrivate *priv;
1024 g_return_if_fail (G_IS_MOUNT (mount));
1026 G_LOCK (priv_lock);
1027 priv = get_private (mount);
1028 priv->shadow_ref_count -= 1;
1029 if (priv->shadow_ref_count < 0)
1030 g_warning ("Shadow ref count on GMount is negative");
1031 G_UNLOCK (priv_lock);
1035 * g_mount_get_sort_key:
1036 * @mount: A #GMount.
1038 * Gets the sort key for @mount, if any.
1040 * Returns: Sorting key for @mount or %NULL if no such key is available.
1042 * Since: 2.32
1044 const gchar *
1045 g_mount_get_sort_key (GMount *mount)
1047 const gchar *ret = NULL;
1048 GMountIface *iface;
1050 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
1052 iface = G_MOUNT_GET_IFACE (mount);
1053 if (iface->get_sort_key != NULL)
1054 ret = iface->get_sort_key (mount);
1056 return ret;