This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix MM doc's use of "SUPER::"
[perl5.git] / lib / File / Find.pm
1 package File::Find;
2 require 5.000;
3 require Exporter;
4 use Config;
5 require Cwd;
6 require File::Basename;
7
8
9 =head1 NAME
10
11 find - traverse a file tree
12
13 finddepth - 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
27 The wanted() function does whatever verifications you want.
28 $File::Find::dir contains the current directory name, and $_ the
29 current filename within that directory.  $File::Find::name contains
30 C<"$File::Find::dir/$_">.  You are chdir()'d to $File::Find::dir when
31 the function is called.  The function may set $File::Find::prune to
32 prune the tree.
33
34 File::Find assumes that you don't alter the $_ variable.  If you do then
35 make sure you return it to its original value before exiting your function.
36
37 This 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
42 produces 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 &&
52         ($File::Find::prune = 1);
53     }
54
55 Set the variable $File::Find::dont_use_nlink if you're using AFS,
56 since AFS cheats.
57
58 C<finddepth> is just like C<find>, except that it does a depth-first
59 search.
60
61 Here's another interesting wanted function.  It will find all symlinks
62 that don't resolve:
63
64     sub wanted {
65         -l && !-e && print "bogus link: $File::Find::name\n";
66     } 
67
68 =cut
69
70 @ISA = qw(Exporter);
71 @EXPORT = qw(find finddepth);
72
73
74 sub find {
75     my $wanted = shift;
76     my $cwd = Cwd::cwd();
77     # Localize these rather than lexicalizing them for backwards
78     # compatibility.
79     local($topdir,$topdev,$topino,$topmode,$topnlink);
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;
88                 my $fixtopdir = $topdir;
89                 $fixtopdir =~ s,/$,, ;
90                 $fixtopdir =~ s/\.dir$// if $Is_VMS;
91                 $fixtopdir =~ s/\\dir$// if $Is_NT;
92                 &finddir($wanted,$fixtopdir,$topnlink);
93             }
94             else {
95                 warn "Can't cd to $topdir: $!\n";
96             }
97         }
98         else {
99             unless (($_,$dir) = File::Basename::fileparse($topdir)) {
100                 ($dir,$_) = ('.', $topdir);
101             }
102             $name = $topdir;
103             chdir $dir && &$wanted;
104         }
105         chdir $cwd;
106     }
107 }
108
109 sub finddir {
110     my($wanted, $nlink);
111     local($dir, $name);
112     ($wanted, $dir, $nlink) = @_;
113
114     my($dev, $ino, $mode, $subcount);
115
116     # Get the list of files in the current directory.
117     opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
118     my(@filenames) = readdir(DIR);
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
142                 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
143                     # unless ($nlink || $dont_use_nlink);
144                 
145                 if (-d _) {
146
147                     # It really is a directory, so do it recursively.
148
149                     if (!$prune && chdir $_) {
150                         $name =~ s/\.dir$// if $Is_VMS;
151                         $name =~ s/\\dir$// if $Is_NT;
152                         &finddir($wanted,$name,$nlink);
153                         chdir '..';
154                     }
155                     --$subcount;
156                 }
157             }
158         }
159     }
160 }
161
162
163 sub finddepth {
164     my $wanted = shift;
165
166     $cwd = Cwd::fastcwd();;
167
168     # Localize these rather than lexicalizing them for backwards
169     # compatibility.
170     local($topdir, $topdev, $topino, $topmode, $topnlink);
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)) {
176                 my $fixtopdir = $topdir;
177                 $fixtopdir =~ s,/$,, ;
178                 $fixtopdir =~ s/\.dir$// if $Is_VMS;
179                 $fixtopdir =~ s/\\dir$// if $Is_NT;
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 {
190             unless (($_,$dir) = File::Basename::fileparse($topdir)) {
191                 ($dir,$_) = ('.', $topdir);
192             }
193             chdir $dir && &$wanted;
194         }
195         chdir $cwd;
196     }
197 }
198
199 sub finddepthdir {
200     my($wanted, $nlink);
201     local($dir, $name);
202     ($wanted,$dir,$nlink) = @_;
203     my($dev, $ino, $mode, $subcount);
204
205     # Get the list of files in the current directory.
206     opendir(DIR,'.') || warn "Can't open $dir: $!\n";
207     my(@filenames) = readdir(DIR);
208     closedir(DIR);
209
210     if ($nlink == 2 && !$dont_use_nlink) {   # This dir has no subdirectories.
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 '..';
224             $nlink = 0;
225             $name = "$dir/$_";
226             if ($subcount > 0 || $dont_use_nlink) {    # Seen all the subdirs?
227
228                 # Get link count and check for directoriness.
229
230                 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
231                 
232                 if (-d _) {
233
234                     # It really is a directory, so do it recursively.
235
236                     if (chdir $_) {
237                         $name =~ s/\.dir$// if $Is_VMS;
238                         $name =~ s/\\dir$// if $Is_NT;
239                         &finddepthdir($wanted,$name,$nlink);
240                         chdir '..';
241                     }
242                     --$subcount;
243                 }
244             }
245             &$wanted;
246         }
247     }
248 }
249
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.
257 if ($^O eq 'VMS') {
258   $Is_VMS = 1;
259   $dont_use_nlink = 1;
260 }
261 if ($^O =~ m:^mswin32:i) {
262   $Is_NT = 1;
263   $dont_use_nlink = 1;
264 }
265
266 $dont_use_nlink = 1
267     if $^O eq 'os2' || $^O eq 'msdos' || $^O eq 'amigaos';
268
269 1;
270