struct mgmt_cmt_info_t {
struct mgmt_cmt_infos_item cmts;
- char cmtid_str[MGMTD_MD5_HASH_STR_HEX_LEN];
- char time_str[MGMTD_COMMIT_TIME_STR_LEN];
+ char cmtid_str[MGMT_SHORT_TIME_MAX_LEN];
+ char time_str[MGMT_LONG_TIME_MAX_LEN];
char cmt_json_file[PATH_MAX];
};
*/
static struct vty *rollback_vty;
-static bool mgmt_history_record_exists(char *file_path)
+static bool file_exists(const char *path)
{
- int exist;
-
- exist = access(file_path, F_OK);
- if (exist == 0)
- return true;
- else
- return false;
+ return !access(path, F_OK);
}
-static void mgmt_history_remove_file(char *name)
+static void remove_file(const char *path)
{
- if (remove(name) == 0)
- zlog_debug("Old commit info deletion succeeded");
- else
- zlog_err("Old commit info deletion failed");
+ if (!file_exists(path))
+ return;
+ if (unlink(path))
+ zlog_err("Failed to remove commit history file %s: %s", path,
+ safe_strerror(errno));
}
-static void mgmt_history_hash(const char *input_str, char *hash)
+static struct mgmt_cmt_info_t *mgmt_history_new_cmt_info(void)
{
- int i;
- unsigned char digest[MGMTD_MD5_HASH_LEN];
- MD5_CTX ctx;
-
- memset(&ctx, 0, sizeof(ctx));
- MD5Init(&ctx);
- MD5Update(&ctx, input_str, strlen(input_str));
- MD5Final(digest, &ctx);
-
- for (i = 0; i < MGMTD_MD5_HASH_LEN; i++)
- snprintf(&hash[i * 2], MGMTD_MD5_HASH_STR_HEX_LEN, "%02x",
- (unsigned int)digest[i]);
+ struct mgmt_cmt_info_t *new;
+ struct timespec tv;
+ struct tm tm;
+
+ new = XCALLOC(MTYPE_MGMTD_CMT_INFO, sizeof(struct mgmt_cmt_info_t));
+
+ clock_gettime(CLOCK_REALTIME, &tv);
+ localtime_r(&tv.tv_sec, &tm);
+
+ mgmt_time_to_string(&tv, true, new->time_str, sizeof(new->time_str));
+ mgmt_time_to_string(&tv, false, new->cmtid_str, sizeof(new->cmtid_str));
+ snprintf(new->cmt_json_file, sizeof(new->cmt_json_file),
+ MGMTD_COMMIT_FILE_PATH, new->cmtid_str);
+
+ return new;
}
static struct mgmt_cmt_info_t *mgmt_history_create_cmt_rec(void)
{
- struct mgmt_cmt_info_t *new;
+ struct mgmt_cmt_info_t *new = mgmt_history_new_cmt_info();
struct mgmt_cmt_info_t *cmt_info;
struct mgmt_cmt_info_t *last_cmt_info = NULL;
- struct timeval cmt_recd_tv;
-
- new = XCALLOC(MTYPE_MGMTD_CMT_INFO, sizeof(struct mgmt_cmt_info_t));
- gettimeofday(&cmt_recd_tv, NULL);
- mgmt_realtime_to_string(&cmt_recd_tv, new->time_str,
- sizeof(new->time_str));
- mgmt_history_hash(new->time_str, new->cmtid_str);
- snprintf(new->cmt_json_file, sizeof(new->cmt_json_file) - 1,
- MGMTD_COMMIT_FILE_PATH, new->cmtid_str);
if (mgmt_cmt_infos_count(&mm->cmts) == MGMTD_MAX_COMMIT_LIST) {
FOREACH_CMT_REC (mm, cmt_info)
last_cmt_info = 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);
XFREE(MTYPE_MGMTD_CMT_INFO, last_cmt_info);
}
struct mgmt_cmt_info_t *cmt_info;
FOREACH_CMT_REC (mm, cmt_info) {
- if (strncmp(cmt_info->cmtid_str, cmtid_str,
- MGMTD_MD5_HASH_STR_HEX_LEN) == 0)
+ if (strcmp(cmt_info->cmtid_str, cmtid_str) == 0)
return cmt_info;
}
struct mgmt_cmt_info_t *new;
int cnt = 0;
+ if (!file_exists(MGMTD_COMMIT_FILE_PATH))
+ return false;
+
fp = fopen(MGMTD_COMMIT_INDEX_FILE_NAME, "rb");
if (!fp) {
- zlog_err("Failed to open file %s rb mode",
- MGMTD_COMMIT_INDEX_FILE_NAME);
+ zlog_err("Failed to open commit history %s for reading: %s",
+ MGMTD_COMMIT_INDEX_FILE_NAME, safe_strerror(errno));
return false;
}
while ((fread(&cmt_info, sizeof(cmt_info), 1, fp)) > 0) {
if (cnt < MGMTD_MAX_COMMIT_LIST) {
- if (!mgmt_history_record_exists(
- cmt_info.cmt_json_file)) {
- zlog_err(
- "Commit record present in index_file, but commit file %s missing",
- cmt_info.cmt_json_file);
+ if (!file_exists(cmt_info.cmt_json_file)) {
+ zlog_err("Commit in index, but file %s missing",
+ cmt_info.cmt_json_file);
continue;
}
memcpy(new, &cmt_info, sizeof(struct mgmt_cmt_info_t));
mgmt_cmt_infos_add_tail(&mm->cmts, new);
} else {
- zlog_err("More records found in index file %s",
- MGMTD_COMMIT_INDEX_FILE_NAME);
+ zlog_warn(
+ "More records found in commit history file %s than expected",
+ MGMTD_COMMIT_INDEX_FILE_NAME);
fclose(fp);
return false;
}
struct mgmt_cmt_info_t cmt_info_set[10];
int cnt = 0;
- mgmt_history_remove_file((char *)MGMTD_COMMIT_INDEX_FILE_NAME);
- fp = fopen(MGMTD_COMMIT_INDEX_FILE_NAME, "ab");
+ fp = fopen(MGMTD_COMMIT_INDEX_FILE_NAME, "wb");
if (!fp) {
- zlog_err("Failed to open file %s ab mode",
- MGMTD_COMMIT_INDEX_FILE_NAME);
+ zlog_err("Failed to open commit history %s for writing: %s",
+ MGMTD_COMMIT_INDEX_FILE_NAME, safe_strerror(errno));
return false;
}
ret = fwrite(&cmt_info_set, sizeof(struct mgmt_cmt_info_t), cnt, fp);
fclose(fp);
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;
- } else {
- return true;
}
+ return true;
}
static int mgmt_history_rollback_to_cmt(struct vty *vty,
}
FOREACH_CMT_REC (mm, cmt_info) {
- if (strncmp(cmt_info->cmtid_str, cmtid_str,
- MGMTD_MD5_HASH_STR_HEX_LEN) == 0) {
+ if (strcmp(cmt_info->cmtid_str, cmtid_str) == 0) {
ret = mgmt_history_rollback_to_cmt(vty, cmt_info,
false);
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);
XFREE(MTYPE_MGMTD_CMT_INFO, cmt_info);
}
}
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);
XFREE(MTYPE_MGMTD_CMT_INFO, cmt_info);
}
int slno = 0;
vty_out(vty, "Last 10 commit history:\n");
- vty_out(vty, " Sl.No\tCommit-ID(HEX)\t\t\t Commit-Record-Time\n");
+ vty_out(vty, "Slot Commit-ID Commit-Record-Time\n");
FOREACH_CMT_REC (mm, cmt_info) {
- vty_out(vty, " %d\t%s %s\n", slno, cmt_info->cmtid_str,
+ vty_out(vty, "%4d %23s %s\n", slno, cmt_info->cmtid_str,
cmt_info->time_str);
slno++;
}