Home | History | Annotate | Line # | Download | only in gzip
znew revision 1.1
      1 #!/bin/ksh -
      2 #
      3 # $OpenBSD: znew,v 1.2 2003/08/05 18:22:17 deraadt Exp $
      4 #
      5 # Copyright (c) 2003 Otto Moerbeek <otto (at] drijf.net>
      6 #
      7 # Permission to use, copy, modify, and distribute this software for any
      8 # purpose with or without fee is hereby granted, provided that the above
      9 # copyright notice and this permission notice appear in all copies.
     10 #
     11 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18 #
     19 
     20 # Return 0 if the first arg file size is smaller than the second, 1 otherwise.
     21 smaller () {
     22 	a=`du -k "$1" | awk '{ print $1 }'`
     23 	b=`du -k "$2" | awk '{ print $1 }'`
     24 	test $a -lt $b
     25 }
     26 
     27 # Check gzip integrity if the -t flag is specified
     28 checkfile () {
     29 	if test $tflag -eq 1; then
     30 		gzip -qt < "$1"
     31 	fi
     32 }
     33 
     34 # Decompress a file and then gzip it
     35 process () {
     36 	prefix="${1%.Z}"
     37 	filez="$prefix".Z
     38 	filegz="$prefix".gz
     39 
     40 	if test ! -e "$filez"; then
     41 		echo "$prog: $filez does not exist"
     42 		return 1
     43 	fi
     44 	if test ! -f "$filez"; then
     45 		echo "$prog: $filez is not a regular file"
     46 		return 1
     47 	fi
     48 	if test -e "$filegz" -a $fflag -eq 0; then
     49 		echo "$prog: $filegz already exists"
     50 		return 1
     51 	fi
     52 
     53 	tmp=`mktemp /tmp/znewXXXXXXXXXX` || {
     54 		echo "$prog: cannot create tmp file"
     55 		return 1
     56 	}
     57 	trap 'rm -f "$tmp"; exit 1' HUP INT QUIT PIPE TERM
     58 
     59 	# Do the actual work, producing a file "$tmp"
     60 	if uncompress -f -c < "$filez" | gzip -f $gzipflags -o "$tmp"; then
     61 
     62 		if test $kflag -eq 1 && smaller "$filez" "$tmp"; then
     63 			echo -n "$prog: $filez is smaller than $filegz"
     64 			echo "; keeping it"
     65 			rm -f "$tmp"
     66 			return 0
     67 		fi
     68 		if ! checkfile "$tmp"; then
     69 			echo "$prog: integrity check of $tmp failed"
     70 			rm -f "$tmp"
     71 			return 1;
     72 		fi
     73 
     74 		# Try to keep the mode of the original file
     75 		if ! cp -fp "$filez" "$filegz"; then
     76 			echo "$prog: warning: could not keep mode of $filez"
     77 		fi
     78 		if  ! cp "$tmp" "$filegz" 2> /dev/null; then
     79 			echo "$prog: warning: could not keep mode of $filez"
     80 			if ! cp -f "$tmp" "$filegz" 2> /dev/null; then
     81 				echo "$prog: could not copy $tmp to $filegz"
     82 				rm -f "$filegz" "$tmp"
     83 				return 1
     84 			fi
     85 		fi
     86 		if ! touch -fr "$filez" "$filegz"; then
     87 			echo -n "$prog: warning: could not keep timestamp of "
     88 			echo "$filez"
     89 		fi
     90 		rm -f "$filez" "$tmp"
     91 	else
     92 		echo "$prog: failed to process $filez"
     93 		rm -f "$tmp"
     94 		return 1
     95 	fi
     96 }
     97 
     98 prog=`basename "$0"`
     99 usage="usage: $prog [-ftv9K] file ..."
    100 
    101 fflag=0
    102 tflag=0
    103 kflag=0
    104 gzipflags=
    105 
    106 # -P flag is recognized to maintain compatibility, but ignored. Pipe mode is
    107 # always used
    108 while getopts :ftv9PK i; do
    109 	case $i in
    110 		f) fflag=1;;
    111 		t) tflag=1;;
    112 		v) gzipflags="-v $gzipflags";;
    113 		9) gzipflags="-9 $gzipflags";;
    114 		P) ;;
    115 		K) kflag=1;;
    116 		\?) echo "$usage"; exit 1;;
    117 	esac
    118 done
    119 
    120 shift OPTIND-1
    121 
    122 if test $# -eq 0; then
    123 	echo "$usage"
    124 	exit 1
    125 fi
    126 
    127 rc=0
    128 
    129 while test $# -ne 0; do
    130 	if ! process "$1"; then
    131 		rc=$?
    132 	fi
    133 	shift
    134 done
    135 exit $rc
    136