Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / gio / gmount.c
blobdad4b85138f69c268b7d0b6f548eda150c59576b
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: (nullable) (transfer full): the UUID for @mount or %NULL if no UUID
259 * can be computed.
260 * The returned string should be freed with g_free()
261 * when no longer needed.
263 char *
264 g_mount_get_uuid (GMount *mount)
266 GMountIface *iface;
268 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
270 iface = G_MOUNT_GET_IFACE (mount);
272 return (* iface->get_uuid) (mount);
276 * g_mount_get_volume:
277 * @mount: a #GMount.
279 * Gets the volume for the @mount.
281 * Returns: (transfer full) (nullable): a #GVolume or %NULL if @mount is not
282 * associated with a volume.
283 * The returned object should be unreffed with
284 * g_object_unref() when no longer needed.
286 GVolume *
287 g_mount_get_volume (GMount *mount)
289 GMountIface *iface;
291 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
293 iface = G_MOUNT_GET_IFACE (mount);
295 return (* iface->get_volume) (mount);
299 * g_mount_get_drive:
300 * @mount: a #GMount.
302 * Gets the drive for the @mount.
304 * This is a convenience method for getting the #GVolume and then
305 * using that object to get the #GDrive.
307 * Returns: (transfer full) (nullable): a #GDrive or %NULL if @mount is not
308 * associated with a volume or a drive.
309 * The returned object should be unreffed with
310 * g_object_unref() when no longer needed.
312 GDrive *
313 g_mount_get_drive (GMount *mount)
315 GMountIface *iface;
317 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
319 iface = G_MOUNT_GET_IFACE (mount);
321 return (* iface->get_drive) (mount);
325 * g_mount_can_unmount:
326 * @mount: a #GMount.
328 * Checks if @mount can be unmounted.
330 * Returns: %TRUE if the @mount can be unmounted.
332 gboolean
333 g_mount_can_unmount (GMount *mount)
335 GMountIface *iface;
337 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
339 iface = G_MOUNT_GET_IFACE (mount);
341 return (* iface->can_unmount) (mount);
345 * g_mount_can_eject:
346 * @mount: a #GMount.
348 * Checks if @mount can be ejected.
350 * Returns: %TRUE if the @mount can be ejected.
352 gboolean
353 g_mount_can_eject (GMount *mount)
355 GMountIface *iface;
357 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
359 iface = G_MOUNT_GET_IFACE (mount);
361 return (* iface->can_eject) (mount);
365 * g_mount_unmount:
366 * @mount: a #GMount.
367 * @flags: flags affecting the operation
368 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
369 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
370 * @user_data: user data passed to @callback.
372 * Unmounts a mount. This is an asynchronous operation, and is
373 * finished by calling g_mount_unmount_finish() with the @mount
374 * and #GAsyncResult data returned in the @callback.
376 * Deprecated: 2.22: Use g_mount_unmount_with_operation() instead.
378 void
379 g_mount_unmount (GMount *mount,
380 GMountUnmountFlags flags,
381 GCancellable *cancellable,
382 GAsyncReadyCallback callback,
383 gpointer user_data)
385 GMountIface *iface;
387 g_return_if_fail (G_IS_MOUNT (mount));
389 iface = G_MOUNT_GET_IFACE (mount);
391 if (iface->unmount == NULL)
393 g_task_report_new_error (mount, callback, user_data,
394 g_mount_unmount_with_operation,
395 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
396 /* Translators: This is an error
397 * message for mount objects that
398 * don't implement unmount. */
399 _("mount doesn’t implement “unmount”"));
400 return;
403 (* iface->unmount) (mount, flags, cancellable, callback, user_data);
407 * g_mount_unmount_finish:
408 * @mount: a #GMount.
409 * @result: a #GAsyncResult.
410 * @error: a #GError location to store the error occurring, or %NULL to
411 * ignore.
413 * Finishes unmounting a mount. If any errors occurred during the operation,
414 * @error will be set to contain the errors and %FALSE will be returned.
416 * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
418 * Deprecated: 2.22: Use g_mount_unmount_with_operation_finish() instead.
420 gboolean
421 g_mount_unmount_finish (GMount *mount,
422 GAsyncResult *result,
423 GError **error)
425 GMountIface *iface;
427 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
428 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
430 if (g_async_result_legacy_propagate_error (result, error))
431 return FALSE;
432 else if (g_async_result_is_tagged (result, g_mount_unmount_with_operation))
433 return g_task_propagate_boolean (G_TASK (result), error);
435 iface = G_MOUNT_GET_IFACE (mount);
436 return (* iface->unmount_finish) (mount, result, error);
441 * g_mount_eject:
442 * @mount: a #GMount.
443 * @flags: flags affecting the unmount if required for eject
444 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
445 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
446 * @user_data: user data passed to @callback.
448 * Ejects a mount. This is an asynchronous operation, and is
449 * finished by calling g_mount_eject_finish() with the @mount
450 * and #GAsyncResult data returned in the @callback.
452 * Deprecated: 2.22: Use g_mount_eject_with_operation() instead.
454 void
455 g_mount_eject (GMount *mount,
456 GMountUnmountFlags flags,
457 GCancellable *cancellable,
458 GAsyncReadyCallback callback,
459 gpointer user_data)
461 GMountIface *iface;
463 g_return_if_fail (G_IS_MOUNT (mount));
465 iface = G_MOUNT_GET_IFACE (mount);
467 if (iface->eject == NULL)
469 g_task_report_new_error (mount, callback, user_data,
470 g_mount_eject_with_operation,
471 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
472 /* Translators: This is an error
473 * message for mount objects that
474 * don't implement eject. */
475 _("mount doesn’t implement “eject”"));
476 return;
479 (* iface->eject) (mount, flags, cancellable, callback, user_data);
483 * g_mount_eject_finish:
484 * @mount: a #GMount.
485 * @result: a #GAsyncResult.
486 * @error: a #GError location to store the error occurring, or %NULL to
487 * ignore.
489 * Finishes ejecting a mount. If any errors occurred during the operation,
490 * @error will be set to contain the errors and %FALSE will be returned.
492 * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
494 * Deprecated: 2.22: Use g_mount_eject_with_operation_finish() instead.
496 gboolean
497 g_mount_eject_finish (GMount *mount,
498 GAsyncResult *result,
499 GError **error)
501 GMountIface *iface;
503 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
504 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
506 if (g_async_result_legacy_propagate_error (result, error))
507 return FALSE;
508 else if (g_async_result_is_tagged (result, g_mount_eject_with_operation))
509 return g_task_propagate_boolean (G_TASK (result), error);
511 iface = G_MOUNT_GET_IFACE (mount);
512 return (* iface->eject_finish) (mount, result, error);
516 * g_mount_unmount_with_operation:
517 * @mount: a #GMount.
518 * @flags: flags affecting the operation
519 * @mount_operation: (nullable): a #GMountOperation or %NULL to avoid
520 * user interaction.
521 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
522 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
523 * @user_data: user data passed to @callback.
525 * Unmounts a mount. This is an asynchronous operation, and is
526 * finished by calling g_mount_unmount_with_operation_finish() with the @mount
527 * and #GAsyncResult data returned in the @callback.
529 * Since: 2.22
531 void
532 g_mount_unmount_with_operation (GMount *mount,
533 GMountUnmountFlags flags,
534 GMountOperation *mount_operation,
535 GCancellable *cancellable,
536 GAsyncReadyCallback callback,
537 gpointer user_data)
539 GMountIface *iface;
541 g_return_if_fail (G_IS_MOUNT (mount));
543 iface = G_MOUNT_GET_IFACE (mount);
545 if (iface->unmount == NULL && iface->unmount_with_operation == NULL)
547 g_task_report_new_error (mount, callback, user_data,
548 g_mount_unmount_with_operation,
549 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
550 /* Translators: This is an error
551 * message for mount objects that
552 * don't implement any of unmount or unmount_with_operation. */
553 _("mount doesn’t implement “unmount” or “unmount_with_operation”"));
554 return;
557 if (iface->unmount_with_operation != NULL)
558 (* iface->unmount_with_operation) (mount, flags, mount_operation, cancellable, callback, user_data);
559 else
560 (* iface->unmount) (mount, flags, cancellable, callback, user_data);
564 * g_mount_unmount_with_operation_finish:
565 * @mount: a #GMount.
566 * @result: a #GAsyncResult.
567 * @error: a #GError location to store the error occurring, or %NULL to
568 * ignore.
570 * Finishes unmounting a mount. If any errors occurred during the operation,
571 * @error will be set to contain the errors and %FALSE will be returned.
573 * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
575 * Since: 2.22
577 gboolean
578 g_mount_unmount_with_operation_finish (GMount *mount,
579 GAsyncResult *result,
580 GError **error)
582 GMountIface *iface;
584 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
585 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
587 if (g_async_result_legacy_propagate_error (result, error))
588 return FALSE;
589 else if (g_async_result_is_tagged (result, g_mount_unmount_with_operation))
590 return g_task_propagate_boolean (G_TASK (result), error);
592 iface = G_MOUNT_GET_IFACE (mount);
593 if (iface->unmount_with_operation_finish != NULL)
594 return (* iface->unmount_with_operation_finish) (mount, result, error);
595 else
596 return (* iface->unmount_finish) (mount, result, error);
601 * g_mount_eject_with_operation:
602 * @mount: a #GMount.
603 * @flags: flags affecting the unmount if required for eject
604 * @mount_operation: (nullable): a #GMountOperation or %NULL to avoid
605 * user interaction.
606 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
607 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
608 * @user_data: user data passed to @callback.
610 * Ejects a mount. This is an asynchronous operation, and is
611 * finished by calling g_mount_eject_with_operation_finish() with the @mount
612 * and #GAsyncResult data returned in the @callback.
614 * Since: 2.22
616 void
617 g_mount_eject_with_operation (GMount *mount,
618 GMountUnmountFlags flags,
619 GMountOperation *mount_operation,
620 GCancellable *cancellable,
621 GAsyncReadyCallback callback,
622 gpointer user_data)
624 GMountIface *iface;
626 g_return_if_fail (G_IS_MOUNT (mount));
628 iface = G_MOUNT_GET_IFACE (mount);
630 if (iface->eject == NULL && iface->eject_with_operation == NULL)
632 g_task_report_new_error (mount, callback, user_data,
633 g_mount_eject_with_operation,
634 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
635 /* Translators: This is an error
636 * message for mount objects that
637 * don't implement any of eject or eject_with_operation. */
638 _("mount doesn’t implement “eject” or “eject_with_operation”"));
639 return;
642 if (iface->eject_with_operation != NULL)
643 (* iface->eject_with_operation) (mount, flags, mount_operation, cancellable, callback, user_data);
644 else
645 (* iface->eject) (mount, flags, cancellable, callback, user_data);
649 * g_mount_eject_with_operation_finish:
650 * @mount: a #GMount.
651 * @result: a #GAsyncResult.
652 * @error: a #GError location to store the error occurring, or %NULL to
653 * ignore.
655 * Finishes ejecting a mount. If any errors occurred during the operation,
656 * @error will be set to contain the errors and %FALSE will be returned.
658 * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
660 * Since: 2.22
662 gboolean
663 g_mount_eject_with_operation_finish (GMount *mount,
664 GAsyncResult *result,
665 GError **error)
667 GMountIface *iface;
669 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
670 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
672 if (g_async_result_legacy_propagate_error (result, error))
673 return FALSE;
674 else if (g_async_result_is_tagged (result, g_mount_eject_with_operation))
675 return g_task_propagate_boolean (G_TASK (result), error);
677 iface = G_MOUNT_GET_IFACE (mount);
678 if (iface->eject_with_operation_finish != NULL)
679 return (* iface->eject_with_operation_finish) (mount, result, error);
680 else
681 return (* iface->eject_finish) (mount, result, error);
685 * g_mount_remount:
686 * @mount: a #GMount.
687 * @flags: flags affecting the operation
688 * @mount_operation: (nullable): a #GMountOperation or %NULL to avoid
689 * user interaction.
690 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
691 * @callback: (nullable): a #GAsyncReadyCallback, or %NULL.
692 * @user_data: user data passed to @callback.
694 * Remounts a mount. This is an asynchronous operation, and is
695 * finished by calling g_mount_remount_finish() with the @mount
696 * and #GAsyncResults data returned in the @callback.
698 * Remounting is useful when some setting affecting the operation
699 * of the volume has been changed, as these may need a remount to
700 * take affect. While this is semantically equivalent with unmounting
701 * and then remounting not all backends might need to actually be
702 * unmounted.
704 void
705 g_mount_remount (GMount *mount,
706 GMountMountFlags flags,
707 GMountOperation *mount_operation,
708 GCancellable *cancellable,
709 GAsyncReadyCallback callback,
710 gpointer user_data)
712 GMountIface *iface;
714 g_return_if_fail (G_IS_MOUNT (mount));
716 iface = G_MOUNT_GET_IFACE (mount);
718 if (iface->remount == NULL)
720 g_task_report_new_error (mount, callback, user_data,
721 g_mount_remount,
722 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
723 /* Translators: This is an error
724 * message for mount objects that
725 * don't implement remount. */
726 _("mount doesn’t implement “remount”"));
727 return;
730 (* iface->remount) (mount, flags, mount_operation, cancellable, callback, user_data);
734 * g_mount_remount_finish:
735 * @mount: a #GMount.
736 * @result: a #GAsyncResult.
737 * @error: a #GError location to store the error occurring, or %NULL to
738 * ignore.
740 * Finishes remounting a mount. If any errors occurred during the operation,
741 * @error will be set to contain the errors and %FALSE will be returned.
743 * Returns: %TRUE if the mount was successfully remounted. %FALSE otherwise.
745 gboolean
746 g_mount_remount_finish (GMount *mount,
747 GAsyncResult *result,
748 GError **error)
750 GMountIface *iface;
752 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
753 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
755 if (g_async_result_legacy_propagate_error (result, error))
756 return FALSE;
757 else if (g_async_result_is_tagged (result, g_mount_remount))
758 return g_task_propagate_boolean (G_TASK (result), error);
760 iface = G_MOUNT_GET_IFACE (mount);
761 return (* iface->remount_finish) (mount, result, error);
765 * g_mount_guess_content_type:
766 * @mount: a #GMount
767 * @force_rescan: Whether to force a rescan of the content.
768 * Otherwise a cached result will be used if available
769 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
770 * @callback: a #GAsyncReadyCallback
771 * @user_data: user data passed to @callback
773 * Tries to guess the type of content stored on @mount. Returns one or
774 * more textual identifiers of well-known content types (typically
775 * prefixed with "x-content/"), e.g. x-content/image-dcf for camera
776 * memory cards. See the
777 * [shared-mime-info](http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec)
778 * specification for more on x-content types.
780 * This is an asynchronous operation (see
781 * g_mount_guess_content_type_sync() for the synchronous version), and
782 * is finished by calling g_mount_guess_content_type_finish() with the
783 * @mount and #GAsyncResult data returned in the @callback.
785 * Since: 2.18
787 void
788 g_mount_guess_content_type (GMount *mount,
789 gboolean force_rescan,
790 GCancellable *cancellable,
791 GAsyncReadyCallback callback,
792 gpointer user_data)
794 GMountIface *iface;
796 g_return_if_fail (G_IS_MOUNT (mount));
798 iface = G_MOUNT_GET_IFACE (mount);
800 if (iface->guess_content_type == NULL)
802 g_task_report_new_error (mount, callback, user_data,
803 g_mount_guess_content_type,
804 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
805 /* Translators: This is an error
806 * message for mount objects that
807 * don't implement content type guessing. */
808 _("mount doesn’t implement content type guessing"));
809 return;
812 (* iface->guess_content_type) (mount, force_rescan, cancellable, callback, user_data);
816 * g_mount_guess_content_type_finish:
817 * @mount: a #GMount
818 * @result: a #GAsyncResult
819 * @error: a #GError location to store the error occurring, or %NULL to
820 * ignore
822 * Finishes guessing content types of @mount. If any errors occurred
823 * during the operation, @error will be set to contain the errors and
824 * %FALSE will be returned. In particular, you may get an
825 * %G_IO_ERROR_NOT_SUPPORTED if the mount does not support content
826 * guessing.
828 * Returns: (transfer full) (element-type utf8): a %NULL-terminated array of content types or %NULL on error.
829 * Caller should free this array with g_strfreev() when done with it.
831 * Since: 2.18
833 gchar **
834 g_mount_guess_content_type_finish (GMount *mount,
835 GAsyncResult *result,
836 GError **error)
838 GMountIface *iface;
840 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
841 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
843 if (g_async_result_legacy_propagate_error (result, error))
844 return NULL;
845 else if (g_async_result_is_tagged (result, g_mount_guess_content_type))
846 return g_task_propagate_pointer (G_TASK (result), error);
848 iface = G_MOUNT_GET_IFACE (mount);
849 return (* iface->guess_content_type_finish) (mount, result, error);
853 * g_mount_guess_content_type_sync:
854 * @mount: a #GMount
855 * @force_rescan: Whether to force a rescan of the content.
856 * Otherwise a cached result will be used if available
857 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
858 * @error: a #GError location to store the error occurring, or %NULL to
859 * ignore
861 * Tries to guess the type of content stored on @mount. Returns one or
862 * more textual identifiers of well-known content types (typically
863 * prefixed with "x-content/"), e.g. x-content/image-dcf for camera
864 * memory cards. See the
865 * [shared-mime-info](http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec)
866 * specification for more on x-content types.
868 * This is an synchronous operation and as such may block doing IO;
869 * see g_mount_guess_content_type() for the asynchronous version.
871 * Returns: (transfer full) (element-type utf8): a %NULL-terminated array of content types or %NULL on error.
872 * Caller should free this array with g_strfreev() when done with it.
874 * Since: 2.18
876 char **
877 g_mount_guess_content_type_sync (GMount *mount,
878 gboolean force_rescan,
879 GCancellable *cancellable,
880 GError **error)
882 GMountIface *iface;
884 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
886 iface = G_MOUNT_GET_IFACE (mount);
888 if (iface->guess_content_type_sync == NULL)
890 g_set_error_literal (error,
891 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
892 /* Translators: This is an error
893 * message for mount objects that
894 * don't implement content type guessing. */
895 _("mount doesn’t implement synchronous content type guessing"));
897 return NULL;
900 return (* iface->guess_content_type_sync) (mount, force_rescan, cancellable, error);
903 G_LOCK_DEFINE_STATIC (priv_lock);
905 /* only access this structure when holding priv_lock */
906 typedef struct
908 gint shadow_ref_count;
909 } GMountPrivate;
911 static void
912 free_private (GMountPrivate *private)
914 G_LOCK (priv_lock);
915 g_free (private);
916 G_UNLOCK (priv_lock);
919 /* may only be called when holding priv_lock */
920 static GMountPrivate *
921 get_private (GMount *mount)
923 GMountPrivate *private;
925 private = g_object_get_data (G_OBJECT (mount), "g-mount-private");
926 if (G_LIKELY (private != NULL))
927 goto out;
929 private = g_new0 (GMountPrivate, 1);
930 g_object_set_data_full (G_OBJECT (mount),
931 "g-mount-private",
932 private,
933 (GDestroyNotify) free_private);
935 out:
936 return private;
940 * g_mount_is_shadowed:
941 * @mount: A #GMount.
943 * Determines if @mount is shadowed. Applications or libraries should
944 * avoid displaying @mount in the user interface if it is shadowed.
946 * A mount is said to be shadowed if there exists one or more user
947 * visible objects (currently #GMount objects) with a root that is
948 * inside the root of @mount.
950 * One application of shadow mounts is when exposing a single file
951 * system that is used to address several logical volumes. In this
952 * situation, a #GVolumeMonitor implementation would create two
953 * #GVolume objects (for example, one for the camera functionality of
954 * the device and one for a SD card reader on the device) with
955 * activation URIs `gphoto2://[usb:001,002]/store1/`
956 * and `gphoto2://[usb:001,002]/store2/`. When the
957 * underlying mount (with root
958 * `gphoto2://[usb:001,002]/`) is mounted, said
959 * #GVolumeMonitor implementation would create two #GMount objects
960 * (each with their root matching the corresponding volume activation
961 * root) that would shadow the original mount.
963 * The proxy monitor in GVfs 2.26 and later, automatically creates and
964 * manage shadow mounts (and shadows the underlying mount) if the
965 * activation root on a #GVolume is set.
967 * Returns: %TRUE if @mount is shadowed.
969 * Since: 2.20
971 gboolean
972 g_mount_is_shadowed (GMount *mount)
974 GMountPrivate *priv;
975 gboolean ret;
977 g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
979 G_LOCK (priv_lock);
980 priv = get_private (mount);
981 ret = (priv->shadow_ref_count > 0);
982 G_UNLOCK (priv_lock);
984 return ret;
988 * g_mount_shadow:
989 * @mount: A #GMount.
991 * Increments the shadow count on @mount. Usually used by
992 * #GVolumeMonitor implementations when creating a shadow mount for
993 * @mount, see g_mount_is_shadowed() for more information. The caller
994 * will need to emit the #GMount::changed signal on @mount manually.
996 * Since: 2.20
998 void
999 g_mount_shadow (GMount *mount)
1001 GMountPrivate *priv;
1003 g_return_if_fail (G_IS_MOUNT (mount));
1005 G_LOCK (priv_lock);
1006 priv = get_private (mount);
1007 priv->shadow_ref_count += 1;
1008 G_UNLOCK (priv_lock);
1012 * g_mount_unshadow:
1013 * @mount: A #GMount.
1015 * Decrements the shadow count on @mount. Usually used by
1016 * #GVolumeMonitor implementations when destroying a shadow mount for
1017 * @mount, see g_mount_is_shadowed() for more information. The caller
1018 * will need to emit the #GMount::changed signal on @mount manually.
1020 * Since: 2.20
1022 void
1023 g_mount_unshadow (GMount *mount)
1025 GMountPrivate *priv;
1027 g_return_if_fail (G_IS_MOUNT (mount));
1029 G_LOCK (priv_lock);
1030 priv = get_private (mount);
1031 priv->shadow_ref_count -= 1;
1032 if (priv->shadow_ref_count < 0)
1033 g_warning ("Shadow ref count on GMount is negative");
1034 G_UNLOCK (priv_lock);
1038 * g_mount_get_sort_key:
1039 * @mount: A #GMount.
1041 * Gets the sort key for @mount, if any.
1043 * Returns: (nullable): Sorting key for @mount or %NULL if no such key is available.
1045 * Since: 2.32
1047 const gchar *
1048 g_mount_get_sort_key (GMount *mount)
1050 const gchar *ret = NULL;
1051 GMountIface *iface;
1053 g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
1055 iface = G_MOUNT_GET_IFACE (mount);
1056 if (iface->get_sort_key != NULL)
1057 ret = iface->get_sort_key (mount);
1059 return ret;