Add a GMount::pre-unmount signal
[platform/upstream/glib.git] / gio / gmount.c
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 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, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * Author: Alexander Larsson <alexl@redhat.com>
23  *         David Zeuthen <davidz@redhat.com>
24  */
25
26 #include "config.h"
27
28 #include <string.h>
29
30 #include "gmount.h"
31 #include "gmountprivate.h"
32 #include "gasyncresult.h"
33 #include "gsimpleasyncresult.h"
34 #include "gioerror.h"
35 #include "glibintl.h"
36
37 #include "gioalias.h"
38
39 /**
40  * SECTION:gmount
41  * @short_description: Mount management
42  * @include: gio/gio.h
43  * @see also: GVolume, GUnixMount
44  *
45  * The #GMount interface represents user-visible mounts. Note, when 
46  * porting from GnomeVFS, #GMount is the moral equivalent of #GnomeVFSVolume.
47  *
48  * #GMount is a "mounted" filesystem that you can access. Mounted is in
49  * quotes because it's not the same as a unix mount, it might be a gvfs
50  * mount, but you can still access the files on it if you use GIO. Might or
51  * might not be related to a volume object.
52  * 
53  * Unmounting a #GMount instance is an asynchronous operation. For
54  * more information about asynchronous operations, see #GAsyncReady
55  * and #GSimpleAsyncReady. To unmount a #GMount instance, first call
56  * g_mount_unmount() with (at least) the #GMount instance and a
57  * #GAsyncReadyCallback.  The callback will be fired when the
58  * operation has resolved (either with success or failure), and a
59  * #GAsyncReady structure will be passed to the callback.  That
60  * callback should then call g_mount_unmount_finish() with the #GMount
61  * and the #GAsyncReady data to see if the operation was completed
62  * successfully.  If an @error is present when g_mount_unmount_finish() 
63  * is called, then it will be filled with any error information.
64  **/
65
66 static void g_mount_base_init (gpointer g_class);
67 static void g_mount_class_init (gpointer g_class,
68                                 gpointer class_data);
69
70 GType
71 g_mount_get_type (void)
72 {
73   static volatile gsize g_define_type_id__volatile = 0;
74
75   if (g_once_init_enter (&g_define_type_id__volatile))
76     {
77       const GTypeInfo mount_info =
78       {
79         sizeof (GMountIface), /* class_size */
80         g_mount_base_init,   /* base_init */
81         NULL,           /* base_finalize */
82         g_mount_class_init,
83         NULL,           /* class_finalize */
84         NULL,           /* class_data */
85         0,
86         0,              /* n_preallocs */
87         NULL
88       };
89       GType g_define_type_id =
90         g_type_register_static (G_TYPE_INTERFACE, I_("GMount"),
91                                 &mount_info, 0);
92
93       g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
94
95       g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
96     }
97
98   return g_define_type_id__volatile;
99 }
100
101 static void
102 g_mount_class_init (gpointer g_class,
103                     gpointer class_data)
104 {
105 }
106
107 static void
108 g_mount_base_init (gpointer g_class)
109 {
110   static gboolean initialized = FALSE;
111
112   if (! initialized)
113     {
114      /**
115       * GMount::changed:
116       * @mount: the object on which the signal is emitted
117       * 
118       * Emitted when the mount has been changed.
119       **/
120       g_signal_new (I_("changed"),
121                     G_TYPE_MOUNT,
122                     G_SIGNAL_RUN_LAST,
123                     G_STRUCT_OFFSET (GMountIface, changed),
124                     NULL, NULL,
125                     g_cclosure_marshal_VOID__VOID,
126                     G_TYPE_NONE, 0);
127
128      /**
129       * GMount::unmounted:
130       * @mount: the object on which the signal is emitted
131       * 
132       * This signal is emitted when the #GMount have been
133       * unmounted. If the recipient is holding references to the
134       * object they should release them so the object can be
135       * finalized.
136       **/
137       g_signal_new (I_("unmounted"),
138                     G_TYPE_MOUNT,
139                     G_SIGNAL_RUN_LAST,
140                     G_STRUCT_OFFSET (GMountIface, unmounted),
141                     NULL, NULL,
142                     g_cclosure_marshal_VOID__VOID,
143                     G_TYPE_NONE, 0);
144      /**
145       * GMount::pre-unmount:
146       * @mount: the object on which the signal is emitted
147       *
148       * This signal is emitted when the #GMount is about to be
149       * unmounted.
150       *
151       * Since: 2.22.
152       **/
153       g_signal_new (I_("pre-unmount"),
154                     G_TYPE_MOUNT,
155                     G_SIGNAL_RUN_LAST,
156                     G_STRUCT_OFFSET (GMountIface, pre_unmount),
157                     NULL, NULL,
158                     g_cclosure_marshal_VOID__VOID,
159                     G_TYPE_NONE, 0);
160
161       initialized = TRUE;
162     }
163 }
164
165 /**
166  * g_mount_get_root:
167  * @mount: a #GMount.
168  * 
169  * Gets the root directory on @mount.
170  * 
171  * Returns: a #GFile. 
172  *      The returned object should be unreffed with 
173  *      g_object_unref() when no longer needed.
174  **/
175 GFile *
176 g_mount_get_root (GMount *mount)
177 {
178   GMountIface *iface;
179
180   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
181
182   iface = G_MOUNT_GET_IFACE (mount);
183
184   return (* iface->get_root) (mount);
185 }
186
187 /**
188  * g_mount_get_name:
189  * @mount: a #GMount.
190  * 
191  * Gets the name of @mount.
192  * 
193  * Returns: the name for the given @mount. 
194  *     The returned string should be freed with g_free()
195  *     when no longer needed.
196  **/
197 char *
198 g_mount_get_name (GMount *mount)
199 {
200   GMountIface *iface;
201
202   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
203
204   iface = G_MOUNT_GET_IFACE (mount);
205
206   return (* iface->get_name) (mount);
207 }
208
209 /**
210  * g_mount_get_icon:
211  * @mount: a #GMount.
212  * 
213  * Gets the icon for @mount.
214  * 
215  * Returns: a #GIcon.
216  *      The returned object should be unreffed with 
217  *      g_object_unref() when no longer needed.
218  **/
219 GIcon *
220 g_mount_get_icon (GMount *mount)
221 {
222   GMountIface *iface;
223
224   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
225
226   iface = G_MOUNT_GET_IFACE (mount);
227
228   return (* iface->get_icon) (mount);
229 }
230
231 /**
232  * g_mount_get_uuid:
233  * @mount: a #GMount.
234  * 
235  * Gets the UUID for the @mount. The reference is typically based on
236  * the file system UUID for the mount in question and should be
237  * considered an opaque string. Returns %NULL if there is no UUID
238  * available.
239  * 
240  * Returns: the UUID for @mount or %NULL if no UUID can be computed.
241  *     The returned string should be freed with g_free()
242  *     when no longer needed.
243  **/
244 char *
245 g_mount_get_uuid (GMount *mount)
246 {
247   GMountIface *iface;
248
249   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
250
251   iface = G_MOUNT_GET_IFACE (mount);
252
253   return (* iface->get_uuid) (mount);
254 }
255
256 /**
257  * g_mount_get_volume:
258  * @mount: a #GMount.
259  * 
260  * Gets the volume for the @mount.
261  * 
262  * Returns: a #GVolume or %NULL if @mount is not associated with a volume.
263  *      The returned object should be unreffed with 
264  *      g_object_unref() when no longer needed.
265  **/
266 GVolume *
267 g_mount_get_volume (GMount *mount)
268 {
269   GMountIface *iface;
270
271   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
272
273   iface = G_MOUNT_GET_IFACE (mount);
274
275   return (* iface->get_volume) (mount);
276 }
277
278 /**
279  * g_mount_get_drive:
280  * @mount: a #GMount.
281  * 
282  * Gets the drive for the @mount.
283  *
284  * This is a convenience method for getting the #GVolume and then
285  * using that object to get the #GDrive.
286  * 
287  * Returns: a #GDrive or %NULL if @mount is not associated with a volume or a drive.
288  *      The returned object should be unreffed with 
289  *      g_object_unref() when no longer needed.
290  **/
291 GDrive *
292 g_mount_get_drive (GMount *mount)
293 {
294   GMountIface *iface;
295
296   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
297
298   iface = G_MOUNT_GET_IFACE (mount);
299
300   return (* iface->get_drive) (mount);
301 }
302
303 /**
304  * g_mount_can_unmount: 
305  * @mount: a #GMount.
306  * 
307  * Checks if @mount can be mounted.
308  * 
309  * Returns: %TRUE if the @mount can be unmounted.
310  **/
311 gboolean
312 g_mount_can_unmount (GMount *mount)
313 {
314   GMountIface *iface;
315
316   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
317
318   iface = G_MOUNT_GET_IFACE (mount);
319
320   return (* iface->can_unmount) (mount);
321 }
322
323 /**
324  * g_mount_can_eject: 
325  * @mount: a #GMount.
326  * 
327  * Checks if @mount can be eject.
328  * 
329  * Returns: %TRUE if the @mount can be ejected.
330  **/
331 gboolean
332 g_mount_can_eject (GMount *mount)
333 {
334   GMountIface *iface;
335
336   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
337
338   iface = G_MOUNT_GET_IFACE (mount);
339
340   return (* iface->can_eject) (mount);
341 }
342
343 /**
344  * g_mount_unmount:
345  * @mount: a #GMount.
346  * @flags: flags affecting the operation
347  * @cancellable: optional #GCancellable object, %NULL to ignore.
348  * @callback: a #GAsyncReadyCallback, or %NULL.
349  * @user_data: user data passed to @callback.
350  * 
351  * Unmounts a mount. This is an asynchronous operation, and is 
352  * finished by calling g_mount_unmount_finish() with the @mount 
353  * and #GAsyncResult data returned in the @callback.
354  **/
355 void
356 g_mount_unmount (GMount              *mount,
357                  GMountUnmountFlags   flags,
358                  GCancellable        *cancellable,
359                  GAsyncReadyCallback  callback,
360                  gpointer             user_data)
361 {
362   GMountIface *iface;
363
364   g_return_if_fail (G_IS_MOUNT (mount));
365   
366   iface = G_MOUNT_GET_IFACE (mount);
367
368   if (iface->unmount == NULL)
369     {
370       g_simple_async_report_error_in_idle (G_OBJECT (mount),
371                                            callback, user_data,
372                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
373                                            /* Translators: This is an error
374                                             * message for mount objects that
375                                             * don't implement unmount. */
376                                            _("mount doesn't implement unmount"));
377
378       return;
379     }
380   
381   (* iface->unmount) (mount, flags, cancellable, callback, user_data);
382 }
383
384 /**
385  * g_mount_unmount_finish:
386  * @mount: a #GMount.
387  * @result: a #GAsyncResult.
388  * @error: a #GError location to store the error occuring, or %NULL to 
389  *     ignore.
390  * 
391  * Finishes unmounting a mount. If any errors occurred during the operation, 
392  * @error will be set to contain the errors and %FALSE will be returned.
393  * 
394  * Returns: %TRUE if the mount was successfully unmounted. %FALSE otherwise.
395  **/
396 gboolean
397 g_mount_unmount_finish (GMount        *mount,
398                         GAsyncResult  *result,
399                         GError       **error)
400 {
401   GMountIface *iface;
402
403   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
404   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
405
406   if (G_IS_SIMPLE_ASYNC_RESULT (result))
407     {
408       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
409       if (g_simple_async_result_propagate_error (simple, error))
410         return FALSE;
411     }
412   
413   iface = G_MOUNT_GET_IFACE (mount);
414   return (* iface->unmount_finish) (mount, result, error);
415 }
416
417
418 /**
419  * g_mount_eject:
420  * @mount: a #GMount.
421  * @flags: flags affecting the unmount if required for eject
422  * @cancellable: optional #GCancellable object, %NULL to ignore.
423  * @callback: a #GAsyncReadyCallback, or %NULL.
424  * @user_data: user data passed to @callback.
425  * 
426  * Ejects a mount. This is an asynchronous operation, and is 
427  * finished by calling g_mount_eject_finish() with the @mount 
428  * and #GAsyncResult data returned in the @callback.
429  **/
430 void
431 g_mount_eject (GMount              *mount,
432                GMountUnmountFlags   flags,
433                GCancellable        *cancellable,
434                GAsyncReadyCallback  callback,
435                gpointer             user_data)
436 {
437   GMountIface *iface;
438
439   g_return_if_fail (G_IS_MOUNT (mount));
440   
441   iface = G_MOUNT_GET_IFACE (mount);
442
443   if (iface->eject == NULL)
444     {
445       g_simple_async_report_error_in_idle (G_OBJECT (mount),
446                                            callback, user_data,
447                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
448                                            /* Translators: This is an error
449                                             * message for mount objects that
450                                             * don't implement eject. */
451                                            _("mount doesn't implement eject"));
452       
453       return;
454     }
455   
456   (* iface->eject) (mount, flags, cancellable, callback, user_data);
457 }
458
459 /**
460  * g_mount_eject_finish:
461  * @mount: a #GMount.
462  * @result: a #GAsyncResult.
463  * @error: a #GError location to store the error occuring, or %NULL to 
464  *     ignore.
465  * 
466  * Finishes ejecting a mount. If any errors occurred during the operation, 
467  * @error will be set to contain the errors and %FALSE will be returned.
468  * 
469  * Returns: %TRUE if the mount was successfully ejected. %FALSE otherwise.
470  **/
471 gboolean
472 g_mount_eject_finish (GMount        *mount,
473                       GAsyncResult  *result,
474                       GError       **error)
475 {
476   GMountIface *iface;
477
478   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
479   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
480
481   if (G_IS_SIMPLE_ASYNC_RESULT (result))
482     {
483       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
484       if (g_simple_async_result_propagate_error (simple, error))
485         return FALSE;
486     }
487   
488   iface = G_MOUNT_GET_IFACE (mount);
489   return (* iface->eject_finish) (mount, result, error);
490 }
491
492 /**
493  * g_mount_remount:
494  * @mount: a #GMount.
495  * @flags: flags affecting the operation
496  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
497  * @cancellable: optional #GCancellable object, %NULL to ignore.
498  * @callback: a #GAsyncReadyCallback, or %NULL.
499  * @user_data: user data passed to @callback.
500  * 
501  * Remounts a mount. This is an asynchronous operation, and is 
502  * finished by calling g_mount_remount_finish() with the @mount 
503  * and #GAsyncResults data returned in the @callback.
504  *
505  * Remounting is useful when some setting affecting the operation
506  * of the volume has been changed, as these may need a remount to
507  * take affect. While this is semantically equivalent with unmounting
508  * and then remounting not all backends might need to actually be
509  * unmounted.
510  **/
511 void
512 g_mount_remount (GMount              *mount,
513                  GMountMountFlags     flags,
514                  GMountOperation     *mount_operation,
515                  GCancellable        *cancellable,
516                  GAsyncReadyCallback  callback,
517                  gpointer             user_data)
518 {
519   GMountIface *iface;
520
521   g_return_if_fail (G_IS_MOUNT (mount));
522   
523   iface = G_MOUNT_GET_IFACE (mount);
524
525   if (iface->remount == NULL)
526     { 
527       g_simple_async_report_error_in_idle (G_OBJECT (mount),
528                                            callback, user_data,
529                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
530                                            /* Translators: This is an error
531                                             * message for mount objects that
532                                             * don't implement remount. */
533                                            _("mount doesn't implement remount"));
534       
535       return;
536     }
537   
538   (* iface->remount) (mount, flags, mount_operation, cancellable, callback, user_data);
539 }
540
541 /**
542  * g_mount_remount_finish:
543  * @mount: a #GMount.
544  * @result: a #GAsyncResult.
545  * @error: a #GError location to store the error occuring, or %NULL to 
546  *     ignore.
547  * 
548  * Finishes remounting a mount. If any errors occurred during the operation, 
549  * @error will be set to contain the errors and %FALSE will be returned.
550  * 
551  * Returns: %TRUE if the mount was successfully remounted. %FALSE otherwise.
552  **/
553 gboolean
554 g_mount_remount_finish (GMount        *mount,
555                         GAsyncResult  *result,
556                         GError       **error)
557 {
558   GMountIface *iface;
559
560   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
561   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
562
563   if (G_IS_SIMPLE_ASYNC_RESULT (result))
564     {
565       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
566       if (g_simple_async_result_propagate_error (simple, error))
567         return FALSE;
568     }
569   
570   iface = G_MOUNT_GET_IFACE (mount);
571   return (* iface->remount_finish) (mount, result, error);
572 }
573
574 /**
575  * g_mount_guess_content_type:
576  * @mount: a #GMount
577  * @force_rescan: Whether to force a rescan of the content. 
578  *     Otherwise a cached result will be used if available
579  * @cancellable: optional #GCancellable object, %NULL to ignore
580  * @callback: a #GAsyncReadyCallback
581  * @user_data: user data passed to @callback
582  * 
583  * Tries to guess the type of content stored on @mount. Returns one or
584  * more textual identifiers of well-known content types (typically
585  * prefixed with "x-content/"), e.g. x-content/image-dcf for camera 
586  * memory cards. See the <ulink url="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec">shared-mime-info</ulink>
587  * specification for more on x-content types.
588  *
589  * This is an asynchronous operation (see
590  * g_mount_guess_content_type_sync() for the synchronous version), and
591  * is finished by calling g_mount_guess_content_type_finish() with the
592  * @mount and #GAsyncResult data returned in the @callback.
593  *
594  * Since: 2.18
595  */
596 void
597 g_mount_guess_content_type (GMount              *mount,
598                             gboolean             force_rescan,
599                             GCancellable        *cancellable,
600                             GAsyncReadyCallback  callback,
601                             gpointer             user_data)
602 {
603   GMountIface *iface;
604
605   g_return_if_fail (G_IS_MOUNT (mount));
606
607   iface = G_MOUNT_GET_IFACE (mount);
608
609   if (iface->guess_content_type == NULL)
610     {
611       g_simple_async_report_error_in_idle (G_OBJECT (mount),
612                                            callback, user_data,
613                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
614                                            /* Translators: This is an error
615                                             * message for mount objects that
616                                             * don't implement content type guessing. */
617                                            _("mount doesn't implement content type guessing"));
618
619       return;
620     }
621   
622   (* iface->guess_content_type) (mount, force_rescan, cancellable, callback, user_data);
623 }
624
625 /**
626  * g_mount_guess_content_type_finish:
627  * @mount: a #GMount
628  * @result: a #GAsyncResult
629  * @error: a #GError location to store the error occuring, or %NULL to 
630  *     ignore
631  * 
632  * Finishes guessing content types of @mount. If any errors occured
633  * during the operation, @error will be set to contain the errors and
634  * %FALSE will be returned. In particular, you may get an 
635  * %G_IO_ERROR_NOT_SUPPORTED if the mount does not support content 
636  * guessing.
637  * 
638  * Returns: a %NULL-terminated array of content types or %NULL on error. 
639  *     Caller should free this array with g_strfreev() when done with it.
640  *
641  * Since: 2.18
642  **/
643 gchar **
644 g_mount_guess_content_type_finish (GMount        *mount,
645                                    GAsyncResult  *result,
646                                    GError       **error)
647 {
648   GMountIface *iface;
649
650   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
651   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
652
653   if (G_IS_SIMPLE_ASYNC_RESULT (result))
654     {
655       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
656       if (g_simple_async_result_propagate_error (simple, error))
657         return FALSE;
658     }
659   
660   iface = G_MOUNT_GET_IFACE (mount);
661   return (* iface->guess_content_type_finish) (mount, result, error);
662 }
663
664 /**
665  * g_mount_guess_content_type_sync:
666  * @mount: a #GMount
667  * @force_rescan: Whether to force a rescan of the content.
668  *     Otherwise a cached result will be used if available
669  * @cancellable: optional #GCancellable object, %NULL to ignore
670  * @error: a #GError location to store the error occuring, or %NULL to
671  *     ignore
672  *
673  * Tries to guess the type of content stored on @mount. Returns one or
674  * more textual identifiers of well-known content types (typically
675  * prefixed with "x-content/"), e.g. x-content/image-dcf for camera 
676  * memory cards. See the <ulink url="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec">shared-mime-info</ulink>
677  * specification for more on x-content types.
678  *
679  * This is an synchronous operation and as such may block doing IO;
680  * see g_mount_guess_content_type() for the asynchronous version.
681  *
682  * Returns: a %NULL-terminated array of content types or %NULL on error.
683  *     Caller should free this array with g_strfreev() when done with it.
684  *
685  * Since: 2.18
686  */
687 char **
688 g_mount_guess_content_type_sync (GMount              *mount,
689                                  gboolean             force_rescan,
690                                  GCancellable        *cancellable,
691                                  GError             **error)
692 {
693   GMountIface *iface;
694
695   g_return_val_if_fail (G_IS_MOUNT (mount), NULL);
696
697   iface = G_MOUNT_GET_IFACE (mount);
698
699   if (iface->guess_content_type_sync == NULL)
700     {
701       g_set_error_literal (error,
702                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
703                            /* Translators: This is an error
704                             * message for mount objects that
705                             * don't implement content type guessing. */
706                            _("mount doesn't implement synchronous content type guessing"));
707
708       return NULL;
709     }
710
711   return (* iface->guess_content_type_sync) (mount, force_rescan, cancellable, error);
712 }
713
714 G_LOCK_DEFINE_STATIC (priv_lock);
715
716 /* only access this structure when holding priv_lock */
717 typedef struct
718 {
719   gint shadow_ref_count;
720 } GMountPrivate;
721
722 static void
723 free_private (GMountPrivate *private)
724 {
725   G_LOCK (priv_lock);
726   g_free (private);
727   G_UNLOCK (priv_lock);
728 }
729
730 /* may only be called when holding priv_lock */
731 static GMountPrivate *
732 get_private (GMount *mount)
733 {
734   GMountPrivate *private;
735
736   private = g_object_get_data (G_OBJECT (mount), "g-mount-private");
737   if (G_LIKELY (private != NULL))
738     goto out;
739
740   private = g_new0 (GMountPrivate, 1);
741   g_object_set_data_full (G_OBJECT (mount),
742                           "g-mount-private",
743                           private,
744                           (GDestroyNotify) free_private);
745
746  out:
747   return private;
748 }
749
750 /**
751  * g_mount_is_shadowed:
752  * @mount: A #GMount.
753  *
754  * Determines if @mount is shadowed. Applications or libraries should
755  * avoid displaying @mount in the user interface if it is shadowed.
756  *
757  * A mount is said to be shadowed if there exists one or more user
758  * visible objects (currently #GMount objects) with a root that is
759  * inside the root of @mount.
760  *
761  * One application of shadow mounts is when exposing a single file
762  * system that is used to address several logical volumes. In this
763  * situation, a #GVolumeMonitor implementation would create two
764  * #GVolume objects (for example, one for the camera functionality of
765  * the device and one for a SD card reader on the device) with
766  * activation URIs <literal>gphoto2://[usb:001,002]/store1/</literal>
767  * and <literal>gphoto2://[usb:001,002]/store2/</literal>. When the
768  * underlying mount (with root
769  * <literal>gphoto2://[usb:001,002]/</literal>) is mounted, said
770  * #GVolumeMonitor implementation would create two #GMount objects
771  * (each with their root matching the corresponding volume activation
772  * root) that would shadow the original mount.
773  *
774  * The proxy monitor in GVfs 2.26 and later, automatically creates and
775  * manage shadow mounts (and shadows the underlying mount) if the
776  * activation root on a #GVolume is set.
777  *
778  * Returns: %TRUE if @mount is shadowed.
779  *
780  * Since: 2.20
781  **/
782 gboolean
783 g_mount_is_shadowed (GMount *mount)
784 {
785   GMountPrivate *priv;
786   gboolean ret;
787
788   g_return_val_if_fail (G_IS_MOUNT (mount), FALSE);
789
790   G_LOCK (priv_lock);
791   priv = get_private (mount);
792   ret = (priv->shadow_ref_count > 0);
793   G_UNLOCK (priv_lock);
794
795   return ret;
796 }
797
798 /**
799  * g_mount_shadow:
800  * @mount: A #GMount.
801  *
802  * Increments the shadow count on @mount. Usually used by
803  * #GVolumeMonitor implementations when creating a shadow mount for
804  * @mount, see g_mount_is_shadowed() for more information. The caller
805  * will need to emit the #GMount::changed signal on @mount manually.
806  *
807  * Since: 2.20
808  **/
809 void
810 g_mount_shadow (GMount *mount)
811 {
812   GMountPrivate *priv;
813
814   g_return_if_fail (G_IS_MOUNT (mount));
815
816   G_LOCK (priv_lock);
817   priv = get_private (mount);
818   priv->shadow_ref_count += 1;
819   G_UNLOCK (priv_lock);
820 }
821
822 /**
823  * g_mount_unshadow:
824  * @mount: A #GMount.
825  *
826  * Decrements the shadow count on @mount. Usually used by
827  * #GVolumeMonitor implementations when destroying a shadow mount for
828  * @mount, see g_mount_is_shadowed() for more information. The caller
829  * will need to emit the #GMount::changed signal on @mount manually.
830  *
831  * Since: 2.20
832  **/
833 void
834 g_mount_unshadow (GMount *mount)
835 {
836   GMountPrivate *priv;
837
838   g_return_if_fail (G_IS_MOUNT (mount));
839
840   G_LOCK (priv_lock);
841   priv = get_private (mount);
842   priv->shadow_ref_count -= 1;
843   if (priv->shadow_ref_count < 0)
844     g_warning ("Shadow ref count on GMount is negative");
845   G_UNLOCK (priv_lock);
846 }
847
848 #define __G_MOUNT_C__
849 #include "gioaliasdef.c"