Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.2 KiB

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