1 package File::CheckTree;
6 @EXPORT = qw(validate);
8 # $RCSfile: validate.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:19 $
10 # The validate routine takes a single multiline string consisting of
11 # lines containing a filename plus a file test to try on it. (The
12 # file test may also be a 'cd', causing subsequent relative filenames
13 # to be interpreted relative to that directory.) After the file test
14 # you may put '|| die' to make it a fatal error if the file test fails.
15 # The default is '|| warn'. The file test may optionally have a ! prepended
16 # to test for the opposite condition. If you do a cd and then list some
17 # relative filenames, you may want to indent them slightly for readability.
18 # If you supply your own "die" or "warn" message, you can use $file to
19 # interpolate the filename.
21 # Filetests may be bunched: -rwx tests for all of -r, -w and -x.
22 # Only the first failed test of the bunch will produce a warning.
24 # The routine returns the number of warnings issued.
27 # use File::CheckTree;
28 # $warnings += validate('
36 # /usr -d || warn "What happened to $file?\n"
40 local($file,$test,$warnings,$oldwarnings);
41 foreach $check (split(/\n/,$_[0])) {
42 next if $check =~ /^#/;
43 next if $check =~ /^$/;
44 ($file,$test) = split(' ',$check,2);
45 if ($test =~ s/^(!?-)(\w{2,}\b)/$1Z/) {
47 @testlist = split(//,$testlist);
52 $oldwarnings = $warnings;
53 foreach $one (@testlist) {
55 $this =~ s/(-\w\b)/$1 \$file/g;
57 $this .= ' || warn' unless $this =~ /\|\|/;
58 $this =~ s/^(.*\S)\s*\|\|\s*(die|warn)$/$1 || valmess('$2','$1')/;
59 $this =~ s/\bcd\b/chdir (\$cwd = \$file)/g;
61 last if $warnings > $oldwarnings;
68 local($disposition,$this) = @_;
69 $file = $cwd . '/' . $file unless $file =~ m|^/|;
70 if ($this =~ /^(!?)-(\w)\s+\$file\s*$/) {
73 $tmp eq 'r' && ($mess = "$file is not readable by uid $>.");
74 $tmp eq 'w' && ($mess = "$file is not writable by uid $>.");
75 $tmp eq 'x' && ($mess = "$file is not executable by uid $>.");
76 $tmp eq 'o' && ($mess = "$file is not owned by uid $>.");
77 $tmp eq 'R' && ($mess = "$file is not readable by you.");
78 $tmp eq 'W' && ($mess = "$file is not writable by you.");
79 $tmp eq 'X' && ($mess = "$file is not executable by you.");
80 $tmp eq 'O' && ($mess = "$file is not owned by you.");
81 $tmp eq 'e' && ($mess = "$file does not exist.");
82 $tmp eq 'z' && ($mess = "$file does not have zero size.");
83 $tmp eq 's' && ($mess = "$file does not have non-zero size.");
84 $tmp eq 'f' && ($mess = "$file is not a plain file.");
85 $tmp eq 'd' && ($mess = "$file is not a directory.");
86 $tmp eq 'l' && ($mess = "$file is not a symbolic link.");
87 $tmp eq 'p' && ($mess = "$file is not a named pipe (FIFO).");
88 $tmp eq 'S' && ($mess = "$file is not a socket.");
89 $tmp eq 'b' && ($mess = "$file is not a block special file.");
90 $tmp eq 'c' && ($mess = "$file is not a character special file.");
91 $tmp eq 'u' && ($mess = "$file does not have the setuid bit set.");
92 $tmp eq 'g' && ($mess = "$file does not have the setgid bit set.");
93 $tmp eq 'k' && ($mess = "$file does not have the sticky bit set.");
94 $tmp eq 'T' && ($mess = "$file is not a text file.");
95 $tmp eq 'B' && ($mess = "$file is not a binary file.");
97 $mess =~ s/ is not / should not be / ||
98 $mess =~ s/ does not / should not / ||
101 print STDERR $mess,"\n";
104 $this =~ s/\$file/'$file'/g;
105 print STDERR "Can't do $this.\n";
107 if ($disposition eq 'die') { exit 1; }