25 #include "dbus-object-tree.h"
26 #include "dbus-connection-internal.h"
27 #include "dbus-internals.h"
28 #include "dbus-hash.h"
29 #include "dbus-protocol.h"
30 #include "dbus-string.h"
107 tree->
root = _dbus_object_subtree_new (
"/",
NULL,
NULL);
160 #define VERBOSE_FIND 0
166 int *index_in_parent,
172 return_deepest_match = exact_match !=
NULL;
174 _dbus_assert (!(return_deepest_match && create_if_not_found));
179 _dbus_verbose (
" path exhausted, returning %s\n",
182 if (exact_match !=
NULL)
188 _dbus_verbose (
" searching children of %s for %s\n",
189 subtree->
name, path[0]);
202 _dbus_verbose (
" %s cmp %s = %d\n",
212 _dbus_verbose (
" storing parent index %d\n", k);
214 *index_in_parent = k;
217 if (return_deepest_match)
221 next = find_subtree_recurse (subtree->
subtrees[k],
222 &path[1], create_if_not_found,
223 index_in_parent, exact_match);
228 _dbus_verbose (
" no deeper match found, returning %s\n",
231 if (exact_match !=
NULL)
232 *exact_match =
FALSE;
239 return find_subtree_recurse (subtree->
subtrees[k],
240 &path[1], create_if_not_found,
241 index_in_parent, exact_match);
254 _dbus_verbose (
" no match found, current tree %s, create_if_not_found = %d\n",
255 subtree->
name, create_if_not_found);
258 if (create_if_not_found)
261 int child_pos, new_n_subtrees;
264 _dbus_verbose (
" creating subtree %s\n",
268 child = _dbus_object_subtree_new (path[0],
276 int new_max_subtrees;
282 if (new_subtrees ==
NULL)
284 _dbus_object_subtree_unref (child);
295 new_n_subtrees <= subtree->max_subtrees);
296 if (child_pos + 1 < new_n_subtrees)
298 memmove (&subtree->
subtrees[child_pos+1],
300 (new_n_subtrees - child_pos - 1) *
303 subtree->
subtrees[child_pos] = child;
306 *index_in_parent = child_pos;
310 return find_subtree_recurse (child,
311 &path[1], create_if_not_found,
312 index_in_parent, exact_match);
316 if (exact_match !=
NULL)
317 *exact_match =
FALSE;
325 int *index_in_parent)
330 _dbus_verbose (
"Looking for exact registered subtree\n");
333 subtree = find_subtree_recurse (tree->
root, path,
FALSE, index_in_parent,
NULL);
346 _dbus_verbose (
"Looking for subtree\n");
357 _dbus_verbose (
"Looking for deepest handler\n");
361 *exact_match =
FALSE;
363 return find_subtree_recurse (tree->
root, path,
FALSE,
NULL, exact_match);
371 _dbus_verbose (
"Ensuring subtree\n");
376 static char *flatten_path (
const char **path);
404 subtree = ensure_subtree (tree, path);
407 _DBUS_SET_OOM (error);
415 char *complete_path = flatten_path (path);
418 "A handler is already registered for %s",
419 complete_path ? complete_path
420 :
"(cannot represent path: out of memory!)");
455 unregister_function =
NULL;
458 subtree = find_subtree (tree, path, &i);
460 #ifndef DBUS_DISABLE_CHECKS
463 _dbus_warn (
"Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
464 path[0] ? path[0] :
"null",
465 path[1] ? path[1] :
"null");
498 _dbus_object_subtree_unref (subtree);
506 #ifdef DBUS_BUILD_TESTS
511 _dbus_verbose (
"unlock\n");
515 if (unregister_function)
516 (* unregister_function) (connection, user_data);
518 #ifdef DBUS_BUILD_TESTS
540 free_subtree_recurse (connection, child);
553 _dbus_object_subtree_unref (subtree);
573 const char **parent_path,
574 char ***child_entries)
582 *child_entries =
NULL;
584 subtree = lookup_subtree (tree, parent_path);
596 while (i < subtree->n_subtrees)
599 if (retval[i] ==
NULL)
611 *child_entries = retval;
612 return retval !=
NULL;
626 const char *v_STRING;
631 already_unlocked =
FALSE;
633 _dbus_verbose (
" considering default Introspect() handler...\n");
641 #ifdef DBUS_BUILD_TESTS
645 _dbus_verbose (
"unlock\n");
652 _dbus_verbose (
" using default Introspect() handler!\n");
656 #ifdef DBUS_BUILD_TESTS
660 _dbus_verbose (
"unlock\n");
670 if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
680 while (children[i] !=
NULL)
701 #ifdef DBUS_BUILD_TESTS
705 already_unlocked =
TRUE;
714 #ifdef DBUS_BUILD_TESTS
718 if (!already_unlocked)
720 _dbus_verbose (
"unlock\n");
759 _dbus_verbose (
"Dispatch of message by object path\n");
765 #ifdef DBUS_BUILD_TESTS
769 _dbus_verbose (
"unlock\n");
773 _dbus_verbose (
"No memory to get decomposed path\n");
780 #ifdef DBUS_BUILD_TESTS
784 _dbus_verbose (
"unlock\n");
788 _dbus_verbose (
"No path field in message\n");
793 subtree = find_handler (tree, (
const char**) path, &exact_match);
796 *found_object = !!subtree;
802 while (subtree !=
NULL)
806 _dbus_object_subtree_ref (subtree);
812 _dbus_object_subtree_unref (subtree);
813 goto free_and_return;
818 subtree = subtree->
parent;
821 _dbus_verbose (
"%d handlers in the path tree for this message\n",
832 subtree = link->
data;
846 _dbus_verbose (
" (invoking a handler)\n");
849 #ifdef DBUS_BUILD_TESTS
853 _dbus_verbose (
"unlock\n");
862 result = (* message_function) (tree->
connection,
866 #ifdef DBUS_BUILD_TESTS
872 goto free_and_return;
884 result = handle_default_introspect_and_unlock (tree, message,
885 (
const char**) path);
889 #ifdef DBUS_BUILD_TESTS
893 _dbus_verbose (
"unlock\n");
901 _dbus_object_subtree_unref (link->
data);
929 subtree = find_handler (tree, (
const char**) path, &exact_match);
931 if ((subtree ==
NULL) || !exact_match)
933 _dbus_verbose (
"No object at specified path found\n");
947 allocate_subtree_object (
const char *name)
962 memcpy (subtree->
name, name, len + 1);
968 _dbus_object_subtree_new (
const char *name,
974 subtree = allocate_subtree_object (name);
1009 #ifdef DBUS_DISABLE_ASSERT
1051 const char **parent_path,
1052 char ***child_entries)
1056 result = _dbus_object_tree_list_registered_unlocked (tree,
1060 #ifdef DBUS_BUILD_TESTS
1064 _dbus_verbose (
"unlock\n");
1073 #define VERBOSE_DECOMPOSE 0
1098 #if VERBOSE_DECOMPOSE
1099 _dbus_verbose (
"Decomposing path \"%s\"\n",
1116 retval =
dbus_new0 (
char*, n_components + 1);
1122 if (n_components == 0)
1126 while (comp < n_components)
1134 while (j < len && data[j] !=
'/')
1142 #if VERBOSE_DECOMPOSE
1143 _dbus_verbose (
" (component in [%d,%d))\n",
1148 if (retval[comp] ==
NULL)
1153 retval[comp][j-i] =
'\0';
1154 #if VERBOSE_DECOMPOSE
1155 _dbus_verbose (
" (component %d = \"%s\")\n",
1156 comp, retval[comp]);
1166 *path_len = n_components;
1174 flatten_path (
const char **path)
1182 if (path[0] ==
NULL)
1217 #ifdef DBUS_BUILD_TESTS
1219 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1221 #include "dbus-test.h"
1233 static StrComparison
1234 path_contains (
const char **container,
1240 while (child[i] !=
NULL)
1244 if (container[i] ==
NULL)
1253 v = strcmp (container[i], child[i]);
1256 return STR_DIFFERENT;
1266 if (container[i] ==
NULL)
1269 return STR_DIFFERENT;
1282 _dbus_verbose (
" ");
1286 _dbus_verbose (
"%s (%d children)\n",
1290 while (i < subtree->n_subtrees)
1292 spew_subtree_recurse (subtree->
subtrees[i], indent + 2);
1301 spew_subtree_recurse (tree->
root, 0);
1321 TreeTestData *ttd = user_data;
1323 ttd->handler_unregistered =
TRUE;
1331 TreeTestData *ttd = user_data;
1333 ttd->message_handled =
TRUE;
1343 TreeTestData *tree_test_data)
1346 test_message_function,
NULL };
1348 tree_test_data[i].message_handled =
FALSE;
1349 tree_test_data[i].handler_unregistered =
FALSE;
1350 tree_test_data[i].handler_fallback = fallback;
1351 tree_test_data[i].path = path;
1360 &tree_test_data[i]);
1369 TreeTestData *tree_test_data,
1379 flat = flatten_path (path);
1385 "org.freedesktop.TestInterface",
1388 if (message ==
NULL)
1392 while (j < n_test_data)
1394 tree_test_data[j].message_handled =
FALSE;
1405 while (j < n_test_data)
1407 if (tree_test_data[j].message_handled)
1409 if (tree_test_data[j].handler_fallback)
1411 path) != STR_DIFFERENT);
1413 _dbus_assert (path_contains (tree_test_data[j].path, path) == STR_EQUAL);
1417 if (tree_test_data[j].handler_fallback)
1419 path) == STR_DIFFERENT);
1421 _dbus_assert (path_contains (tree_test_data[j].path, path) != STR_EQUAL);
1438 string_array_length (
const char **array)
1441 for (i = 0; array[i]; i++) ;
1448 const char *result[20];
1449 } DecomposePathTest;
1451 static DecomposePathTest decompose_tests[] = {
1452 {
"/foo", {
"foo",
NULL } },
1453 {
"/foo/bar", {
"foo",
"bar",
NULL } },
1455 {
"/a/b", {
"a",
"b",
NULL } },
1456 {
"/a/b/c", {
"a",
"b",
"c",
NULL } },
1457 {
"/a/b/c/d", {
"a",
"b",
"c",
"d",
NULL } },
1458 {
"/foo/bar/q", {
"foo",
"bar",
"q",
NULL } },
1459 {
"/foo/bar/this/is/longer", {
"foo",
"bar",
"this",
"is",
"longer",
NULL } }
1463 run_decompose_tests (
void)
1475 strlen (decompose_tests[i].path),
1476 &result, &result_len))
1479 expected_len = string_array_length (decompose_tests[i].result);
1481 if (result_len != (
int) string_array_length ((
const char**)result) ||
1482 expected_len != result_len ||
1483 path_contains (decompose_tests[i].result,
1484 (
const char**) result) != STR_EQUAL)
1486 int real_len = string_array_length ((
const char**)result);
1487 _dbus_warn (
"Expected decompose of %s to have len %d, returned %d, appears to have %d\n",
1488 decompose_tests[i].path, expected_len, result_len,
1490 _dbus_warn (
"Decompose resulted in elements: { ");
1492 while (i < real_len)
1495 (i + 1) == real_len ?
"" :
", ");
1511 object_tree_test_iteration (
void *data)
1513 const char *path0[] = {
NULL };
1514 const char *path1[] = {
"foo",
NULL };
1515 const char *path2[] = {
"foo",
"bar",
NULL };
1516 const char *path3[] = {
"foo",
"bar",
"baz",
NULL };
1517 const char *path4[] = {
"foo",
"bar",
"boo",
NULL };
1518 const char *path5[] = {
"blah",
NULL };
1519 const char *path6[] = {
"blah",
"boof",
NULL };
1520 const char *path7[] = {
"blah",
"boof",
"this",
"is",
"really",
"long",
NULL };
1521 const char *path8[] = {
"childless",
NULL };
1523 TreeTestData tree_test_data[9];
1527 if (!run_decompose_tests ())
1536 if (!do_register (tree, path0,
TRUE, 0, tree_test_data))
1549 _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
1550 _dbus_assert (find_handler (tree, path1, &exact_match) == tree->
root && !exact_match);
1551 _dbus_assert (find_handler (tree, path2, &exact_match) == tree->
root && !exact_match);
1552 _dbus_assert (find_handler (tree, path3, &exact_match) == tree->
root && !exact_match);
1553 _dbus_assert (find_handler (tree, path4, &exact_match) == tree->
root && !exact_match);
1554 _dbus_assert (find_handler (tree, path5, &exact_match) == tree->
root && !exact_match);
1555 _dbus_assert (find_handler (tree, path6, &exact_match) == tree->
root && !exact_match);
1556 _dbus_assert (find_handler (tree, path7, &exact_match) == tree->
root && !exact_match);
1557 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->
root && !exact_match);
1559 if (!do_register (tree, path1,
TRUE, 1, tree_test_data))
1572 _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
1573 _dbus_assert (find_handler (tree, path1, &exact_match) && exact_match);
1574 _dbus_assert (find_handler (tree, path2, &exact_match) && !exact_match);
1575 _dbus_assert (find_handler (tree, path3, &exact_match) && !exact_match);
1576 _dbus_assert (find_handler (tree, path4, &exact_match) && !exact_match);
1577 _dbus_assert (find_handler (tree, path5, &exact_match) == tree->
root && !exact_match);
1578 _dbus_assert (find_handler (tree, path6, &exact_match) == tree->
root && !exact_match);
1579 _dbus_assert (find_handler (tree, path7, &exact_match) == tree->
root && !exact_match);
1580 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->
root && !exact_match);
1582 if (!do_register (tree, path2,
TRUE, 2, tree_test_data))
1594 if (!do_register (tree, path3,
TRUE, 3, tree_test_data))
1607 if (!do_register (tree, path4,
TRUE, 4, tree_test_data))
1620 if (!do_register (tree, path5,
TRUE, 5, tree_test_data))
1633 _dbus_assert (find_handler (tree, path0, &exact_match) == tree->
root && exact_match);
1634 _dbus_assert (find_handler (tree, path1, &exact_match) != tree->
root && exact_match);
1635 _dbus_assert (find_handler (tree, path2, &exact_match) != tree->
root && exact_match);
1636 _dbus_assert (find_handler (tree, path3, &exact_match) != tree->
root && exact_match);
1637 _dbus_assert (find_handler (tree, path4, &exact_match) != tree->
root && exact_match);
1638 _dbus_assert (find_handler (tree, path5, &exact_match) != tree->
root && exact_match);
1639 _dbus_assert (find_handler (tree, path6, &exact_match) != tree->
root && !exact_match);
1640 _dbus_assert (find_handler (tree, path7, &exact_match) != tree->
root && !exact_match);
1641 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->
root && !exact_match);
1643 if (!do_register (tree, path6,
TRUE, 6, tree_test_data))
1656 if (!do_register (tree, path7,
TRUE, 7, tree_test_data))
1669 if (!do_register (tree, path8,
TRUE, 8, tree_test_data))
1682 _dbus_assert (find_handler (tree, path0, &exact_match) == tree->
root && exact_match);
1683 _dbus_assert (find_handler (tree, path1, &exact_match) != tree->
root && exact_match);
1684 _dbus_assert (find_handler (tree, path2, &exact_match) != tree->
root && exact_match);
1685 _dbus_assert (find_handler (tree, path3, &exact_match) != tree->
root && exact_match);
1686 _dbus_assert (find_handler (tree, path4, &exact_match) != tree->
root && exact_match);
1687 _dbus_assert (find_handler (tree, path5, &exact_match) != tree->
root && exact_match);
1688 _dbus_assert (find_handler (tree, path6, &exact_match) != tree->
root && exact_match);
1689 _dbus_assert (find_handler (tree, path7, &exact_match) != tree->
root && exact_match);
1690 _dbus_assert (find_handler (tree, path8, &exact_match) != tree->
root && exact_match);
1695 const char *root[] = {
NULL };
1696 char **child_entries;
1699 _dbus_object_tree_list_registered_unlocked (tree, path1, &child_entries);
1700 if (child_entries !=
NULL)
1702 nb = string_array_length ((
const char**)child_entries);
1707 _dbus_object_tree_list_registered_unlocked (tree, path2, &child_entries);
1708 if (child_entries !=
NULL)
1710 nb = string_array_length ((
const char**)child_entries);
1715 _dbus_object_tree_list_registered_unlocked (tree, path8, &child_entries);
1716 if (child_entries !=
NULL)
1718 nb = string_array_length ((
const char**)child_entries);
1723 _dbus_object_tree_list_registered_unlocked (tree, root, &child_entries);
1724 if (child_entries !=
NULL)
1726 nb = string_array_length ((
const char**)child_entries);
1748 if (!do_register (tree, path0,
TRUE, 0, tree_test_data))
1750 if (!do_register (tree, path1,
TRUE, 1, tree_test_data))
1752 if (!do_register (tree, path2,
TRUE, 2, tree_test_data))
1754 if (!do_register (tree, path3,
TRUE, 3, tree_test_data))
1756 if (!do_register (tree, path4,
TRUE, 4, tree_test_data))
1758 if (!do_register (tree, path5,
TRUE, 5, tree_test_data))
1760 if (!do_register (tree, path6,
TRUE, 6, tree_test_data))
1762 if (!do_register (tree, path7,
TRUE, 7, tree_test_data))
1764 if (!do_register (tree, path8,
TRUE, 8, tree_test_data))
1894 if (!do_register (tree, path0,
TRUE, 0, tree_test_data))
1896 if (!do_register (tree, path1,
FALSE, 1, tree_test_data))
1898 if (!do_register (tree, path2,
TRUE, 2, tree_test_data))
1900 if (!do_register (tree, path3,
TRUE, 3, tree_test_data))
1902 if (!do_register (tree, path4,
TRUE, 4, tree_test_data))
1904 if (!do_register (tree, path5,
TRUE, 5, tree_test_data))
1906 if (!do_register (tree, path6,
FALSE, 6, tree_test_data))
1908 if (!do_register (tree, path7,
TRUE, 7, tree_test_data))
1910 if (!do_register (tree, path8,
TRUE, 8, tree_test_data))
1917 if (!do_test_dispatch (tree, path0, 0, tree_test_data,
_DBUS_N_ELEMENTS (tree_test_data)))
1919 if (!do_test_dispatch (tree, path1, 1, tree_test_data,
_DBUS_N_ELEMENTS (tree_test_data)))
1921 if (!do_test_dispatch (tree, path2, 2, tree_test_data,
_DBUS_N_ELEMENTS (tree_test_data)))
1923 if (!do_test_dispatch (tree, path3, 3, tree_test_data,
_DBUS_N_ELEMENTS (tree_test_data)))
1925 if (!do_test_dispatch (tree, path4, 4, tree_test_data,
_DBUS_N_ELEMENTS (tree_test_data)))
1927 if (!do_test_dispatch (tree, path5, 5, tree_test_data,
_DBUS_N_ELEMENTS (tree_test_data)))
1929 if (!do_test_dispatch (tree, path6, 6, tree_test_data,
_DBUS_N_ELEMENTS (tree_test_data)))
1931 if (!do_test_dispatch (tree, path7, 7, tree_test_data,
_DBUS_N_ELEMENTS (tree_test_data)))
1933 if (!do_test_dispatch (tree, path8, 8, tree_test_data,
_DBUS_N_ELEMENTS (tree_test_data)))
1954 _dbus_object_tree_test (
void)
1956 _dbus_test_oom_handling (
"object tree",
1957 object_tree_test_iteration,