home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2 / Openstep-4.2-Intel-User.iso / NextLibrary / PrivateFrameworks / Uucp.framework / Versions / A / Resources / contrib / savelog.sh < prev    next >
Linux/UNIX/POSIX Shell Script  |  1992-03-04  |  6KB  |  248 lines

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