Commit a519e5f9 authored by Daniel Salzman's avatar Daniel Salzman

conf: export module sections right before the template section

parent 65f6161a
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <string.h>
#include <urcu.h> #include <urcu.h>
#include "knot/conf/base.h" #include "knot/conf/base.h"
...@@ -196,7 +197,8 @@ int conf_new( ...@@ -196,7 +197,8 @@ int conf_new(
// Remove the database to ensure it is temporary. // Remove the database to ensure it is temporary.
if (!remove_path(lmdb_opts.path)) { if (!remove_path(lmdb_opts.path)) {
CONF_LOG(LOG_WARNING, "failed to purge temporary directory '%s'", lmdb_opts.path); CONF_LOG(LOG_WARNING, "failed to purge temporary directory '%s'",
lmdb_opts.path);
} }
} else { } else {
// Set the specified database. // Set the specified database.
...@@ -768,6 +770,65 @@ static int export_group( ...@@ -768,6 +770,65 @@ static int export_group(
return KNOT_EOK; return KNOT_EOK;
} }
static int export_item(
conf_t *conf,
FILE *fp,
const yp_item_t *item,
char *buff,
size_t buff_len,
yp_style_t style)
{
bool exported = false;
// Skip non-group items (include).
if (item->type != YP_TGRP) {
return KNOT_EOK;
}
// Export simple group without identifiers.
if (!(item->flags & YP_FMULTI)) {
return export_group(conf, fp, item, NULL, 0, buff, buff_len,
style, &exported);
}
// Iterate over all identifiers.
conf_iter_t iter;
int ret = conf_db_iter_begin(conf, &conf->read_txn, item->name, &iter);
switch (ret) {
case KNOT_EOK:
break;
case KNOT_ENOENT:
return KNOT_EOK;
default:
return ret;
}
while (ret == KNOT_EOK) {
const uint8_t *id;
size_t id_len;
ret = conf_db_iter_id(conf, &iter, &id, &id_len);
if (ret != KNOT_EOK) {
conf_db_iter_finish(conf, &iter);
return ret;
}
// Export group with identifiers.
ret = export_group(conf, fp, item, id, id_len, buff, buff_len,
style, &exported);
if (ret != KNOT_EOK) {
conf_db_iter_finish(conf, &iter);
return ret;
}
ret = conf_db_iter_next(conf, &iter);
}
if (ret != KNOT_EOF) {
return ret;
}
return KNOT_EOK;
}
int conf_export( int conf_export(
conf_t *conf, conf_t *conf,
const char *file_name, const char *file_name,
...@@ -792,60 +853,37 @@ int conf_export( ...@@ -792,60 +853,37 @@ int conf_export(
fprintf(fp, "# Configuration export (Knot DNS %s)\n\n", PACKAGE_VERSION); fprintf(fp, "# Configuration export (Knot DNS %s)\n\n", PACKAGE_VERSION);
const char *mod_prefix = KNOTD_MOD_NAME_PREFIX;
const size_t mod_prefix_len = strlen(mod_prefix);
int ret; int ret;
// Iterate over the scheme. // Iterate over the scheme.
for (yp_item_t *item = conf->scheme; item->name != NULL; item++) { for (yp_item_t *item = conf->scheme; item->name != NULL; item++) {
bool exported = false; // Don't export module sections again.
if (strncmp(item->name + 1, mod_prefix, mod_prefix_len) == 0) {
// Skip non-group items (include).
if (item->type != YP_TGRP) {
continue;
}
// Export simple group without identifiers.
if ((item->flags & YP_FMULTI) == 0) {
ret = export_group(conf, fp, item, NULL, 0, buff,
buff_len, style, &exported);
if (ret != KNOT_EOK) {
goto export_error;
}
continue;
}
// Iterate over all identifiers.
conf_iter_t iter;
ret = conf_db_iter_begin(conf, &conf->read_txn, item->name, &iter);
switch (ret) {
case KNOT_EOK:
break; break;
case KNOT_ENOENT:
continue;
default:
goto export_error;
} }
while (ret == KNOT_EOK) { // Export module sections before the template section.
const uint8_t *id; if (strcmp(item->name + 1, C_TPL + 1) == 0) {
size_t id_len; for (yp_item_t *mod = item + 1; mod->name != NULL; mod++) {
ret = conf_db_iter_id(conf, &iter, &id, &id_len); // Skip non-module sections.
if (ret != KNOT_EOK) { if (strncmp(mod->name + 1, mod_prefix, mod_prefix_len) != 0) {
conf_db_iter_finish(conf, &iter); continue;
goto export_error; }
}
// Export module section.
// Export group with identifiers. ret = export_item(conf, fp, mod, buff, buff_len, style);
ret = export_group(conf, fp, item, id, id_len, buff, if (ret != KNOT_EOK) {
buff_len, style, &exported); goto export_error;
if (ret != KNOT_EOK) { }
conf_db_iter_finish(conf, &iter);
goto export_error;
} }
ret = conf_db_iter_next(conf, &iter);
} }
if (ret != KNOT_EOF) {
// Export non-module section.
ret = export_item(conf, fp, item, buff, buff_len, style);
if (ret != KNOT_EOK) {
goto export_error; goto export_error;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment