From 0554bf024077f6772f9b543c3f45d4f38b6e321a Mon Sep 17 00:00:00 2001 From: jfreegman Date: Wed, 18 Nov 2020 17:19:08 -0500 Subject: [PATCH] Create backup of duplicate log file instead of deleting it This case should never occur, but just in case it does it's good to handle it without any data loss --- src/log.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/log.c b/src/log.c index 4460500..2d3e03e 100644 --- a/src/log.c +++ b/src/log.c @@ -358,8 +358,15 @@ int rename_logfile(const char *src, const char *dest, const char *selfkey, const } if (file_exists(newpath)) { - if (remove(oldpath) != 0) { - fprintf(stderr, "Failed to remove old path `%s`\n", oldpath); + char new_backup[MAX_STR_SIZE + 4]; + snprintf(new_backup, sizeof(new_backup), "%s.old", newpath); + + if (file_exists(new_backup)) { + goto on_error; + } + + if (rename(newpath, new_backup) != 0) { + goto on_error; } } else { if (rename(oldpath, newpath) != 0) {