Blame SOURCES/logrotate-3.8.6-sortglob.patch

22898a
diff --git a/logrotate.c b/logrotate.c
22898a
index 174a26b..4ef044e 100644
22898a
--- a/logrotate.c
22898a
+++ b/logrotate.c
22898a
@@ -77,6 +77,11 @@ struct logNames {
22898a
     char *baseName;
22898a
 };
22898a
 
22898a
+struct compData {
22898a
+	int prefix_len;
22898a
+	const char *dformat;
22898a
+};
22898a
+
22898a
 struct logStates {
22898a
 	LIST_HEAD(stateSet, logState) head;
22898a
 } **states;
22898a
@@ -142,6 +147,34 @@ int switch_user_permanently(const struct logInfo *log) {
22898a
 	return 0;
22898a
 }
22898a
 
22898a
+static int compGlobResult(const void *result1, const void *result2, void *data)  {
22898a
+	struct tm time;
22898a
+	time_t t1, t2;
22898a
+	struct compData *d = (struct compData *) data;
22898a
+	const char *r1 = *(const char **)(result1);
22898a
+	const char *r2 = *(const char **)(result2);
22898a
+
22898a
+	memset(&time, 0, sizeof(struct tm));
22898a
+	strptime(r1 + d->prefix_len, d->dformat, &time);
22898a
+	t1 = mktime(&time);
22898a
+
22898a
+	memset(&time, 0, sizeof(struct tm));
22898a
+	strptime(r2 + d->prefix_len, d->dformat, &time);
22898a
+	t2 = mktime(&time);
22898a
+
22898a
+	if (t1 < t2) return -1;
22898a
+	if (t1 > t2) return  1;
22898a
+	return 0;
22898a
+}
22898a
+
22898a
+static void sortGlobResult(glob_t *result, int prefix_len, const char *dformat) {
22898a
+	struct compData d;
22898a
+	if (!dformat || *dformat == '\0') return;
22898a
+	d.prefix_len = prefix_len;
22898a
+	d.dformat = dformat;
22898a
+	qsort_r(result->gl_pathv, result->gl_pathc, sizeof(char *), compGlobResult, &d);
22898a
+}
22898a
+
22898a
 static void unescape(char *arg)
22898a
 {
22898a
 	char *p = arg;
22898a
@@ -923,7 +956,7 @@ int prerotateSingleLog(struct logInfo *log, int logNum, struct logState *state,
22898a
 #define DATEEXT_LEN 64
22898a
 #define PATTERN_LEN (DATEEXT_LEN * 2)
22898a
 	char dext_str[DATEEXT_LEN];
22898a
-	char dformat[DATEEXT_LEN];
22898a
+	char dformat[DATEEXT_LEN] = "";
22898a
 	char dext_pattern[PATTERN_LEN];
22898a
 	char *dext;
22898a
 
22898a
@@ -1112,6 +1145,7 @@ int prerotateSingleLog(struct logInfo *log, int logNum, struct logState *state,
22898a
 		}
22898a
 	    rc = glob(glob_pattern, 0, globerr, &globResult);
22898a
 	    if (!rc && globResult.gl_pathc > 0) {
22898a
+		sortGlobResult(&globResult, strlen(rotNames->dirName) + 1 + strlen(rotNames->baseName), dformat);
22898a
 		for (i = 0; i < globResult.gl_pathc && !hasErrors; i++) {
22898a
 		    struct stat sbprev;
22898a
 
22898a
@@ -1176,6 +1210,7 @@ int prerotateSingleLog(struct logInfo *log, int logNum, struct logState *state,
22898a
 	    /* remove the first (n - rotateCount) matches
22898a
 	     * no real rotation needed, since the files have
22898a
 	     * the date in their name */
22898a
+		sortGlobResult(&globResult, strlen(rotNames->dirName) + 1 + strlen(rotNames->baseName), dformat);
22898a
 	    for (i = 0; i < globResult.gl_pathc; i++) {
22898a
 		if (!stat((globResult.gl_pathv)[i], &fst_buf)) {
22898a
 		    if ((i <= ((int) globResult.gl_pathc - rotateCount))
22898a
diff --git a/test/test b/test/test
22898a
index 793bf77..25b76a6 100755
22898a
--- a/test/test
22898a
+++ b/test/test
22898a
@@ -1347,4 +1347,73 @@ test.log 0
22898a
 test.log.1 0 zero
22898a
 EOF
22898a
 
22898a
+cleanup 54
22898a
+
22898a
+# ------------------------------- Test 54 ------------------------------------
22898a
+# removing last log file when using %Y-%m-%d
22898a
+rm -f *test.log*
22898a
+preptest test.log 54 1 0
22898a
+
22898a
+DATE=""
22898a
+for i in {1..60}
22898a
+do
22898a
+    DATE=$(/bin/date "+%Y-%m-%d" --date "$i day ago" 2>/dev/null)   
22898a
+    echo "x" > test.log-$DATE
22898a
+done
22898a
+
22898a
+$RLR test-config.54 --force
22898a
+
22898a
+if [ -e test.log-$DATE ]; then
22898a
+    echo "File test.log-$DATE should not exist (it should be deleted)"
22898a
+    exit 3
22898a
+fi
22898a
+
22898a
+rm -f *test.log*
22898a
+
22898a
+cleanup 55
22898a
+
22898a
+# ------------------------------- Test 55 ------------------------------------
22898a
+# removing last log file when using %s and hourly
22898a
+rm -f *test.log*
22898a
+preptest test.log 55 1 0
22898a
+
22898a
+DATE=""
22898a
+for i in {1..60}
22898a
+do
22898a
+    DATE=$(/bin/date "+%s" --date "$i hour ago" 2>/dev/null)   
22898a
+    echo "x" > test.log-$DATE.gz
22898a
+done
22898a
+
22898a
+$RLR test-config.55 --force
22898a
+
22898a
+if [ -e test.log-$DATE.gz ]; then
22898a
+    echo "File test.log-$DATE.gz should not exist (it should be deleted)"
22898a
+    exit 3
22898a
+fi
22898a
+
22898a
+rm -f *test.log*
22898a
+
22898a
+cleanup 56
22898a
+
22898a
+# ------------------------------- Test 56 ------------------------------------
22898a
+# removing last log file when using %d-%m-%Y
22898a
+rm -f *test.log*
22898a
+preptest test.log 56 1 0
22898a
+
22898a
+DATE=""
22898a
+for i in {1..60}
22898a
+do
22898a
+    DATE=$(/bin/date "+%d-%m-%Y" --date "$i day ago" 2>/dev/null)   
22898a
+    echo "x" > test.log-$DATE
22898a
+done
22898a
+
22898a
+$RLR test-config.56 --force
22898a
+
22898a
+if [ -e test.log-$DATE ]; then
22898a
+    echo "File test.log-$DATE should not exist (it should be deleted)"
22898a
+    exit 3
22898a
+fi
22898a
+
22898a
+rm -f *test.log*
22898a
+
22898a
 cleanup
22898a
diff --git a/test/test-config.54.in b/test/test-config.54.in
22898a
new file mode 100644
22898a
index 0000000..c946af1
22898a
--- /dev/null
22898a
+++ b/test/test-config.54.in
22898a
@@ -0,0 +1,8 @@
22898a
+create
22898a
+
22898a
+&DIR&/test.log {
22898a
+    daily
22898a
+	dateext
22898a
+	dateformat -%Y-%m-%d
22898a
+    rotate 60
22898a
+}
22898a
diff --git a/test/test-config.55.in b/test/test-config.55.in
22898a
new file mode 100644
22898a
index 0000000..8b10ad1
22898a
--- /dev/null
22898a
+++ b/test/test-config.55.in
22898a
@@ -0,0 +1,21 @@
22898a
+create
22898a
+
22898a
+# continue and throw no error message when log file is not present
22898a
+missingok
22898a
+
22898a
+# truncate the original log file in place after creating a copy
22898a
+copytruncate
22898a
+
22898a
+# compress the file
22898a
+compress
22898a
+
22898a
+# do only rotate when not empty
22898a
+notifempty
22898a
+
22898a
+&DIR&/test.log {
22898a
+    hourly
22898a
+	dateext
22898a
+	dateformat -%s
22898a
+    rotate 60
22898a
+    nosharedscripts
22898a
+}
22898a
diff --git a/test/test-config.56.in b/test/test-config.56.in
22898a
new file mode 100644
22898a
index 0000000..adaf2a5
22898a
--- /dev/null
22898a
+++ b/test/test-config.56.in
22898a
@@ -0,0 +1,8 @@
22898a
+create
22898a
+
22898a
+&DIR&/test.log {
22898a
+    daily
22898a
+	dateext
22898a
+	dateformat -%d-%m-%Y
22898a
+    rotate 60
22898a
+}