b0312be10e14472b470105e7cdd0f845db0ffbb5
[p5sagit/p5-mst-13.2.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     my ($topdir,$topdev,$topino,$topmode,$topnlink);
78     foreach $topdir (@_) {
79         (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
80           || (warn("Can't stat $topdir: $!\n"), next);
81         if (-d _) {
82             if (chdir($topdir)) {
83                 ($dir,$_) = ($topdir,'.');
84                 $name = $topdir;
85                 &$wanted;
86                 my $fixtopdir = $topdir;
87                 $fixtopdir =~ s,/$,, ;
88                 $fixtopdir =~ s/\.dir$// if $Is_VMS;
89                 $fixtopdir =~ s/\\dir$// if $Is_NT;
90                 &finddir($wanted,$fixtopdir,$topnlink);
91             }
92             else {
93                 warn "Can't cd to $topdir: $!\n";
94             }
95         }
96         else {
97             unless (($_,$dir) = File::Basename::fileparse($topdir)) {
98                 ($dir,$_) = ('.', $topdir);
99             }
100             $name = $topdir;
101             chdir $dir && &$wanted;
102         }
103         chdir $cwd;
104     }
105 }
106
107 sub finddir {
108     my($wanted, $nlink);
109     local($dir, $name);
110     ($wanted, $dir, $nlink) = @_;
111
112     my($dev, $ino, $mode, $subcount);
113
114     # Get the list of files in the current directory.
115     opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
116     my(@filenames) = readdir(DIR);
117     closedir(DIR);
118
119     if ($nlink == 2 && !$dont_use_nlink) {  # This dir has no subdirectories.
120         for (@filenames) {
121             next if $_ eq '.';
122             next if $_ eq '..';
123             $name = "$dir/$_";
124             $nlink = 0;
125             &$wanted;
126         }
127     }
128     else {                    # This dir has subdirectories.
129         $subcount = $nlink - 2;
130         for (@filenames) {
131             next if $_ eq '.';
132             next if $_ eq '..';
133             $nlink = $prune = 0;
134             $name = "$dir/$_";
135             &$wanted;
136             if ($subcount > 0 || $dont_use_nlink) {    # Seen all the subdirs?
137
138                 # Get link count and check for directoriness.
139
140                 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
141                     # unless ($nlink || $dont_use_nlink);
142                 
143                 if (-d _) {
144
145                     # It really is a directory, so do it recursively.
146
147                     if (!$prune && chdir $_) {
148                         $name =~ s/\.dir$// if $Is_VMS;
149                         $name =~ s/\\dir$// if $Is_NT;
150                         &finddir($wanted,$name,$nlink);
151                         chdir '..';
152                     }
153                     --$subcount;
154                 }
155             }
156         }
157     }
158 }
159
160
161 sub finddepth {
162     my $wanted = shift;
163
164     $cwd = Cwd::fastcwd();;
165
166     my($topdir, $topdev, $topino, $topmode, $topnlink);
167     foreach $topdir (@_) {
168         (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
169           || (warn("Can't stat $topdir: $!\n"), next);
170         if (-d _) {
171             if (chdir($topdir)) {
172                 my $fixtopdir = $topdir;
173                 $fixtopdir =~ s,/$,, ;
174                 $fixtopdir =~ s/\.dir$// if $Is_VMS;
175                 $fixtopdir =~ s/\\dir$// if $Is_NT;
176                 &finddepthdir($wanted,$fixtopdir,$topnlink);
177                 ($dir,$_) = ($fixtopdir,'.');
178                 $name = $fixtopdir;
179                 &$wanted;
180             }
181             else {
182                 warn "Can't cd to $topdir: $!\n";
183             }
184         }
185         else {
186             unless (($_,$dir) = File::Basename::fileparse($topdir)) {
187                 ($dir,$_) = ('.', $topdir);
188             }
189             chdir $dir && &$wanted;
190         }
191         chdir $cwd;
192     }
193 }
194
195 sub finddepthdir {
196     my($wanted, $nlink);
197     local($dir, $name);
198     ($wanted,$dir,$nlink) = @_;
199     my($dev, $ino, $mode, $subcount);
200
201     # Get the list of files in the current directory.
202     opendir(DIR,'.') || warn "Can't open $dir: $!\n";
203     my(@filenames) = readdir(DIR);
204     closedir(DIR);
205
206     if ($nlink == 2 && !$dont_use_nlink) {   # This dir has no subdirectories.
207         for (@filenames) {
208             next if $_ eq '.';
209             next if $_ eq '..';
210             $name = "$dir/$_";
211             $nlink = 0;
212             &$wanted;
213         }
214     }
215     else {                    # This dir has subdirectories.
216         $subcount = $nlink - 2;
217         for (@filenames) {
218             next if $_ eq '.';
219             next if $_ eq '..';
220             $nlink = 0;
221             $name = "$dir/$_";
222             if ($subcount > 0 || $dont_use_nlink) {    # Seen all the subdirs?
223
224                 # Get link count and check for directoriness.
225
226                 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
227                 
228                 if (-d _) {
229
230                     # It really is a directory, so do it recursively.
231
232                     if (chdir $_) {
233                         $name =~ s/\.dir$// if $Is_VMS;
234                         $name =~ s/\\dir$// if $Is_NT;
235                         &finddepthdir($wanted,$name,$nlink);
236                         chdir '..';
237                     }
238                     --$subcount;
239                 }
240             }
241             &$wanted;
242         }
243     }
244 }
245
246 # Set dont_use_nlink in your hint file if your system's stat doesn't
247 # report the number of links in a directory as an indication
248 # of the number of files.
249 # See, e.g. hints/machten.sh for MachTen 2.2.
250 $dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
251
252 # These are hard-coded for now, but may move to hint files.
253 if ($^O eq 'VMS') {
254   $Is_VMS = 1;
255   $dont_use_nlink = 1;
256 }
257 if ($^O =~ m:^mswin32:i) {
258   $Is_NT = 1;
259   $dont_use_nlink = 1;
260 }
261
262 $dont_use_nlink = 1 if $^O eq 'os2';
263
264 1;
265