File logrotate-rename-existing-file.patch of Package logrotate.5563

From fc1c3eff61edf8e9f0a4bfa980f3a6030a6b271f Mon Sep 17 00:00:00 2001
From: Mathieu Parent <Mathieu.PARENT@nantesmetropole.fr>
Date: Tue, 8 Mar 2016 16:56:50 +0100
Subject: [PATCH] createOutputFile: rename already existing file

See https://bugs.debian.org/734688

Closes #23
---
 logrotate.c            | 20 ++++++++++++++++++--
 test/test              | 29 ++++++++++++++++++++++++++++-
 test/test-config.72.in |  7 +++++++
 3 files changed, 53 insertions(+), 3 deletions(-)
 create mode 100644 test/test-config.72.in

Index: logrotate-3.8.7/logrotate.c
===================================================================
--- logrotate-3.8.7.orig/logrotate.c
+++ logrotate-3.8.7/logrotate.c
@@ -310,8 +310,24 @@ static int runScript(struct logInfo *log
 int createOutputFile(char *fileName, int flags, struct stat *sb, acl_type acl, int force_mode)
 {
     int fd;
-	struct stat sb_create;
-	int acl_set = 0;
+    struct stat sb_create;
+    int acl_set = 0;
+
+    if (stat(fileName, &sb_create) == 0) {
+	/* the destination file already exists, while it should not */
+	struct tm now = *localtime(&nowSecs);
+	size_t fileName_size = strlen(fileName);
+	char* backupName = alloca(fileName_size + sizeof("-YYYYMMDDHH.backup"));
+	strncpy(backupName, fileName, fileName_size);
+	size_t date_size=strftime(backupName+fileName_size, 12, "-%Y%m%d%H", &now);
+	strncpy(backupName+fileName_size+date_size, ".backup\0", 8);
+	message(MESS_ERROR, "destination %s already exists, renaming to %s\n", fileName, backupName);
+	if (rename(fileName, backupName) != 0) {
+	    message(MESS_ERROR, "error renaming already existing output file %s to %s: %s\n",
+		    fileName, backupName, strerror(errno));
+	    return -1;
+	}
+    }
 
 	fd = open(fileName, (flags | O_EXCL | O_NOFOLLOW),
 		(S_IRUSR | S_IWUSR) & sb->st_mode);
Index: logrotate-3.8.7/test/test
===================================================================
--- logrotate-3.8.7.orig/test/test
+++ logrotate-3.8.7/test/test
@@ -1345,9 +1345,36 @@ test.log 0
 test.log.1 0 zero
 EOF
 
-# check rotation with extension appended to the filename
+cleanup 72
+
+# ------------------------------- Test 72 ------------------------------------
+preptest test.log 72 2
+
+$RLR test-config.72 --force
+
+checkoutput <<EOF
+test.log 0
+test.log.1 0 zero
+test.log.2.gz 1 first
+EOF
+
+echo 'unexpected' > test.log.1.gz
+
+$RLR test-config.72 --force
+dt="$(date +%Y%m%d%H)"
+
+checkoutput <<EOF
+test.log 0
+test.log.1 0
+test.log.1.gz-$dt.backup 0 unexpected
+test.log.2.gz 1 zero
+test.log.3.gz 1 first
+EOF
+
 cleanup 100
 
+# ------------------------------- Test 100 ------------------------------------
+# check rotation with extension appended to the filename
 preptest test.log 100 1 0
 $RLR test-config.100 --force
 
Index: logrotate-3.8.7/test/test-config.72.in
===================================================================
--- /dev/null
+++ logrotate-3.8.7/test/test-config.72.in
@@ -0,0 +1,7 @@
+&DIR&/test.log {
+    daily
+    rotate 3
+    compress
+    delaycompress
+    create
+}
openSUSE Build Service is sponsored by