From david@catwhisker.org  Mon Aug 13 23:24:16 2007
Return-Path: <david@catwhisker.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 3779816A417
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 13 Aug 2007 23:24:16 +0000 (UTC)
	(envelope-from david@catwhisker.org)
Received: from bunrab.catwhisker.org (adsl-63-193-123-122.dsl.snfc21.pacbell.net [63.193.123.122])
	by mx1.freebsd.org (Postfix) with ESMTP id E628413C428
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 13 Aug 2007 23:24:15 +0000 (UTC)
	(envelope-from david@catwhisker.org)
Received: from bunrab.catwhisker.org (localhost [127.0.0.1])
	by bunrab.catwhisker.org (8.13.3/8.13.3) with ESMTP id l7DNOAb2001648;
	Mon, 13 Aug 2007 16:24:10 -0700 (PDT)
	(envelope-from david@bunrab.catwhisker.org)
Received: (from david@localhost)
	by bunrab.catwhisker.org (8.13.3/8.13.1/Submit) id l7DNOAMQ001647;
	Mon, 13 Aug 2007 16:24:10 -0700 (PDT)
	(envelope-from david)
Message-Id: <200708132324.l7DNOAMQ001647@bunrab.catwhisker.org>
Date: Mon, 13 Aug 2007 16:24:10 -0700 (PDT)
From: David Wolfskill <david@catwhisker.org>
Reply-To: David Wolfskill <david@catwhisker.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc: Artis Caune <Artis.Caune@latnet.lv>,
        Dirk GOUDERS <gouders@et.bocholt.fh-ge.de>
Subject: newsyslog(8) -- provide ability to compress Nth rotated file
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         115486
>Category:       bin
>Synopsis:       [patch] [request] newsyslog(8) -- provide ability to compress Nth rotated file
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Aug 13 23:30:01 GMT 2007
>Closed-Date:    
>Last-Modified:  Sat Jan 26 09:22:39 UTC 2008
>Originator:     David Wolfskill
>Release:        FreeBSD 7.0-CURRENT i386
>Organization:
Wolfskill & Dowling Residence
>Environment:
System: FreeBSD catmint.mail-abuse.org 7.0-CURRENT FreeBSD 7.0-CURRENT #94: Sun Aug 12 09:27:33 PDT 2007     root@catmint.mail-abuse.org:/common/S4/obj/usr/src/sys/CATMINT  i386

>Description:
	As it stands, newsyslog(8) provides for optional compression for
	rotated files.

	However, that compression only takes place for the newly-created
	".0" file.

	Artis Caune <Artis.Caune@latnet.lv> suggested (in -hackers@)
	that providing an ability to defer the compression to the
	".1" file might be useful for certain applications, such
	as apache.

	Dirk GOUDERS <gouders@et.bocholt.fh-ge.de> followed up,
	suggesting the use of the "0" flag, which is an enhancement
	available in another implementation of newsyslog(8).

	I mentioned that while this might be useful, it would still fall
	short of something I needed to do at work recently: leave the
	newest N rotated log files uncompressed, while compressing the
	others.

	After a small amount of additional discussion, Dirk volunteered
	to supply patches; I volunteered to test (and later, to hack the
	man page).

	Dirk supplied the patches; I tested to my satisfaction, and
	attacked the man page.  The attached patches are the result.

>How-To-Repeat:
	Try to figure out  how to get newsyslog(8) to only compress a
	rotated log file if it exceeds a certain "age" (or "generation
	number").  If you figure out a way, please document it. :-}
>Fix:
	Apply the following patches in /usr/src/usr.sbin/newsyslog,
	then make  && make install.  That done, the "generation number"
	of the rotated file that is to be compressed may be specified in
	the "flags" field of a newsyslog.conf entry:


Index: newsyslog.c
===================================================================
RCS file: /cvs/freebsd/src/usr.sbin/newsyslog/newsyslog.c,v
retrieving revision 1.107
diff -u -r1.107 newsyslog.c
--- newsyslog.c	17 Aug 2006 18:15:43 -0000	1.107
+++ newsyslog.c	12 Aug 2007 17:26:08 -0000
@@ -127,6 +127,8 @@
 	struct ptime_data *trim_at;	/* Specific time to do trimming */
 	unsigned int permissions;	/* File permissions on the log */
 	int flags;		/* CE_COMPACT, CE_BZCOMPACT, CE_BINARY */
+	int nuncompact;		/* number of rotations that should not
+				 * be compressed; -1 turns this off */
 	int sig;		/* Signal to send */
 	int def_cfg;		/* Using the <default> rule for this file */
 	struct conf_entry *next;/* Linked list pointer */
@@ -1187,6 +1189,11 @@
 		}
 
 		for (; q && *q && !isspacech(*q); q++) {
+			if (isdigit(*q)) {
+				working->nuncompact = strtol(q, NULL, 10);
+				while(isdigit(*(q+1))) q++;
+				continue;
+			}
 			switch (tolowerch(*q)) {
 			case 'b':
 				working->flags |= CE_BINARY;
@@ -1456,6 +1463,12 @@
 			(void)rename(zfile1, zfile2);
 		}
 		change_attrs(zfile2, ent);
+		if ((flags & (CE_COMPACT | CE_BZCOMPACT)) &&
+		    (ent->nuncompact != -1) &&
+		    (numlogs_c == ent->nuncompact)) {
+			free_or_keep = KEEP_ENT;
+			save_zipwork(ent, NULL, ent->fsize, file2);
+		}
 	}
 
 	if (ent->numlogs > 0) {
@@ -1494,7 +1507,8 @@
 	swork = NULL;
 	if (ent->pid_file != NULL)
 		swork = save_sigwork(ent);
-	if (ent->numlogs > 0 && (flags & (CE_COMPACT | CE_BZCOMPACT))) {
+	if (ent->numlogs > 0 && (flags & (CE_COMPACT | CE_BZCOMPACT))
+	    && ent->nuncompact == -1) {
 		/*
 		 * The zipwork_entry will include a pointer to this
 		 * conf_entry, so the conf_entry should not be freed.
Index: newsyslog.conf.5
===================================================================
RCS file: /cvs/freebsd/src/usr.sbin/newsyslog/newsyslog.conf.5,v
retrieving revision 1.6
diff -u -r1.6 newsyslog.conf.5
--- newsyslog.conf.5	29 Sep 2006 17:57:03 -0000	1.6
+++ newsyslog.conf.5	13 Aug 2007 18:08:03 -0000
@@ -242,6 +242,16 @@
 files matched by this line.
 The following are valid flags:
 .Bl -tag -width indent
+.It Ar number
+In combination with one of the compression flags
+.Cm J
+or
+.Cm Z
+, this selects which one of the already-rotated archives will be
+subject to the requested compression.  If it is omitted and compression
+is specified, the most recently-rotated archive file will be compressed.
+(Compression is done subsequent to rotation, but the selection
+specified here is done prior to the rotation.)
 .It Cm B
 indicates that the log file is a binary file, or has some
 special format.
>Release-Note:
>Audit-Trail:
>Unformatted:
