commit 3c79d8418cbeb502e490ee80207310e47ea11b89
Author: jkaluza <jkaluza@ec1272ba-9ed1-42ef-8245-99669996828e>
Date:   Mon Oct 13 10:15:44 2014 +0000

    Sort files returned by glob() according to date extension. Fixes bug when the oldest log was not removed when dateext %d-%m-%Y has been used

Modified for bsc#982315, mkoutny@suse.com

diff --git a/logrotate.c b/logrotate.c
index d3a3b88..f2a44a0 100644
--- a/logrotate.c
+++ b/logrotate.c
@@ -77,6 +77,11 @@ struct logNames {
     char *baseName;
 };
 
+struct compData {
+	int prefix_len;
+	const char *dformat;
+};
+
 struct logStates {
 	LIST_HEAD(stateSet, logState) head;
 } **states;
@@ -100,6 +105,33 @@ static int globerr(const char *pathname, int theerr)
     return 1;
 }
 
+static int compGlobResult(const void *result1, const void *result2, void *data)  {
+	struct tm time;
+	time_t t1, t2;
+	struct compData *d = (struct compData *) data;
+	const char *r1 = *(const char **)(result1);
+	const char *r2 = *(const char **)(result2);
+
+	memset(&time, 0, sizeof(struct tm));
+	strptime(r1 + d->prefix_len, d->dformat, &time);
+	t1 = mktime(&time);
+
+	memset(&time, 0, sizeof(struct tm));
+	strptime(r2 + d->prefix_len, d->dformat, &time);
+	t2 = mktime(&time);
+
+	if (t1 < t2) return -1;
+	if (t1 > t2) return  1;
+	return 0;
+}
+
+static void sortGlobResult(glob_t *result, int prefix_len, const char *dformat) {
+	struct compData d;
+	d.prefix_len = prefix_len;
+	d.dformat = dformat;
+	qsort_r(result->gl_pathv, result->gl_pathc, sizeof(char *), compGlobResult, &d);
+}
+
 int switch_user(uid_t user, gid_t group) {
 	save_egid = getegid();
 	save_euid = geteuid();
@@ -1113,6 +1145,7 @@ int prerotateSingleLog(struct logInfo *log, int logNum, struct logState *state,
 				rotNames->dirName, rotNames->baseName, dext_pattern, fileext);
 	    rc = glob(glob_pattern, 0, globerr, &globResult);
 	    if (!rc && globResult.gl_pathc > 0) {
+		sortGlobResult(&globResult, strlen(rotNames->dirName) + 1 + strlen(rotNames->baseName), dformat);
 		for (i = 0; i < globResult.gl_pathc && !hasErrors; i++) {
 		    struct stat sbprev;
 
@@ -1177,6 +1210,7 @@ int prerotateSingleLog(struct logInfo *log, int logNum, struct logState *state,
 	    /* remove the first (n - rotateCount) matches
 	     * no real rotation needed, since the files have
 	     * the date in their name */
+		sortGlobResult(&globResult, strlen(rotNames->dirName) + 1 + strlen(rotNames->baseName), dformat);
 	    for (i = 0; i < globResult.gl_pathc; i++) {
 		if (!stat((globResult.gl_pathv)[i], &fst_buf)) {
 		    if ((i <= ((int) globResult.gl_pathc - rotateCount))
diff --git a/test/test b/test/test
index b91ba5a..59f40ab 100755
--- a/test/test
+++ b/test/test
@@ -1358,4 +1358,50 @@ test.log 0
 scriptout 0 foo
 EOF
 
+cleanup 54
+
+# ------------------------------- Test 54 ------------------------------------
+# removing last log file when using %Y-%m-%d
+rm -f *test.log*
+preptest test.log 54 1 0
+
+DATE=""
+for i in {1..60}
+do
+    DATE=$(/bin/date "+%Y-%m-%d" --date "$i day ago" 2>/dev/null)   
+    echo "x" > test.log-$DATE
+done
+
+$RLR test-config.54 --force
+
+if [ -e test.log-$DATE ]; then
+    echo "File test.log-$DATE should not exist (it should be deleted)"
+    exit 3
+fi
+
+rm -f *test.log*
+
+cleanup 56
+
+# ------------------------------- Test 56 ------------------------------------
+# removing last log file when using %d-%m-%Y
+rm -f *test.log*
+preptest test.log 56 1 0
+
+DATE=""
+for i in {1..60}
+do
+    DATE=$(/bin/date "+%d-%m-%Y" --date "$i day ago" 2>/dev/null)   
+    echo "x" > test.log-$DATE
+done
+
+$RLR test-config.56 --force
+
+if [ -e test.log-$DATE ]; then
+    echo "File test.log-$DATE should not exist (it should be deleted)"
+    exit 3
+fi
+
+rm -f *test.log*
+
 cleanup
diff --git a/test/test-config.54.in b/test/test-config.54.in
new file mode 100644
index 0000000..c946af1
--- /dev/null
+++ b/test/test-config.54.in
@@ -0,0 +1,8 @@
+create
+
+&DIR&/test.log {
+    daily
+	dateext
+	dateformat -%Y-%m-%d
+    rotate 60
+}
diff --git a/test/test-config.56.in b/test/test-config.56.in
new file mode 100644
index 0000000..adaf2a5
--- /dev/null
+++ b/test/test-config.56.in
@@ -0,0 +1,8 @@
+create
+
+&DIR&/test.log {
+    daily
+	dateext
+	dateformat -%d-%m-%Y
+    rotate 60
+}
