diff --git a/lib/isc/cfgmgr.c b/lib/isc/cfgmgr.c index fd50fad5c7..7315d6ac03 100644 --- a/lib/isc/cfgmgr.c +++ b/lib/isc/cfgmgr.c @@ -497,6 +497,23 @@ isc__cfgmgr_keylen(const char *key) { return strlen(key + 5) + 5; } +static void +isc__cfgmgr_addindex(MDB_cursor *cursor, const char *name) { + char key[BUFLEN]; + MDB_val dbkey; + MDB_val dbval = { .mv_size = strlen(name), .mv_data = (void *)name }; + + /* + * Add the index of the node into it's parent node + */ + isc__cfgmgr_buildkey(key, sizeof(key), NODEINDEXFLG, + isc__cfgmgr_curhash(), "%s%s/", + isc__cfgmgr_curprefix(), name); + dbkey.mv_size = isc__cfgmgr_keylen(key) + 1; + dbkey.mv_data = key; + REQUIRE(mdb_cursor_put(cursor, &dbkey, &dbval, 0) == 0); +} + void isc_cfgmgr_open(const char *name) { isc__cfgmgr_node_t *node = NULL; @@ -518,21 +535,9 @@ isc_cfgmgr_open(const char *name) { name) < BUFLEN); if (isc__cfgmgr_ctx.readonly == false) { - char key[BUFLEN]; - MDB_val dbkey; - MDB_val dbval = { .mv_size = strlen(name), - .mv_data = (void *)name }; - - /* - * Add the index of the node into it's parent node - */ - isc__cfgmgr_buildkey(key, sizeof(key), NODEINDEXFLG, - isc__cfgmgr_curhash(), "%s%s/", - isc__cfgmgr_curprefix(), name); - dbkey.mv_size = isc__cfgmgr_keylen(key) + 1; - dbkey.mv_data = key; - REQUIRE(mdb_cursor_put(isc__cfgmgr_lmdbcursor(), &dbkey, &dbval, - 0) == 0); + isc__cfgmgr_addindex(isc__cfgmgr_ctx.builtincursor, name); + isc__cfgmgr_addindex(isc__cfgmgr_ctx.usercursor, name); + isc__cfgmgr_addindex(isc__cfgmgr_ctx.runningcursor, name); } ISC_LIST_APPEND(isc__cfgmgr_ctx.openednodes, node, link); @@ -573,6 +578,44 @@ isc__cfgmgr_findprefix(MDB_cursor *cursor, char flag, uint32_t hash, return false; } +static void +isc__cfgmgr_clearindex(MDB_cursor *cursor, uint32_t hash, const char *prefix) { + MDB_val dbkey; + char key[BUFLEN]; + + if (isc__cfgmgr_findprefix(cursor, PROPERTYFLG, hash, prefix, NULL, + NULL)) + { + /* + * There still are properties in the closed node. Keep its index + * in the parent. + */ + return; + } + + if (isc__cfgmgr_findprefix(cursor, NODEINDEXFLG, hash, prefix, NULL, + NULL)) + { + /* + * There still are sub-nodes indexes in the closed node. Keep + * its index in the parent. + */ + return; + } + + /* + * No properties or sub-node indexes found in the closed node. Delete + * the index of the closed node from it's parent node: the closed node + * doesn't exists anymore. + */ + isc__cfgmgr_buildkey(key, sizeof(key), NODEINDEXFLG, + isc__cfgmgr_curhash(), "%s", prefix); + dbkey.mv_size = isc__cfgmgr_keylen(key) + 1; + dbkey.mv_data = key; + REQUIRE(mdb_cursor_get(cursor, &dbkey, NULL, MDB_SET) == 0); + REQUIRE(mdb_cursor_del(cursor, 0) == 0); +} + void isc_cfgmgr_close(void) { isc__cfgmgr_node_t *node; @@ -582,46 +625,14 @@ isc_cfgmgr_close(void) { ISC_LIST_UNLINK(isc__cfgmgr_ctx.openednodes, node, link); if (isc__cfgmgr_ctx.readonly == false) { - MDB_val dbkey; - char key[BUFLEN]; - - if (isc__cfgmgr_findprefix(isc__cfgmgr_lmdbcursor(), - PROPERTYFLG, node->hash, - node->prefix, NULL, NULL)) - { - /* - * There still are properties in the current (being - * closed) node. Keep its index in the parent. - */ - goto free; - } - - if (isc__cfgmgr_findprefix(isc__cfgmgr_lmdbcursor(), - NODEINDEXFLG, node->hash, - node->prefix, NULL, NULL)) - { - /* - * There still are sub-nodes indexes in the current - * (being closed) node. Keep its index in the parent. - */ - goto free; - } - - /* - * No properties or sub-node indexes found in the current node, - * delete the index of the current (being closed) node from it's - * parent node: the node doesn't exists anymore. - */ - isc__cfgmgr_buildkey(key, sizeof(key), NODEINDEXFLG, - isc__cfgmgr_curhash(), "%s", node->prefix); - dbkey.mv_size = isc__cfgmgr_keylen(key) + 1; - dbkey.mv_data = key; - REQUIRE(mdb_cursor_get(isc__cfgmgr_lmdbcursor(), &dbkey, NULL, - MDB_SET) == 0); - REQUIRE(mdb_cursor_del(isc__cfgmgr_lmdbcursor(), 0) == 0); + isc__cfgmgr_clearindex(isc__cfgmgr_ctx.builtincursor, + node->hash, node->prefix); + isc__cfgmgr_clearindex(isc__cfgmgr_ctx.usercursor, node->hash, + node->prefix); + isc__cfgmgr_clearindex(isc__cfgmgr_ctx.runningcursor, + node->hash, node->prefix); } -free: isc_mem_free(isc__cfgmgr_mctx, node->name); isc_mem_put(isc__cfgmgr_mctx, node->prefix, BUFLEN); isc_mem_put(isc__cfgmgr_mctx, node, sizeof(*node));