nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 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-2007 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, 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 <glib.h>
29 #include "gunionvolumemonitor.h"
30 #include "gmountprivate.h"
31 #include "giomodule-priv.h"
32 #ifdef G_OS_UNIX
33 #include "gunixvolumemonitor.h"
34 #endif
35 #include "gnativevolumemonitor.h"
36  
37 #include "glibintl.h"
38  
39  
40 struct _GUnionVolumeMonitor {
41 GVolumeMonitor parent;
42  
43 GList *monitors;
44 };
45  
46 static void g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
47 GVolumeMonitor *child_monitor);
48  
49  
50 #define g_union_volume_monitor_get_type _g_union_volume_monitor_get_type
51 G_DEFINE_TYPE (GUnionVolumeMonitor, g_union_volume_monitor, G_TYPE_VOLUME_MONITOR);
52  
53 static GRecMutex the_volume_monitor_mutex;
54  
55 static GUnionVolumeMonitor *the_volume_monitor = NULL;
56  
57 static void
58 g_union_volume_monitor_finalize (GObject *object)
59 {
60 GUnionVolumeMonitor *monitor;
61 GVolumeMonitor *child_monitor;
62  
63 monitor = G_UNION_VOLUME_MONITOR (object);
64  
65 while (monitor->monitors != NULL)
66 {
67 child_monitor = monitor->monitors->data;
68 g_union_volume_monitor_remove_monitor (monitor,
69 child_monitor);
70 g_object_unref (child_monitor);
71 }
72  
73 G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->finalize (object);
74 }
75  
76 static void
77 g_union_volume_monitor_dispose (GObject *object)
78 {
79 GUnionVolumeMonitor *monitor;
80 GVolumeMonitor *child_monitor;
81 GList *l;
82  
83 monitor = G_UNION_VOLUME_MONITOR (object);
84  
85 g_rec_mutex_lock (&the_volume_monitor_mutex);
86 the_volume_monitor = NULL;
87  
88 for (l = monitor->monitors; l != NULL; l = l->next)
89 {
90 child_monitor = l->data;
91 g_object_run_dispose (G_OBJECT (child_monitor));
92 }
93  
94 g_rec_mutex_unlock (&the_volume_monitor_mutex);
95  
96 G_OBJECT_CLASS (g_union_volume_monitor_parent_class)->dispose (object);
97 }
98  
99 static GList *
100 get_mounts (GVolumeMonitor *volume_monitor)
101 {
102 GUnionVolumeMonitor *monitor;
103 GVolumeMonitor *child_monitor;
104 GList *res;
105 GList *l;
106  
107 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
108  
109 res = NULL;
110  
111 g_rec_mutex_lock (&the_volume_monitor_mutex);
112  
113 for (l = monitor->monitors; l != NULL; l = l->next)
114 {
115 child_monitor = l->data;
116  
117 res = g_list_concat (res, g_volume_monitor_get_mounts (child_monitor));
118 }
119  
120 g_rec_mutex_unlock (&the_volume_monitor_mutex);
121  
122 return res;
123 }
124  
125 static GList *
126 get_volumes (GVolumeMonitor *volume_monitor)
127 {
128 GUnionVolumeMonitor *monitor;
129 GVolumeMonitor *child_monitor;
130 GList *res;
131 GList *l;
132  
133 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
134  
135 res = NULL;
136  
137 g_rec_mutex_lock (&the_volume_monitor_mutex);
138  
139 for (l = monitor->monitors; l != NULL; l = l->next)
140 {
141 child_monitor = l->data;
142  
143 res = g_list_concat (res, g_volume_monitor_get_volumes (child_monitor));
144 }
145  
146 g_rec_mutex_unlock (&the_volume_monitor_mutex);
147  
148 return res;
149 }
150  
151 static GList *
152 get_connected_drives (GVolumeMonitor *volume_monitor)
153 {
154 GUnionVolumeMonitor *monitor;
155 GVolumeMonitor *child_monitor;
156 GList *res;
157 GList *l;
158  
159 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
160  
161 res = NULL;
162  
163 g_rec_mutex_lock (&the_volume_monitor_mutex);
164  
165 for (l = monitor->monitors; l != NULL; l = l->next)
166 {
167 child_monitor = l->data;
168  
169 res = g_list_concat (res, g_volume_monitor_get_connected_drives (child_monitor));
170 }
171  
172 g_rec_mutex_unlock (&the_volume_monitor_mutex);
173  
174 return res;
175 }
176  
177 static GVolume *
178 get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
179 {
180 GUnionVolumeMonitor *monitor;
181 GVolumeMonitor *child_monitor;
182 GVolume *volume;
183 GList *l;
184  
185 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
186  
187 volume = NULL;
188  
189 g_rec_mutex_lock (&the_volume_monitor_mutex);
190  
191 for (l = monitor->monitors; l != NULL; l = l->next)
192 {
193 child_monitor = l->data;
194  
195 volume = g_volume_monitor_get_volume_for_uuid (child_monitor, uuid);
196 if (volume != NULL)
197 break;
198  
199 }
200  
201 g_rec_mutex_unlock (&the_volume_monitor_mutex);
202  
203 return volume;
204 }
205  
206 static GMount *
207 get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
208 {
209 GUnionVolumeMonitor *monitor;
210 GVolumeMonitor *child_monitor;
211 GMount *mount;
212 GList *l;
213  
214 monitor = G_UNION_VOLUME_MONITOR (volume_monitor);
215  
216 mount = NULL;
217  
218 g_rec_mutex_lock (&the_volume_monitor_mutex);
219  
220 for (l = monitor->monitors; l != NULL; l = l->next)
221 {
222 child_monitor = l->data;
223  
224 mount = g_volume_monitor_get_mount_for_uuid (child_monitor, uuid);
225 if (mount != NULL)
226 break;
227  
228 }
229  
230 g_rec_mutex_unlock (&the_volume_monitor_mutex);
231  
232 return mount;
233 }
234  
235 static void
236 g_union_volume_monitor_class_init (GUnionVolumeMonitorClass *klass)
237 {
238 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
239 GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
240  
241 gobject_class->finalize = g_union_volume_monitor_finalize;
242 gobject_class->dispose = g_union_volume_monitor_dispose;
243  
244 monitor_class->get_connected_drives = get_connected_drives;
245 monitor_class->get_volumes = get_volumes;
246 monitor_class->get_mounts = get_mounts;
247 monitor_class->get_volume_for_uuid = get_volume_for_uuid;
248 monitor_class->get_mount_for_uuid = get_mount_for_uuid;
249 }
250  
251 static void
252 child_volume_added (GVolumeMonitor *child_monitor,
253 GVolume *child_volume,
254 GUnionVolumeMonitor *union_monitor)
255 {
256 g_signal_emit_by_name (union_monitor,
257 "volume-added",
258 child_volume);
259 }
260  
261 static void
262 child_volume_removed (GVolumeMonitor *child_monitor,
263 GVolume *child_volume,
264 GUnionVolumeMonitor *union_monitor)
265 {
266 g_signal_emit_by_name (union_monitor,
267 "volume-removed",
268 child_volume);
269 }
270  
271 static void
272 child_volume_changed (GVolumeMonitor *child_monitor,
273 GVolume *child_volume,
274 GUnionVolumeMonitor *union_monitor)
275 {
276 g_signal_emit_by_name (union_monitor,
277 "volume-changed",
278 child_volume);
279 }
280  
281 static void
282 child_mount_added (GVolumeMonitor *child_monitor,
283 GMount *child_mount,
284 GUnionVolumeMonitor *union_monitor)
285 {
286 g_signal_emit_by_name (union_monitor,
287 "mount-added",
288 child_mount);
289 }
290  
291 static void
292 child_mount_removed (GVolumeMonitor *child_monitor,
293 GMount *child_mount,
294 GUnionVolumeMonitor *union_monitor)
295 {
296 g_signal_emit_by_name (union_monitor,
297 "mount-removed",
298 child_mount);
299 }
300  
301 static void
302 child_mount_pre_unmount (GVolumeMonitor *child_monitor,
303 GMount *child_mount,
304 GUnionVolumeMonitor *union_monitor)
305 {
306 g_signal_emit_by_name (union_monitor,
307 "mount-pre-unmount",
308 child_mount);
309 }
310  
311  
312 static void
313 child_mount_changed (GVolumeMonitor *child_monitor,
314 GMount *child_mount,
315 GUnionVolumeMonitor *union_monitor)
316 {
317 g_signal_emit_by_name (union_monitor,
318 "mount-changed",
319 child_mount);
320 }
321  
322 static void
323 child_drive_connected (GVolumeMonitor *child_monitor,
324 GDrive *child_drive,
325 GUnionVolumeMonitor *union_monitor)
326 {
327 g_signal_emit_by_name (union_monitor,
328 "drive-connected",
329 child_drive);
330 }
331  
332 static void
333 child_drive_disconnected (GVolumeMonitor *child_monitor,
334 GDrive *child_drive,
335 GUnionVolumeMonitor *union_monitor)
336 {
337 g_signal_emit_by_name (union_monitor,
338 "drive-disconnected",
339 child_drive);
340 }
341  
342 static void
343 child_drive_changed (GVolumeMonitor *child_monitor,
344 GDrive *child_drive,
345 GUnionVolumeMonitor *union_monitor)
346 {
347 g_signal_emit_by_name (union_monitor,
348 "drive-changed",
349 child_drive);
350 }
351  
352 static void
353 child_drive_eject_button (GVolumeMonitor *child_monitor,
354 GDrive *child_drive,
355 GUnionVolumeMonitor *union_monitor)
356 {
357 g_signal_emit_by_name (union_monitor,
358 "drive-eject-button",
359 child_drive);
360 }
361  
362 static void
363 child_drive_stop_button (GVolumeMonitor *child_monitor,
364 GDrive *child_drive,
365 GUnionVolumeMonitor *union_monitor)
366 {
367 g_signal_emit_by_name (union_monitor,
368 "drive-stop-button",
369 child_drive);
370 }
371  
372 static void
373 g_union_volume_monitor_add_monitor (GUnionVolumeMonitor *union_monitor,
374 GVolumeMonitor *volume_monitor)
375 {
376 if (g_list_find (union_monitor->monitors, volume_monitor))
377 return;
378  
379 union_monitor->monitors =
380 g_list_prepend (union_monitor->monitors,
381 g_object_ref (volume_monitor));
382  
383 g_signal_connect (volume_monitor, "volume-added", (GCallback)child_volume_added, union_monitor);
384 g_signal_connect (volume_monitor, "volume-removed", (GCallback)child_volume_removed, union_monitor);
385 g_signal_connect (volume_monitor, "volume-changed", (GCallback)child_volume_changed, union_monitor);
386 g_signal_connect (volume_monitor, "mount-added", (GCallback)child_mount_added, union_monitor);
387 g_signal_connect (volume_monitor, "mount-removed", (GCallback)child_mount_removed, union_monitor);
388 g_signal_connect (volume_monitor, "mount-pre-unmount", (GCallback)child_mount_pre_unmount, union_monitor);
389 g_signal_connect (volume_monitor, "mount-changed", (GCallback)child_mount_changed, union_monitor);
390 g_signal_connect (volume_monitor, "drive-connected", (GCallback)child_drive_connected, union_monitor);
391 g_signal_connect (volume_monitor, "drive-disconnected", (GCallback)child_drive_disconnected, union_monitor);
392 g_signal_connect (volume_monitor, "drive-changed", (GCallback)child_drive_changed, union_monitor);
393 g_signal_connect (volume_monitor, "drive-eject-button", (GCallback)child_drive_eject_button, union_monitor);
394 g_signal_connect (volume_monitor, "drive-stop-button", (GCallback)child_drive_stop_button, union_monitor);
395 }
396  
397 static void
398 g_union_volume_monitor_remove_monitor (GUnionVolumeMonitor *union_monitor,
399 GVolumeMonitor *child_monitor)
400 {
401 GList *l;
402  
403 l = g_list_find (union_monitor->monitors, child_monitor);
404 if (l == NULL)
405 return;
406  
407 union_monitor->monitors = g_list_delete_link (union_monitor->monitors, l);
408  
409 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_added, union_monitor);
410 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_removed, union_monitor);
411 g_signal_handlers_disconnect_by_func (child_monitor, child_volume_changed, union_monitor);
412 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_added, union_monitor);
413 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_removed, union_monitor);
414 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_pre_unmount, union_monitor);
415 g_signal_handlers_disconnect_by_func (child_monitor, child_mount_changed, union_monitor);
416 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_connected, union_monitor);
417 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_disconnected, union_monitor);
418 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_changed, union_monitor);
419 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_eject_button, union_monitor);
420 g_signal_handlers_disconnect_by_func (child_monitor, child_drive_stop_button, union_monitor);
421 }
422  
423 static GType
424 get_default_native_class (gpointer data)
425 {
426 GNativeVolumeMonitorClass *klass, *native_class, **native_class_out;
427 const char *use_this;
428 GIOExtensionPoint *ep;
429 GIOExtension *extension;
430 GList *l;
431  
432 native_class_out = data;
433  
434 use_this = g_getenv ("GIO_USE_VOLUME_MONITOR");
435  
436 /* Ensure vfs in modules loaded */
437 _g_io_modules_ensure_loaded ();
438  
439 ep = g_io_extension_point_lookup (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
440  
441 native_class = NULL;
442 if (use_this)
443 {
444 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
445 if (extension)
446 {
447 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_io_extension_ref_class (extension));
448 if (G_VOLUME_MONITOR_CLASS (klass)->is_supported())
449 native_class = klass;
450 else
451 g_type_class_unref (klass);
452 }
453 }
454  
455 if (native_class == NULL)
456 {
457 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
458 {
459 extension = l->data;
460 klass = G_NATIVE_VOLUME_MONITOR_CLASS (g_io_extension_ref_class (extension));
461 if (G_VOLUME_MONITOR_CLASS (klass)->is_supported())
462 {
463 native_class = klass;
464 break;
465 }
466 else
467 g_type_class_unref (klass);
468 }
469 }
470  
471 if (native_class)
472 {
473 *native_class_out = native_class;
474 return G_TYPE_FROM_CLASS (native_class);
475 }
476 else
477 return G_TYPE_INVALID;
478 }
479  
480 /* We return the class, with a ref taken.
481 * This way we avoid unloading the class/module
482 * between selecting the type and creating the
483 * instance on the first call.
484 */
485 static GNativeVolumeMonitorClass *
486 get_native_class (void)
487 {
488 static GOnce once_init = G_ONCE_INIT;
489 GTypeClass *type_class;
490  
491 type_class = NULL;
492 g_once (&once_init, (GThreadFunc)get_default_native_class, &type_class);
493  
494 if (type_class == NULL && once_init.retval != GUINT_TO_POINTER(G_TYPE_INVALID))
495 type_class = g_type_class_ref ((GType)once_init.retval);
496  
497 return (GNativeVolumeMonitorClass *)type_class;
498 }
499  
500 static void
501 g_union_volume_monitor_init (GUnionVolumeMonitor *union_monitor)
502 {
503 }
504  
505 static void
506 populate_union_monitor (GUnionVolumeMonitor *union_monitor)
507 {
508 GVolumeMonitor *monitor;
509 GNativeVolumeMonitorClass *native_class;
510 GVolumeMonitorClass *klass;
511 GIOExtensionPoint *ep;
512 GIOExtension *extension;
513 GList *l;
514  
515 native_class = get_native_class ();
516  
517 if (native_class != NULL)
518 {
519 monitor = g_object_new (G_TYPE_FROM_CLASS (native_class), NULL);
520 g_union_volume_monitor_add_monitor (union_monitor, monitor);
521 g_object_unref (monitor);
522 g_type_class_unref (native_class);
523 }
524  
525 ep = g_io_extension_point_lookup (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
526 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
527 {
528 extension = l->data;
529  
530 klass = G_VOLUME_MONITOR_CLASS (g_io_extension_ref_class (extension));
531 if (klass->is_supported == NULL || klass->is_supported())
532 {
533 monitor = g_object_new (g_io_extension_get_type (extension), NULL);
534 g_union_volume_monitor_add_monitor (union_monitor, monitor);
535 g_object_unref (monitor);
536 }
537 g_type_class_unref (klass);
538 }
539 }
540  
541 static GUnionVolumeMonitor *
542 g_union_volume_monitor_new (void)
543 {
544 GUnionVolumeMonitor *monitor;
545  
546 monitor = g_object_new (G_TYPE_UNION_VOLUME_MONITOR, NULL);
547  
548 return monitor;
549 }
550  
551 /**
552 * g_volume_monitor_get:
553 *
554 * Gets the volume monitor used by gio.
555 *
556 * Returns: (transfer full): a reference to the #GVolumeMonitor used by gio. Call
557 * g_object_unref() when done with it.
558 **/
559 GVolumeMonitor *
560 g_volume_monitor_get (void)
561 {
562 GVolumeMonitor *vm;
563  
564 g_rec_mutex_lock (&the_volume_monitor_mutex);
565  
566 if (the_volume_monitor)
567 vm = G_VOLUME_MONITOR (g_object_ref (the_volume_monitor));
568 else
569 {
570 the_volume_monitor = g_union_volume_monitor_new ();
571 populate_union_monitor (the_volume_monitor);
572 vm = G_VOLUME_MONITOR (the_volume_monitor);
573 }
574  
575 g_rec_mutex_unlock (&the_volume_monitor_mutex);
576  
577 return vm;
578 }
579  
580 GMount *
581 _g_mount_get_for_mount_path (const gchar *mount_path,
582 GCancellable *cancellable)
583 {
584 GNativeVolumeMonitorClass *klass;
585 GMount *mount;
586  
587 klass = get_native_class ();
588 if (klass == NULL)
589 return NULL;
590  
591 mount = NULL;
592  
593 if (klass->get_mount_for_mount_path)
594 mount = klass->get_mount_for_mount_path (mount_path, cancellable);
595  
596 /* TODO: How do we know this succeeded? Keep in mind that the native
597 * volume monitor may fail (e.g. not being able to connect to
598 * hald). Is the get_mount_for_mount_path() method allowed to
599 * return NULL? Seems like it is ... probably the method needs
600 * to take a boolean and write if it succeeds or not.. Messy.
601 * Very messy.
602 */
603  
604 g_type_class_unref (klass);
605  
606 return mount;
607 }
608  
609 /**
610 * g_volume_monitor_adopt_orphan_mount:
611 * @mount: a #GMount object to find a parent for
612 *
613 * This function should be called by any #GVolumeMonitor
614 * implementation when a new #GMount object is created that is not
615 * associated with a #GVolume object. It must be called just before
616 * emitting the @mount_added signal.
617 *
618 * If the return value is not %NULL, the caller must associate the
619 * returned #GVolume object with the #GMount. This involves returning
620 * it in its g_mount_get_volume() implementation. The caller must
621 * also listen for the "removed" signal on the returned object
622 * and give up its reference when handling that signal
623 *
624 * Similary, if implementing g_volume_monitor_adopt_orphan_mount(),
625 * the implementor must take a reference to @mount and return it in
626 * its g_volume_get_mount() implemented. Also, the implementor must
627 * listen for the "unmounted" signal on @mount and give up its
628 * reference upon handling that signal.
629 *
630 * There are two main use cases for this function.
631 *
632 * One is when implementing a user space file system driver that reads
633 * blocks of a block device that is already represented by the native
634 * volume monitor (for example a CD Audio file system driver). Such
635 * a driver will generate its own #GMount object that needs to be
636 * associated with the #GVolume object that represents the volume.
637 *
638 * The other is for implementing a #GVolumeMonitor whose sole purpose
639 * is to return #GVolume objects representing entries in the users
640 * "favorite servers" list or similar.
641 *
642 * Returns: (transfer full): the #GVolume object that is the parent for @mount or %NULL
643 * if no wants to adopt the #GMount.
644 *
645 * Deprecated: 2.20: Instead of using this function, #GVolumeMonitor
646 * implementations should instead create shadow mounts with the URI of
647 * the mount they intend to adopt. See the proxy volume monitor in
648 * gvfs for an example of this. Also see g_mount_is_shadowed(),
649 * g_mount_shadow() and g_mount_unshadow() functions.
650 */
651 GVolume *
652 g_volume_monitor_adopt_orphan_mount (GMount *mount)
653 {
654 GVolumeMonitor *child_monitor;
655 GVolumeMonitorClass *child_monitor_class;
656 GVolume *volume;
657 GList *l;
658  
659 g_return_val_if_fail (mount != NULL, NULL);
660  
661 if (the_volume_monitor == NULL)
662 return NULL;
663  
664 volume = NULL;
665  
666 g_rec_mutex_lock (&the_volume_monitor_mutex);
667  
668 for (l = the_volume_monitor->monitors; l != NULL; l = l->next)
669 {
670 child_monitor = l->data;
671 child_monitor_class = G_VOLUME_MONITOR_GET_CLASS (child_monitor);
672  
673 if (child_monitor_class->adopt_orphan_mount != NULL)
674 {
675 volume = child_monitor_class->adopt_orphan_mount (mount, child_monitor);
676 if (volume != NULL)
677 break;
678 }
679 }
680  
681 g_rec_mutex_unlock (&the_volume_monitor_mutex);
682  
683 return volume;
684 }