On Sat, Oct 20, 2007 at 02:52:16PM +0200, Lasse Karstensen wrote:
> Attached is a simple patch that let you choose the date 
> format used for -d on the command line.
> 
> Using YYYYmmddHHMMSS seems to noisy, and with this patch both
> YYYYmmdd and (what we want) YYYY-mm-dd is possible.

I believe this patch must have been created for a version of
debian-utils older than 2.17.4, because it breaks completely in
combination with the patch for #401143, introduced in 2.17.4. 
That patch assumes that the suffix consists of exactly 14 numeric
characters. Obviously, this is not always the case when users are
allowed to create arbitrary suffixes using -D.

I have attached a patch which (as far as I can see) removes all
assumptions about the format of the date string. It also solves the
problem with gzip errors from the initial rotation, as mentioned by Paul
Slootman earlier. I also hard coded .gz and .bz2 in there, so people can
change from gz to bz2 (or vice versa) without double-compressing logs.
$DOT_Z is also matched, to minimize the breakage in case of new formats
being added in the future.

Note that this patch might cause odd behaviour if one log filename is a
prefix of another. For example, when rotating the file "foo", "foo.bar"
will also be affected, since savelog doesn't know if "bar" is part of
the date string supplied with -D or not. I am not aware of log files
which would be affected, but didn't investigate the matter very
throughly. I considered a hack with limiting the matching to the length
of $DATUM, but that would assume that the format string supplied with -C
always creates dates of equal length, and we don't know that. Besides,
it would still break if $DATUM had the same amount of characters as
"bar".

Also note that DATUM=`date +$OPTARG` doesn't quote $OPTARG, and thus
requires the string supplied to -D to be escaped twice if it contains
spaces or other weird characters. I didn't patch this, because I'm not
sure if the possibility to use -D "%Y-%m-%d -d yesterday" to get
yesterdays date (useful when savelog is ran right after midnight) is a
feature or a bug ;-)

-- 
Knut Auvor Grythe
--- savelog.orig        2007-10-29 20:28:36.000000000 +0100
+++ savelog     2007-11-09 13:15:15.000000000 +0100
@@ -265,16 +265,27 @@
 
        # compress the old uncompressed log if needed
        if test -n "$datum" && test -n "$COMPRESS"; then
-               $COMPRESS $COMPRESS_OPTS -- 
"$newname".[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
+
+               # Loop through all matching files
+               for f in "$newname".*; do
+
+                       # Skip if glob didn't expand (this means no file was 
found)
+                       test "$f" = "$newname.*" && continue
+
+                       # Skip if file is already compressed
+                       echo "$f" | egrep "(\\.gz|\\.bz2|\\$DOT_Z)\$" 
>/dev/null && continue
+                       
+                       $COMPRESS $COMPRESS_OPTS -- "$f"
+               done
        fi
 
        # remove old files if so desired
        if [ -n "$forceclean" ]; then
                cycle=$(( $count - 1))
                if [ -z "$COMPRESS" ]; then
-                       rm -f -- `ls -t -- $newname.[0-9]* | sed -e 1,${cycle}d`
+                       rm -f -- `ls -t -- $newname.* | sed -e 1,${cycle}d`
                else
-                       rm -f -- `ls -t -- $newname.[0-9]*$DOT_Z | sed -e 
1,${cycle}d`
+                       rm -f -- `ls -t -- $newname.*$DOT_Z | sed -e 
1,${cycle}d`
                fi
        fi
 

Reply via email to