bin/166448: [patch] newsyslog -t fails to find previous rotated
log
Eugene Grosbein
eugen at grosbein.pp.ru
Tue Mar 27 19:20:11 UTC 2012
The following reply was made to PR bin/166448; it has been noted by GNATS.
From: Eugene Grosbein <eugen at grosbein.pp.ru>
To: bug-followup at FreeBSD.ORG
Cc:
Subject: Re: bin/166448: [patch] newsyslog -t fails to find previous rotated
log
Date: Wed, 28 Mar 2012 02:10:50 +0700
Sorry, I've sent buggy patch (does stat() on wrong filename). Here is corrected one.
--- usr.sbin/newsyslog/newsyslog.c.orig 2012-02-13 17:02:03.000000000 +0400
+++ usr.sbin/newsyslog/newsyslog.c 2012-03-27 22:53:46.000000000 +0400
@@ -2206,6 +2206,79 @@
return (kbytes(dbtob(sb.st_blocks)));
}
+/* Return the age of previous old log file, when using time based filenames. */
+static time_t
+find_oldest_timelog(const char *dir, const char *logfname)
+{
+ struct stat sb;
+ int c, valid;
+ size_t logfname_len;
+ struct tm tm;
+ time_t oldt;
+ struct dirent *dp;
+ DIR *dirp;
+ char *s;
+ char tmp[MAXPATHLEN];
+
+ if ((dirp = opendir(dir)) == NULL)
+ err(1, "Cannot open log directory '%s'", dir);
+
+ oldt = -1;
+ logfname_len = strlen(logfname);
+ while ((dp = readdir(dirp)) != NULL) {
+ if (dp->d_type != DT_REG)
+ continue;
+ /* Ignore everything but files with our logfile prefix */
+ if (strncmp(dp->d_name, logfname, logfname_len) != 0)
+ continue;
+ /* Ignore the actual non-rotated logfile */
+ if (dp->d_namlen == logfname_len)
+ continue;
+ /*
+ * Make sure we have found a logfile, so the
+ * postfix is valid, IE format is: '.<time>(.[bg]z)?'.
+ */
+ if (dp->d_name[logfname_len] != '.') {
+ if (verbose)
+ printf("Ignoring %s which has unexpected "
+ "extension '%s'\n", dp->d_name,
+ &dp->d_name[logfname_len]);
+ continue;
+ }
+ if ((s = strptime(&dp->d_name[logfname_len + 1],
+ timefnamefmt, &tm)) == NULL) {
+ if (verbose)
+ printf("Ignoring %s which does not "
+ "match time format\n", dp->d_name);
+ continue;
+ }
+
+ valid = 0;
+ c = 0;
+ while (!valid && c < COMPRESS_TYPES)
+ if (strcmp(s, compress_type[c++].suffix) == 0)
+ valid = 1;
+
+ if (valid != 1) {
+ if (verbose)
+ printf("Ignoring %s which has unexpected "
+ "extension '%s'\n", dp->d_name, s);
+ continue;
+ }
+ snprintf(tmp, sizeof(tmp), "%s/%s", dir, dp->d_name);
+ if (stat(tmp, &sb) < 0)
+ err(1, "Cannot stat '%s'", tmp);
+
+ /* We have found more recent old logfile */
+ if (oldt < sb.st_mtime)
+ oldt = sb.st_mtime;
+ }
+ closedir(dirp);
+
+ /* Return -1 if nothing found */
+ return oldt;
+}
+
/* Return the age of old log file (file.0) */
static int
age_old_log(char *file)
@@ -2241,6 +2314,17 @@
(void) strlcpy(tmp, file, sizeof(tmp));
}
+ if (timefnamefmt != NULL) {
+ char *bd;
+ time_t t;
+
+ if ((bd = dirname(tmp)) == NULL)
+ err(1, "'%s'", tmp);
+ if ((t = find_oldest_timelog(bd, basename(tmp))) == -1)
+ return (-1);
+ return ((int)(ptimeget_secs(timenow) - t + 1800) / 3600);
+ }
+
strlcat(tmp, ".0", sizeof(tmp));
logfile_suffix = get_logfile_suffix(tmp);
if (logfile_suffix == NULL)
More information about the freebsd-bugs
mailing list