|
61 | 61 | #include <arpa/inet.h> |
62 | 62 | #include <netinet/in.h> |
63 | 63 | #include <libgen.h> |
64 | | - |
| 64 | +#include <ftw.h> |
65 | 65 | /* cligen */ |
66 | 66 | #include <cligen/cligen.h> |
67 | 67 |
|
@@ -418,6 +418,56 @@ usage(clixon_handle h, |
418 | 418 | exit(-1); |
419 | 419 | } |
420 | 420 |
|
| 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 | + |
421 | 471 | /* Clixon backend application main entry point |
422 | 472 | */ |
423 | 473 | int |
@@ -659,7 +709,6 @@ main(int argc, |
659 | 709 | /* Defer: Wait to the last minute to print help message */ |
660 | 710 | if (help) |
661 | 711 | usage(h, argv[0]); |
662 | | - |
663 | 712 | /* Init cligen buffers */ |
664 | 713 | cligen_buflen = clicon_option_int(h, "CLICON_CLI_BUF_START"); |
665 | 714 | cligen_bufthreshold = clicon_option_int(h, "CLICON_CLI_BUF_THRESHOLD"); |
@@ -860,7 +909,11 @@ main(int argc, |
860 | 909 | else |
861 | 910 | if (startup_mode == SM_RUNNING_STARTUP) |
862 | 911 | 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 |
864 | 917 | xmldb_delete(h, "candidate"); |
865 | 918 | /* If startup fails, lib functions report invalidation info in a cbuf */ |
866 | 919 | if ((cbret = cbuf_new()) == NULL){ |
|
0 commit comments