Annotation of coherent/g/usr/lib/uucp/tay104/contrib/savelog.sh, revision 1.1.1.1

1.1       root        1: #! /bin/sh
                      2: # @(#)util/savelog.sh  1.4 26 Oct 1991 22:49:39
                      3: #
                      4: # savelog - save a log file
                      5: #
                      6: #    Copyright (C) 1987, 1988 Ronald S. Karr and Landon Curt Noll
                      7: # 
                      8: # See the file COPYING, distributed with smail, for restriction
                      9: # and warranty information.
                     10: #
                     11: # usage: savelog [-m mode] [-u user] [-g group] [-t] [-c cycle] [-l] file...
                     12: #
                     13: #      -m mode   - chmod log files to mode
                     14: #      -u user   - chown log files to user
                     15: #      -g group  - chgrp log files to group
                     16: #      -c cycle  - save cycle versions of the logfile  (default: 7)
                     17: #      -t        - touch file
                     18: #      -l        - don't compress any log files        (default: compress)
                     19: #      file      - log file names
                     20: #
                     21: # The savelog command saves and optionally compresses old copies of files
                     22: # into an 'dir'/OLD sub-directory.  The 'dir' directory is determined from
                     23: # the directory of each 'file'.  
                     24: #
                     25: # Older version of 'file' are named:
                     26: #
                     27: #              OLD/'file'.<number><compress_suffix>
                     28: #
                     29: # where <number> is the version number, 0 being the newest.  By default,
                     30: # version numbers > 0 are compressed (unless -l prevents it). The
                     31: # version number 0 is never compressed on the off chance that a process
                     32: # still has 'file' opened for I/O.
                     33: #
                     34: # If the 'file' does not exist or if it is zero length, no further processing
                     35: # is performed.  However if -t was also given, it will be created.
                     36: #
                     37: # For files that do exist and have lengths greater than zero, the following 
                     38: # actions are performed.
                     39: #
                     40: #      1) Version numered files are cycled.  That is version 6 is moved to
                     41: #         version 7, version is moved to becomes version 6, ... and finally
                     42: #         version 0 is moved to version 1.  Both compressed names and
                     43: #         uncompressed names are cycled, regardless of -t.  Missing version 
                     44: #         files are ignored.
                     45: #
                     46: #      2) The new OLD/file.1 is compressed and is changed subject to 
                     47: #         the -m, -u and -g flags.  This step is skipped if the -t flag 
                     48: #         was given.
                     49: #
                     50: #      3) The main file is moved to OLD/file.0.
                     51: #
                     52: #      4) If the -m, -u, -g or -t flags are given, then file is created 
                     53: #         (as an empty file) subject to the given flags.
                     54: #
                     55: #      5) The new OLD/file.0 is chanegd subject to the -m, -u and -g flags.
                     56: #
                     57: # Note: If the OLD sub-directory does not exist, it will be created 
                     58: #       with mode 0755.
                     59: #
                     60: # Note: If no -m, -u or -g flag is given, then the primary log file is 
                     61: #      not created.
                     62: #
                     63: # Note: Since the version numbers start with 0, version number <cycle>
                     64: #       is never formed.  The <cycle> count must be at least 2.
                     65: #
                     66: # Bugs: If a process is still writing to the file.0 and savelog
                     67: #      moved it to file.1 and compresses it, data could be lost.
                     68: #      Smail does not have this problem in general because it
                     69: #      restats files often.
                     70: 
                     71: # common location
                     72: PATH="X_UTIL_PATH_X:X_SECURE_PATH_X"; export PATH
                     73: COMPRESS="X_COMPRESS_X"
                     74: COMP_FLAG="X_COMP_FLAG_X"
                     75: DOT_Z="X_DOT_Z_X"
                     76: CHOWN="X_CHOWN_X"
                     77: GETOPT="X_UTIL_BIN_DIR_X/getopt"
                     78: 
                     79: # parse args
                     80: exitcode=0     # no problems to far
                     81: prog=$0
                     82: mode=
                     83: user=
                     84: group=
                     85: touch=
                     86: count=7
                     87: set -- `$GETOPT m:u:g:c:lt $*`
                     88: if [ $# -eq 0 -o  $? -ne 0 ]; then
                     89:        echo "usage: $prog [-m mode][-u user][-g group][-t][-c cycle][-l] file ..." 1>&2
                     90:        exit 1
                     91: fi
                     92: for i in $*; do
                     93:        case $i in
                     94:        -m) mode=$2; shift 2;;
                     95:        -u) user=$2; shift 2;;
                     96:        -g) group=$2; shift 2;;
                     97:        -c) count=$2; shift 2;;
                     98:        -t) touch=1; shift;;
                     99:        -l) COMPRESS=""; shift;;
                    100:        --) shift; break;;
                    101:        esac
                    102: done
                    103: if [ "$count" -lt 2 ]; then
                    104:        echo "$prog: count must be at least 2" 1>&2
                    105:        exit 2
                    106: fi
                    107: 
                    108: # cycle thru filenames
                    109: while [ $# -gt 0 ]; do
                    110: 
                    111:        # get the filename
                    112:        filename=$1
                    113:        shift
                    114: 
                    115:        # catch bogus files
                    116:        if [ -b "$filename" -o -c "$filename" -o -d "$filename" ]; then
                    117:                echo "$prog: $filename is not a regular file" 1>&2
                    118:                exitcode=3
                    119:                continue
                    120:        fi
                    121: 
                    122:        # if not a file or empty, do nothing major
                    123:        if [ ! -s $filename ]; then
                    124:                # if -t was given and it does not exist, create it
                    125:                if [ ! -z "$touch" -a ! -f $filename ]; then 
                    126:                        touch $filename
                    127:                        if [ "$?" -ne 0 ]; then
                    128:                                echo "$prog: could not touch $filename" 1>&2
                    129:                                exitcode=4
                    130:                                continue
                    131:                        fi
                    132:                        if [ ! -z "$user" ]; then 
                    133:                                $CHOWN $user $filename
                    134:                        fi
                    135:                        if [ ! -z "$group" ]; then 
                    136:                                chgrp $group $filename
                    137:                        fi
                    138:                        if [ ! -z "$mode" ]; then 
                    139:                                chmod $mode $filename
                    140:                        fi
                    141:                fi
                    142:                continue
                    143:        fi
                    144: 
                    145:        # be sure that the savedir exists and is writable
                    146:        savedir=`expr "$filename" : '\(.*\)/'`
                    147:        if [ -z "$savedir" ]; then
                    148:                savedir=./OLD
                    149:        else
                    150:                savedir=$savedir/OLD
                    151:        fi
                    152:        if [ ! -s $savedir ]; then
                    153:                mkdir $savedir
                    154:                if [ "$?" -ne 0 ]; then
                    155:                        echo "$prog: could not mkdir $savedir" 1>&2
                    156:                        exitcode=5
                    157:                        continue
                    158:                fi
                    159:                chmod 0755 $savedir
                    160:        fi
                    161:        if [ ! -d $savedir ]; then
                    162:                echo "$prog: $savedir is not a directory" 1>&2
                    163:                exitcode=6
                    164:                continue
                    165:        fi
                    166:        if [ ! -w $savedir ]; then
                    167:                echo "$prog: directory $savedir is not writable" 1>&2
                    168:                exitcode=7
                    169:                continue
                    170:        fi
                    171: 
                    172:        # deterine our uncompressed file names
                    173:        newname=`expr "$filename" : '.*/\(.*\)'`
                    174:        if [ -z "$newname" ]; then
                    175:                newname=$savedir/$filename
                    176:        else
                    177:                newname=$savedir/$newname
                    178:        fi
                    179: 
                    180:        # cycle the old compressed log files
                    181:        cycle=`expr $count - 1`
                    182:        rm -f $newname.$cycle $newname.$cycle$DOT_Z
                    183:        while [ "$cycle" -gt 1 ]; do
                    184:                # --cycle
                    185:                oldcycle=$cycle
                    186:                cycle=`expr $cycle - 1`
                    187:                # cycle log
                    188:                if [ -f $newname.$cycle$DOT_Z ]; then
                    189:                        mv -f $newname.$cycle$DOT_Z $newname.$oldcycle$DOT_Z
                    190:                fi
                    191:                if [ -f $newname.$cycle ]; then
                    192:                        # file was not compressed for some reason move it anyway
                    193:                        mv -f $newname.$cycle $newname.$oldcycle
                    194:                fi
                    195:        done
                    196: 
                    197:        # compress the old uncompressed log if needed
                    198:        if [ -f $newname.0 ]; then
                    199:                if [ -z "$COMPRESS" ]; then
                    200:                        newfile=$newname.1
                    201:                        mv $newname.0 $newfile
                    202:                else
                    203:                        newfile=$newname.1$DOT_Z
                    204:                        $COMPRESS $COMP_FLAG < $newname.0 > $newfile
                    205:                        rm -f $newname.0
                    206:                fi
                    207:                if [ ! -z "$user" ]; then 
                    208:                        $CHOWN $user $newfile
                    209:                fi
                    210:                if [ ! -z "$group" ]; then 
                    211:                        chgrp $group $newfile
                    212:                fi
                    213:                if [ ! -z "$mode" ]; then 
                    214:                        chmod $mode $newfile
                    215:                fi
                    216:        fi
                    217: 
                    218:        # move the file into the file.0 holding place
                    219:        mv -f $filename $newname.0
                    220: 
                    221:        # replace file if needed
                    222:        if [ ! -z "$touch" -o ! -z "$user" -o \
                    223:             ! -z "$group" -o ! -z "$mode" ]; then 
                    224:                touch $filename
                    225:        fi
                    226:        if [ ! -z "$user" ]; then 
                    227:                $CHOWN $user $filename
                    228:        fi
                    229:        if [ ! -z "$group" ]; then 
                    230:                chgrp $group $filename
                    231:        fi
                    232:        if [ ! -z "$mode" ]; then 
                    233:                chmod $mode $filename
                    234:        fi
                    235: 
                    236:        # fix the permissions on the holding place file.0 file
                    237:        if [ ! -z "$user" ]; then 
                    238:                $CHOWN $user $newname.0
                    239:        fi
                    240:        if [ ! -z "$group" ]; then 
                    241:                chgrp $group $newname.0
                    242:        fi
                    243:        if [ ! -z "$mode" ]; then 
                    244:                chmod $mode $newname.0
                    245:        fi
                    246: done
                    247: exit $exitcode

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.