Blame SOURCES/logrotate-3.8.6-weekly.patch

285127
From 7cb2dde2b3423158f5cba06df0df078ab3bee09b Mon Sep 17 00:00:00 2001
285127
From: Kamil Dudka <kdudka@redhat.com>
285127
Date: Wed, 7 Dec 2016 16:34:13 +0100
285127
Subject: [PATCH] weekly: trigger the rotation more predictably
285127
285127
... by ignoring the exact time.  If the (absolute) day counter
285127
advances by 7+ days since the last rotation, a new rotation is
285127
triggered.
285127
285127
Additionally, introduce an optional argument of the 'weekly' directive
285127
to trigger the rotation on a selected day of the week.  If the argument
285127
is omitted, default to Sunday to preserve backward compatibility.
285127
285127
Closes #93
285127
285127
Upstream-commit: bd2638856dbbb6c0a47beb85fe8a8a628160772e
285127
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
285127
---
285127
 config.c    | 19 +++++++++++++++++++
285127
 logrotate.8 | 12 ++++++------
285127
 logrotate.c | 39 +++++++++++++++++++++++++++++----------
285127
 logrotate.h |  3 ++-
285127
 4 files changed, 56 insertions(+), 17 deletions(-)
285127
285127
diff --git a/config.c b/config.c
285127
index 5e7951e..700ad85 100644
285127
--- a/config.c
285127
+++ b/config.c
285127
@@ -410,6 +410,7 @@ static void copyLogInfo(struct logInfo *to, struct logInfo *from)
285127
     if (from->oldDir)
285127
 	to->oldDir = strdup(from->oldDir);
285127
     to->criterium = from->criterium;
285127
+    to->weekday = from->weekday;
285127
     to->threshhold = from->threshhold;
285127
     to->minsize = from->minsize;
285127
 	to->maxsize = from->maxsize;
285127
@@ -1050,7 +1051,25 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
285127
 				} else if (!strcmp(key, "monthly")) {
285127
 					newlog->criterium = ROT_MONTHLY;
285127
 				} else if (!strcmp(key, "weekly")) {
285127
+					unsigned weekday;
285127
+					char tmp;
285127
 					newlog->criterium = ROT_WEEKLY;
285127
+					free(key);
285127
+					key = isolateLine(&start, &buf, length);
285127
+					if (key == NULL || key[0] == '\0') {
285127
+						/* default to Sunday if no argument was given */
285127
+						newlog->weekday = 0;
285127
+						continue;
285127
+					}
285127
+
285127
+					if (1 == sscanf(key, "%u%c", &weekday, &tmp) && weekday <= 7) {
285127
+						/* use the selected weekday, 7 means "once per week" */
285127
+						newlog->weekday = weekday;
285127
+						continue;
285127
+					}
285127
+					message(MESS_ERROR, "%s:%d bad weekly directive '%s'\n",
285127
+							configFile, lineNum, key);
285127
+					goto error;
285127
 				} else if (!strcmp(key, "yearly")) {
285127
 					newlog->criterium = ROT_YEARLY;
285127
 				} else if (!strcmp(key, "rotate")) {
285127
diff --git a/logrotate.8 b/logrotate.8
285127
index 2db6f65..468ba0e 100644
285127
--- a/logrotate.8
285127
+++ b/logrotate.8
285127
@@ -526,12 +526,12 @@ is replaced. At startup, the taboo extension list
285127
 contains .rpmsave, .rpmorig, ~, .disabled, .dpkg\-old, .dpkg\-dist, .dpkg\-new, .cfsaved, .ucf\-old, .ucf\-dist, .ucf\-new, .rpmnew, .swp, .cfsaved, .rhn\-cfg\-tmp\-*
285127
 
285127
 .TP
285127
-\fBweekly\fR
285127
-Log files are rotated if the current weekday is less than the weekday
285127
-of the last rotation or if more than a week has passed since the last
285127
-rotation. This is normally the same as rotating logs on the first day
285127
-of the week, but it works better if \fIlogrotate\fR is not run every
285127
-night.
285127
+\fBweekly\fR [\fIweekday\fR]
285127
+Log files are rotated once each \fIweekday\fR, or if the date is advanced by at
285127
+least 7 days since the last rotation (while ignoring the exact time).  The
285127
+\fIweekday\fR intepretation is following:  0 means Sunday, 1 means Monday, ...,
285127
+6 means Saturday; the special value 7 means each 7 days, irrespectively of
285127
+weekday.  Defaults to 0 if the \fIweekday\fR argument is omitted.
285127
 
285127
 .TP
285127
 \fByearly\fR
285127
diff --git a/logrotate.c b/logrotate.c
285127
index d5da299..e056ccd 100644
285127
--- a/logrotate.c
285127
+++ b/logrotate.c
285127
@@ -842,6 +842,27 @@ static int copyTruncate(char *currLog, char *saveLog, struct stat *sb,
285127
     return 0;
285127
 }
285127
 
285127
+/* return value similar to mktime() but the exact time is ignored */
285127
+static time_t mktimeFromDateOnly(const struct tm *src)
285127
+{
285127
+    /* explicit struct copy to retain C89 compatibility */
285127
+    struct tm tmp;
285127
+    memcpy(&tmp, src, sizeof tmp);
285127
+
285127
+    /* abstract out (nullify) fields expressing the exact time */
285127
+    tmp.tm_hour = 0;
285127
+    tmp.tm_min  = 0;
285127
+    tmp.tm_sec  = 0;
285127
+    return mktime(&tmp);
285127
+}
285127
+
285127
+/* return by how many days the date was advanced but ignore exact time */
285127
+static int daysElapsed(const struct tm *now, const struct tm *last)
285127
+{
285127
+    const time_t diff = mktimeFromDateOnly(now) - mktimeFromDateOnly(last);
285127
+    return diff / (24 * 3600);
285127
+}
285127
+
285127
 int findNeedRotating(struct logInfo *log, int logNum, int force)
285127
 {
285127
     struct stat sb;
285127
@@ -924,18 +945,16 @@ int findNeedRotating(struct logInfo *log, int logNum, int force)
285127
 	       state->lastRotated.tm_mon != now.tm_mon ||
285127
 	       state->lastRotated.tm_mday != now.tm_mday ||
285127
 	       state->lastRotated.tm_hour != now.tm_hour) {
285127
+	int days;
285127
 	switch (log->criterium) {
285127
 	case ROT_WEEKLY:
285127
-	    /* rotate if:
285127
-	       1) the current weekday is before the weekday of the
285127
-	       last rotation
285127
-	       2) more then a week has passed since the last
285127
-	       rotation */
285127
-	    state->doRotate = ((now.tm_wday < state->lastRotated.tm_wday)
285127
-			       ||
285127
-			       ((mktime(&now) -
285127
-				 mktime(&state->lastRotated)) >
285127
-				(7 * 24 * 3600)));
285127
+	    days = daysElapsed(&now, &state->lastRotated);
285127
+	    /* rotate if date is advanced by 7+ days (exact time is ignored) */
285127
+	    state->doRotate = (days >= 7)
285127
+		/* ... or if we have not yet rotated today */
285127
+		|| (days >= 1
285127
+			/* ... and the selected weekday is today */
285127
+			&& now.tm_wday == log->weekday);
285127
 	    if (!state->doRotate) {
285127
 	    message(MESS_DEBUG, "  log does not need rotating "
285127
 		    "(log has been rotated at %d-%d-%d %d:%d, "
285127
diff --git a/logrotate.h b/logrotate.h
285127
index cf42703..f2d2103 100644
285127
--- a/logrotate.h
285127
+++ b/logrotate.h
285127
@@ -36,8 +36,9 @@ struct logInfo {
285127
     char *oldDir;
285127
     enum { ROT_HOURLY, ROT_DAYS, ROT_WEEKLY, ROT_MONTHLY, ROT_YEARLY, ROT_SIZE
285127
             } criterium;
285127
+    int weekday; /* used by ROT_WEEKLY only */
285127
     unsigned long long threshhold;
285127
-	unsigned long long maxsize;
285127
+    unsigned long long maxsize;
285127
     unsigned long long minsize;
285127
     int rotateCount;
285127
     int rotateAge;
285127
-- 
285127
2.13.5
285127