diff --git a/CHANGELOG.md b/CHANGELOG.md index 0637ab66..a39ee238 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -191,6 +191,9 @@ ### Minor changes +* `startup_extraxml` triggers unnecessary validation + * Renamed startup_db_reset -> xmldb_db_reset (its a general function) + * In startup_extraxml(), check if reset callbacks or extraxml file actually makes and changes to the tmp db. * Print CLICON_YANG_DIR and CLICON_FEATURE lists on startup with debug flag * Extended `util/clixon_util_xml` with yang and validate functionality so it can be used as a stand-alone utility for validating XML/JSON files * JSON parse and print improvements diff --git a/apps/backend/backend_main.c b/apps/backend/backend_main.c index dae76e94..5e9a5354 100644 --- a/apps/backend/backend_main.c +++ b/apps/backend/backend_main.c @@ -642,7 +642,7 @@ main(int argc, switch (startup_mode){ case SM_INIT: /* Scratch running and start from empty */ /* [Delete and] create running db */ - if (startup_db_reset(h, "running") < 0) + if (xmldb_db_reset(h, "running") < 0) goto done; case SM_NONE: /* Fall through * * Load plugins and call plugin_init() */ diff --git a/apps/backend/backend_startup.c b/apps/backend/backend_startup.c index 2e5342b3..8fd12e0e 100644 --- a/apps/backend/backend_startup.c +++ b/apps/backend/backend_startup.c @@ -70,22 +70,6 @@ #include "backend_commit.h" #include "backend_startup.h" -/*! Create an XML database. If it exists already, delete it before creating - * @param[in] h Clixon handle - * @param[in] db Symbolic database name, eg "candidate", "running" - */ -int -startup_db_reset(clicon_handle h, - char *db) -{ - if (xmldb_exists(h, db) == 1){ - if (xmldb_delete(h, db) != 0 && errno != ENOENT) - return -1; - } - if (xmldb_create(h, db) < 0) - return -1; - return 0; -} /*! Merge db1 into db2 without commit * @retval -1 Error @@ -235,34 +219,45 @@ startup_extraxml(clicon_handle h, cbuf *cbret) { int retval = -1; - char *db = "tmp"; + char *tmp_db = "tmp"; int ret; cxobj *xt = NULL; /* Potentially upgraded XML */ /* Clear tmp db */ - if (startup_db_reset(h, db) < 0) + if (xmldb_db_reset(h, tmp_db) < 0) goto done; /* Application may define extra xml in its reset function*/ - if (clixon_plugin_reset(h, db) < 0) + if (clixon_plugin_reset(h, tmp_db) < 0) goto done; /* Extra XML can also be added via file */ if (file){ /* Parse and load file into tmp db */ - if ((ret = load_extraxml(h, file, db, cbret)) < 0) + if ((ret = load_extraxml(h, file, tmp_db, cbret)) < 0) goto done; if (ret == 0) goto fail; } + /* + * Check if tmp db is empty. + * It should be empty if extra-xml is null and reset plugins did nothing + * then skip validation. + */ + if (xmldb_get(h, tmp_db, NULL, &xt) < 0) + goto done; + if (xt==NULL || xml_child_nr(xt)==0) + goto ok; + xml_free(xt); + xt = NULL; /* Validate the tmp db and return possibly upgraded xml in xt */ - if ((ret = startup_validate(h, db, &xt, cbret)) < 0) + if ((ret = startup_validate(h, tmp_db, &xt, cbret)) < 0) goto done; if (ret == 0) goto fail; if (xt==NULL || xml_child_nr(xt)==0) goto ok; /* Merge tmp into running (no commit) */ - if ((ret = db_merge(h, db, "running", cbret)) < 0) + if ((ret = db_merge(h, tmp_db, "running", cbret)) < 0) goto fail; if (ret == 0) goto fail; @@ -270,7 +265,7 @@ startup_extraxml(clicon_handle h, retval = 1; done: xmldb_get0_free(h, &xt); - if (xmldb_delete(h, db) != 0 && errno != ENOENT) + if (xmldb_delete(h, tmp_db) != 0 && errno != ENOENT) return -1; return retval; fail: @@ -306,7 +301,7 @@ startup_failsafe(clicon_handle h) /* Copy original running to tmp as backup (restore if error) */ if (xmldb_copy(h, "running", "tmp") < 0) goto done; - if (startup_db_reset(h, "running") < 0) + if (xmldb_db_reset(h, "running") < 0) goto done; ret = candidate_commit(h, db, cbret); if (ret != 1) diff --git a/apps/backend/backend_startup.h b/apps/backend/backend_startup.h index 7eed20a6..52d1ef06 100644 --- a/apps/backend/backend_startup.h +++ b/apps/backend/backend_startup.h @@ -40,7 +40,6 @@ /* * Prototypes */ -int startup_db_reset(clicon_handle h, char *db); int startup_mode_startup(clicon_handle h, char *db, cbuf *cbret); int startup_extraxml(clicon_handle h, char *file, cbuf *cbret); int startup_failsafe(clicon_handle h); diff --git a/lib/clixon/clixon_datastore.h b/lib/clixon/clixon_datastore.h index f29903bf..a600c30d 100644 --- a/lib/clixon/clixon_datastore.h +++ b/lib/clixon/clixon_datastore.h @@ -62,5 +62,7 @@ int xmldb_islocked(clicon_handle h, const char *db); int xmldb_exists(clicon_handle h, const char *db); int xmldb_delete(clicon_handle h, const char *db); int xmldb_create(clicon_handle h, const char *db); +/* utility functions */ +int xmldb_db_reset(clicon_handle h, char *db); #endif /* _CLIXON_DATASTORE_H */ diff --git a/lib/src/clixon_datastore.c b/lib/src/clixon_datastore.c index b27ce1b4..d2f58762 100644 --- a/lib/src/clixon_datastore.c +++ b/lib/src/clixon_datastore.c @@ -78,6 +78,7 @@ #include "clixon_datastore_write.h" #include "clixon_datastore_read.h" + /*! Translate from symbolic database name to actual filename in file-system * @param[in] th text handle handle * @param[in] db Symbolic database name, eg "candidate", "running" @@ -446,3 +447,21 @@ xmldb_create(clicon_handle h, close(fd); return retval; } + +/*! Create an XML database. If it exists already, delete it before creating + * Utility function. + * @param[in] h Clixon handle + * @param[in] db Symbolic database name, eg "candidate", "running" + */ +int +xmldb_db_reset(clicon_handle h, + char *db) +{ + if (xmldb_exists(h, db) == 1){ + if (xmldb_delete(h, db) != 0 && errno != ENOENT) + return -1; + } + if (xmldb_create(h, db) < 0) + return -1; + return 0; +} diff --git a/lib/src/clixon_datastore_read.c b/lib/src/clixon_datastore_read.c index eb099521..b4358ae7 100644 --- a/lib/src/clixon_datastore_read.c +++ b/lib/src/clixon_datastore_read.c @@ -641,7 +641,8 @@ xmldb_get_zerocopy(clicon_handle h, * @param[in] db Name of database to search in (filename including dir path * @param[in] xpath String with XPATH syntax. or NULL for all * @param[out] xret Single return XML tree. Free with xml_free() - + * @retval 0 OK + * @retval -1 Error * @code * if (xmldb_get(xh, "running", "/interfaces/interface[name="eth"]", &xt) < 0) * err; diff --git a/test/test_transaction.sh b/test/test_transaction.sh index 3699a9b5..69bddea0 100755 --- a/test/test_transaction.sh +++ b/test/test_transaction.sh @@ -87,6 +87,7 @@ checklog(){ s=$1 # statement l0=$2 # linenr new "Check $s in log" +# echo "grep \"transaction_log $s\" $flog" t=$(grep -n "transaction_log $s" $flog) if [ -z "$t" ]; then echo -e "\e[31m\nError in Test$testnr [$testname]:" @@ -124,7 +125,7 @@ if [ $BE -ne 0 ]; then sleep $RCWAIT fi -let nr=1 +let nr=0 new "Basic transaction to add top-level x" expecteof "$clixon_netconf -qf $cfg" 0 "$nr]]>]]>" '^]]>]]>$'