diff --git a/apteryx.c b/apteryx.c index 0adf267..71a43dd 100644 --- a/apteryx.c +++ b/apteryx.c @@ -1232,11 +1232,18 @@ apteryx_path_to_node (GNode* root, const char *path, const char *value) next = strchr (path, '/'); if (!next) { - if (value) + rnode = apteryx_find_child (root, path); + if (rnode && value) + { + /* We already have this leaf - replace the value */ + free (g_node_first_child (rnode)->data); + g_node_first_child (rnode)->data = strdup (value); + } + else if (value) { rnode = APTERYX_LEAF (root, strdup (path), strdup (value)); } - else + else if (!rnode) { rnode = APTERYX_NODE (root, strdup (path)); } diff --git a/apteryxd.c b/apteryxd.c index 7fa4c3e..adfdbd8 100644 --- a/apteryxd.c +++ b/apteryxd.c @@ -1156,6 +1156,10 @@ handle_set (rpc_message msg, bool ack) ERROR ("SET: Failed to decode message\n"); return false; } + if (apteryx_debug && root) { + DEBUG ("SET:\n"); + apteryx_print_tree (root, stdout); + } /* Figure out if we need the lists for checking callbacks */ _node_to_path(root, &root_path); @@ -1296,11 +1300,11 @@ get_value (const char *path) /* Call refreshers */ call_refreshers (path, false); - /* Database second */ - if (!db_get (path, (unsigned char**)&value, &vsize)) + /* Provide second */ + if ((value = provide_get (path)) == NULL) { - /* Provide third */ - if ((value = provide_get (path)) == NULL) + /* Database third */ + if (!db_get (path, (unsigned char**)&value, &vsize)) { DEBUG ("GET: not in database or provided or proxied\n"); } @@ -1737,6 +1741,7 @@ handle_traverse (rpc_message msg) { path = (char *) ipath->data; value = (char *) ivalue->data; + /* Overwrite any database values with those from provide */ apteryx_path_to_node (root, path, value); } } @@ -1969,6 +1974,7 @@ handle_query (rpc_message msg) { path = (char *) ipath->data; value = (char *) ivalue->data; + /* Overwrite any database values with those from provide */ apteryx_path_to_node (root, path, value); } } diff --git a/test.c b/test.c index 6baa2db..cb2c467 100644 --- a/test.c +++ b/test.c @@ -3258,7 +3258,7 @@ test_provide_search_root () char * test_provide_cb(const char *path) { - return strdup("tmp"); + return strdup("provided"); } void @@ -3308,7 +3308,7 @@ test_provide_search_db () } void -test_provide_after_db () +test_provide_before_db () { const char *path = TEST_PATH"/interfaces/eth0/state"; const char *value = NULL; @@ -3316,7 +3316,7 @@ test_provide_after_db () CU_ASSERT (apteryx_set (path, "down")); CU_ASSERT (apteryx_provide (path, test_provide_callback_up)); CU_ASSERT (( value = apteryx_get (path)) != NULL); - CU_ASSERT (value && strcmp (value, "down") == 0); + CU_ASSERT (value && strcmp (value, "up") == 0); if (value) free ((void *) value); apteryx_unprovide (path, test_provide_callback_up); @@ -3743,7 +3743,7 @@ test_get_tree_provided () } void -test_get_tree_provided_after_db () +test_get_tree_provided_before_db () { const char *path = TEST_PATH"/interfaces/eth0/state"; GNode *root = NULL; @@ -3757,7 +3757,7 @@ test_get_tree_provided_after_db () CU_ASSERT (node && g_node_n_children (node) == 1); node = node ? g_node_first_child (node) : NULL; CU_ASSERT (node && strcmp (APTERYX_NAME (node), "state") == 0); - CU_ASSERT (node && strcmp (APTERYX_VALUE (node), "up") == 0); + CU_ASSERT (node && strcmp (APTERYX_VALUE (node), "provided") == 0); CU_ASSERT (apteryx_set (path, NULL)); CU_ASSERT (apteryx_unprovide (path, test_provide_cb)); @@ -3805,7 +3805,7 @@ char * test_provide_writes_cb (const char *path) { apteryx_set (TEST_PATH"/unimportant", NULL); - return strdup ("tmp"); + return strdup ("provided"); } void @@ -4528,7 +4528,7 @@ test_query_provided () } void -test_query_provided_after_db () +test_query_provided_before_db () { const char *path = TEST_PATH"/system/state"; GNode *root = NULL; @@ -4547,7 +4547,7 @@ test_query_provided_after_db () CU_ASSERT (node && g_node_n_children (node) == 1); node = node ? g_node_first_child (node) : NULL; CU_ASSERT (node && strcmp (APTERYX_NAME (node), "state") == 0); - CU_ASSERT (node && strcmp (APTERYX_VALUE (node), "up") == 0); + CU_ASSERT (node && strcmp (APTERYX_VALUE (node), "provided") == 0); apteryx_free_tree (rroot); apteryx_free_tree (root); @@ -4560,7 +4560,7 @@ test_query_provided_after_db () CU_ASSERT (node && g_node_n_children (node) == 1); node = node ? g_node_first_child (node) : NULL; CU_ASSERT (node && strcmp (APTERYX_NAME (node), "state") == 0); - CU_ASSERT (node && strcmp (APTERYX_VALUE (node), "up") == 0); + CU_ASSERT (node && strcmp (APTERYX_VALUE (node), "provided") == 0); apteryx_free_tree (rroot); apteryx_free_tree (root); @@ -7780,7 +7780,7 @@ static CU_TestInfo tests_api_provide[] = { { "provide search root", test_provide_search_root }, { "provide wildcard + search", test_provider_wildcard_search }, { "provide and db search", test_provide_search_db }, - { "provide after db", test_provide_after_db }, + { "provide before db", test_provide_before_db }, { "provider wildcard", test_provider_wildcard }, { "provider wildcard internal", test_provider_wildcard_internal }, { "provider search exact provide", test_search_of_provide }, @@ -7826,7 +7826,7 @@ static CU_TestInfo tests_api_tree[] = { { "get tree indexed/provided wildcards", test_get_tree_indexed_provided_wildcards }, { "get tree indexed/provided deeper", test_get_tree_indexed_provided_two_levels }, { "get tree provided", test_get_tree_provided }, - { "get tree provided after db", test_get_tree_provided_after_db }, + { "get tree provided before db", test_get_tree_provided_before_db }, { "get tree provided + set", test_get_tree_provided_plus_set }, { "get tree provider writes", test_get_tree_provider_write }, { "get tree thrashing" , test_get_tree_while_thrashing }, @@ -7850,7 +7850,7 @@ static CU_TestInfo tests_api_tree[] = { { "query null values", test_query_null_values}, { "query two branches", test_query_two_branches}, { "query provided", test_query_provided}, - { "query provided after db", test_query_provided_after_db}, + { "query provided before db", test_query_provided_before_db}, { "query provided and refreshed", test_query_provided_refreshed}, { "query provided trunk request", test_query_trunk_provided}, { "query provided wildcard", test_query_provided_wildcard},