New file, part of MakeMaker 5.21.
[p5sagit/p5-mst-13.2.git] / lib / File / Find.pm
CommitLineData
a0d0e21e 1package File::Find;
2require 5.000;
3require Exporter;
6280b799 4require Config;
5require Cwd;
6require File::Basename;
7
a0d0e21e 8
f06db76b 9=head1 NAME
10
11find - traverse a file tree
12
13finddepth - traverse a directory structure depth-first
14
15=head1 SYNOPSIS
16
17 use File::Find;
18 find(\&wanted, '/foo','/bar');
19 sub wanted { ... }
20
21 use File::Find;
22 finddepth(\&wanted, '/foo','/bar');
23 sub wanted { ... }
24
25=head1 DESCRIPTION
26
6280b799 27The wanted() function does whatever verifications you want.
28$File::Find::dir contains the current directory name, and $_ the
29current filename within that directory. $File::Find::name contains
30C<"$File::Find::dir/$_">. You are chdir()'d to $File::Find::dir when
31the function is called. The function may set $File::Find::prune to
32prune the tree.
f06db76b 33
34This library is primarily for the C<find2perl> tool, which when fed,
35
36 find2perl / -name .nfs\* -mtime +7 \
37 -exec rm -f {} \; -o -fstype nfs -prune
38
39produces something like:
40
41 sub wanted {
42 /^\.nfs.*$/ &&
43 (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
44 int(-M _) > 7 &&
45 unlink($_)
46 ||
47 ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
48 $dev < 0 &&
6280b799 49 ($File::Find::prune = 1);
f06db76b 50 }
51
6280b799 52Set the variable $File::Find::dont_use_nlink if you're using AFS,
53since AFS cheats.
f06db76b 54
55C<finddepth> is just like C<find>, except that it does a depth-first
56search.
57
58Here's another interesting wanted function. It will find all symlinks
59that don't resolve:
60
61 sub wanted {
6280b799 62 -l && !-e && print "bogus link: $File::Find::name\n";
f06db76b 63 }
64
65=cut
66
a0d0e21e 67@ISA = qw(Exporter);
6280b799 68@EXPORT = qw(find finddepth);
69
a0d0e21e 70
71sub find {
72 my $wanted = shift;
6280b799 73 my $cwd = Cwd::fastcwd();
74 my ($topdir,$topdev,$topino,$topmode,$topnlink);
a0d0e21e 75 foreach $topdir (@_) {
76 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
77 || (warn("Can't stat $topdir: $!\n"), next);
78 if (-d _) {
79 if (chdir($topdir)) {
80 ($dir,$_) = ($topdir,'.');
81 $name = $topdir;
82 &$wanted;
6280b799 83 my $fixtopdir = $topdir;
84 $fixtopdir =~ s,/$,, ;
748a9306 85 $fixtopdir =~ s/\.dir$// if $Is_VMS; ;
a0d0e21e 86 &finddir($wanted,$fixtopdir,$topnlink);
87 }
88 else {
89 warn "Can't cd to $topdir: $!\n";
90 }
91 }
92 else {
6280b799 93 unless (($dir,$_) = File::Basename::fileparse($topdir)) {
a0d0e21e 94 ($dir,$_) = ('.', $topdir);
95 }
96 $name = $topdir;
97 chdir $dir && &$wanted;
98 }
99 chdir $cwd;
100 }
101}
102
103sub finddir {
6280b799 104 my($wanted, $nlink);
105 local($dir, $name);
106 ($wanted, $dir, $nlink) = @_;
a0d0e21e 107
6280b799 108 my($dev, $ino, $mode, $subcount);
a0d0e21e 109
6280b799 110 # Get the list of files in the current directory.
a0d0e21e 111 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
6280b799 112 my(@filenames) = readdir(DIR);
a0d0e21e 113 closedir(DIR);
114
115 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
116 for (@filenames) {
117 next if $_ eq '.';
118 next if $_ eq '..';
119 $name = "$dir/$_";
120 $nlink = 0;
121 &$wanted;
122 }
123 }
124 else { # This dir has subdirectories.
125 $subcount = $nlink - 2;
126 for (@filenames) {
127 next if $_ eq '.';
128 next if $_ eq '..';
129 $nlink = $prune = 0;
130 $name = "$dir/$_";
131 &$wanted;
132 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
133
134 # Get link count and check for directoriness.
135
748a9306 136 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_))
137 unless ($nlink || $dont_use_nlink);
a0d0e21e 138
139 if (-d _) {
140
141 # It really is a directory, so do it recursively.
142
143 if (!$prune && chdir $_) {
748a9306 144 $name =~ s/\.dir$// if $Is_VMS;
a0d0e21e 145 &finddir($wanted,$name,$nlink);
146 chdir '..';
147 }
148 --$subcount;
149 }
150 }
151 }
152 }
153}
154
a0d0e21e 155
156sub finddepth {
157 my $wanted = shift;
6280b799 158
159 $cwd = Cwd::fastcwd();;
160
161 my($topdir, $topdev, $topino, $topmode, $topnlink);
a0d0e21e 162 foreach $topdir (@_) {
163 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
164 || (warn("Can't stat $topdir: $!\n"), next);
165 if (-d _) {
166 if (chdir($topdir)) {
6280b799 167 my $fixtopdir = $topdir;
168 $fixtopdir =~ s,/$,, ;
748a9306 169 $fixtopdir =~ s/\.dir$// if $Is_VMS;
a0d0e21e 170 &finddepthdir($wanted,$fixtopdir,$topnlink);
171 ($dir,$_) = ($fixtopdir,'.');
172 $name = $fixtopdir;
173 &$wanted;
174 }
175 else {
176 warn "Can't cd to $topdir: $!\n";
177 }
178 }
179 else {
6280b799 180 unless (($dir,$_) = File::Basename::fileparse($topdir)) {
a0d0e21e 181 ($dir,$_) = ('.', $topdir);
182 }
183 chdir $dir && &$wanted;
184 }
185 chdir $cwd;
186 }
187}
188
189sub finddepthdir {
6280b799 190 my($wanted, $nlink);
191 local($dir, $name);
192 ($wanted,$dir,$nlink) = @_;
193 my($dev, $ino, $mode, $subcount);
a0d0e21e 194
195 # Get the list of files in the current directory.
a0d0e21e 196 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
197 my(@filenames) = readdir(DIR);
198 closedir(DIR);
199
748a9306 200 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
a0d0e21e 201 for (@filenames) {
202 next if $_ eq '.';
203 next if $_ eq '..';
204 $name = "$dir/$_";
205 $nlink = 0;
206 &$wanted;
207 }
208 }
209 else { # This dir has subdirectories.
210 $subcount = $nlink - 2;
211 for (@filenames) {
212 next if $_ eq '.';
213 next if $_ eq '..';
6280b799 214 $nlink = 0;
a0d0e21e 215 $name = "$dir/$_";
748a9306 216 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
a0d0e21e 217
218 # Get link count and check for directoriness.
219
748a9306 220 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
a0d0e21e 221
222 if (-d _) {
223
224 # It really is a directory, so do it recursively.
225
6280b799 226 if (chdir $_) {
748a9306 227 $name =~ s/\.dir$// if $Is_VMS;
a0d0e21e 228 &finddepthdir($wanted,$name,$nlink);
229 chdir '..';
230 }
231 --$subcount;
232 }
233 }
234 &$wanted;
235 }
236 }
237}
238
6280b799 239# Set dont_use_nlink in your hint file if your system's stat doesn't
240# report the number of links in a directory as an indication
241# of the number of files.
242# See, e.g. hints/machten.sh for MachTen 2.2.
243$dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
244
245# These are hard-coded for now, but may move to hint files.
246if ($Config::Config{'osname'} eq 'VMS') {
748a9306 247 $Is_VMS = 1;
248 $dont_use_nlink = 1;
249}
250
6280b799 251$dont_use_nlink = 1 if $Config::Config{'osname'} =~ m:^os/?2$:i ;
252$dont_use_nlink = 1 if $Config::Config{'osname'} =~ m:^mswin32$:i ;
253
a0d0e21e 2541;
255