Skip to content

Commit 706f701

Browse files
joc-seolofhagsand
authored andcommitted
clean up old private candidate files on startup
1 parent 253da77 commit 706f701

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

apps/backend/backend_main.c

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
#include <arpa/inet.h>
6262
#include <netinet/in.h>
6363
#include <libgen.h>
64-
64+
#include <ftw.h>
6565
/* cligen */
6666
#include <cligen/cligen.h>
6767

@@ -418,6 +418,56 @@ usage(clixon_handle h,
418418
exit(-1);
419419
}
420420

421+
/* Function called by ftw, removes the file if the string "candidate" is present in the path */
422+
static
423+
int ftw_rm_candidate_file(const char *fpath, const struct stat *sb, int tflag) {
424+
if (tflag == FTW_F && strstr(fpath, "candidate") != NULL)
425+
return unlink(fpath);
426+
return 0;
427+
}
428+
/* Function called by ftw, removes the directory if the string "candidate" is present in the path */
429+
static
430+
int ftw_rm_candidate_dir(const char *fpath, const struct stat *sb, int tflag) {
431+
if (tflag == FTW_D && strstr(fpath, "candidate") != NULL)
432+
return rmdir(fpath);
433+
return 0;
434+
}
435+
436+
/* Clear any candidate information that may remain in the file system
437+
* if the previous execution did not terminate properly:
438+
* candidate.db
439+
* candidate.123.db
440+
* candidate_orig.123.db
441+
* candidate.d/0.xml
442+
*
443+
* @param[in] h Clixon handle
444+
* @retval 0 OK
445+
* @retval -1 Error
446+
*/
447+
static
448+
int xmldb_cleanup_candidates(clixon_handle h) {
449+
int retval = -1;
450+
char *dir;
451+
452+
if ((dir = clicon_xmldb_dir(h)) == NULL){
453+
clixon_err(OE_FATAL, 0, "CLICON_XMLDB_DIR not set");
454+
goto done;
455+
}
456+
/* First file tree walk to removes all files where the string "candidate" is present in the path. */
457+
if (ftw(dir, ftw_rm_candidate_file, 15) < 0) {
458+
clixon_err(OE_FATAL, 0, "Cannot delete candidate files");
459+
goto done;
460+
}
461+
/* Second file tree walk to removes all empty directories where the string "candidate" is present in the path. */
462+
if (ftw(dir, ftw_rm_candidate_dir, 15) < 0) {
463+
clixon_err(OE_FATAL, 0, "Cannot delete candidate directories");
464+
goto done;
465+
}
466+
retval = 0;
467+
done:
468+
return retval;
469+
}
470+
421471
/* Clixon backend application main entry point
422472
*/
423473
int
@@ -659,7 +709,6 @@ main(int argc,
659709
/* Defer: Wait to the last minute to print help message */
660710
if (help)
661711
usage(h, argv[0]);
662-
663712
/* Init cligen buffers */
664713
cligen_buflen = clicon_option_int(h, "CLICON_CLI_BUF_START");
665714
cligen_bufthreshold = clicon_option_int(h, "CLICON_CLI_BUF_THRESHOLD");
@@ -860,7 +909,11 @@ main(int argc,
860909
else
861910
if (startup_mode == SM_RUNNING_STARTUP)
862911
startup_mode = SM_RUNNING;
863-
if (!clicon_option_bool(h, "CLICON_XMLDB_PRIVATE_CANDIDATE"))
912+
if (clicon_option_bool(h, "CLICON_XMLDB_PRIVATE_CANDIDATE")){
913+
if (xmldb_cleanup_candidates(h) < 0)
914+
goto done;
915+
}
916+
else
864917
xmldb_delete(h, "candidate");
865918
/* If startup fails, lib functions report invalidation info in a cbuf */
866919
if ((cbret = cbuf_new()) == NULL){

test/test_netconf_privcand.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ cat <<EOF > $dbdir/startup_db
168168
</${DATASTORE_TOP}>"
169169
EOF
170170

171+
# Add candidate datastore files to be removed by backend
172+
echo "left-behind" > $dir/db/candidate.333.db
173+
echo "left-behind" > $dir/db/candidate_orig.333.db
174+
mkdir $dir/db/candidate.444.d/
175+
echo "multi" > $dir/db/candidate.444.d/sha.db
176+
171177
# Bring your own backend
172178
if [ $BE -ne 0 ]; then
173179
# kill old backend (if any)

0 commit comments

Comments
 (0)