1 package File::CheckTree;
7 validate - run many filetest checks on a tree
13 $warnings += validate( q{
21 /usr -d || warn "What happened to $file?\n"
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.
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.
40 The routine returns the number of warnings issued.
45 @EXPORT = qw(validate);
47 # $RCSfile: validate.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:19 $
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.
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.
63 # The routine returns the number of warnings issued.
66 # use File::CheckTree;
67 # $warnings += validate('
75 # /usr -d || warn "What happened to $file?\n"
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/) {
86 @testlist = split(//,$testlist);
91 $oldwarnings = $warnings;
92 foreach $one (@testlist) {
94 $this =~ s/(-\w\b)/$1 \$file/g;
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;
100 last if $warnings > $oldwarnings;
107 local($disposition,$this) = @_;
108 $file = $cwd . '/' . $file unless $file =~ m|^/|;
109 if ($this =~ /^(!?)-(\w)\s+\$file\s*$/) {
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.");
136 $mess =~ s/ is not / should not be / ||
137 $mess =~ s/ does not / should not / ||
142 $this =~ s/\$file/'$file'/g;
143 $mess = "Can't do $this.\n";
145 die "$mess\n" if $disposition eq 'die';