1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 
3 /* GIO - GLib Input, Output and Streaming Library
4  *
5  * Copyright (C) 2006-2008 Red Hat, Inc.
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  *         David Zeuthen <davidz@redhat.com>
22  */
23 
24 #include "config.h"
25 
26 #include <string.h>
27 
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"
35 
36 
37 /**
38  * SECTION:gmount
39  * @short_description: Mount management
40  * @include: gio/gio.h
41  * @see_also: GVolume, GUnixMountEntry, GUnixMountPoint
42  *
43  * The #GMount interface represents user-visible mounts. Note, when
44  * porting from GnomeVFS, #GMount is the moral equivalent of #GnomeVFSVolume.
45  *
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.
50  *
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  **/
63 
64 typedef GMountIface GMountInterface;
G_DEFINE_INTERFACE(GMount,g_mount,G_TYPE_OBJECT)65 G_DEFINE_INTERFACE (GMount, g_mount, G_TYPE_OBJECT)
66 
67 static void
68 g_mount_default_init (GMountInterface *iface)
69 {
70   /**
71    * GMount::changed:
72    * @mount: the object on which the signal is emitted
73    *
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                 NULL,
82                 G_TYPE_NONE, 0);
83 
84   /**
85    * GMount::unmounted:
86    * @mount: the object on which the signal is emitted
87    *
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                 NULL,
99                 G_TYPE_NONE, 0);
100   /**
101    * GMount::pre-unmount:
102    * @mount: the object on which the signal is emitted
103    *
104    * This signal may be emitted when the #GMount is about to be
105    * unmounted.
106    *
107    * This signal depends on the backend and is only emitted if
108    * GIO was used to unmount.
109    *
110    * Since: 2.22
111    **/
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                 NULL,
118                 G_TYPE_NONE, 0);
119 }
120 
121 /**
122  * g_mount_get_root:
123  * @mount: a #GMount.
124  *
125  * Gets the root directory on @mount.
126  *
127  * Returns: (transfer full): a #GFile.
128  *      The returned object should be unreffed with
129  *      g_object_unref() when no longer needed.
130  **/
131 GFile *
g_mount_get_root(GMount * mount)132 g_mount_get_root (GMount *mount)
133 {
134   GMountIface *iface;
135 
136   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
137 
138   iface = G_MOUNT_GET_IFACE (mount);
139 
140   return (* iface->get_root) (mount);
141 }
142 
143 /**
144  * g_mount_get_default_location:
145  * @mount: a #GMount.
146  *
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).
150  *
151  * Returns: (transfer full): a #GFile.
152  *      The returned object should be unreffed with
153  *      g_object_unref() when no longer needed.
154  **/
155 GFile *
g_mount_get_default_location(GMount * mount)156 g_mount_get_default_location (GMount *mount)
157 {
158   GMountIface *iface;
159   GFile       *file;
160 
161   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
162 
163   iface = G_MOUNT_GET_IFACE (mount);
164 
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);
170 
171   return file;
172 }
173 
174 /**
175  * g_mount_get_name:
176  * @mount: a #GMount.
177  *
178  * Gets the name of @mount.
179  *
180  * Returns: the name for the given @mount.
181  *     The returned string should be freed with g_free()
182  *     when no longer needed.
183  **/
184 char *
g_mount_get_name(GMount * mount)185 g_mount_get_name (GMount *mount)
186 {
187   GMountIface *iface;
188 
189   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
190 
191   iface = G_MOUNT_GET_IFACE (mount);
192 
193   return (* iface->get_name) (mount);
194 }
195 
196 /**
197  * g_mount_get_icon:
198  * @mount: a #GMount.
199  *
200  * Gets the icon for @mount.
201  *
202  * Returns: (transfer full): a #GIcon.
203  *      The returned object should be unreffed with
204  *      g_object_unref() when no longer needed.
205  **/
206 GIcon *
g_mount_get_icon(GMount * mount)207 g_mount_get_icon (GMount *mount)
208 {
209   GMountIface *iface;
210 
211   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
212 
213   iface = G_MOUNT_GET_IFACE (mount);
214 
215   return (* iface->get_icon) (mount);
216 }
217 
218 
219 /**
220  * g_mount_get_symbolic_icon:
221  * @mount: a #GMount.
222  *
223  * Gets the symbolic icon for @mount.
224  *
225  * Returns: (transfer full): a #GIcon.
226  *      The returned object should be unreffed with
227  *      g_object_unref() when no longer needed.
228  *
229  * Since: 2.34
230  **/
231 GIcon *
g_mount_get_symbolic_icon(GMount * mount)232 g_mount_get_symbolic_icon (GMount *mount)
233 {
234   GMountIface *iface;
235   GIcon *ret;
236 
237   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
238 
239   iface = G_MOUNT_GET_IFACE (mount);
240 
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");
245 
246   return ret;
247 }
248 
249 /**
250  * g_mount_get_uuid:
251  * @mount: a #GMount.
252  *
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.
257  *
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.
262  **/
263 char *
g_mount_get_uuid(GMount * mount)264 g_mount_get_uuid (GMount *mount)
265 {
266   GMountIface *iface;
267 
268   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
269 
270   iface = G_MOUNT_GET_IFACE (mount);
271 
272   return (* iface->get_uuid) (mount);
273 }
274 
275 /**
276  * g_mount_get_volume:
277  * @mount: a #GMount.
278  *
279  * Gets the volume for the @mount.
280  *
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.
285  **/
286 GVolume *
g_mount_get_volume(GMount * mount)287 g_mount_get_volume (GMount *mount)
288 {
289   GMountIface *iface;
290 
291   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
292 
293   iface = G_MOUNT_GET_IFACE (mount);
294 
295   return (* iface->get_volume) (mount);
296 }
297 
298 /**
299  * g_mount_get_drive:
300  * @mount: a #GMount.
301  *
302  * Gets the drive for the @mount.
303  *
304  * This is a convenience method for getting the #GVolume and then
305  * using that object to get the #GDrive.
306  *
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.
311  **/
312 GDrive *
g_mount_get_drive(GMount * mount)313 g_mount_get_drive (GMount *mount)
314 {
315   GMountIface *iface;
316 
317   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
318 
319   iface = G_MOUNT_GET_IFACE (mount);
320 
321   return (* iface->get_drive) (mount);
322 }
323 
324 /**
325  * g_mount_can_unmount:
326  * @mount: a #GMount.
327  *
328  * Checks if @mount can be unmounted.
329  *
330  * Returns: %TRUE if the @mount can be unmounted.
331  **/
332 gboolean
g_mount_can_unmount(GMount * mount)333 g_mount_can_unmount (GMount *mount)
334 {
335   GMountIface *iface;
336 
337   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
338 
339   iface = G_MOUNT_GET_IFACE (mount);
340 
341   return (* iface->can_unmount) (mount);
342 }
343 
344 /**
345  * g_mount_can_eject:
346  * @mount: a #GMount.
347  *
348  * Checks if @mount can be ejected.
349  *
350  * Returns: %TRUE if the @mount can be ejected.
351  **/
352 gboolean
g_mount_can_eject(GMount * mount)353 g_mount_can_eject (GMount *mount)
354 {
355   GMountIface *iface;
356 
357   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
358 
359   iface = G_MOUNT_GET_IFACE (mount);
360 
361   return (* iface->can_eject) (mount);
362 }
363 
364 /**
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.
371  *
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.
375  *
376  * Deprecated: 2.22: Use g_mount_unmount_with_operation() instead.
377  **/
378 void
g_mount_unmount(GMount * mount,GMountUnmountFlags flags,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)379 g_mount_unmount (GMount              *mount,
380                  GMountUnmountFlags   flags,
381                  GCancellable        *cancellable,
382                  GAsyncReadyCallback  callback,
383                  gpointer             user_data)
384 {
385   GMountIface *iface;
386 
387   g_return_if_fail (G_IS_MOUNT (mount));
388 
389   iface = G_MOUNT_GET_IFACE (mount);
390 
391   if (iface->unmount == NULL)
392     {
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;
401     }
402 
403   (* iface->unmount) (mount, flags, cancellable, callback, user_data);
404 }
405 
406 /**
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.
412  *
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.
415  *
416  * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
417  *
418  * Deprecated: 2.22: Use g_mount_unmount_with_operation_finish() instead.
419  **/
420 gboolean
g_mount_unmount_finish(GMount * mount,GAsyncResult * result,GError ** error)421 g_mount_unmount_finish (GMount        *mount,
422                         GAsyncResult  *result,
423                         GError       **error)
424 {
425   GMountIface *iface;
426 
427   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
428   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
429 
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);
434 
435   iface = G_MOUNT_GET_IFACE (mount);
436   return (* iface->unmount_finish) (mount, result, error);
437 }
438 
439 
440 /**
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.
447  *
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.
451  *
452  * Deprecated: 2.22: Use g_mount_eject_with_operation() instead.
453  **/
454 void
g_mount_eject(GMount * mount,GMountUnmountFlags flags,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)455 g_mount_eject (GMount              *mount,
456                GMountUnmountFlags   flags,
457                GCancellable        *cancellable,
458                GAsyncReadyCallback  callback,
459                gpointer             user_data)
460 {
461   GMountIface *iface;
462 
463   g_return_if_fail (G_IS_MOUNT (mount));
464 
465   iface = G_MOUNT_GET_IFACE (mount);
466 
467   if (iface->eject == NULL)
468     {
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;
477     }
478 
479   (* iface->eject) (mount, flags, cancellable, callback, user_data);
480 }
481 
482 /**
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.
488  *
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.
491  *
492  * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
493  *
494  * Deprecated: 2.22: Use g_mount_eject_with_operation_finish() instead.
495  **/
496 gboolean
g_mount_eject_finish(GMount * mount,GAsyncResult * result,GError ** error)497 g_mount_eject_finish (GMount        *mount,
498                       GAsyncResult  *result,
499                       GError       **error)
500 {
501   GMountIface *iface;
502 
503   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
504   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
505 
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);
510 
511   iface = G_MOUNT_GET_IFACE (mount);
512   return (* iface->eject_finish) (mount, result, error);
513 }
514 
515 /**
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.
524  *
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.
528  *
529  * Since: 2.22
530  **/
531 void
g_mount_unmount_with_operation(GMount * mount,GMountUnmountFlags flags,GMountOperation * mount_operation,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)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)
538 {
539   GMountIface *iface;
540 
541   g_return_if_fail (G_IS_MOUNT (mount));
542 
543   iface = G_MOUNT_GET_IFACE (mount);
544 
545   if (iface->unmount == NULL && iface->unmount_with_operation == NULL)
546     {
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;
555     }
556 
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);
561 }
562 
563 /**
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.
569  *
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.
572  *
573  * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
574  *
575  * Since: 2.22
576  **/
577 gboolean
g_mount_unmount_with_operation_finish(GMount * mount,GAsyncResult * result,GError ** error)578 g_mount_unmount_with_operation_finish (GMount        *mount,
579                                        GAsyncResult  *result,
580                                        GError       **error)
581 {
582   GMountIface *iface;
583 
584   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
585   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
586 
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);
591 
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);
597 }
598 
599 
600 /**
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.
609  *
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.
613  *
614  * Since: 2.22
615  **/
616 void
g_mount_eject_with_operation(GMount * mount,GMountUnmountFlags flags,GMountOperation * mount_operation,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)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)
623 {
624   GMountIface *iface;
625 
626   g_return_if_fail (G_IS_MOUNT (mount));
627 
628   iface = G_MOUNT_GET_IFACE (mount);
629 
630   if (iface->eject == NULL && iface->eject_with_operation == NULL)
631     {
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;
640     }
641 
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);
646 }
647 
648 /**
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.
654  *
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.
657  *
658  * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
659  *
660  * Since: 2.22
661  **/
662 gboolean
g_mount_eject_with_operation_finish(GMount * mount,GAsyncResult * result,GError ** error)663 g_mount_eject_with_operation_finish (GMount        *mount,
664                                      GAsyncResult  *result,
665                                      GError       **error)
666 {
667   GMountIface *iface;
668 
669   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
670   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
671 
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);
676 
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);
682 }
683 
684 /**
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.
693  *
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.
697  *
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.
703  **/
704 void
g_mount_remount(GMount * mount,GMountMountFlags flags,GMountOperation * mount_operation,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)705 g_mount_remount (GMount              *mount,
706                  GMountMountFlags     flags,
707                  GMountOperation     *mount_operation,
708                  GCancellable        *cancellable,
709                  GAsyncReadyCallback  callback,
710                  gpointer             user_data)
711 {
712   GMountIface *iface;
713 
714   g_return_if_fail (G_IS_MOUNT (mount));
715 
716   iface = G_MOUNT_GET_IFACE (mount);
717 
718   if (iface->remount == NULL)
719     {
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;
728     }
729 
730   (* iface->remount) (mount, flags, mount_operation, cancellable, callback, user_data);
731 }
732 
733 /**
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.
739  *
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.
742  *
743  * Returns: %TRUE if the mount was successfully remounted. %FALSE otherwise.
744  **/
745 gboolean
g_mount_remount_finish(GMount * mount,GAsyncResult * result,GError ** error)746 g_mount_remount_finish (GMount        *mount,
747                         GAsyncResult  *result,
748                         GError       **error)
749 {
750   GMountIface *iface;
751 
752   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
753   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
754 
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);
759 
760   iface = G_MOUNT_GET_IFACE (mount);
761   return (* iface->remount_finish) (mount, result, error);
762 }
763 
764 /**
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
772  *
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.
779  *
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.
784  *
785  * Since: 2.18
786  */
787 void
g_mount_guess_content_type(GMount * mount,gboolean force_rescan,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)788 g_mount_guess_content_type (GMount              *mount,
789                             gboolean             force_rescan,
790                             GCancellable        *cancellable,
791                             GAsyncReadyCallback  callback,
792                             gpointer             user_data)
793 {
794   GMountIface *iface;
795 
796   g_return_if_fail (G_IS_MOUNT (mount));
797 
798   iface = G_MOUNT_GET_IFACE (mount);
799 
800   if (iface->guess_content_type == NULL)
801     {
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;
810     }
811 
812   (* iface->guess_content_type) (mount, force_rescan, cancellable, callback, user_data);
813 }
814 
815 /**
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
821  *
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.
827  *
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.
830  *
831  * Since: 2.18
832  **/
833 gchar **
g_mount_guess_content_type_finish(GMount * mount,GAsyncResult * result,GError ** error)834 g_mount_guess_content_type_finish (GMount        *mount,
835                                    GAsyncResult  *result,
836                                    GError       **error)
837 {
838   GMountIface *iface;
839 
840   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
841   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
842 
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);
847 
848   iface = G_MOUNT_GET_IFACE (mount);
849   return (* iface->guess_content_type_finish) (mount, result, error);
850 }
851 
852 /**
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
860  *
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.
867  *
868  * This is a synchronous operation and as such may block doing IO;
869  * see g_mount_guess_content_type() for the asynchronous version.
870  *
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.
873  *
874  * Since: 2.18
875  */
876 char **
g_mount_guess_content_type_sync(GMount * mount,gboolean force_rescan,GCancellable * cancellable,GError ** error)877 g_mount_guess_content_type_sync (GMount              *mount,
878                                  gboolean             force_rescan,
879                                  GCancellable        *cancellable,
880                                  GError             **error)
881 {
882   GMountIface *iface;
883 
884   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
885 
886   iface = G_MOUNT_GET_IFACE (mount);
887 
888   if (iface->guess_content_type_sync == NULL)
889     {
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"));
896 
897       return NULL;
898     }
899 
900   return (* iface->guess_content_type_sync) (mount, force_rescan, cancellable, error);
901 }
902 
903 G_LOCK_DEFINE_STATIC (priv_lock);
904 
905 /* only access this structure when holding priv_lock */
906 typedef struct
907 {
908   gint shadow_ref_count;
909 } GMountPrivate;
910 
911 static void
free_private(GMountPrivate * private)912 free_private (GMountPrivate *private)
913 {
914   G_LOCK (priv_lock);
915   g_free (private);
916   G_UNLOCK (priv_lock);
917 }
918 
919 /* may only be called when holding priv_lock */
920 static GMountPrivate *
get_private(GMount * mount)921 get_private (GMount *mount)
922 {
923   GMountPrivate *private;
924 
925   private = g_object_get_data (G_OBJECT (mount), "g-mount-private");
926   if (G_LIKELY (private != NULL))
927     goto out;
928 
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);
934 
935  out:
936   return private;
937 }
938 
939 /**
940  * g_mount_is_shadowed:
941  * @mount: A #GMount.
942  *
943  * Determines if @mount is shadowed. Applications or libraries should
944  * avoid displaying @mount in the user interface if it is shadowed.
945  *
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.
949  *
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.
962  *
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.
966  *
967  * Returns: %TRUE if @mount is shadowed.
968  *
969  * Since: 2.20
970  **/
971 gboolean
g_mount_is_shadowed(GMount * mount)972 g_mount_is_shadowed (GMount *mount)
973 {
974   GMountPrivate *priv;
975   gboolean ret;
976 
977   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
978 
979   G_LOCK (priv_lock);
980   priv = get_private (mount);
981   ret = (priv->shadow_ref_count > 0);
982   G_UNLOCK (priv_lock);
983 
984   return ret;
985 }
986 
987 /**
988  * g_mount_shadow:
989  * @mount: A #GMount.
990  *
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.
995  *
996  * Since: 2.20
997  **/
998 void
g_mount_shadow(GMount * mount)999 g_mount_shadow (GMount *mount)
1000 {
1001   GMountPrivate *priv;
1002 
1003   g_return_if_fail (G_IS_MOUNT (mount));
1004 
1005   G_LOCK (priv_lock);
1006   priv = get_private (mount);
1007   priv->shadow_ref_count += 1;
1008   G_UNLOCK (priv_lock);
1009 }
1010 
1011 /**
1012  * g_mount_unshadow:
1013  * @mount: A #GMount.
1014  *
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.
1019  *
1020  * Since: 2.20
1021  **/
1022 void
g_mount_unshadow(GMount * mount)1023 g_mount_unshadow (GMount *mount)
1024 {
1025   GMountPrivate *priv;
1026 
1027   g_return_if_fail (G_IS_MOUNT (mount));
1028 
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);
1035 }
1036 
1037 /**
1038  * g_mount_get_sort_key:
1039  * @mount: A #GMount.
1040  *
1041  * Gets the sort key for @mount, if any.
1042  *
1043  * Returns: (nullable): Sorting key for @mount or %NULL if no such key is available.
1044  *
1045  * Since: 2.32
1046  */
1047 const gchar *
g_mount_get_sort_key(GMount * mount)1048 g_mount_get_sort_key (GMount  *mount)
1049 {
1050   const gchar *ret = NULL;
1051   GMountIface *iface;
1052 
1053   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
1054 
1055   iface = G_MOUNT_GET_IFACE (mount);
1056   if (iface->get_sort_key != NULL)
1057     ret = iface->get_sort_key (mount);
1058 
1059   return ret;
1060 }
1061