Skip to content

Commit 12656f2

Browse files
committed
Issue 631: Private candidate datastores are not deleted
1 parent 02c9d32 commit 12656f2

File tree

6 files changed

+48
-24
lines changed

6 files changed

+48
-24
lines changed

apps/backend/backend_client.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ release_all_dbs(clixon_handle h,
183183
goto done;
184184
if (de0 != NULL){
185185
if (clicon_option_bool(h, "CLICON_XMLDB_PRIVATE_CANDIDATE")) {
186-
if (xmldb_delete(h, db0) < 0)
186+
if (xmldb_delete(h, db0, 1) < 0)
187187
goto done;
188188
if (xmldb_candidate_find(h, "candidate-orig", ceid, NULL, &db1) < 0)
189189
goto done;
190190
if (db1 != NULL){
191-
if (xmldb_delete(h, db1) < 0)
191+
if (xmldb_delete(h, db1, 1) < 0)
192192
goto done;
193193
}
194194
}
@@ -764,12 +764,12 @@ from_client_edit_config(clixon_handle h,
764764
char *db1 = NULL;
765765

766766
/* Remove candidate and candidate-orig*/
767-
if (xmldb_delete(h, xmldb_name_get(de)) < 0)
767+
if (xmldb_delete(h, xmldb_name_get(de), 1) < 0)
768768
goto done;
769769
if (xmldb_candidate_find(h, "candidate-orig", ce->ce_id, NULL, &db1) < 0)
770770
goto done;
771771
if (db1 != NULL){
772-
if (xmldb_delete(h, db1) < 0)
772+
if (xmldb_delete(h, db1, 1) < 0)
773773
goto done;
774774
}
775775
}
@@ -968,7 +968,7 @@ from_client_delete_config(clixon_handle h,
968968
goto done;
969969
goto ok;
970970
}
971-
if (xmldb_delete(h, target) < 0){
971+
if (xmldb_delete(h, target, 0) < 0){
972972
if ((cbmsg = cbuf_new()) == NULL){
973973
clixon_err(OE_UNIX, errno, "cbuf_new");
974974
goto done;
@@ -988,7 +988,7 @@ from_client_delete_config(clixon_handle h,
988988
clixon_err(OE_DB, 0, "candidate-orig not found");
989989
goto done;
990990
}
991-
if (xmldb_delete(h, db1) < 0)
991+
if (xmldb_delete(h, db1, 1) < 0)
992992
goto done;
993993
}
994994
else {

apps/backend/backend_commit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ startup_commit(clixon_handle h,
461461
goto done;
462462
/* [Delete and] create running db */
463463
if (xmldb_exists(h, "running") == 1){
464-
if (xmldb_delete(h, "running") != 0 && errno != ENOENT)
464+
if (xmldb_delete(h, "running", 0) != 0 && errno != ENOENT)
465465
goto done;;
466466
}
467467
if (xmldb_create(h, "running") < 0)
@@ -983,12 +983,12 @@ from_client_commit(clixon_handle h,
983983
char *db1 = NULL;
984984

985985
/* Remove candidate and candidate-orig*/
986-
if (xmldb_delete(h, xmldb_name_get(de)) < 0)
986+
if (xmldb_delete(h, xmldb_name_get(de), 1) < 0)
987987
goto done;
988988
if (xmldb_candidate_find(h, "candidate-orig", ce->ce_id, NULL, &db1) < 0)
989989
goto done;
990990
if (db1 != NULL){
991-
if (xmldb_delete(h, db1) < 0)
991+
if (xmldb_delete(h, db1, 1) < 0)
992992
goto done;
993993
}
994994
}

apps/backend/backend_main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ main(int argc,
861861
if (startup_mode == SM_RUNNING_STARTUP)
862862
startup_mode = SM_RUNNING;
863863
if (!clicon_option_bool(h, "CLICON_XMLDB_PRIVATE_CANDIDATE"))
864-
xmldb_delete(h, "candidate");
864+
xmldb_delete(h, "candidate", 0);
865865
/* If startup fails, lib functions report invalidation info in a cbuf */
866866
if ((cbret = cbuf_new()) == NULL){
867867
clixon_err(OE_XML, errno, "cbuf_new");
@@ -870,7 +870,7 @@ main(int argc,
870870
switch (startup_mode){
871871
case SM_INIT: /* Scratch running and start from empty */
872872
/* Delete any rollback database, if it exists */
873-
xmldb_delete(h, "rollback");
873+
xmldb_delete(h, "rollback", 0);
874874
/* [Delete and] create running db */
875875
if (xmldb_db_reset(h, "running") < 0)
876876
goto done;
@@ -889,7 +889,7 @@ main(int argc,
889889
if (ret != 1)
890890
if (xmldb_copy(h, "tmp", "running") < 0)
891891
goto done;
892-
xmldb_delete(h, "tmp");
892+
xmldb_delete(h, "tmp", 0);
893893
if (ret2status(ret, &status) < 0)
894894
goto done;
895895
break;

lib/clixon/clixon_datastore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ uint32_t xmldb_islocked(clixon_handle h, const char *db);
9595
int xmldb_lock_timestamp(clixon_handle h, const char *db, struct timeval *tv);
9696
int xmldb_exists(clixon_handle h, const char *db);
9797
int xmldb_clear(clixon_handle h, const char *db);
98-
int xmldb_delete(clixon_handle h, const char *db);
98+
int xmldb_delete(clixon_handle h, const char *db, int rm);
9999
int xmldb_create(clixon_handle h, const char *db);
100100
/* utility functions */
101101
int xmldb_db_reset(clixon_handle h, const char *db);

lib/src/clixon_datastore.c

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -888,18 +888,20 @@ xmldb_clear(clixon_handle h,
888888
return 0;
889889
}
890890

891-
/*! Delete database, clear cache if any. Remove file and dir
891+
/*! Delete database, clear cache if any. Remove or truncate file and dir
892892
*
893893
* @param[in] h Clixon handle
894894
* @param[in] db Database
895+
* @param[in] rm Remove file if non-zero, else truncate file
895896
* @retval 0 OK
896897
* @retval -1 Error
897898
* @note Datastores / dirs are not actually deleted so that a backend after dropping priviliges
898899
* can re-create them
899900
*/
900901
int
901902
xmldb_delete(clixon_handle h,
902-
const char *db)
903+
const char *db,
904+
int rm)
903905
{
904906
int retval = -1;
905907
char *filename = NULL;
@@ -916,12 +918,21 @@ xmldb_delete(clixon_handle h,
916918
goto done;
917919
if (xmldb_db2file(h, db, &filename) < 0)
918920
goto done;
919-
if (lstat(filename, &st) == 0)
920-
if (truncate(filename, 0) < 0){
921-
clixon_err(OE_DB, errno, "truncate %s", filename);
922-
goto done;
921+
if (lstat(filename, &st) == 0){
922+
if (!rm){
923+
if (truncate(filename, 0) < 0){
924+
clixon_err(OE_DB, errno, "truncate %s", filename);
925+
goto done;
926+
}
923927
}
924-
if (clicon_option_bool(h, "CLICON_XMLDB_MULTI")){
928+
else {
929+
if (unlink(filename) < 0){
930+
clixon_err(OE_DB, errno, "unlink %s", filename);
931+
goto done;
932+
}
933+
}
934+
}
935+
if (clicon_option_bool(h, "CLICON_XMLDB_MULTI")){
925936
if (xmldb_db2subdir(h, db, &subdir) < 0)
926937
goto done;
927938
if (stat(subdir, &st) == 0){
@@ -934,9 +945,17 @@ xmldb_delete(clixon_handle h,
934945
for (i = 0; i < ndp; i++){
935946
cbuf_reset(cb);
936947
cprintf(cb, "%s/%s", subdir, dp[i].d_name);
937-
if (truncate(cbuf_get(cb), 0) < 0){
938-
clixon_err(OE_DB, errno, "truncate %s", filename);
939-
goto done;
948+
if (!rm){
949+
if (truncate(cbuf_get(cb), 0) < 0){
950+
clixon_err(OE_DB, errno, "truncate %s", filename);
951+
goto done;
952+
}
953+
}
954+
else {
955+
if (unlink(filename) < 0){
956+
clixon_err(OE_DB, errno, "unlink %s", filename);
957+
goto done;
958+
}
940959
}
941960
}
942961
}
@@ -1012,7 +1031,7 @@ xmldb_db_reset(clixon_handle h,
10121031
const char *db)
10131032
{
10141033
if (xmldb_exists(h, db) == 1){
1015-
if (xmldb_delete(h, db) != 0 && errno != ENOENT)
1034+
if (xmldb_delete(h, db, 0) != 0 && errno != ENOENT)
10161035
return -1;
10171036
}
10181037
if (xmldb_create(h, db) < 0)

test/test_netconf_privcand.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,11 @@ if [ $? -ne 0 ]; then
645645
err1 "Failed: test private candidate using expect"
646646
fi
647647

648+
new "Issue 631: Private candidate datastores are not deleted"
649+
if ls $dir/db/candidate* >/dev/null 2>&1; then
650+
err
651+
fi
652+
648653
if [ $BE -ne 0 ]; then
649654
new "Kill backend"
650655
# Check if premature kill

0 commit comments

Comments
 (0)