mgmtd: fix uninit warning and cleanup history error messages

Signed-off-by: Christian Hopps <chopps@labn.net>
This commit is contained in:
Christian Hopps 2023-05-18 08:42:42 -04:00
parent 72207cfe9a
commit 0030b5825e
2 changed files with 29 additions and 33 deletions

View File

@ -542,7 +542,7 @@ int mgmt_ds_iter_data(struct mgmt_ds_ctx *ds_ctx, char *base_xpath,
void *ctx), void *ctx),
void *ctx, bool alloc_xp_copy) void *ctx, bool alloc_xp_copy)
{ {
int ret; int ret = 0;
char xpath[MGMTD_MAX_XPATH_LEN]; char xpath[MGMTD_MAX_XPATH_LEN];
struct lyd_node *base_dnode = NULL; struct lyd_node *base_dnode = NULL;
struct lyd_node *node; struct lyd_node *node;

View File

@ -35,23 +35,18 @@ DECLARE_DLIST(mgmt_cmt_infos, struct mgmt_cmt_info_t, cmts);
*/ */
static struct vty *rollback_vty; static struct vty *rollback_vty;
static bool mgmt_history_record_exists(char *file_path) static bool file_exists(const char *path)
{ {
int exist; return !access(path, F_OK);
exist = access(file_path, F_OK);
if (exist == 0)
return true;
else
return false;
} }
static void mgmt_history_remove_file(char *name) static void remove_file(const char *path)
{ {
if (remove(name) == 0) if (!file_exists(path))
zlog_debug("Old commit info deletion succeeded"); return;
else if (unlink(path))
zlog_err("Old commit info deletion failed"); zlog_err("Failed to remove commit history file %s: %s", path,
safe_strerror(errno));
} }
static struct mgmt_cmt_info_t *mgmt_history_new_cmt_info(void) static struct mgmt_cmt_info_t *mgmt_history_new_cmt_info(void)
@ -84,7 +79,7 @@ static struct mgmt_cmt_info_t *mgmt_history_create_cmt_rec(void)
last_cmt_info = cmt_info; last_cmt_info = cmt_info;
if (last_cmt_info) { if (last_cmt_info) {
mgmt_history_remove_file(last_cmt_info->cmt_json_file); remove_file(last_cmt_info->cmt_json_file);
mgmt_cmt_infos_del(&mm->cmts, last_cmt_info); mgmt_cmt_infos_del(&mm->cmts, last_cmt_info);
XFREE(MTYPE_MGMTD_CMT_INFO, last_cmt_info); XFREE(MTYPE_MGMTD_CMT_INFO, last_cmt_info);
} }
@ -114,20 +109,21 @@ static bool mgmt_history_read_cmt_record_index(void)
struct mgmt_cmt_info_t *new; struct mgmt_cmt_info_t *new;
int cnt = 0; int cnt = 0;
if (!file_exists(MGMTD_COMMIT_FILE_PATH))
return false;
fp = fopen(MGMTD_COMMIT_INDEX_FILE_NAME, "rb"); fp = fopen(MGMTD_COMMIT_INDEX_FILE_NAME, "rb");
if (!fp) { if (!fp) {
zlog_err("Failed to open file %s rb mode", zlog_err("Failed to open commit history %s for reading: %s",
MGMTD_COMMIT_INDEX_FILE_NAME); MGMTD_COMMIT_INDEX_FILE_NAME, safe_strerror(errno));
return false; return false;
} }
while ((fread(&cmt_info, sizeof(cmt_info), 1, fp)) > 0) { while ((fread(&cmt_info, sizeof(cmt_info), 1, fp)) > 0) {
if (cnt < MGMTD_MAX_COMMIT_LIST) { if (cnt < MGMTD_MAX_COMMIT_LIST) {
if (!mgmt_history_record_exists( if (!file_exists(cmt_info.cmt_json_file)) {
cmt_info.cmt_json_file)) { zlog_err("Commit in index, but file %s missing",
zlog_err( cmt_info.cmt_json_file);
"Commit record present in index_file, but commit file %s missing",
cmt_info.cmt_json_file);
continue; continue;
} }
@ -136,8 +132,9 @@ static bool mgmt_history_read_cmt_record_index(void)
memcpy(new, &cmt_info, sizeof(struct mgmt_cmt_info_t)); memcpy(new, &cmt_info, sizeof(struct mgmt_cmt_info_t));
mgmt_cmt_infos_add_tail(&mm->cmts, new); mgmt_cmt_infos_add_tail(&mm->cmts, new);
} else { } else {
zlog_err("More records found in index file %s", zlog_warn(
MGMTD_COMMIT_INDEX_FILE_NAME); "More records found in commit history file %s than expected",
MGMTD_COMMIT_INDEX_FILE_NAME);
fclose(fp); fclose(fp);
return false; return false;
} }
@ -157,11 +154,10 @@ static bool mgmt_history_dump_cmt_record_index(void)
struct mgmt_cmt_info_t cmt_info_set[10]; struct mgmt_cmt_info_t cmt_info_set[10];
int cnt = 0; int cnt = 0;
mgmt_history_remove_file((char *)MGMTD_COMMIT_INDEX_FILE_NAME); fp = fopen(MGMTD_COMMIT_INDEX_FILE_NAME, "wb");
fp = fopen(MGMTD_COMMIT_INDEX_FILE_NAME, "ab");
if (!fp) { if (!fp) {
zlog_err("Failed to open file %s ab mode", zlog_err("Failed to open commit history %s for writing: %s",
MGMTD_COMMIT_INDEX_FILE_NAME); MGMTD_COMMIT_INDEX_FILE_NAME, safe_strerror(errno));
return false; return false;
} }
@ -179,11 +175,11 @@ static bool mgmt_history_dump_cmt_record_index(void)
ret = fwrite(&cmt_info_set, sizeof(struct mgmt_cmt_info_t), cnt, fp); ret = fwrite(&cmt_info_set, sizeof(struct mgmt_cmt_info_t), cnt, fp);
fclose(fp); fclose(fp);
if (ret != cnt) { if (ret != cnt) {
zlog_err("Write record failed"); zlog_err("Failed to write full commit history, removing file");
remove_file(MGMTD_COMMIT_INDEX_FILE_NAME);
return false; return false;
} else {
return true;
} }
return true;
} }
static int mgmt_history_rollback_to_cmt(struct vty *vty, static int mgmt_history_rollback_to_cmt(struct vty *vty,
@ -281,7 +277,7 @@ int mgmt_history_rollback_by_id(struct vty *vty, const char *cmtid_str)
return ret; return ret;
} }
mgmt_history_remove_file(cmt_info->cmt_json_file); remove_file(cmt_info->cmt_json_file);
mgmt_cmt_infos_del(&mm->cmts, cmt_info); mgmt_cmt_infos_del(&mm->cmts, cmt_info);
XFREE(MTYPE_MGMTD_CMT_INFO, cmt_info); XFREE(MTYPE_MGMTD_CMT_INFO, cmt_info);
} }
@ -322,7 +318,7 @@ int mgmt_history_rollback_n(struct vty *vty, int num_cmts)
} }
cnt++; cnt++;
mgmt_history_remove_file(cmt_info->cmt_json_file); remove_file(cmt_info->cmt_json_file);
mgmt_cmt_infos_del(&mm->cmts, cmt_info); mgmt_cmt_infos_del(&mm->cmts, cmt_info);
XFREE(MTYPE_MGMTD_CMT_INFO, cmt_info); XFREE(MTYPE_MGMTD_CMT_INFO, cmt_info);
} }