2005-01-30 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, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
634     goto out;
635   
636   if (!_dbus_string_append (&xml, "<node>\n"))
637     goto out;
638
639   i = 0;
640   while (children[i] != NULL)
641     {
642       if (!_dbus_string_append_printf (&xml, "  <node name=\"%s\"/>\n",
643                                        children[i]))
644         goto out;
645
646       ++i;
647     }
648
649   if (!_dbus_string_append (&xml, "</node>\n"))
650     goto out;
651   
652   result = DBUS_HANDLER_RESULT_HANDLED;
653   
654  out:
655   _dbus_string_free (&xml);
656   dbus_free_string_array (children);
657   
658   return result;
659 }
660
661 /**
662  * Tries to dispatch a message by directing it to handler for the
663  * object path listed in the message header, if any. Messages are
664  * dispatched first to the registered handler that matches the largest
665  * number of path elements; that is, message to /foo/bar/baz would go
666  * to the handler for /foo/bar before the one for /foo.
667  *
668  * @todo thread problems
669  *
670  * @param tree the global object tree
671  * @param message the message to dispatch
672  * @returns whether message was handled successfully
673  */
674 DBusHandlerResult
675 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
676                                        DBusMessage             *message)
677 {
678   char **path;
679   dbus_bool_t exact_match;
680   DBusList *list;
681   DBusList *link;
682   DBusHandlerResult result;
683   DBusObjectSubtree *subtree;
684   
685 #if 0
686   _dbus_verbose ("Dispatch of message by object path\n");
687 #endif
688   
689   path = NULL;
690   if (!dbus_message_get_path_decomposed (message, &path))
691     {
692 #ifdef DBUS_BUILD_TESTS
693       if (tree->connection)
694 #endif
695         _dbus_connection_unlock (tree->connection);
696       
697       _dbus_verbose ("No memory to get decomposed path\n");
698
699       return DBUS_HANDLER_RESULT_NEED_MEMORY;
700     }
701
702   if (path == NULL)
703     {
704 #ifdef DBUS_BUILD_TESTS
705       if (tree->connection)
706 #endif
707         _dbus_connection_unlock (tree->connection);
708       
709       _dbus_verbose ("No path field in message\n");
710       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
711     }
712   
713   /* Find the deepest path that covers the path in the message */
714   subtree = find_handler (tree, (const char**) path, &exact_match);
715   
716   /* Build a list of all paths that cover the path in the message */
717
718   list = NULL;
719
720   while (subtree != NULL)
721     {
722       if (subtree->message_function != NULL && (exact_match || subtree->invoke_as_fallback))
723         {
724           _dbus_object_subtree_ref (subtree);
725
726           /* run deepest paths first */
727           if (!_dbus_list_append (&list, subtree))
728             {
729               result = DBUS_HANDLER_RESULT_NEED_MEMORY;
730               _dbus_object_subtree_unref (subtree);
731               goto free_and_return;
732             }
733         }
734
735       exact_match = FALSE;
736       subtree = subtree->parent;
737     }
738
739   _dbus_verbose ("%d handlers in the path tree for this message\n",
740                  _dbus_list_get_length (&list));
741
742   /* Invoke each handler in the list */
743
744   result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
745
746   link = _dbus_list_get_first_link (&list);
747   while (link != NULL)
748     {
749       DBusList *next = _dbus_list_get_next_link (&list, link);
750       subtree = link->data;
751
752       /* message_function is NULL if we're unregistered
753        * due to reentrancy
754        */
755       if (subtree->message_function)
756         {
757           DBusObjectPathMessageFunction message_function;
758           void *user_data;
759
760           message_function = subtree->message_function;
761           user_data = subtree->user_data;
762
763 #if 0
764           _dbus_verbose ("  (invoking a handler)\n");
765 #endif
766           
767 #ifdef DBUS_BUILD_TESTS
768           if (tree->connection)
769 #endif
770             _dbus_connection_unlock (tree->connection);
771
772           /* FIXME you could unregister the subtree in another thread
773            * before we invoke the callback, and I can't figure out a
774            * good way to solve this.
775            */
776
777           result = (* message_function) (tree->connection,
778                                          message,
779                                          user_data);
780
781 #ifdef DBUS_BUILD_TESTS
782           if (tree->connection)
783 #endif
784             _dbus_connection_lock (tree->connection);
785
786           if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
787             goto free_and_return;
788         }
789
790       link = next;
791     }
792
793  free_and_return:
794
795   if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
796     {
797       /* This hardcoded default handler does a minimal Introspect()
798        */
799       result = handle_default_introspect_unlocked (tree, message,
800                                                    (const char**) path);
801     }
802
803 #ifdef DBUS_BUILD_TESTS
804   if (tree->connection)
805 #endif
806     _dbus_connection_unlock (tree->connection);
807   
808   while (list != NULL)
809     {
810       link = _dbus_list_get_first_link (&list);
811       _dbus_object_subtree_unref (link->data);
812       _dbus_list_remove_link (&list, link);
813     }
814   
815   dbus_free_string_array (path);
816
817   return result;
818 }
819
820 /**
821  * Allocates a subtree object.
822  *
823  * @param name name to duplicate.
824  * @returns newly-allocated subtree
825  */
826 static DBusObjectSubtree*
827 allocate_subtree_object (const char *name)
828 {
829   int len;
830   DBusObjectSubtree *subtree;
831   const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
832
833   _dbus_assert (name != NULL);
834
835   len = strlen (name);
836
837   subtree = dbus_malloc (front_padding + (len + 1));
838
839   if (subtree == NULL)
840     return NULL;
841
842   memcpy (subtree->name, name, len + 1);
843
844   return subtree;
845 }
846
847 static DBusObjectSubtree*
848 _dbus_object_subtree_new (const char                  *name,
849                           const DBusObjectPathVTable  *vtable,
850                           void                        *user_data)
851 {
852   DBusObjectSubtree *subtree;
853
854   subtree = allocate_subtree_object (name);
855   if (subtree == NULL)
856     goto oom;
857
858   _dbus_assert (name != NULL);
859
860   subtree->parent = NULL;
861
862   if (vtable)
863     {
864       subtree->message_function = vtable->message_function;
865       subtree->unregister_function = vtable->unregister_function;
866     }
867   else
868     {
869       subtree->message_function = NULL;
870       subtree->unregister_function = NULL;
871     }
872
873   subtree->user_data = user_data;
874   subtree->refcount.value = 1;
875   subtree->subtrees = NULL;
876   subtree->n_subtrees = 0;
877   subtree->subtrees_sorted = TRUE;
878   subtree->invoke_as_fallback = FALSE;
879
880   return subtree;
881
882  oom:
883   if (subtree)
884     {
885       dbus_free (subtree);
886     }
887
888   return NULL;
889 }
890
891 static DBusObjectSubtree *
892 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
893 {
894   _dbus_assert (subtree->refcount.value > 0);
895   _dbus_atomic_inc (&subtree->refcount);
896
897   return subtree;
898 }
899
900 static void
901 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
902 {
903   _dbus_assert (subtree->refcount.value > 0);
904
905   if (_dbus_atomic_dec (&subtree->refcount) == 1)
906     {
907       _dbus_assert (subtree->unregister_function == NULL);
908       _dbus_assert (subtree->message_function == NULL);
909
910       dbus_free (subtree->subtrees);
911       dbus_free (subtree);
912     }
913 }
914
915 /**
916  * Lists the registered fallback handlers and object path handlers at
917  * the given parent_path. The returned array should be freed with
918  * dbus_free_string_array().
919  *
920  * @param tree the object tree
921  * @param parent_path the path to list the child handlers of
922  * @param child_entries returns #NULL-terminated array of children
923  * @returns #FALSE if no memory to allocate the child entries
924  */
925 dbus_bool_t
926 _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
927                                               const char    **parent_path,
928                                               char         ***child_entries)
929 {
930   dbus_bool_t result;
931
932   result = _dbus_object_tree_list_registered_unlocked (tree,
933                                                        parent_path,
934                                                        child_entries);
935   
936 #ifdef DBUS_BUILD_TESTS
937   if (tree->connection)
938 #endif
939     _dbus_connection_unlock (tree->connection);
940
941   return result;
942 }
943
944
945 /** Set to 1 to get a bunch of spew about disassembling the path string */
946 #define VERBOSE_DECOMPOSE 0
947
948 /**
949  * Decompose an object path.  A path of just "/" is
950  * represented as an empty vector of strings.
951  * The path need not be nul terminated.
952  * 
953  * @param data the path data
954  * @param len  the length of the path string
955  * @param path address to store new object path
956  * @param path_len length of stored path
957  */
958 dbus_bool_t
959 _dbus_decompose_path (const char*     data,
960                       int             len,
961                       char         ***path,
962                       int            *path_len)
963 {
964   char **retval;
965   int n_components;
966   int i, j, comp;
967
968   _dbus_assert (data != NULL);
969   
970 #if VERBOSE_DECOMPOSE
971   _dbus_verbose ("Decomposing path \"%s\"\n",
972                  data);
973 #endif
974   
975   n_components = 0;
976   if (len > 1) /* if path is not just "/" */
977     {
978       i = 0;
979       while (i < len)
980         {
981           if (data[i] == '/')
982             n_components += 1;
983           ++i;
984         }
985     }
986   
987   retval = dbus_new0 (char*, n_components + 1);
988
989   if (retval == NULL)
990     return FALSE;
991
992   comp = 0;
993   if (n_components == 0)
994     i = 1;
995   else
996     i = 0;
997   while (comp < n_components)
998     {
999       _dbus_assert (i < len);
1000       
1001       if (data[i] == '/')
1002         ++i;
1003       j = i;
1004
1005       while (j < len && data[j] != '/')
1006         ++j;
1007
1008       /* Now [i, j) is the path component */
1009       _dbus_assert (i < j);
1010       _dbus_assert (data[i] != '/');
1011       _dbus_assert (j == len || data[j] == '/');
1012
1013 #if VERBOSE_DECOMPOSE
1014       _dbus_verbose ("  (component in [%d,%d))\n",
1015                      i, j);
1016 #endif
1017       
1018       retval[comp] = _dbus_memdup (&data[i], j - i + 1);
1019       if (retval[comp] == NULL)
1020         {
1021           dbus_free_string_array (retval);
1022           return FALSE;
1023         }
1024       retval[comp][j-i] = '\0';
1025 #if VERBOSE_DECOMPOSE
1026       _dbus_verbose ("  (component %d = \"%s\")\n",
1027                      comp, retval[comp]);
1028 #endif
1029
1030       ++comp;
1031       i = j;
1032     }
1033   _dbus_assert (i == len);
1034   
1035   *path = retval;
1036   if (path_len)
1037     *path_len = n_components;
1038   
1039   return TRUE;
1040 }
1041
1042 /** @} */
1043
1044 #ifdef DBUS_BUILD_TESTS
1045 #include "dbus-test.h"
1046 #include <stdio.h>
1047
1048 static char*
1049 flatten_path (const char **path)
1050 {
1051   DBusString str;
1052   char *s;
1053
1054   if (!_dbus_string_init (&str))
1055     return NULL;
1056
1057   if (path[0] == NULL)
1058     {
1059       if (!_dbus_string_append_byte (&str, '/'))
1060         goto nomem;
1061     }
1062   else
1063     {
1064       int i;
1065       
1066       i = 0;
1067       while (path[i])
1068         {
1069           if (!_dbus_string_append_byte (&str, '/'))
1070             goto nomem;
1071           
1072           if (!_dbus_string_append (&str, path[i]))
1073             goto nomem;
1074           
1075           ++i;
1076         }
1077     }
1078
1079   if (!_dbus_string_steal_data (&str, &s))
1080     goto nomem;
1081
1082   _dbus_string_free (&str);
1083
1084   return s;
1085
1086  nomem:
1087   _dbus_string_free (&str);
1088   return NULL;
1089 }
1090
1091
1092 typedef enum 
1093 {
1094   STR_EQUAL,
1095   STR_PREFIX,
1096   STR_DIFFERENT
1097 } StrComparison;
1098
1099 /* Returns TRUE if container is a parent of child
1100  */
1101 static StrComparison
1102 path_contains (const char **container,
1103                const char **child)
1104 {
1105   int i;
1106
1107   i = 0;
1108   while (child[i] != NULL)
1109     {
1110       int v;
1111
1112       if (container[i] == NULL)
1113         return STR_PREFIX; /* container ran out, child continues;
1114                         * thus the container is a parent of the
1115                         * child.
1116                         */
1117
1118       _dbus_assert (container[i] != NULL);
1119       _dbus_assert (child[i] != NULL);
1120
1121       v = strcmp (container[i], child[i]);
1122
1123       if (v != 0)
1124         return STR_DIFFERENT; /* they overlap until here and then are different,
1125                            * not overlapping
1126                            */
1127
1128       ++i;
1129     }
1130
1131   /* Child ran out; if container also did, they are equal;
1132    * otherwise, the child is a parent of the container.
1133    */
1134   if (container[i] == NULL)
1135     return STR_EQUAL;
1136   else
1137     return STR_DIFFERENT;
1138 }
1139
1140 #if 0
1141 static void
1142 spew_subtree_recurse (DBusObjectSubtree *subtree,
1143                       int                indent)
1144 {
1145   int i;
1146
1147   i = 0;
1148   while (i < indent)
1149     {
1150       _dbus_verbose (" ");
1151       ++i;
1152     }
1153
1154   _dbus_verbose ("%s (%d children)\n",
1155                  subtree->name, subtree->n_subtrees);
1156
1157   i = 0;
1158   while (i < subtree->n_subtrees)
1159     {
1160       spew_subtree_recurse (subtree->subtrees[i], indent + 2);
1161
1162       ++i;
1163     }
1164 }
1165
1166 static void
1167 spew_tree (DBusObjectTree *tree)
1168 {
1169   spew_subtree_recurse (tree->root, 0);
1170 }
1171 #endif
1172
1173 /**
1174  * Callback data used in tests
1175  */
1176 typedef struct
1177 {
1178   const char **path; /**< Path */
1179   dbus_bool_t handler_fallback; /**< true if the handler may be called as fallback */
1180   dbus_bool_t message_handled; /**< Gets set to true if message handler called */
1181   dbus_bool_t handler_unregistered; /**< gets set to true if handler is unregistered */
1182 } TreeTestData;
1183
1184
1185 static void
1186 test_unregister_function (DBusConnection  *connection,
1187                           void            *user_data)
1188 {
1189   TreeTestData *ttd = user_data;
1190
1191   ttd->handler_unregistered = TRUE;
1192 }
1193
1194 static DBusHandlerResult
1195 test_message_function (DBusConnection  *connection,
1196                        DBusMessage     *message,
1197                        void            *user_data)
1198 {
1199   TreeTestData *ttd = user_data;
1200
1201   ttd->message_handled = TRUE;
1202
1203   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1204 }
1205
1206 static dbus_bool_t
1207 do_register (DBusObjectTree *tree,
1208              const char    **path,
1209              dbus_bool_t     fallback,
1210              int             i,
1211              TreeTestData   *tree_test_data)
1212 {
1213   DBusObjectPathVTable vtable = { test_unregister_function,
1214                                   test_message_function, NULL };
1215   
1216   tree_test_data[i].message_handled = FALSE;
1217   tree_test_data[i].handler_unregistered = FALSE;
1218   tree_test_data[i].handler_fallback = fallback;
1219   tree_test_data[i].path = path;
1220
1221   if (!_dbus_object_tree_register (tree, fallback, path,
1222                                    &vtable,
1223                                    &tree_test_data[i]))
1224     return FALSE;
1225
1226   return TRUE;
1227 }
1228
1229 static dbus_bool_t
1230 do_test_dispatch (DBusObjectTree *tree,
1231                   const char    **path,
1232                   int             i,
1233                   TreeTestData   *tree_test_data,
1234                   int             n_test_data)
1235 {
1236   DBusMessage *message;
1237   int j;
1238   DBusHandlerResult result;
1239   char *flat;
1240
1241   message = NULL;
1242   
1243   flat = flatten_path (path);
1244   if (flat == NULL)
1245     goto oom;
1246
1247   message = dbus_message_new_method_call (NULL,
1248                                           flat,
1249                                           "org.freedesktop.TestInterface",
1250                                           "Foo");
1251   dbus_free (flat);
1252   if (message == NULL)
1253     goto oom;
1254
1255   j = 0;
1256   while (j < n_test_data)
1257     {
1258       tree_test_data[j].message_handled = FALSE;
1259       ++j;
1260     }
1261
1262   result = _dbus_object_tree_dispatch_and_unlock (tree, message);
1263   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
1264     goto oom;
1265
1266   _dbus_assert (tree_test_data[i].message_handled);
1267
1268   j = 0;
1269   while (j < n_test_data)
1270     {
1271       if (tree_test_data[j].message_handled)
1272         {
1273           if (tree_test_data[j].handler_fallback)
1274             _dbus_assert (path_contains (tree_test_data[j].path,
1275                                          path) != STR_DIFFERENT);
1276           else
1277             _dbus_assert (path_contains (tree_test_data[j].path, path) == STR_EQUAL);
1278         }
1279       else
1280         {
1281           if (tree_test_data[j].handler_fallback)
1282             _dbus_assert (path_contains (tree_test_data[j].path,
1283                                          path) == STR_DIFFERENT);
1284           else
1285             _dbus_assert (path_contains (tree_test_data[j].path, path) != STR_EQUAL);
1286         }
1287
1288       ++j;
1289     }
1290
1291   dbus_message_unref (message);
1292
1293   return TRUE;
1294
1295  oom:
1296   if (message)
1297     dbus_message_unref (message);
1298   return FALSE;
1299 }
1300
1301 static size_t
1302 string_array_length (const char **array)
1303 {
1304   size_t i;
1305   for (i = 0; array[i]; i++) ;
1306   return i;
1307 }
1308
1309 typedef struct
1310 {
1311   const char *path;
1312   const char *result[20];
1313 } DecomposePathTest;
1314
1315 static DecomposePathTest decompose_tests[] = {
1316   { "/foo", { "foo", NULL } },
1317   { "/foo/bar", { "foo", "bar", NULL } },
1318   { "/", { NULL } },
1319   { "/a/b", { "a", "b", NULL } },
1320   { "/a/b/c", { "a", "b", "c", NULL } },
1321   { "/a/b/c/d", { "a", "b", "c", "d", NULL } },
1322   { "/foo/bar/q", { "foo", "bar", "q", NULL } },
1323   { "/foo/bar/this/is/longer", { "foo", "bar", "this", "is", "longer", NULL } }
1324 };
1325
1326 static dbus_bool_t
1327 run_decompose_tests (void)
1328 {
1329   int i;
1330
1331   i = 0;
1332   while (i < _DBUS_N_ELEMENTS (decompose_tests))
1333     {
1334       char **result;
1335       int    result_len;
1336       int    expected_len;
1337
1338       if (!_dbus_decompose_path (decompose_tests[i].path,
1339                                  strlen (decompose_tests[i].path),
1340                                  &result, &result_len))
1341         return FALSE;
1342
1343       expected_len = string_array_length (decompose_tests[i].result);
1344       
1345       if (result_len != (int) string_array_length ((const char**)result) ||
1346           expected_len != result_len ||
1347           path_contains (decompose_tests[i].result,
1348                          (const char**) result) != STR_EQUAL)
1349         {
1350           int real_len = string_array_length ((const char**)result);
1351           _dbus_warn ("Expected decompose of %s to have len %d, returned %d, appears to have %d\n",
1352                       decompose_tests[i].path, expected_len, result_len,
1353                       real_len);
1354           _dbus_warn ("Decompose resulted in elements: { ");
1355           i = 0;
1356           while (i < real_len)
1357             {
1358               _dbus_warn ("\"%s\"%s", result[i],
1359                           (i + 1) == real_len ? "" : ", ");
1360               ++i;
1361             }
1362           _dbus_warn ("}\n");
1363           _dbus_assert_not_reached ("path decompose failed\n");
1364         }
1365
1366       dbus_free_string_array (result);
1367
1368       ++i;
1369     }
1370   
1371   return TRUE;
1372 }
1373
1374 static dbus_bool_t
1375 object_tree_test_iteration (void *data)
1376 {
1377   const char *path0[] = { NULL };
1378   const char *path1[] = { "foo", NULL };
1379   const char *path2[] = { "foo", "bar", NULL };
1380   const char *path3[] = { "foo", "bar", "baz", NULL };
1381   const char *path4[] = { "foo", "bar", "boo", NULL };
1382   const char *path5[] = { "blah", NULL };
1383   const char *path6[] = { "blah", "boof", NULL };
1384   const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
1385   const char *path8[] = { "childless", NULL };
1386   DBusObjectTree *tree;
1387   TreeTestData tree_test_data[9];
1388   int i;
1389   dbus_bool_t exact_match;
1390
1391   if (!run_decompose_tests ())
1392     return FALSE;
1393   
1394   tree = NULL;
1395
1396   tree = _dbus_object_tree_new (NULL);
1397   if (tree == NULL)
1398     goto out;
1399
1400   if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1401     goto out;
1402
1403   _dbus_assert (find_subtree (tree, path0, NULL));
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   _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
1414   _dbus_assert (find_handler (tree, path1, &exact_match) == tree->root && !exact_match);
1415   _dbus_assert (find_handler (tree, path2, &exact_match) == tree->root && !exact_match);
1416   _dbus_assert (find_handler (tree, path3, &exact_match) == tree->root && !exact_match);
1417   _dbus_assert (find_handler (tree, path4, &exact_match) == tree->root && !exact_match);
1418   _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
1419   _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
1420   _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
1421   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1422   
1423   if (!do_register (tree, path1, TRUE, 1, tree_test_data))
1424     goto out;
1425
1426   _dbus_assert (find_subtree (tree, path0, NULL));
1427   _dbus_assert (find_subtree (tree, path1, NULL));
1428   _dbus_assert (!find_subtree (tree, path2, NULL));
1429   _dbus_assert (!find_subtree (tree, path3, NULL));
1430   _dbus_assert (!find_subtree (tree, path4, NULL));
1431   _dbus_assert (!find_subtree (tree, path5, NULL));
1432   _dbus_assert (!find_subtree (tree, path6, NULL));
1433   _dbus_assert (!find_subtree (tree, path7, NULL));
1434   _dbus_assert (!find_subtree (tree, path8, NULL));
1435
1436   _dbus_assert (find_handler (tree, path0, &exact_match) &&  exact_match);
1437   _dbus_assert (find_handler (tree, path1, &exact_match) &&  exact_match);
1438   _dbus_assert (find_handler (tree, path2, &exact_match) && !exact_match);
1439   _dbus_assert (find_handler (tree, path3, &exact_match) && !exact_match);
1440   _dbus_assert (find_handler (tree, path4, &exact_match) && !exact_match);
1441   _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
1442   _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
1443   _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
1444   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1445
1446   if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1447     goto out;
1448
1449   _dbus_assert (find_subtree (tree, path1, NULL));
1450   _dbus_assert (find_subtree (tree, path2, NULL));
1451   _dbus_assert (!find_subtree (tree, path3, NULL));
1452   _dbus_assert (!find_subtree (tree, path4, NULL));
1453   _dbus_assert (!find_subtree (tree, path5, NULL));
1454   _dbus_assert (!find_subtree (tree, path6, NULL));
1455   _dbus_assert (!find_subtree (tree, path7, NULL));
1456   _dbus_assert (!find_subtree (tree, path8, NULL));
1457
1458   if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1459     goto out;
1460
1461   _dbus_assert (find_subtree (tree, path0, NULL));
1462   _dbus_assert (find_subtree (tree, path1, NULL));
1463   _dbus_assert (find_subtree (tree, path2, NULL));
1464   _dbus_assert (find_subtree (tree, path3, NULL));
1465   _dbus_assert (!find_subtree (tree, path4, NULL));
1466   _dbus_assert (!find_subtree (tree, path5, NULL));
1467   _dbus_assert (!find_subtree (tree, path6, NULL));
1468   _dbus_assert (!find_subtree (tree, path7, NULL));
1469   _dbus_assert (!find_subtree (tree, path8, NULL));
1470   
1471   if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1472     goto out;
1473
1474   _dbus_assert (find_subtree (tree, path0, NULL));
1475   _dbus_assert (find_subtree (tree, path1, NULL));
1476   _dbus_assert (find_subtree (tree, path2, NULL));
1477   _dbus_assert (find_subtree (tree, path3, NULL));  
1478   _dbus_assert (find_subtree (tree, path4, NULL));
1479   _dbus_assert (!find_subtree (tree, path5, NULL));
1480   _dbus_assert (!find_subtree (tree, path6, NULL));
1481   _dbus_assert (!find_subtree (tree, path7, NULL));
1482   _dbus_assert (!find_subtree (tree, path8, NULL));
1483   
1484   if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1485     goto out;
1486
1487   _dbus_assert (find_subtree (tree, path0, NULL));
1488   _dbus_assert (find_subtree (tree, path1, NULL));
1489   _dbus_assert (find_subtree (tree, path2, NULL));
1490   _dbus_assert (find_subtree (tree, path3, NULL));
1491   _dbus_assert (find_subtree (tree, path4, NULL));
1492   _dbus_assert (find_subtree (tree, path5, NULL));
1493   _dbus_assert (!find_subtree (tree, path6, NULL));
1494   _dbus_assert (!find_subtree (tree, path7, NULL));
1495   _dbus_assert (!find_subtree (tree, path8, NULL));
1496
1497   _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root &&  exact_match);
1498   _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root &&  exact_match);
1499   _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root &&  exact_match);
1500   _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root &&  exact_match);
1501   _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root &&  exact_match);
1502   _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root &&  exact_match);
1503   _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && !exact_match);
1504   _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && !exact_match);
1505   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1506
1507   if (!do_register (tree, path6, TRUE, 6, tree_test_data))
1508     goto out;
1509
1510   _dbus_assert (find_subtree (tree, path0, NULL));
1511   _dbus_assert (find_subtree (tree, path1, NULL));
1512   _dbus_assert (find_subtree (tree, path2, NULL));
1513   _dbus_assert (find_subtree (tree, path3, NULL));
1514   _dbus_assert (find_subtree (tree, path4, NULL));
1515   _dbus_assert (find_subtree (tree, path5, NULL));
1516   _dbus_assert (find_subtree (tree, path6, NULL));
1517   _dbus_assert (!find_subtree (tree, path7, NULL));
1518   _dbus_assert (!find_subtree (tree, path8, NULL));
1519
1520   if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1521     goto out;
1522
1523   _dbus_assert (find_subtree (tree, path0, NULL));
1524   _dbus_assert (find_subtree (tree, path1, NULL));
1525   _dbus_assert (find_subtree (tree, path2, NULL));
1526   _dbus_assert (find_subtree (tree, path3, NULL));
1527   _dbus_assert (find_subtree (tree, path4, NULL));
1528   _dbus_assert (find_subtree (tree, path5, NULL));
1529   _dbus_assert (find_subtree (tree, path6, NULL));
1530   _dbus_assert (find_subtree (tree, path7, NULL));
1531   _dbus_assert (!find_subtree (tree, path8, NULL));
1532
1533   if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1534     goto out;
1535
1536   _dbus_assert (find_subtree (tree, path0, NULL));
1537   _dbus_assert (find_subtree (tree, path1, NULL));
1538   _dbus_assert (find_subtree (tree, path2, NULL));
1539   _dbus_assert (find_subtree (tree, path3, NULL));
1540   _dbus_assert (find_subtree (tree, path4, NULL));
1541   _dbus_assert (find_subtree (tree, path5, NULL));
1542   _dbus_assert (find_subtree (tree, path6, NULL));
1543   _dbus_assert (find_subtree (tree, path7, NULL));
1544   _dbus_assert (find_subtree (tree, path8, NULL));
1545
1546   _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root &&  exact_match);
1547   _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
1548   _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
1549   _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
1550   _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
1551   _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
1552   _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && exact_match);
1553   _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && exact_match);
1554   _dbus_assert (find_handler (tree, path8, &exact_match) != tree->root && exact_match);
1555   
1556   /* test the list_registered function */
1557
1558   {
1559     const char *root[] = { NULL };
1560     char **child_entries;
1561     int nb;
1562
1563     _dbus_object_tree_list_registered_unlocked (tree, path1, &child_entries);
1564     if (child_entries != NULL)
1565       {
1566         nb = string_array_length ((const char**)child_entries);
1567         _dbus_assert (nb == 1);
1568         dbus_free_string_array (child_entries);
1569       }
1570
1571     _dbus_object_tree_list_registered_unlocked (tree, path2, &child_entries);
1572     if (child_entries != NULL)
1573       {
1574         nb = string_array_length ((const char**)child_entries);
1575         _dbus_assert (nb == 2);
1576         dbus_free_string_array (child_entries);
1577       }
1578
1579     _dbus_object_tree_list_registered_unlocked (tree, path8, &child_entries);
1580     if (child_entries != NULL)
1581       {
1582         nb = string_array_length ((const char**)child_entries);
1583         _dbus_assert (nb == 0);
1584         dbus_free_string_array (child_entries);
1585       }
1586
1587     _dbus_object_tree_list_registered_unlocked (tree, root, &child_entries);
1588     if (child_entries != NULL)
1589       {
1590         nb = string_array_length ((const char**)child_entries);
1591         _dbus_assert (nb == 3);
1592         dbus_free_string_array (child_entries);
1593       }
1594   }
1595
1596   /* Check that destroying tree calls unregister funcs */
1597   _dbus_object_tree_unref (tree);
1598
1599   i = 0;
1600   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1601     {
1602       _dbus_assert (tree_test_data[i].handler_unregistered);
1603       _dbus_assert (!tree_test_data[i].message_handled);
1604       ++i;
1605     }
1606
1607   /* Now start again and try the individual unregister function */
1608   tree = _dbus_object_tree_new (NULL);
1609   if (tree == NULL)
1610     goto out;
1611
1612   if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1613     goto out;
1614   if (!do_register (tree, path1, TRUE, 1, tree_test_data))
1615     goto out;
1616   if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1617     goto out;
1618   if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1619     goto out;
1620   if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1621     goto out;
1622   if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1623     goto out;
1624   if (!do_register (tree, path6, TRUE, 6, tree_test_data))
1625     goto out;
1626   if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1627     goto out;
1628   if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1629     goto out;
1630
1631   _dbus_object_tree_unregister_and_unlock (tree, path0);
1632
1633   _dbus_assert (!find_subtree (tree, path0, NULL));
1634   _dbus_assert (find_subtree (tree, path1, NULL));
1635   _dbus_assert (find_subtree (tree, path2, NULL));
1636   _dbus_assert (find_subtree (tree, path3, NULL));
1637   _dbus_assert (find_subtree (tree, path4, NULL));
1638   _dbus_assert (find_subtree (tree, path5, NULL));
1639   _dbus_assert (find_subtree (tree, path6, NULL));
1640   _dbus_assert (find_subtree (tree, path7, NULL));
1641   _dbus_assert (find_subtree (tree, path8, NULL));
1642   
1643   _dbus_object_tree_unregister_and_unlock (tree, path1);
1644
1645   _dbus_assert (!find_subtree (tree, path0, NULL));
1646   _dbus_assert (!find_subtree (tree, path1, NULL));
1647   _dbus_assert (find_subtree (tree, path2, NULL));
1648   _dbus_assert (find_subtree (tree, path3, NULL));
1649   _dbus_assert (find_subtree (tree, path4, NULL));
1650   _dbus_assert (find_subtree (tree, path5, NULL));
1651   _dbus_assert (find_subtree (tree, path6, NULL));
1652   _dbus_assert (find_subtree (tree, path7, NULL));
1653   _dbus_assert (find_subtree (tree, path8, NULL));
1654
1655   _dbus_object_tree_unregister_and_unlock (tree, path2);
1656
1657   _dbus_assert (!find_subtree (tree, path0, NULL));
1658   _dbus_assert (!find_subtree (tree, path1, NULL));
1659   _dbus_assert (!find_subtree (tree, path2, NULL));
1660   _dbus_assert (find_subtree (tree, path3, NULL));
1661   _dbus_assert (find_subtree (tree, path4, NULL));
1662   _dbus_assert (find_subtree (tree, path5, NULL));
1663   _dbus_assert (find_subtree (tree, path6, NULL));
1664   _dbus_assert (find_subtree (tree, path7, NULL));
1665   _dbus_assert (find_subtree (tree, path8, NULL));
1666   
1667   _dbus_object_tree_unregister_and_unlock (tree, path3);
1668
1669   _dbus_assert (!find_subtree (tree, path0, NULL));
1670   _dbus_assert (!find_subtree (tree, path1, NULL));
1671   _dbus_assert (!find_subtree (tree, path2, NULL));
1672   _dbus_assert (!find_subtree (tree, path3, NULL));
1673   _dbus_assert (find_subtree (tree, path4, NULL));
1674   _dbus_assert (find_subtree (tree, path5, NULL));
1675   _dbus_assert (find_subtree (tree, path6, NULL));
1676   _dbus_assert (find_subtree (tree, path7, NULL));
1677   _dbus_assert (find_subtree (tree, path8, NULL));
1678   
1679   _dbus_object_tree_unregister_and_unlock (tree, path4);
1680
1681   _dbus_assert (!find_subtree (tree, path0, NULL));
1682   _dbus_assert (!find_subtree (tree, path1, NULL));
1683   _dbus_assert (!find_subtree (tree, path2, NULL));
1684   _dbus_assert (!find_subtree (tree, path3, NULL));
1685   _dbus_assert (!find_subtree (tree, path4, NULL));
1686   _dbus_assert (find_subtree (tree, path5, NULL));
1687   _dbus_assert (find_subtree (tree, path6, NULL));
1688   _dbus_assert (find_subtree (tree, path7, NULL));
1689   _dbus_assert (find_subtree (tree, path8, NULL));
1690   
1691   _dbus_object_tree_unregister_and_unlock (tree, path5);
1692
1693   _dbus_assert (!find_subtree (tree, path0, NULL));
1694   _dbus_assert (!find_subtree (tree, path1, NULL));
1695   _dbus_assert (!find_subtree (tree, path2, NULL));
1696   _dbus_assert (!find_subtree (tree, path3, NULL));
1697   _dbus_assert (!find_subtree (tree, path4, NULL));
1698   _dbus_assert (!find_subtree (tree, path5, NULL));
1699   _dbus_assert (find_subtree (tree, path6, NULL));
1700   _dbus_assert (find_subtree (tree, path7, NULL));
1701   _dbus_assert (find_subtree (tree, path8, NULL));
1702   
1703   _dbus_object_tree_unregister_and_unlock (tree, path6);
1704
1705   _dbus_assert (!find_subtree (tree, path0, NULL));
1706   _dbus_assert (!find_subtree (tree, path1, NULL));
1707   _dbus_assert (!find_subtree (tree, path2, NULL));
1708   _dbus_assert (!find_subtree (tree, path3, NULL));
1709   _dbus_assert (!find_subtree (tree, path4, NULL));
1710   _dbus_assert (!find_subtree (tree, path5, NULL));
1711   _dbus_assert (!find_subtree (tree, path6, NULL));
1712   _dbus_assert (find_subtree (tree, path7, NULL));
1713   _dbus_assert (find_subtree (tree, path8, NULL));
1714
1715   _dbus_object_tree_unregister_and_unlock (tree, path7);
1716
1717   _dbus_assert (!find_subtree (tree, path0, NULL));
1718   _dbus_assert (!find_subtree (tree, path1, NULL));
1719   _dbus_assert (!find_subtree (tree, path2, NULL));
1720   _dbus_assert (!find_subtree (tree, path3, NULL));
1721   _dbus_assert (!find_subtree (tree, path4, NULL));
1722   _dbus_assert (!find_subtree (tree, path5, NULL));
1723   _dbus_assert (!find_subtree (tree, path6, NULL));
1724   _dbus_assert (!find_subtree (tree, path7, NULL));
1725   _dbus_assert (find_subtree (tree, path8, NULL));
1726
1727   _dbus_object_tree_unregister_and_unlock (tree, path8);
1728
1729   _dbus_assert (!find_subtree (tree, path0, NULL));
1730   _dbus_assert (!find_subtree (tree, path1, NULL));
1731   _dbus_assert (!find_subtree (tree, path2, NULL));
1732   _dbus_assert (!find_subtree (tree, path3, NULL));
1733   _dbus_assert (!find_subtree (tree, path4, NULL));
1734   _dbus_assert (!find_subtree (tree, path5, NULL));
1735   _dbus_assert (!find_subtree (tree, path6, NULL));
1736   _dbus_assert (!find_subtree (tree, path7, NULL));
1737   _dbus_assert (!find_subtree (tree, path8, NULL));
1738   
1739   i = 0;
1740   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1741     {
1742       _dbus_assert (tree_test_data[i].handler_unregistered);
1743       _dbus_assert (!tree_test_data[i].message_handled);
1744       ++i;
1745     }
1746
1747   /* Register it all again, and test dispatch */
1748   
1749   if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1750     goto out;
1751   if (!do_register (tree, path1, FALSE, 1, tree_test_data))
1752     goto out;
1753   if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1754     goto out;
1755   if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1756     goto out;
1757   if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1758     goto out;
1759   if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1760     goto out;
1761   if (!do_register (tree, path6, FALSE, 6, tree_test_data))
1762     goto out;
1763   if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1764     goto out;
1765   if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1766     goto out;
1767
1768 #if 0
1769   spew_tree (tree);
1770 #endif
1771
1772   if (!do_test_dispatch (tree, path0, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1773     goto out;
1774   if (!do_test_dispatch (tree, path1, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1775     goto out;
1776   if (!do_test_dispatch (tree, path2, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1777     goto out;
1778   if (!do_test_dispatch (tree, path3, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1779     goto out;
1780   if (!do_test_dispatch (tree, path4, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1781     goto out;
1782   if (!do_test_dispatch (tree, path5, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1783     goto out;
1784   if (!do_test_dispatch (tree, path6, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1785     goto out;
1786   if (!do_test_dispatch (tree, path7, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1787     goto out;
1788   if (!do_test_dispatch (tree, path8, 8, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1789     goto out;
1790   
1791  out:
1792   if (tree)
1793     {
1794       /* test ref */
1795       _dbus_object_tree_ref (tree);
1796       _dbus_object_tree_unref (tree);
1797       _dbus_object_tree_unref (tree);
1798     }
1799
1800   return TRUE;
1801 }
1802
1803 /**
1804  * @ingroup DBusObjectTree
1805  * Unit test for DBusObjectTree
1806  * @returns #TRUE on success.
1807  */
1808 dbus_bool_t
1809 _dbus_object_tree_test (void)
1810 {
1811   _dbus_test_oom_handling ("object tree",
1812                            object_tree_test_iteration,
1813                            NULL);
1814
1815   return TRUE;
1816 }
1817
1818 #endif /* DBUS_BUILD_TESTS */