2005-01-27 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-object-tree.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-object-tree.c  DBusObjectTree (internals of DBusConnection)
3  *
4  * Copyright (C) 2003  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include "dbus-object-tree.h"
24 #include "dbus-connection-internal.h"
25 #include "dbus-internals.h"
26 #include "dbus-hash.h"
27 #include "dbus-protocol.h"
28 #include "dbus-string.h"
29 #include <string.h>
30 #include <stdlib.h>
31
32 /**
33  * @defgroup DBusObjectTree A hierarchy of objects with container-contained relationship
34  * @ingroup  DBusInternals
35  * @brief DBusObjectTree is used by DBusConnection to track the object tree
36  *
37  * Types and functions related to DBusObjectTree. These
38  * are all library-internal.
39  *
40  * @{
41  */
42
43 /** Subnode of the object hierarchy */
44 typedef struct DBusObjectSubtree DBusObjectSubtree;
45
46 static DBusObjectSubtree* _dbus_object_subtree_new   (const char                  *name,
47                                                       const DBusObjectPathVTable  *vtable,
48                                                       void                        *user_data);
49 static DBusObjectSubtree* _dbus_object_subtree_ref   (DBusObjectSubtree           *subtree);
50 static void               _dbus_object_subtree_unref (DBusObjectSubtree           *subtree);
51
52 /**
53  * Internals of DBusObjectTree
54  */
55 struct DBusObjectTree
56 {
57   int                 refcount;   /**< Reference count */
58   DBusConnection     *connection; /**< Connection this tree belongs to */
59
60   DBusObjectSubtree  *root;       /**< Root of the tree ("/" node) */
61 };
62
63 /**
64  * Struct representing a single registered subtree handler, or node
65  * that's a parent of a registered subtree handler. If
66  * message_function != NULL there's actually a handler at this node.
67  */
68 struct DBusObjectSubtree
69 {
70   DBusAtomic                         refcount;            /**< Reference count */
71   DBusObjectSubtree                 *parent;              /**< Parent node */
72   DBusObjectPathUnregisterFunction   unregister_function; /**< Function to call on unregister */
73   DBusObjectPathMessageFunction      message_function;    /**< Function to handle messages */
74   void                              *user_data;           /**< Data for functions */
75   DBusObjectSubtree                **subtrees;            /**< Child nodes */
76   int                                n_subtrees;          /**< Number of child nodes */
77   unsigned int                       subtrees_sorted : 1; /**< Whether children are sorted */
78   unsigned int                       invoke_as_fallback : 1; /**< Whether to invoke message_function when child nodes don't handle the message */
79   char                               name[1]; /**< Allocated as large as necessary */
80 };
81
82 /**
83  * Creates a new object tree, representing a mapping from paths
84  * to handler vtables.
85  *
86  * @param connection the connection this tree belongs to
87  * @returns the new tree or #NULL if no memory
88  */
89 DBusObjectTree*
90 _dbus_object_tree_new (DBusConnection *connection)
91 {
92   DBusObjectTree *tree;
93
94   /* the connection passed in here isn't fully constructed,
95    * so don't do anything more than store a pointer to
96    * it
97    */
98
99   tree = dbus_new0 (DBusObjectTree, 1);
100   if (tree == NULL)
101     goto oom;
102
103   tree->refcount = 1;
104   tree->connection = connection;
105   tree->root = _dbus_object_subtree_new ("/", NULL, NULL);
106   if (tree->root == NULL)
107     goto oom;
108   tree->root->invoke_as_fallback = TRUE;
109   
110   return tree;
111
112  oom:
113   if (tree)
114     {
115       dbus_free (tree);
116     }
117
118   return NULL;
119 }
120
121 /**
122  * Increment the reference count
123  * @param tree the object tree
124  * @returns the object tree
125  */
126 DBusObjectTree *
127 _dbus_object_tree_ref (DBusObjectTree *tree)
128 {
129   _dbus_assert (tree->refcount > 0);
130
131   tree->refcount += 1;
132
133   return tree;
134 }
135
136 /**
137  * Decrement the reference count
138  * @param tree the object tree
139  */
140 void
141 _dbus_object_tree_unref (DBusObjectTree *tree)
142 {
143   _dbus_assert (tree->refcount > 0);
144
145   tree->refcount -= 1;
146
147   if (tree->refcount == 0)
148     {
149       _dbus_object_tree_free_all_unlocked (tree);
150
151       dbus_free (tree);
152     }
153 }
154
155 static int
156 subtree_cmp (DBusObjectSubtree *subtree_a,
157              DBusObjectSubtree *subtree_b)
158 {
159   return strcmp (subtree_a->name, subtree_b->name);
160 }
161
162 static int
163 subtree_qsort_cmp (const void *a,
164                    const void *b)
165 {
166   DBusObjectSubtree **subtree_a_p = (void*) a;
167   DBusObjectSubtree **subtree_b_p = (void*) b;
168
169   return subtree_cmp (*subtree_a_p, *subtree_b_p);
170 }
171
172 static void
173 ensure_sorted (DBusObjectSubtree *subtree)
174 {
175   if (subtree->subtrees && !subtree->subtrees_sorted)
176     {
177       qsort (subtree->subtrees,
178              subtree->n_subtrees,
179              sizeof (DBusObjectSubtree*),
180              subtree_qsort_cmp);
181       subtree->subtrees_sorted = TRUE;
182     }
183 }
184
185 /** Set to 1 to get a bunch of debug spew about finding the
186  * subtree nodes
187  */
188 #define VERBOSE_FIND 0
189
190 static DBusObjectSubtree*
191 find_subtree_recurse (DBusObjectSubtree  *subtree,
192                       const char        **path,
193                       dbus_bool_t         create_if_not_found,
194                       int                *index_in_parent,
195                       dbus_bool_t        *exact_match)
196 {
197   int i;
198   dbus_bool_t return_deepest_match;
199
200   return_deepest_match = exact_match != NULL;
201
202   _dbus_assert (!(return_deepest_match && create_if_not_found));
203
204   if (path[0] == NULL)
205     {
206 #if VERBOSE_FIND
207       _dbus_verbose ("  path exhausted, returning %s\n",
208                      subtree->name);
209 #endif
210       if (exact_match != NULL)
211         *exact_match = TRUE;
212       return subtree;
213     }
214
215 #if VERBOSE_FIND
216   _dbus_verbose ("  searching children of %s for %s\n",
217                  subtree->name, path[0]);
218 #endif
219   
220   ensure_sorted (subtree);
221
222   /* FIXME we should do a binary search here instead
223    * of O(n)
224    */
225
226   i = 0;
227   while (i < subtree->n_subtrees)
228     {
229       int v;
230
231       v = strcmp (path[0], subtree->subtrees[i]->name);
232
233 #if VERBOSE_FIND
234       _dbus_verbose ("  %s cmp %s = %d\n",
235                      path[0], subtree->subtrees[i]->name,
236                      v);
237 #endif
238       
239       if (v == 0)
240         {
241           if (index_in_parent)
242             {
243 #if VERBOSE_FIND
244               _dbus_verbose ("  storing parent index %d\n", i);
245 #endif
246               *index_in_parent = i;
247             }
248
249           if (return_deepest_match)
250             {
251               DBusObjectSubtree *next;
252
253               next = find_subtree_recurse (subtree->subtrees[i],
254                                            &path[1], create_if_not_found, 
255                                            index_in_parent, exact_match);
256               if (next == NULL &&
257                   subtree->invoke_as_fallback)
258                 {
259 #if VERBOSE_FIND
260                   _dbus_verbose ("  no deeper match found, returning %s\n",
261                                  subtree->name);
262 #endif
263                   if (exact_match != NULL)
264                     *exact_match = FALSE;
265                   return subtree;
266                 }
267               else
268                 return next;
269             }
270           else
271             return find_subtree_recurse (subtree->subtrees[i],
272                                          &path[1], create_if_not_found, 
273                                          index_in_parent, exact_match);
274         }
275       else if (v < 0)
276         {
277           goto not_found;
278         }
279
280       ++i;
281     }
282
283  not_found:
284 #if VERBOSE_FIND
285   _dbus_verbose ("  no match found, current tree %s, create_if_not_found = %d\n",
286                  subtree->name, create_if_not_found);
287 #endif
288   
289   if (create_if_not_found)
290     {
291       DBusObjectSubtree* child;
292       DBusObjectSubtree **new_subtrees;
293       int new_n_subtrees;
294
295 #if VERBOSE_FIND
296       _dbus_verbose ("  creating subtree %s\n",
297                      path[0]);
298 #endif
299       
300       child = _dbus_object_subtree_new (path[0],
301                                         NULL, NULL);
302       if (child == NULL)
303         return NULL;
304
305       /* FIXME we should do the "double alloc each time" standard thing */
306       new_n_subtrees = subtree->n_subtrees + 1;
307       new_subtrees = dbus_realloc (subtree->subtrees,
308                                    new_n_subtrees * sizeof (DBusObjectSubtree*));
309       if (new_subtrees == NULL)
310         {
311           child->unregister_function = NULL;
312           child->message_function = NULL;
313           _dbus_object_subtree_unref (child);
314           return NULL;
315         }
316
317       new_subtrees[subtree->n_subtrees] = child;
318       if (index_in_parent)
319         *index_in_parent = subtree->n_subtrees;
320       subtree->subtrees_sorted = FALSE;
321       subtree->n_subtrees = new_n_subtrees;
322       subtree->subtrees = new_subtrees;
323
324       child->parent = subtree;
325
326       return find_subtree_recurse (child,
327                                    &path[1], create_if_not_found, 
328                                    index_in_parent, exact_match);
329     }
330   else
331     {
332       if (exact_match != NULL)
333         *exact_match = FALSE;
334       return (return_deepest_match && subtree->invoke_as_fallback) ? subtree : NULL;
335     }
336 }
337
338 static DBusObjectSubtree*
339 find_subtree (DBusObjectTree *tree,
340               const char    **path,
341               int            *index_in_parent)
342 {
343   DBusObjectSubtree *subtree;
344
345 #if VERBOSE_FIND
346   _dbus_verbose ("Looking for exact registered subtree\n");
347 #endif
348   
349   subtree = find_subtree_recurse (tree->root, path, FALSE, index_in_parent, NULL);
350
351   if (subtree && subtree->message_function == NULL)
352     return NULL;
353   else
354     return subtree;
355 }
356
357 static DBusObjectSubtree*
358 lookup_subtree (DBusObjectTree *tree,
359                 const char    **path)
360 {
361 #if VERBOSE_FIND
362   _dbus_verbose ("Looking for subtree\n");
363 #endif
364   return find_subtree_recurse (tree->root, path, FALSE, NULL, NULL);
365 }
366
367 static DBusObjectSubtree*
368 find_handler (DBusObjectTree *tree,
369               const char    **path,
370               dbus_bool_t    *exact_match)
371 {
372 #if VERBOSE_FIND
373   _dbus_verbose ("Looking for deepest handler\n");
374 #endif
375   _dbus_assert (exact_match != NULL);
376   return find_subtree_recurse (tree->root, path, FALSE, NULL, exact_match);
377 }
378
379 static DBusObjectSubtree*
380 ensure_subtree (DBusObjectTree *tree,
381                 const char    **path)
382 {
383 #if VERBOSE_FIND
384   _dbus_verbose ("Ensuring subtree\n");
385 #endif
386   return find_subtree_recurse (tree->root, path, TRUE, NULL, NULL);
387 }
388
389 /**
390  * Registers a new subtree in the global object tree.
391  *
392  * @param tree the global object tree
393  * @param fallback #TRUE to handle messages to children of this path
394  * @param path NULL-terminated array of path elements giving path to subtree
395  * @param vtable the vtable used to traverse this subtree
396  * @param user_data user data to pass to methods in the vtable
397  * @returns #FALSE if not enough memory
398  */
399 dbus_bool_t
400 _dbus_object_tree_register (DBusObjectTree              *tree,
401                             dbus_bool_t                  fallback,
402                             const char                 **path,
403                             const DBusObjectPathVTable  *vtable,
404                             void                        *user_data)
405 {
406   DBusObjectSubtree  *subtree;
407
408   _dbus_assert (tree != NULL);
409   _dbus_assert (vtable->message_function != NULL);
410   _dbus_assert (path != NULL);
411
412   subtree = ensure_subtree (tree, path);
413   if (subtree == NULL)
414     return FALSE;
415
416 #ifndef DBUS_DISABLE_CHECKS
417   if (subtree->message_function != NULL)
418     {
419       _dbus_warn ("A handler is already registered for the path starting with path[0] = \"%s\"\n",
420                   path[0] ? path[0] : "null");
421       return FALSE;
422     }
423 #else
424   _dbus_assert (subtree->message_function == NULL);
425 #endif
426
427   subtree->message_function = vtable->message_function;
428   subtree->unregister_function = vtable->unregister_function;
429   subtree->user_data = user_data;
430   subtree->invoke_as_fallback = fallback != FALSE;
431   
432   return TRUE;
433 }
434
435 /**
436  * Unregisters an object subtree that was registered with the
437  * same path.
438  *
439  * @param tree the global object tree
440  * @param path path to the subtree (same as the one passed to _dbus_object_tree_register())
441  */
442 void
443 _dbus_object_tree_unregister_and_unlock (DBusObjectTree          *tree,
444                                          const char             **path)
445 {
446   int i;
447   DBusObjectSubtree *subtree;
448   DBusObjectPathUnregisterFunction unregister_function;
449   void *user_data;
450   DBusConnection *connection;
451
452   _dbus_assert (path != NULL);
453
454   subtree = find_subtree (tree, path, &i);
455
456 #ifndef DBUS_DISABLE_CHECKS
457   if (subtree == NULL)
458     {
459       _dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
460                   path[0] ? path[0] : "null",
461                   path[1] ? path[1] : "null");
462       return;
463     }
464 #else
465   _dbus_assert (subtree != NULL);
466 #endif
467
468   _dbus_assert (subtree->parent == NULL ||
469                 (i >= 0 && subtree->parent->subtrees[i] == subtree));
470
471   subtree->message_function = NULL;
472
473   unregister_function = subtree->unregister_function;
474   user_data = subtree->user_data;
475
476   subtree->unregister_function = NULL;
477   subtree->user_data = NULL;
478
479   /* If we have no subtrees of our own, remove from
480    * our parent (FIXME could also be more aggressive
481    * and remove our parent if it becomes empty)
482    */
483   if (subtree->parent && subtree->n_subtrees == 0)
484     {
485       /* assumes a 0-byte memmove is OK */
486       memmove (&subtree->parent->subtrees[i],
487                &subtree->parent->subtrees[i+1],
488                (subtree->parent->n_subtrees - i - 1) *
489                sizeof (subtree->parent->subtrees[0]));
490       subtree->parent->n_subtrees -= 1;
491
492       subtree->parent = NULL;
493
494       _dbus_object_subtree_unref (subtree);
495     }
496   subtree = NULL;
497
498   connection = tree->connection;
499
500   /* Unlock and call application code */
501 #ifdef DBUS_BUILD_TESTS
502   if (connection)
503 #endif
504     {
505       _dbus_connection_ref_unlocked (connection);
506       _dbus_connection_unlock (connection);
507     }
508
509   if (unregister_function)
510     (* unregister_function) (connection, user_data);
511
512 #ifdef DBUS_BUILD_TESTS
513   if (connection)
514 #endif
515     dbus_connection_unref (connection);
516 }
517
518 static void
519 free_subtree_recurse (DBusConnection    *connection,
520                       DBusObjectSubtree *subtree)
521 {
522   /* Delete them from the end, for slightly
523    * more robustness against odd reentrancy.
524    */
525   while (subtree->n_subtrees > 0)
526     {
527       DBusObjectSubtree *child;
528
529       child = subtree->subtrees[subtree->n_subtrees - 1];
530       subtree->subtrees[subtree->n_subtrees - 1] = NULL;
531       subtree->n_subtrees -= 1;
532       child->parent = NULL;
533
534       free_subtree_recurse (connection, child);
535     }
536
537   /* Call application code */
538   if (subtree->unregister_function)
539     (* subtree->unregister_function) (connection,
540                                       subtree->user_data);
541
542   subtree->message_function = NULL;
543   subtree->unregister_function = NULL;
544   subtree->user_data = NULL;
545
546   /* Now free ourselves */
547   _dbus_object_subtree_unref (subtree);
548 }
549
550 /**
551  * Free all the handlers in the tree. Lock on tree's connection
552  * must not be held.
553  *
554  * @param tree the object tree
555  */
556 void
557 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
558 {
559   if (tree->root)
560     free_subtree_recurse (tree->connection,
561                           tree->root);
562   tree->root = NULL;
563 }
564
565 static dbus_bool_t
566 _dbus_object_tree_list_registered_unlocked (DBusObjectTree *tree,
567                                             const char    **parent_path,
568                                             char         ***child_entries)
569 {
570   DBusObjectSubtree *subtree;
571   char **retval;
572   
573   _dbus_assert (parent_path != NULL);
574   _dbus_assert (child_entries != NULL);
575
576   *child_entries = NULL;
577   
578   subtree = lookup_subtree (tree, parent_path);
579   if (subtree == NULL)
580     {
581       retval = dbus_new0 (char *, 1);
582     }
583   else
584     {
585       int i;
586       retval = dbus_new0 (char*, subtree->n_subtrees + 1);
587       if (retval == NULL)
588         goto out;
589       i = 0;
590       while (i < subtree->n_subtrees)
591         {
592           retval[i] = _dbus_strdup (subtree->subtrees[i]->name);
593           if (retval[i] == NULL)
594             {
595               dbus_free_string_array (retval);
596               retval = NULL;
597               goto out;
598             }
599           ++i;
600         }
601     }
602
603  out:
604     
605   *child_entries = retval;
606   return retval != NULL;
607 }
608
609 static DBusHandlerResult
610 handle_default_introspect_unlocked (DBusObjectTree          *tree,
611                                     DBusMessage             *message,
612                                     const char             **path)
613 {
614   DBusString xml;
615   DBusHandlerResult result;
616   char **children;
617   int i;
618
619   if (!dbus_message_is_method_call (message,
620                                     DBUS_INTERFACE_ORG_FREEDESKTOP_INTROSPECTABLE,
621                                     "Introspect"))
622     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
623   
624   if (!_dbus_string_init (&xml))
625     return DBUS_HANDLER_RESULT_NEED_MEMORY;
626
627   result = DBUS_HANDLER_RESULT_NEED_MEMORY;
628
629   children = NULL;
630   if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
631     goto out;
632
633   if (!_dbus_string_append (&xml, "<node>\n"))
634     goto out;
635
636   i = 0;
637   while (children[i] != NULL)
638     {
639       if (!_dbus_string_append_printf (&xml, "  <node name=\"%s\"/>\n",
640                                        children[i]))
641         goto out;
642
643       ++i;
644     }
645
646   if (!_dbus_string_append (&xml, "</node>\n"))
647     goto out;
648   
649   result = DBUS_HANDLER_RESULT_HANDLED;
650   
651  out:
652   _dbus_string_free (&xml);
653   dbus_free_string_array (children);
654   
655   return result;
656 }
657
658 /**
659  * Tries to dispatch a message by directing it to handler for the
660  * object path listed in the message header, if any. Messages are
661  * dispatched first to the registered handler that matches the largest
662  * number of path elements; that is, message to /foo/bar/baz would go
663  * to the handler for /foo/bar before the one for /foo.
664  *
665  * @todo thread problems
666  *
667  * @param tree the global object tree
668  * @param message the message to dispatch
669  * @returns whether message was handled successfully
670  */
671 DBusHandlerResult
672 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
673                                        DBusMessage             *message)
674 {
675   char **path;
676   dbus_bool_t exact_match;
677   DBusList *list;
678   DBusList *link;
679   DBusHandlerResult result;
680   DBusObjectSubtree *subtree;
681   
682 #if 0
683   _dbus_verbose ("Dispatch of message by object path\n");
684 #endif
685   
686   path = NULL;
687   if (!dbus_message_get_path_decomposed (message, &path))
688     {
689 #ifdef DBUS_BUILD_TESTS
690       if (tree->connection)
691 #endif
692         _dbus_connection_unlock (tree->connection);
693       
694       _dbus_verbose ("No memory to get decomposed path\n");
695
696       return DBUS_HANDLER_RESULT_NEED_MEMORY;
697     }
698
699   if (path == NULL)
700     {
701 #ifdef DBUS_BUILD_TESTS
702       if (tree->connection)
703 #endif
704         _dbus_connection_unlock (tree->connection);
705       
706       _dbus_verbose ("No path field in message\n");
707       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
708     }
709   
710   /* Find the deepest path that covers the path in the message */
711   subtree = find_handler (tree, (const char**) path, &exact_match);
712   
713   /* Build a list of all paths that cover the path in the message */
714
715   list = NULL;
716
717   while (subtree != NULL)
718     {
719       if (subtree->message_function != NULL && (exact_match || subtree->invoke_as_fallback))
720         {
721           _dbus_object_subtree_ref (subtree);
722
723           /* run deepest paths first */
724           if (!_dbus_list_append (&list, subtree))
725             {
726               result = DBUS_HANDLER_RESULT_NEED_MEMORY;
727               _dbus_object_subtree_unref (subtree);
728               goto free_and_return;
729             }
730         }
731
732       exact_match = FALSE;
733       subtree = subtree->parent;
734     }
735
736   _dbus_verbose ("%d handlers in the path tree for this message\n",
737                  _dbus_list_get_length (&list));
738
739   /* Invoke each handler in the list */
740
741   result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
742
743   link = _dbus_list_get_first_link (&list);
744   while (link != NULL)
745     {
746       DBusList *next = _dbus_list_get_next_link (&list, link);
747       subtree = link->data;
748
749       /* message_function is NULL if we're unregistered
750        * due to reentrancy
751        */
752       if (subtree->message_function)
753         {
754           DBusObjectPathMessageFunction message_function;
755           void *user_data;
756
757           message_function = subtree->message_function;
758           user_data = subtree->user_data;
759
760 #if 0
761           _dbus_verbose ("  (invoking a handler)\n");
762 #endif
763           
764 #ifdef DBUS_BUILD_TESTS
765           if (tree->connection)
766 #endif
767             _dbus_connection_unlock (tree->connection);
768
769           /* FIXME you could unregister the subtree in another thread
770            * before we invoke the callback, and I can't figure out a
771            * good way to solve this.
772            */
773
774           result = (* message_function) (tree->connection,
775                                          message,
776                                          user_data);
777
778 #ifdef DBUS_BUILD_TESTS
779           if (tree->connection)
780 #endif
781             _dbus_connection_lock (tree->connection);
782
783           if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
784             goto free_and_return;
785         }
786
787       link = next;
788     }
789
790  free_and_return:
791
792   if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
793     {
794       /* This hardcoded default handler does a minimal Introspect()
795        */
796       result = handle_default_introspect_unlocked (tree, message,
797                                                    (const char**) path);
798     }
799
800 #ifdef DBUS_BUILD_TESTS
801   if (tree->connection)
802 #endif
803     _dbus_connection_unlock (tree->connection);
804   
805   while (list != NULL)
806     {
807       link = _dbus_list_get_first_link (&list);
808       _dbus_object_subtree_unref (link->data);
809       _dbus_list_remove_link (&list, link);
810     }
811   
812   dbus_free_string_array (path);
813
814   return result;
815 }
816
817 /**
818  * Allocates a subtree object.
819  *
820  * @param name name to duplicate.
821  * @returns newly-allocated subtree
822  */
823 static DBusObjectSubtree*
824 allocate_subtree_object (const char *name)
825 {
826   int len;
827   DBusObjectSubtree *subtree;
828   const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
829
830   _dbus_assert (name != NULL);
831
832   len = strlen (name);
833
834   subtree = dbus_malloc (front_padding + (len + 1));
835
836   if (subtree == NULL)
837     return NULL;
838
839   memcpy (subtree->name, name, len + 1);
840
841   return subtree;
842 }
843
844 static DBusObjectSubtree*
845 _dbus_object_subtree_new (const char                  *name,
846                           const DBusObjectPathVTable  *vtable,
847                           void                        *user_data)
848 {
849   DBusObjectSubtree *subtree;
850
851   subtree = allocate_subtree_object (name);
852   if (subtree == NULL)
853     goto oom;
854
855   _dbus_assert (name != NULL);
856
857   subtree->parent = NULL;
858
859   if (vtable)
860     {
861       subtree->message_function = vtable->message_function;
862       subtree->unregister_function = vtable->unregister_function;
863     }
864   else
865     {
866       subtree->message_function = NULL;
867       subtree->unregister_function = NULL;
868     }
869
870   subtree->user_data = user_data;
871   subtree->refcount.value = 1;
872   subtree->subtrees = NULL;
873   subtree->n_subtrees = 0;
874   subtree->subtrees_sorted = TRUE;
875   subtree->invoke_as_fallback = FALSE;
876
877   return subtree;
878
879  oom:
880   if (subtree)
881     {
882       dbus_free (subtree);
883     }
884
885   return NULL;
886 }
887
888 static DBusObjectSubtree *
889 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
890 {
891   _dbus_assert (subtree->refcount.value > 0);
892   _dbus_atomic_inc (&subtree->refcount);
893
894   return subtree;
895 }
896
897 static void
898 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
899 {
900   _dbus_assert (subtree->refcount.value > 0);
901
902   if (_dbus_atomic_dec (&subtree->refcount) == 1)
903     {
904       _dbus_assert (subtree->unregister_function == NULL);
905       _dbus_assert (subtree->message_function == NULL);
906
907       dbus_free (subtree->subtrees);
908       dbus_free (subtree);
909     }
910 }
911
912 /**
913  * Lists the registered fallback handlers and object path handlers at
914  * the given parent_path. The returned array should be freed with
915  * dbus_free_string_array().
916  *
917  * @param tree the object tree
918  * @param parent_path the path to list the child handlers of
919  * @param child_entries returns #NULL-terminated array of children
920  * @returns #FALSE if no memory to allocate the child entries
921  */
922 dbus_bool_t
923 _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
924                                               const char    **parent_path,
925                                               char         ***child_entries)
926 {
927   dbus_bool_t result;
928
929   result = _dbus_object_tree_list_registered_unlocked (tree,
930                                                        parent_path,
931                                                        child_entries);
932   
933 #ifdef DBUS_BUILD_TESTS
934   if (tree->connection)
935 #endif
936     _dbus_connection_unlock (tree->connection);
937
938   return result;
939 }
940
941
942 /** Set to 1 to get a bunch of spew about disassembling the path string */
943 #define VERBOSE_DECOMPOSE 0
944
945 /**
946  * Decompose an object path.  A path of just "/" is
947  * represented as an empty vector of strings.
948  * 
949  * @param data the path data
950  * @param len  the length of the path string
951  * @param path address to store new object path
952  * @param path_len length of stored path
953  */
954 dbus_bool_t
955 _dbus_decompose_path (const char*     data,
956                       int             len,
957                       char         ***path,
958                       int            *path_len)
959 {
960   char **retval;
961   int n_components;
962   int i, j, comp;
963
964   _dbus_assert (data != NULL);
965
966 #if VERBOSE_DECOMPOSE
967   _dbus_verbose ("Decomposing path \"%s\"\n",
968                  data);
969 #endif
970   
971   n_components = 0;
972   i = 0;
973   while (i < len)
974     {
975       if (data[i] == '/')
976         n_components += 1;
977       ++i;
978     }
979   
980   retval = dbus_new0 (char*, n_components + 1);
981
982   if (retval == NULL)
983     return FALSE;
984
985   comp = 0;
986   i = 0;
987   while (i < len)
988     {
989       if (data[i] == '/')
990         ++i;
991       j = i;
992
993       while (j < len && data[j] != '/')
994         ++j;
995
996       /* Now [i, j) is the path component */
997       _dbus_assert (i < j);
998       _dbus_assert (data[i] != '/');
999       _dbus_assert (j == len || data[j] == '/');
1000
1001 #if VERBOSE_DECOMPOSE
1002       _dbus_verbose ("  (component in [%d,%d))\n",
1003                      i, j);
1004 #endif
1005       
1006       retval[comp] = _dbus_memdup (&data[i], j - i + 1);
1007       if (retval[comp] == NULL)
1008         {
1009           dbus_free_string_array (retval);
1010           return FALSE;
1011         }
1012       retval[comp][j-i] = '\0';
1013 #if VERBOSE_DECOMPOSE
1014       _dbus_verbose ("  (component %d = \"%s\")\n",
1015                      comp, retval[comp]);
1016 #endif
1017
1018       ++comp;
1019       i = j;
1020     }
1021   _dbus_assert (i == len);
1022   
1023   *path = retval;
1024   if (path_len)
1025     *path_len = n_components;
1026   
1027   return TRUE;
1028 }
1029
1030 /** @} */
1031
1032 #ifdef DBUS_BUILD_TESTS
1033 #include "dbus-test.h"
1034 #include <stdio.h>
1035
1036 static char*
1037 flatten_path (const char **path)
1038 {
1039   DBusString str;
1040   int i;
1041   char *s;
1042
1043   if (!_dbus_string_init (&str))
1044     return NULL;
1045
1046   i = 0;
1047   while (path[i])
1048     {
1049       if (!_dbus_string_append_byte (&str, '/'))
1050         goto nomem;
1051
1052       if (!_dbus_string_append (&str, path[i]))
1053         goto nomem;
1054
1055       ++i;
1056     }
1057
1058   if (!_dbus_string_steal_data (&str, &s))
1059     goto nomem;
1060
1061   _dbus_string_free (&str);
1062
1063   return s;
1064
1065  nomem:
1066   _dbus_string_free (&str);
1067   return NULL;
1068 }
1069
1070
1071 typedef enum 
1072 {
1073   STR_EQUAL,
1074   STR_PREFIX,
1075   STR_DIFFERENT
1076 } StrComparison;
1077
1078 /* Returns TRUE if container is a parent of child
1079  */
1080 static StrComparison
1081 path_contains (const char **container,
1082                const char **child)
1083 {
1084   int i;
1085
1086   i = 0;
1087   while (child[i] != NULL)
1088     {
1089       int v;
1090
1091       if (container[i] == NULL)
1092         return STR_PREFIX; /* container ran out, child continues;
1093                         * thus the container is a parent of the
1094                         * child.
1095                         */
1096
1097       _dbus_assert (container[i] != NULL);
1098       _dbus_assert (child[i] != NULL);
1099
1100       v = strcmp (container[i], child[i]);
1101
1102       if (v != 0)
1103         return STR_DIFFERENT; /* they overlap until here and then are different,
1104                            * not overlapping
1105                            */
1106
1107       ++i;
1108     }
1109
1110   /* Child ran out; if container also did, they are equal;
1111    * otherwise, the child is a parent of the container.
1112    */
1113   if (container[i] == NULL)
1114     return STR_EQUAL;
1115   else
1116     return STR_DIFFERENT;
1117 }
1118
1119 #if 0
1120 static void
1121 spew_subtree_recurse (DBusObjectSubtree *subtree,
1122                       int                indent)
1123 {
1124   int i;
1125
1126   i = 0;
1127   while (i < indent)
1128     {
1129       _dbus_verbose (" ");
1130       ++i;
1131     }
1132
1133   _dbus_verbose ("%s (%d children)\n",
1134                  subtree->name, subtree->n_subtrees);
1135
1136   i = 0;
1137   while (i < subtree->n_subtrees)
1138     {
1139       spew_subtree_recurse (subtree->subtrees[i], indent + 2);
1140
1141       ++i;
1142     }
1143 }
1144
1145 static void
1146 spew_tree (DBusObjectTree *tree)
1147 {
1148   spew_subtree_recurse (tree->root, 0);
1149 }
1150 #endif
1151
1152 /**
1153  * Callback data used in tests
1154  */
1155 typedef struct
1156 {
1157   const char **path; /**< Path */
1158   dbus_bool_t handler_fallback; /**< true if the handler may be called as fallback */
1159   dbus_bool_t message_handled; /**< Gets set to true if message handler called */
1160   dbus_bool_t handler_unregistered; /**< gets set to true if handler is unregistered */
1161 } TreeTestData;
1162
1163
1164 static void
1165 test_unregister_function (DBusConnection  *connection,
1166                           void            *user_data)
1167 {
1168   TreeTestData *ttd = user_data;
1169
1170   ttd->handler_unregistered = TRUE;
1171 }
1172
1173 static DBusHandlerResult
1174 test_message_function (DBusConnection  *connection,
1175                        DBusMessage     *message,
1176                        void            *user_data)
1177 {
1178   TreeTestData *ttd = user_data;
1179
1180   ttd->message_handled = TRUE;
1181
1182   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1183 }
1184
1185 static dbus_bool_t
1186 do_register (DBusObjectTree *tree,
1187              const char    **path,
1188              dbus_bool_t     fallback,
1189              int             i,
1190              TreeTestData   *tree_test_data)
1191 {
1192   DBusObjectPathVTable vtable = { test_unregister_function,
1193                                   test_message_function, NULL };
1194
1195   tree_test_data[i].message_handled = FALSE;
1196   tree_test_data[i].handler_unregistered = FALSE;
1197   tree_test_data[i].handler_fallback = fallback;
1198   tree_test_data[i].path = path;
1199
1200   if (!_dbus_object_tree_register (tree, fallback, path,
1201                                    &vtable,
1202                                    &tree_test_data[i]))
1203     return FALSE;
1204
1205   return TRUE;
1206 }
1207
1208 static dbus_bool_t
1209 do_test_dispatch (DBusObjectTree *tree,
1210                   const char    **path,
1211                   int             i,
1212                   TreeTestData   *tree_test_data,
1213                   int             n_test_data)
1214 {
1215   DBusMessage *message;
1216   int j;
1217   DBusHandlerResult result;
1218   char *flat;
1219
1220   message = NULL;
1221   
1222   flat = flatten_path (path);
1223   if (flat == NULL)
1224     goto oom;
1225
1226   message = dbus_message_new_method_call (NULL,
1227                                           flat,
1228                                           "org.freedesktop.TestInterface",
1229                                           "Foo");
1230   dbus_free (flat);
1231   if (message == NULL)
1232     goto oom;
1233
1234   j = 0;
1235   while (j < n_test_data)
1236     {
1237       tree_test_data[j].message_handled = FALSE;
1238       ++j;
1239     }
1240
1241   result = _dbus_object_tree_dispatch_and_unlock (tree, message);
1242   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
1243     goto oom;
1244
1245   _dbus_assert (tree_test_data[i].message_handled);
1246
1247   j = 0;
1248   while (j < n_test_data)
1249     {
1250       if (tree_test_data[j].message_handled)
1251         {
1252           if (tree_test_data[j].handler_fallback)
1253             _dbus_assert (path_contains (tree_test_data[j].path,
1254                                          path) != STR_DIFFERENT);
1255           else
1256             _dbus_assert (path_contains (tree_test_data[j].path, path) == STR_EQUAL);
1257         }
1258       else
1259         {
1260           if (tree_test_data[j].handler_fallback)
1261             _dbus_assert (path_contains (tree_test_data[j].path,
1262                                          path) == STR_DIFFERENT);
1263           else
1264             _dbus_assert (path_contains (tree_test_data[j].path, path) != STR_EQUAL);
1265         }
1266
1267       ++j;
1268     }
1269
1270   dbus_message_unref (message);
1271
1272   return TRUE;
1273
1274  oom:
1275   if (message)
1276     dbus_message_unref (message);
1277   return FALSE;
1278 }
1279
1280 static size_t
1281 string_array_length (char **array)
1282 {
1283   size_t i;
1284   for (i = 0; array[i]; i++) ;
1285   return i;
1286 }
1287
1288
1289 static dbus_bool_t
1290 object_tree_test_iteration (void *data)
1291 {
1292   const char *path1[] = { "foo", NULL };
1293   const char *path2[] = { "foo", "bar", NULL };
1294   const char *path3[] = { "foo", "bar", "baz", NULL };
1295   const char *path4[] = { "foo", "bar", "boo", NULL };
1296   const char *path5[] = { "blah", NULL };
1297   const char *path6[] = { "blah", "boof", NULL };
1298   const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
1299   const char *path8[] = { "childless", NULL };
1300   DBusObjectTree *tree;
1301   TreeTestData tree_test_data[8];
1302   int i;
1303   dbus_bool_t exact_match;
1304
1305   tree = NULL;
1306
1307   tree = _dbus_object_tree_new (NULL);
1308   if (tree == NULL)
1309     goto out;
1310
1311   if (!do_register (tree, path1, TRUE, 0, tree_test_data))
1312     goto out;
1313
1314   _dbus_assert (find_subtree (tree, path1, NULL));
1315   _dbus_assert (!find_subtree (tree, path2, NULL));
1316   _dbus_assert (!find_subtree (tree, path3, NULL));
1317   _dbus_assert (!find_subtree (tree, path4, NULL));
1318   _dbus_assert (!find_subtree (tree, path5, NULL));
1319   _dbus_assert (!find_subtree (tree, path6, NULL));
1320   _dbus_assert (!find_subtree (tree, path7, NULL));
1321   _dbus_assert (!find_subtree (tree, path8, NULL));
1322
1323   _dbus_assert (find_handler (tree, path1, &exact_match) &&  exact_match);
1324   _dbus_assert (find_handler (tree, path2, &exact_match) && !exact_match);
1325   _dbus_assert (find_handler (tree, path3, &exact_match) && !exact_match);
1326   _dbus_assert (find_handler (tree, path4, &exact_match) && !exact_match);
1327   _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
1328   _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
1329   _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
1330   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1331
1332   if (!do_register (tree, path2, TRUE, 1, tree_test_data))
1333     goto out;
1334
1335   _dbus_assert (find_subtree (tree, path1, NULL));
1336   _dbus_assert (find_subtree (tree, path2, NULL));
1337   _dbus_assert (!find_subtree (tree, path3, NULL));
1338   _dbus_assert (!find_subtree (tree, path4, NULL));
1339   _dbus_assert (!find_subtree (tree, path5, NULL));
1340   _dbus_assert (!find_subtree (tree, path6, NULL));
1341   _dbus_assert (!find_subtree (tree, path7, NULL));
1342   _dbus_assert (!find_subtree (tree, path8, NULL));
1343
1344   if (!do_register (tree, path3, TRUE, 2, tree_test_data))
1345     goto out;
1346
1347   _dbus_assert (find_subtree (tree, path1, NULL));
1348   _dbus_assert (find_subtree (tree, path2, NULL));
1349   _dbus_assert (find_subtree (tree, path3, NULL));
1350   _dbus_assert (!find_subtree (tree, path4, NULL));
1351   _dbus_assert (!find_subtree (tree, path5, NULL));
1352   _dbus_assert (!find_subtree (tree, path6, NULL));
1353   _dbus_assert (!find_subtree (tree, path7, NULL));
1354   _dbus_assert (!find_subtree (tree, path8, NULL));
1355   
1356   if (!do_register (tree, path4, TRUE, 3, tree_test_data))
1357     goto out;
1358
1359   _dbus_assert (find_subtree (tree, path1, NULL));
1360   _dbus_assert (find_subtree (tree, path2, NULL));
1361   _dbus_assert (find_subtree (tree, path3, NULL));  
1362   _dbus_assert (find_subtree (tree, path4, NULL));
1363   _dbus_assert (!find_subtree (tree, path5, NULL));
1364   _dbus_assert (!find_subtree (tree, path6, NULL));
1365   _dbus_assert (!find_subtree (tree, path7, NULL));
1366   _dbus_assert (!find_subtree (tree, path8, NULL));
1367   
1368   if (!do_register (tree, path5, TRUE, 4, tree_test_data))
1369     goto out;
1370
1371   _dbus_assert (find_subtree (tree, path1, NULL));
1372   _dbus_assert (find_subtree (tree, path2, NULL));
1373   _dbus_assert (find_subtree (tree, path3, NULL));
1374   _dbus_assert (find_subtree (tree, path4, NULL));
1375   _dbus_assert (find_subtree (tree, path5, NULL));
1376   _dbus_assert (!find_subtree (tree, path6, NULL));
1377   _dbus_assert (!find_subtree (tree, path7, NULL));
1378   _dbus_assert (!find_subtree (tree, path8, NULL));
1379   
1380   _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root &&  exact_match);
1381   _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root &&  exact_match);
1382   _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root &&  exact_match);
1383   _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root &&  exact_match);
1384   _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root &&  exact_match);
1385   _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && !exact_match);
1386   _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && !exact_match);
1387   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1388
1389   if (!do_register (tree, path6, TRUE, 5, tree_test_data))
1390     goto out;
1391
1392   _dbus_assert (find_subtree (tree, path1, NULL));
1393   _dbus_assert (find_subtree (tree, path2, NULL));
1394   _dbus_assert (find_subtree (tree, path3, NULL));
1395   _dbus_assert (find_subtree (tree, path4, NULL));
1396   _dbus_assert (find_subtree (tree, path5, NULL));
1397   _dbus_assert (find_subtree (tree, path6, NULL));
1398   _dbus_assert (!find_subtree (tree, path7, NULL));
1399   _dbus_assert (!find_subtree (tree, path8, NULL));
1400
1401   if (!do_register (tree, path7, TRUE, 6, tree_test_data))
1402     goto out;
1403
1404   _dbus_assert (find_subtree (tree, path1, NULL));
1405   _dbus_assert (find_subtree (tree, path2, NULL));
1406   _dbus_assert (find_subtree (tree, path3, NULL));
1407   _dbus_assert (find_subtree (tree, path4, NULL));
1408   _dbus_assert (find_subtree (tree, path5, NULL));
1409   _dbus_assert (find_subtree (tree, path6, NULL));
1410   _dbus_assert (find_subtree (tree, path7, NULL));
1411   _dbus_assert (!find_subtree (tree, path8, NULL));
1412
1413   if (!do_register (tree, path8, TRUE, 7, tree_test_data))
1414     goto out;
1415
1416   _dbus_assert (find_subtree (tree, path1, NULL));
1417   _dbus_assert (find_subtree (tree, path2, NULL));
1418   _dbus_assert (find_subtree (tree, path3, NULL));
1419   _dbus_assert (find_subtree (tree, path4, NULL));
1420   _dbus_assert (find_subtree (tree, path5, NULL));
1421   _dbus_assert (find_subtree (tree, path6, NULL));
1422   _dbus_assert (find_subtree (tree, path7, NULL));
1423   _dbus_assert (find_subtree (tree, path8, NULL));
1424   
1425   _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
1426   _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
1427   _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
1428   _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
1429   _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
1430   _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && exact_match);
1431   _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && exact_match);
1432   _dbus_assert (find_handler (tree, path8, &exact_match) != tree->root && exact_match);
1433   
1434   /* test the list_registered function */
1435
1436   {
1437     const char *root[] = { NULL };
1438     char **child_entries;
1439     int nb;
1440
1441     _dbus_object_tree_list_registered_unlocked (tree, path1, &child_entries);
1442     if (child_entries != NULL)
1443       {
1444         nb = string_array_length (child_entries);
1445         _dbus_assert (nb == 1);
1446         dbus_free_string_array (child_entries);
1447       }
1448
1449     _dbus_object_tree_list_registered_unlocked (tree, path2, &child_entries);
1450     if (child_entries != NULL)
1451       {
1452         nb = string_array_length (child_entries);
1453         _dbus_assert (nb == 2);
1454         dbus_free_string_array (child_entries);
1455       }
1456
1457     _dbus_object_tree_list_registered_unlocked (tree, path8, &child_entries);
1458     if (child_entries != NULL)
1459       {
1460         nb = string_array_length (child_entries);
1461         _dbus_assert (nb == 0);
1462         dbus_free_string_array (child_entries);
1463       }
1464
1465     _dbus_object_tree_list_registered_unlocked (tree, root, &child_entries);
1466     if (child_entries != NULL)
1467       {
1468         nb = string_array_length (child_entries);
1469         _dbus_assert (nb == 3);
1470         dbus_free_string_array (child_entries);
1471       }
1472   }
1473
1474   /* Check that destroying tree calls unregister funcs */
1475   _dbus_object_tree_unref (tree);
1476
1477   i = 0;
1478   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1479     {
1480       _dbus_assert (tree_test_data[i].handler_unregistered);
1481       _dbus_assert (!tree_test_data[i].message_handled);
1482       ++i;
1483     }
1484
1485   /* Now start again and try the individual unregister function */
1486   tree = _dbus_object_tree_new (NULL);
1487   if (tree == NULL)
1488     goto out;
1489
1490   if (!do_register (tree, path1, TRUE, 0, tree_test_data))
1491     goto out;
1492   if (!do_register (tree, path2, TRUE, 1, tree_test_data))
1493     goto out;
1494   if (!do_register (tree, path3, TRUE, 2, tree_test_data))
1495     goto out;
1496   if (!do_register (tree, path4, TRUE, 3, tree_test_data))
1497     goto out;
1498   if (!do_register (tree, path5, TRUE, 4, tree_test_data))
1499     goto out;
1500   if (!do_register (tree, path6, TRUE, 5, tree_test_data))
1501     goto out;
1502   if (!do_register (tree, path7, TRUE, 6, tree_test_data))
1503     goto out;
1504   if (!do_register (tree, path8, TRUE, 7, tree_test_data))
1505     goto out;
1506   
1507   _dbus_object_tree_unregister_and_unlock (tree, path1);
1508
1509   _dbus_assert (!find_subtree (tree, path1, NULL));
1510   _dbus_assert (find_subtree (tree, path2, NULL));
1511   _dbus_assert (find_subtree (tree, path3, NULL));
1512   _dbus_assert (find_subtree (tree, path4, NULL));
1513   _dbus_assert (find_subtree (tree, path5, NULL));
1514   _dbus_assert (find_subtree (tree, path6, NULL));
1515   _dbus_assert (find_subtree (tree, path7, NULL));
1516   _dbus_assert (find_subtree (tree, path8, NULL));
1517
1518   _dbus_object_tree_unregister_and_unlock (tree, path2);
1519
1520   _dbus_assert (!find_subtree (tree, path1, NULL));
1521   _dbus_assert (!find_subtree (tree, path2, NULL));
1522   _dbus_assert (find_subtree (tree, path3, NULL));
1523   _dbus_assert (find_subtree (tree, path4, NULL));
1524   _dbus_assert (find_subtree (tree, path5, NULL));
1525   _dbus_assert (find_subtree (tree, path6, NULL));
1526   _dbus_assert (find_subtree (tree, path7, NULL));
1527   _dbus_assert (find_subtree (tree, path8, NULL));
1528   
1529   _dbus_object_tree_unregister_and_unlock (tree, path3);
1530
1531   _dbus_assert (!find_subtree (tree, path1, NULL));
1532   _dbus_assert (!find_subtree (tree, path2, NULL));
1533   _dbus_assert (!find_subtree (tree, path3, NULL));
1534   _dbus_assert (find_subtree (tree, path4, NULL));
1535   _dbus_assert (find_subtree (tree, path5, NULL));
1536   _dbus_assert (find_subtree (tree, path6, NULL));
1537   _dbus_assert (find_subtree (tree, path7, NULL));
1538   _dbus_assert (find_subtree (tree, path8, NULL));
1539   
1540   _dbus_object_tree_unregister_and_unlock (tree, path4);
1541
1542   _dbus_assert (!find_subtree (tree, path1, NULL));
1543   _dbus_assert (!find_subtree (tree, path2, NULL));
1544   _dbus_assert (!find_subtree (tree, path3, NULL));
1545   _dbus_assert (!find_subtree (tree, path4, NULL));
1546   _dbus_assert (find_subtree (tree, path5, NULL));
1547   _dbus_assert (find_subtree (tree, path6, NULL));
1548   _dbus_assert (find_subtree (tree, path7, NULL));
1549   _dbus_assert (find_subtree (tree, path8, NULL));
1550   
1551   _dbus_object_tree_unregister_and_unlock (tree, path5);
1552
1553   _dbus_assert (!find_subtree (tree, path1, NULL));
1554   _dbus_assert (!find_subtree (tree, path2, NULL));
1555   _dbus_assert (!find_subtree (tree, path3, NULL));
1556   _dbus_assert (!find_subtree (tree, path4, NULL));
1557   _dbus_assert (!find_subtree (tree, path5, NULL));
1558   _dbus_assert (find_subtree (tree, path6, NULL));
1559   _dbus_assert (find_subtree (tree, path7, NULL));
1560   _dbus_assert (find_subtree (tree, path8, NULL));
1561   
1562   _dbus_object_tree_unregister_and_unlock (tree, path6);
1563
1564   _dbus_assert (!find_subtree (tree, path1, NULL));
1565   _dbus_assert (!find_subtree (tree, path2, NULL));
1566   _dbus_assert (!find_subtree (tree, path3, NULL));
1567   _dbus_assert (!find_subtree (tree, path4, NULL));
1568   _dbus_assert (!find_subtree (tree, path5, NULL));
1569   _dbus_assert (!find_subtree (tree, path6, NULL));
1570   _dbus_assert (find_subtree (tree, path7, NULL));
1571   _dbus_assert (find_subtree (tree, path8, NULL));
1572
1573   _dbus_object_tree_unregister_and_unlock (tree, path7);
1574
1575   _dbus_assert (!find_subtree (tree, path1, NULL));
1576   _dbus_assert (!find_subtree (tree, path2, NULL));
1577   _dbus_assert (!find_subtree (tree, path3, NULL));
1578   _dbus_assert (!find_subtree (tree, path4, NULL));
1579   _dbus_assert (!find_subtree (tree, path5, NULL));
1580   _dbus_assert (!find_subtree (tree, path6, NULL));
1581   _dbus_assert (!find_subtree (tree, path7, NULL));
1582   _dbus_assert (find_subtree (tree, path8, NULL));
1583
1584   _dbus_object_tree_unregister_and_unlock (tree, path8);
1585
1586   _dbus_assert (!find_subtree (tree, path1, NULL));
1587   _dbus_assert (!find_subtree (tree, path2, NULL));
1588   _dbus_assert (!find_subtree (tree, path3, NULL));
1589   _dbus_assert (!find_subtree (tree, path4, NULL));
1590   _dbus_assert (!find_subtree (tree, path5, NULL));
1591   _dbus_assert (!find_subtree (tree, path6, NULL));
1592   _dbus_assert (!find_subtree (tree, path7, NULL));
1593   _dbus_assert (!find_subtree (tree, path8, NULL));
1594   
1595   i = 0;
1596   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1597     {
1598       _dbus_assert (tree_test_data[i].handler_unregistered);
1599       _dbus_assert (!tree_test_data[i].message_handled);
1600       ++i;
1601     }
1602
1603   /* Register it all again, and test dispatch */
1604
1605   if (!do_register (tree, path1, FALSE, 0, tree_test_data))
1606     goto out;
1607   if (!do_register (tree, path2, TRUE, 1, tree_test_data))
1608     goto out;
1609   if (!do_register (tree, path3, TRUE, 2, tree_test_data))
1610     goto out;
1611   if (!do_register (tree, path4, TRUE, 3, tree_test_data))
1612     goto out;
1613   if (!do_register (tree, path5, TRUE, 4, tree_test_data))
1614     goto out;
1615   if (!do_register (tree, path6, FALSE, 5, tree_test_data))
1616     goto out;
1617   if (!do_register (tree, path7, TRUE, 6, tree_test_data))
1618     goto out;
1619   if (!do_register (tree, path8, TRUE, 7, tree_test_data))
1620     goto out;
1621
1622 #if 0
1623   spew_tree (tree);
1624 #endif
1625   
1626   if (!do_test_dispatch (tree, path1, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1627     goto out;
1628   if (!do_test_dispatch (tree, path2, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1629     goto out;
1630   if (!do_test_dispatch (tree, path3, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1631     goto out;
1632   if (!do_test_dispatch (tree, path4, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1633     goto out;
1634   if (!do_test_dispatch (tree, path5, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1635     goto out;
1636   if (!do_test_dispatch (tree, path6, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1637     goto out;
1638   if (!do_test_dispatch (tree, path7, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1639     goto out;
1640   if (!do_test_dispatch (tree, path8, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1641     goto out;
1642   
1643  out:
1644   if (tree)
1645     {
1646       /* test ref */
1647       _dbus_object_tree_ref (tree);
1648       _dbus_object_tree_unref (tree);
1649       _dbus_object_tree_unref (tree);
1650     }
1651
1652   return TRUE;
1653 }
1654
1655 /**
1656  * @ingroup DBusObjectTree
1657  * Unit test for DBusObjectTree
1658  * @returns #TRUE on success.
1659  */
1660 dbus_bool_t
1661 _dbus_object_tree_test (void)
1662 {
1663   _dbus_test_oom_handling ("object tree",
1664                            object_tree_test_iteration,
1665                            NULL);
1666
1667   return TRUE;
1668 }
1669
1670 #endif /* DBUS_BUILD_TESTS */