perl5.001 patch.1f
[p5sagit/p5-mst-13.2.git] / lib / File / CheckTree.pm
CommitLineData
a0d0e21e 1package File::CheckTree;
2require 5.000;
3require Exporter;
4
5@ISA = qw(Exporter);
6@EXPORT = qw(validate);
7
8# $RCSfile: validate.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:19 $
9
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.
20
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.
23
24# The routine returns the number of warnings issued.
25
26# Usage:
27# use File::CheckTree;
28# $warnings += validate('
29# /vmunix -e || die
30# /boot -e || die
31# /bin cd
32# csh -ex
33# csh !-ug
34# sh -ex
35# sh !-ug
36# /usr -d || warn "What happened to $file?\n"
37# ');
38
39sub validate {
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/) {
46 $testlist = $2;
47 @testlist = split(//,$testlist);
48 }
49 else {
50 @testlist = ('Z');
51 }
52 $oldwarnings = $warnings;
53 foreach $one (@testlist) {
54 $this = $test;
55 $this =~ s/(-\w\b)/$1 \$file/g;
56 $this =~ s/-Z/-$one/;
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;
60 eval $this;
61 last if $warnings > $oldwarnings;
62 }
63 }
64 $warnings;
65}
66
67sub valmess {
68 local($disposition,$this) = @_;
69 $file = $cwd . '/' . $file unless $file =~ m|^/|;
70 if ($this =~ /^(!?)-(\w)\s+\$file\s*$/) {
71 $neg = $1;
72 $tmp = $2;
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.");
96 if ($neg eq '!') {
97 $mess =~ s/ is not / should not be / ||
98 $mess =~ s/ does not / should not / ||
99 $mess =~ s/ not / /;
100 }
748a9306 101 print STDERR $mess,"\n";
a0d0e21e 102 }
103 else {
104 $this =~ s/\$file/'$file'/g;
748a9306 105 print STDERR "Can't do $this.\n";
a0d0e21e 106 }
107 if ($disposition eq 'die') { exit 1; }
108 ++$warnings;
109}
110
1111;
112