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