#21151 debianutils: savelog: [patch] Sortable numeric filename extensions.

Package:
debianutils
Source:
debianutils
Description:
Miscellaneous utilities specific to Debian
Submitter:
"Karl M. Hegbloom"
Date:
2005-07-18 03:38:27 UTC
Severity:
wishlist
#21151#5
Date:
1998-04-14 21:35:08 UTC
From:
To:
 Enclosed is a patch against `savelog' that makes the filenames have
multi digit zero prefix filled extensions so that they sort correctly
in a directory listing.  Many people keep a 30-90 day backlog of web
server or sendmail logs, so often the `cycle' count will be > 10.

 The diff is larger than it could be since I tabified the file.
Please apply the patch and use `ediff' from your xemacs to compare
this version with the orginal, if you like. (You can toggle
`ignore-whitespace' with `##'.)
--- savelog	1998/01/25 02:35:28	1.1
+++ savelog	1998/01/25 04:29:23	1.2
@@ -1,7 +1,7 @@
 #! /bin/bash
 # savelog - save a log file
 #    Copyright (C) 1987, 1988 Ronald S. Karr and Landon Curt Noll
-#    Copyright (C) 1992  Ronald S. Karr
+#    Copyright (C) 1992	 Ronald S. Karr
 # Slight modifications by Ian A. Murdock <imurdock@gnu.ai.mit.edu>:
 #	* uses `gzip' rather than `compress'
 #	* doesn't use $savedir; keeps saved log files in the same directory
@@ -9,19 +9,21 @@
 #	* for the sake of consistency, files are rotated even if they are
 #	  empty
 # More modifications by Guy Maor <maor@debian.org>:
-#       * cleanup.
-#       * -p (preserve) option
+#	* cleanup.
+#	* -p (preserve) option
+# Karl M. Hegbloom <karlheg@debian.org>
+#	* Use ${#cycle} digit number extensions so they will sort.
 #
 # usage: savelog [-m mode] [-u user] [-g group] [-t] [-p] [-c cycle] [-l]
-#                file...
+#		 file...
 #	-m mode	  - chmod log files to mode
 #	-u user	  - chown log files to user
 #	-g group  - chgrp log files to group
 #	-c cycle  - save cycle versions of the logfile	(default: 7)
 #	-t	  - touch file
 #	-l	  - don't compress any log files	(default: compress)
-#       -p        - preserve mode/user/group of original file
-#	file 	  - log file names
+#	-p	  - preserve mode/user/group of original file
+#	file	  - log file names
 #
 # The savelog command saves and optionally compresses old copies of files.
 # Older version of 'file' are named:
@@ -51,8 +53,8 @@
 #	3) The main file is moved to file.0.
 #
 #	4) If the -m, -u, -g, -t, or -p flags are given, then the file is
-#          touched into existence subject to the given flags.  The -p flag
-#          will preserve the original owner, group, and permissions.
+#	   touched into existence subject to the given flags.  The -p flag
+#	   will preserve the original owner, group, and permissions.
 #
 #	5) The new file.0 is changed subject to the -m, -u and -g flags.
 #
@@ -60,7 +62,7 @@
 #	not created.
 #
 # Note: Since the version numbers start with 0, version number <cycle>
-#       is never formed.  The <cycle> count must be at least 2.
+#	is never formed.  The <cycle> count must be at least 2.
 #
 # Bugs: If a process is still writing to the file.0 and savelog
 #	moved it to file.1 and compresses it, data could be lost.
@@ -70,6 +72,7 @@
 # common location
 export PATH=$PATH:/sbin:/bin:/usr/sbin:/usr/bin
 COMPRESS="gzip -9f"
+PRINTF="printf"
 DOT_Z=".gz"

 # parse args
@@ -153,38 +156,41 @@
 	#	continue
 	#fi

- 	# be sure that the savedir exists and is writable
+	# be sure that the savedir exists and is writable
 	# (in the Debian version, $savedir is . and not ./OLD)
- 	savedir=`dirname "$filename"`
- 	if [ -z "$savedir" ]; then
- 		savedir=.
- 	fi
- 	if [ ! -d "$savedir" ]; then
- 		mkdir "$savedir"
- 		if [ "$?" -ne 0 ]; then
- 			echo "$prog: could not mkdir $savedir" 1>&2
- 			exitcode=5
- 			continue
- 		fi
- 		chmod 0755 "$savedir"
- 	fi
- 	if [ ! -w "$savedir" ]; then
- 		echo "$prog: directory $savedir is not writable" 1>&2
- 		exitcode=7
- 		continue
- 	fi
+	savedir=`dirname "$filename"`
+	if [ -z "$savedir" ]; then
+		savedir=.
+	fi
+	if [ ! -d "$savedir" ]; then
+		mkdir "$savedir"
+		if [ "$?" -ne 0 ]; then
+			echo "$prog: could not mkdir $savedir" 1>&2
+			exitcode=5
+			continue
+		fi
+		chmod 0755 "$savedir"
+	fi
+	if [ ! -w "$savedir" ]; then
+		echo "$prog: directory $savedir is not writable" 1>&2
+		exitcode=7
+		continue
+	fi

 	# determine our uncompressed file names
 	newname=`basename "$filename"`
 	newname="$savedir/$newname"

 	# cycle the old compressed log files
-	cycle=$(( $count - 1))
+	export w=${#count}
+	count=$(( count - 1))
+	cycle=$($PRINTF "%0${w}d" $count)
 	rm -f "$newname.$cycle" "$newname.$cycle$DOT_Z"
-	while [ $cycle -gt 1 ]; do
+	while [ $count -gt 1 ]; do
 		# --cycle
 		oldcycle=$cycle
-		cycle=$(( $cycle - 1 ))
+		count=$(( count - 1))
+		cycle=$($PRINTF "%0${w}d" $count)
 		# cycle log
 		if [ -f "$newname.$cycle$DOT_Z" ]; then
 			mv -f "$newname.$cycle$DOT_Z" \
@@ -197,16 +203,18 @@
 	done

 	# compress the old uncompressed log if needed
-	if [ -f "$newname.0" ]; then
+	export newname_zero=$newname.$($PRINTF "%0${w}d" 0)
+	export newname_one=$newname.$($PRINTF "%0${w}d" 1)
+	if [ -f "$newname_zero" ]; then
 		if [ -z "$COMPRESS" ]; then
-			newfile="$newname.1"
-			mv "$newname.0" "$newfile"
+			newfile="$newname_one"
+			mv "$newname_zero" "$newfile"
 		else
-			newfile="$newname.1$DOT_Z"
+			newfile="$newname_one$DOT_Z"
 #			$COMPRESS < $newname.0 > $newfile
 #			rm -f $newname.0
-			$COMPRESS "$newname.0"
-			mv "$newname.0$DOT_Z" "$newfile"
+			$COMPRESS "$newname_zero"
+			mv "$newname_zero$DOT_Z" "$newfile"
 		fi
 		fixfile "$newfile"
 	fi
@@ -225,14 +233,14 @@
 	# link the file into the file.0 holding place
 	if [ -f "$filename" ]; then
 		if [ -n "$filenew" ]; then
-			ln -f "$filename" "$newname.0"
+			ln -f "$filename" "$newname_zero"
 			mv "$filename.new" "$filename"
 		else
-			mv "$filename" "$newname.0"
+			mv "$filename" "$newname_zero"
 		fi
 	fi
-	touch "$newname.0"
-	fixfile "$newname.0"
+	touch "$newname_zero"
+	fixfile "$newname_zero"

 	# report successful rotation
 	echo "Rotated \`$filename' at `date`."

#21151#10
Date:
1998-08-11 08:08:41 UTC
From:
To:
"Karl M. Hegbloom" <karlheg@bittersweet.inetarena.com> writes:

Only one problem, it should recognize shortened names and rotate them
through the cycle.

You can generate the diff with -w to ignore whitespace, btw.


Guy