home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl501m.zip / lib / File / CheckTree.pm < prev    next >
Text File  |  1995-07-03  |  5KB  |  152 lines

  1. package File::CheckTree;
  2. require 5.000;
  3. require Exporter;
  4.  
  5. =head1 NAME
  6.  
  7. validate - run many filetest checks on a tree
  8.  
  9. =head1 SYNOPSIS
  10.  
  11.     use File::CheckTree;
  12.  
  13.     $warnings += validate( q{
  14.     /vmunix                 -e || die
  15.     /boot                   -e || die
  16.     /bin                    cd
  17.         csh                 -ex
  18.         csh                 !-ug
  19.         sh                  -ex
  20.         sh                  !-ug
  21.     /usr                    -d || warn "What happened to $file?\n"
  22.     });
  23.  
  24. =head1 DESCRIPTION
  25.  
  26. The validate() routine takes a single multiline string consisting of
  27. lines containing a filename plus a file test to try on it.  (The
  28. file test may also be a "cd", causing subsequent relative filenames
  29. to be interpreted relative to that directory.)  After the file test
  30. you may put C<|| die> to make it a fatal error if the file test fails.
  31. The default is C<|| warn>.  The file test may optionally have a "!' prepended
  32. to test for the opposite condition.  If you do a cd and then list some
  33. relative filenames, you may want to indent them slightly for readability.
  34. If you supply your own die() or warn() message, you can use $file to
  35. interpolate the filename.
  36.  
  37. Filetests may be bunched:  "-rwx" tests for all of C<-r>, C<-w>, and C<-x>.
  38. Only the first failed test of the bunch will produce a warning.
  39.  
  40. The routine returns the number of warnings issued.
  41.  
  42. =cut
  43.  
  44. @ISA = qw(Exporter);
  45. @EXPORT = qw(validate);
  46.  
  47. # $RCSfile: validate.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:19 $
  48.  
  49. # The validate routine takes a single multiline string consisting of
  50. # lines containing a filename plus a file test to try on it.  (The
  51. # file test may also be a 'cd', causing subsequent relative filenames
  52. # to be interpreted relative to that directory.)  After the file test
  53. # you may put '|| die' to make it a fatal error if the file test fails.
  54. # The default is '|| warn'.  The file test may optionally have a ! prepended
  55. # to test for the opposite condition.  If you do a cd and then list some
  56. # relative filenames, you may want to indent them slightly for readability.
  57. # If you supply your own "die" or "warn" message, you can use $file to
  58. # interpolate the filename.
  59.  
  60. # Filetests may be bunched:  -rwx tests for all of -r, -w and -x.
  61. # Only the first failed test of the bunch will produce a warning.
  62.  
  63. # The routine returns the number of warnings issued.
  64.  
  65. # Usage:
  66. #    use File::CheckTree;
  67. #    $warnings += validate('
  68. #    /vmunix            -e || die
  69. #    /boot            -e || die
  70. #    /bin            cd
  71. #        csh            -ex
  72. #        csh            !-ug
  73. #        sh            -ex
  74. #        sh            !-ug
  75. #    /usr            -d || warn "What happened to $file?\n"
  76. #    ');
  77.  
  78. sub validate {
  79.     local($file,$test,$warnings,$oldwarnings);
  80.     foreach $check (split(/\n/,$_[0])) {
  81.     next if $check =~ /^#/;
  82.     next if $check =~ /^$/;
  83.     ($file,$test) = split(' ',$check,2);
  84.     if ($test =~ s/^(!?-)(\w{2,}\b)/$1Z/) {
  85.         $testlist = $2;
  86.         @testlist = split(//,$testlist);
  87.     }
  88.     else {
  89.         @testlist = ('Z');
  90.     }
  91.     $oldwarnings = $warnings;
  92.     foreach $one (@testlist) {
  93.         $this = $test;
  94.         $this =~ s/(-\w\b)/$1 \$file/g;
  95.         $this =~ s/-Z/-$one/;
  96.         $this .= ' || warn' unless $this =~ /\|\|/;
  97.         $this =~ s/^(.*\S)\s*\|\|\s*(die|warn)$/$1 || valmess('$2','$1')/;
  98.         $this =~ s/\bcd\b/chdir (\$cwd = \$file)/g;
  99.         eval $this;
  100.         last if $warnings > $oldwarnings;
  101.     }
  102.     }
  103.     $warnings;
  104. }
  105.  
  106. sub valmess {
  107.     local($disposition,$this) = @_;
  108.     $file = $cwd . '/' . $file unless $file =~ m|^/|;
  109.     if ($this =~ /^(!?)-(\w)\s+\$file\s*$/) {
  110.     $neg = $1;
  111.     $tmp = $2;
  112.     $tmp eq 'r' && ($mess = "$file is not readable by uid $>.");
  113.     $tmp eq 'w' && ($mess = "$file is not writable by uid $>.");
  114.     $tmp eq 'x' && ($mess = "$file is not executable by uid $>.");
  115.     $tmp eq 'o' && ($mess = "$file is not owned by uid $>.");
  116.     $tmp eq 'R' && ($mess = "$file is not readable by you.");
  117.     $tmp eq 'W' && ($mess = "$file is not writable by you.");
  118.     $tmp eq 'X' && ($mess = "$file is not executable by you.");
  119.     $tmp eq 'O' && ($mess = "$file is not owned by you.");
  120.     $tmp eq 'e' && ($mess = "$file does not exist.");
  121.     $tmp eq 'z' && ($mess = "$file does not have zero size.");
  122.     $tmp eq 's' && ($mess = "$file does not have non-zero size.");
  123.     $tmp eq 'f' && ($mess = "$file is not a plain file.");
  124.     $tmp eq 'd' && ($mess = "$file is not a directory.");
  125.     $tmp eq 'l' && ($mess = "$file is not a symbolic link.");
  126.     $tmp eq 'p' && ($mess = "$file is not a named pipe (FIFO).");
  127.     $tmp eq 'S' && ($mess = "$file is not a socket.");
  128.     $tmp eq 'b' && ($mess = "$file is not a block special file.");
  129.     $tmp eq 'c' && ($mess = "$file is not a character special file.");
  130.     $tmp eq 'u' && ($mess = "$file does not have the setuid bit set.");
  131.     $tmp eq 'g' && ($mess = "$file does not have the setgid bit set.");
  132.     $tmp eq 'k' && ($mess = "$file does not have the sticky bit set.");
  133.     $tmp eq 'T' && ($mess = "$file is not a text file.");
  134.     $tmp eq 'B' && ($mess = "$file is not a binary file.");
  135.     if ($neg eq '!') {
  136.         $mess =~ s/ is not / should not be / ||
  137.         $mess =~ s/ does not / should not / ||
  138.         $mess =~ s/ not / /;
  139.     }
  140.     print STDERR $mess,"\n";
  141.     }
  142.     else {
  143.     $this =~ s/\$file/'$file'/g;
  144.     print STDERR "Can't do $this.\n";
  145.     }
  146.     if ($disposition eq 'die') { exit 1; }
  147.     ++$warnings;
  148. }
  149.  
  150. 1;
  151.  
  152.