summaryrefslogtreecommitdiffstats
path: root/patches/logrotate-3.7.1/rh-dateext.patch
blob: b1ccecd30eb23b3c01ce2a44c8e8de0500e7f8d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
Enhance logrotate with "dateext".
From RedHat patches

---
 config.c    |    8 ++
 logrotate.8 |    5 +
 logrotate.c |  177 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 logrotate.h |    1 
 4 files changed, 173 insertions(+), 18 deletions(-)

Index: logrotate-3.7.1/logrotate.h
===================================================================
--- logrotate-3.7.1.orig/logrotate.h
+++ logrotate-3.7.1/logrotate.h
@@ -15,6 +15,7 @@
 #define LOG_FLAG_MAILFIRST	(1 << 6)
 #define LOG_FLAG_SHAREDSCRIPTS	(1 << 7)
 #define LOG_FLAG_COPY		(1 << 8)
+#define LOG_FLAG_DATEEXT	(1 << 9)
 
 #define NO_FORCE_ROTATE 0
 #define FORCE_ROTATE    1
Index: logrotate-3.7.1/logrotate.c
===================================================================
--- logrotate-3.7.1.orig/logrotate.c
+++ logrotate-3.7.1/logrotate.c
@@ -11,6 +11,7 @@
 #include <sys/wait.h>
 #include <time.h>
 #include <unistd.h>
+#include <glob.h>
 
 #ifdef WITH_SELINUX
 #include <selinux/selinux.h>
@@ -22,6 +23,10 @@ int selinux_enabled=0;
 #include "log.h"
 #include "logrotate.h"
 
+#if !defined(GLOB_ABORTED) && defined(GLOB_ABEND)
+#define GLOB_ABORTED GLOB_ABEND
+#endif
+
 typedef struct {
     char * fn;
     struct tm lastRotated;	/* only tm.mon, tm_mday, tm_year are good! */
@@ -42,6 +47,14 @@ int debug = 0;
 char * mailCommand = DEFAULT_MAIL_COMMAND;
 time_t nowSecs = 0;
 
+static int globerr(const char * pathname, int theerr) {
+    message(MESS_ERROR, "error accessing %s: %s\n", pathname,
+	strerror(theerr));
+
+    /* We want the glob operation to continue, so return 0 */
+    return 1;
+}
+
 static logState * findState(const char * fn, struct stateSet * sip) {
     int i;
     logState * states = sip->states;
@@ -121,6 +134,17 @@ static int runScript(char * logfn, char 
     return rc;
 }
 
+static int removeLogFile(char * name) {
+    message(MESS_DEBUG, "removing old log %s\n", name);
+
+    if (!debug && unlink(name)) {
+       message(MESS_ERROR, "Failed to remove old log %s: %s\n",
+           name, strerror(errno));
+       return 1;
+    }
+    return 0;
+}
+
 static int compressLogFile(char * name, logInfo * log, struct stat *sb) {
     char * compressedName;
     const char ** fullCommand;
@@ -265,6 +289,25 @@ static int mailLog(char * logFile, char 
     return rc;
 }
 
+static int mailLogWrapper (char * mailFilename, char * mailCommand, int logNum, logInfo * log) {
+    /* if the log is compressed (and we're not mailing a
+     * file whose compression has been delayed), we need
+     * to uncompress it */
+    if ((log->flags & LOG_FLAG_COMPRESS) &&
+       !((log->flags & LOG_FLAG_DELAYCOMPRESS) &&
+           (log->flags & LOG_FLAG_MAILFIRST))) {
+       if (mailLog(mailFilename, mailCommand,
+                   log->uncompress_prog, log->logAddress,
+                   log->files[logNum]))
+           return 1;
+    } else {
+       if (mailLog(mailFilename, mailCommand, NULL,
+                   log->logAddress, mailFilename))
+           return 1;
+    }
+    return 0;
+}
+
 static int copyTruncate(char * currLog, char * saveLog, struct stat * sb, int flags) {
     char buf[BUFSIZ];
     int fdcurr = -1, fdsave = -1;
@@ -479,6 +522,9 @@ int rotateSingleLog(logInfo * log, int l
     char * baseName;
     char * dirName;
     char * firstRotated;
+    char * glob_pattern;
+    glob_t globResult;
+    int rc;
     size_t alloc_size;
     int rotateCount = log->rotateCount ? log->rotateCount : 1;
     int logStart = (log->logStart == -1) ? 1 : log->logStart;
@@ -509,7 +555,7 @@ int rotateSingleLog(logInfo * log, int l
 
     alloc_size = strlen(dirName) + strlen(baseName) + 
                  strlen(log->files[logNum]) + strlen(fileext) +
-                 strlen(compext) + 10;
+                 strlen(compext) + 18;
     
     oldName = alloca(alloc_size);
     newName = alloca(alloc_size);
@@ -531,25 +577,106 @@ int rotateSingleLog(logInfo * log, int l
     /* First compress the previous log when necessary */
     if (log->flags & LOG_FLAG_COMPRESS &&
         log->flags & LOG_FLAG_DELAYCOMPRESS) {
-        struct stat sbprev;
-	
-        sprintf(oldName, "%s/%s.%d%s", dirName, baseName, logStart, fileext);
-        if (stat(oldName, &sbprev)) {
-            message(MESS_DEBUG, "previous log %s does not exist\n",
-		    oldName);
-        } else {
-	    hasErrors = compressLogFile(oldName, log, &sbprev);
+	if (log->flags & LOG_FLAG_DATEEXT) {
+	               /* glob for uncompressed files with our pattern */
+	    glob_pattern = malloc(strlen(dirName) + strlen(baseName)
+				  + strlen(fileext) + 44 );
+	    sprintf(glob_pattern,
+		    "%s/%s-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%s",
+		    dirName, baseName, fileext);
+	    rc = glob(glob_pattern, 0, globerr, &globResult);
+	    if (!rc && globResult.gl_pathc > 0) {
+		for (i = 0; i < globResult.gl_pathc && !hasErrors; i++) {
+	    	    struct stat sbprev;
+		    
+		    sprintf(oldName,"%s",(globResult.gl_pathv)[i]);
+		    if (stat(oldName, &sbprev)) {
+			message(MESS_DEBUG, "previous log %s does not exist\n",
+			oldName);
+		    } else {
+			hasErrors = compressLogFile(oldName, log, &sbprev);
+		    }
+		}
+	    } else {
+		message (MESS_DEBUG, "glob finding logs to compress failed\n");
+		/* fallback to old behaviour */
+		sprintf(oldName, "%s/%s.%d%s", dirName, baseName, logStart, fileext);
+	    }
+	    globfree(&globResult);
+	    free(glob_pattern);
+	} else {
+	    struct stat sbprev;
+
+	    sprintf(oldName, "%s/%s.%d%s", dirName, baseName, logStart, fileext);
+	    if (stat(oldName, &sbprev)) {
+		message(MESS_DEBUG, "previous log %s does not exist\n",
+			oldName);
+	    } else {
+		hasErrors = compressLogFile(oldName, log, &sbprev);
+	    }
 	}
     }
     
+    firstRotated = alloca(strlen(dirName) + strlen(baseName) +
+			  strlen(fileext) + strlen(compext) + 30);
+
+    if(log->flags & LOG_FLAG_DATEEXT) {
+	/* glob for compressed files with our pattern
+	 * and compress ext */
+	glob_pattern = malloc(strlen(dirName)+strlen(baseName)
+			      +strlen(fileext)+strlen(compext)+44);
+	sprintf(glob_pattern,
+		"%s/%s-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%s%s",
+		dirName, baseName, fileext, compext);
+	rc = glob(glob_pattern, 0, globerr, &globResult);
+	if (!rc) {
+	    /* search for files to drop, if we find one remember it,
+	     * if we find another one mail and remove the first and
+	     * remember the second and so on */
+	    struct stat fst_buf;
+	    int mail_out = -1;
+	    /* remove the first (n - rotateCount) matches
+	     * no real rotation needed, since the files have
+	     * the date in their name */
+	    for (i = 0; i < globResult.gl_pathc; i++) {
+		if( !stat((globResult.gl_pathv)[i],&fst_buf) ) {
+		    if (i <= ((int)globResult.gl_pathc - rotateCount)) {
+			if ( mail_out != -1 ) {
+			    if (!hasErrors && log->logAddress) {
+				char * mailFilename = (globResult.gl_pathv)[mail_out];
+				hasErrors = mailLogWrapper(mailFilename, mailCommand, logNum, log);
+				if (!hasErrors)
+				    hasErrors = removeLogFile(mailFilename);
+			    }
+			}
+			mail_out = i;
+		    }
+		}
+	    }
+	    if ( mail_out != -1 ) {
+		/* oldName is oldest Backup found (for unlink later) */
+		sprintf(oldName, "%s", (globResult.gl_pathv)[mail_out]);
+		strcpy(disposeName, oldName);
+	    } else
+		disposeName = NULL;
+	} else {
+	    message (MESS_DEBUG, "glob finding old rotated logs failed\n");
+	    disposeName = NULL;
+	}
+	/* firstRotated is most recently created/compressed rotated log */
+	sprintf(firstRotated, "%s/%s-%04d%02d%02d%s%s",
+		dirName, baseName, now.tm_year+1900,
+		now.tm_mon+1, now.tm_mday, fileext, compext);
+	globfree(&globResult);
+	free(glob_pattern);
+    } else {
+
     sprintf(oldName, "%s/%s.%d%s%s", dirName, baseName,
             logStart + rotateCount, fileext, compext);
     strcpy(newName, oldName);
     
     strcpy(disposeName, oldName);
     
-    firstRotated = alloca(strlen(dirName) + strlen(baseName) +
-                          strlen(fileext) + strlen(compext) + 30);
     sprintf(firstRotated, "%s/%s.%d%s%s", dirName, baseName,
             logStart, fileext, 
 	    (log->flags & LOG_FLAG_DELAYCOMPRESS) ? "" : compext);
@@ -599,12 +726,27 @@ int rotateSingleLog(logInfo * log, int l
                 hasErrors = 1;
 	    }
 	}
-    }
-    
+    } 
+    } /* !LOG_FLAG_DATEEXT */
+ 
     finalName = oldName;
     
-    /* note: the gzip extension is *not* used here! */
-    sprintf(finalName, "%s/%s.%d%s", dirName, baseName, logStart, fileext);
+    if(log->flags & LOG_FLAG_DATEEXT) {
+	char * destFile = alloca(strlen(dirName) + strlen(baseName) +
+				 strlen(fileext) + strlen(compext) + 30);
+	struct stat fst_buf;
+	sprintf(finalName, "%s/%s-%04d%02d%02d%s",
+		dirName, baseName, now.tm_year+1900,
+		now.tm_mon+1, now.tm_mday, fileext);
+	sprintf(destFile, "%s%s", finalName, compext);
+	if(!stat(destFile,&fst_buf)) {
+	    message (MESS_DEBUG, "destination %s already exists, skipping rotation\n", firstRotated);
+	    hasErrors = 1;
+	}
+    } else {
+	/* note: the gzip extension is *not* used here! */
+	sprintf(finalName, "%s/%s.%d%s", dirName, baseName, logStart, fileext);
+    }
     
     /* if the last rotation doesn't exist, that's okay */
     if (!debug && access(disposeName, F_OK)) {
@@ -613,9 +755,6 @@ int rotateSingleLog(logInfo * log, int l
         disposeName = NULL;
     } 
     
-    free(dirName);
-    free(baseName);
-    
     if (!hasErrors) {
         if (log->pre && !(log->flags & LOG_FLAG_SHAREDSCRIPTS)) {
             message(MESS_DEBUG, "running prerotate script\n");
@@ -761,6 +900,8 @@ int rotateSingleLog(logInfo * log, int l
 	  }
 	}
 #endif
+    free(dirName);
+    free(baseName);
     return hasErrors;
 }
 
Index: logrotate-3.7.1/config.c
===================================================================
--- logrotate-3.7.1.orig/config.c
+++ logrotate-3.7.1/config.c
@@ -511,6 +511,14 @@ static int readConfigFile(const char * c
 		newlog->flags &= ~LOG_FLAG_IFEMPTY;
 
 		*endtag = oldchar, start = endtag;
+	    } else if (!strcmp(start, "dateext")) {
+		newlog->flags |= LOG_FLAG_DATEEXT;
+
+		*endtag = oldchar, start = endtag;
+	    } else if (!strcmp(start, "nodateext")) {
+		newlog->flags &= ~LOG_FLAG_DATEEXT;
+
+		*endtag = oldchar, start = endtag;
 	    } else if (!strcmp(start, "noolddir")) {
 		newlog->oldDir = NULL;
 
Index: logrotate-3.7.1/logrotate.8
===================================================================
--- logrotate-3.7.1.orig/logrotate.8
+++ logrotate-3.7.1/logrotate.8
@@ -200,6 +200,11 @@ file for the omitted attributes. This op
 Log files are rotated every day.
 
 .TP
+\fBdateext\fR
+Archive old versions of log files adding a daily extension like YYYYMMDD
+instead of simply adding a number.
+
+.TP
 \fBdelaycompress\fR
 Postpone compression of the previous log file to the next rotation cycle.
 This has only effect when used in combination with \fBcompress\fR.