Commit | Line | Data |
a0d0e21e |
1 | package File::Find; |
2 | require 5.000; |
3 | require Exporter; |
10eba763 |
4 | use Config; |
6280b799 |
5 | require Cwd; |
6 | require File::Basename; |
7 | |
a0d0e21e |
8 | |
f06db76b |
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 | |
6280b799 |
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. |
f06db76b |
33 | |
47a735e8 |
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 | |
f06db76b |
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 && |
6280b799 |
52 | ($File::Find::prune = 1); |
f06db76b |
53 | } |
54 | |
6280b799 |
55 | Set the variable $File::Find::dont_use_nlink if you're using AFS, |
56 | since AFS cheats. |
f06db76b |
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 { |
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 | |
74 | sub find { |
75 | my $wanted = shift; |
5a13742d |
76 | my $cwd = Cwd::cwd(); |
6280b799 |
77 | my ($topdir,$topdev,$topino,$topmode,$topnlink); |
a0d0e21e |
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; |
6280b799 |
86 | my $fixtopdir = $topdir; |
87 | $fixtopdir =~ s,/$,, ; |
9f637d3d |
88 | $fixtopdir =~ s/\.dir$// if $Is_VMS; |
89 | $fixtopdir =~ s/\\dir$// if $Is_NT; |
a0d0e21e |
90 | &finddir($wanted,$fixtopdir,$topnlink); |
91 | } |
92 | else { |
93 | warn "Can't cd to $topdir: $!\n"; |
94 | } |
95 | } |
96 | else { |
9f637d3d |
97 | unless (($_,$dir) = File::Basename::fileparse($topdir)) { |
a0d0e21e |
98 | ($dir,$_) = ('.', $topdir); |
99 | } |
100 | $name = $topdir; |
101 | chdir $dir && &$wanted; |
102 | } |
103 | chdir $cwd; |
104 | } |
105 | } |
106 | |
107 | sub finddir { |
6280b799 |
108 | my($wanted, $nlink); |
109 | local($dir, $name); |
110 | ($wanted, $dir, $nlink) = @_; |
a0d0e21e |
111 | |
6280b799 |
112 | my($dev, $ino, $mode, $subcount); |
a0d0e21e |
113 | |
6280b799 |
114 | # Get the list of files in the current directory. |
a0d0e21e |
115 | opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return); |
6280b799 |
116 | my(@filenames) = readdir(DIR); |
a0d0e21e |
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 | |
10eba763 |
140 | ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_)); |
141 | # unless ($nlink || $dont_use_nlink); |
a0d0e21e |
142 | |
143 | if (-d _) { |
144 | |
145 | # It really is a directory, so do it recursively. |
146 | |
147 | if (!$prune && chdir $_) { |
748a9306 |
148 | $name =~ s/\.dir$// if $Is_VMS; |
9f637d3d |
149 | $name =~ s/\\dir$// if $Is_NT; |
a0d0e21e |
150 | &finddir($wanted,$name,$nlink); |
151 | chdir '..'; |
152 | } |
153 | --$subcount; |
154 | } |
155 | } |
156 | } |
157 | } |
158 | } |
159 | |
a0d0e21e |
160 | |
161 | sub finddepth { |
162 | my $wanted = shift; |
6280b799 |
163 | |
164 | $cwd = Cwd::fastcwd();; |
165 | |
166 | my($topdir, $topdev, $topino, $topmode, $topnlink); |
a0d0e21e |
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)) { |
6280b799 |
172 | my $fixtopdir = $topdir; |
173 | $fixtopdir =~ s,/$,, ; |
748a9306 |
174 | $fixtopdir =~ s/\.dir$// if $Is_VMS; |
9f637d3d |
175 | $fixtopdir =~ s/\\dir$// if $Is_NT; |
a0d0e21e |
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 { |
9f637d3d |
186 | unless (($_,$dir) = File::Basename::fileparse($topdir)) { |
a0d0e21e |
187 | ($dir,$_) = ('.', $topdir); |
188 | } |
189 | chdir $dir && &$wanted; |
190 | } |
191 | chdir $cwd; |
192 | } |
193 | } |
194 | |
195 | sub finddepthdir { |
6280b799 |
196 | my($wanted, $nlink); |
197 | local($dir, $name); |
198 | ($wanted,$dir,$nlink) = @_; |
199 | my($dev, $ino, $mode, $subcount); |
a0d0e21e |
200 | |
201 | # Get the list of files in the current directory. |
a0d0e21e |
202 | opendir(DIR,'.') || warn "Can't open $dir: $!\n"; |
203 | my(@filenames) = readdir(DIR); |
204 | closedir(DIR); |
205 | |
748a9306 |
206 | if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories. |
a0d0e21e |
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 '..'; |
6280b799 |
220 | $nlink = 0; |
a0d0e21e |
221 | $name = "$dir/$_"; |
748a9306 |
222 | if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs? |
a0d0e21e |
223 | |
224 | # Get link count and check for directoriness. |
225 | |
748a9306 |
226 | ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_)); |
a0d0e21e |
227 | |
228 | if (-d _) { |
229 | |
230 | # It really is a directory, so do it recursively. |
231 | |
6280b799 |
232 | if (chdir $_) { |
748a9306 |
233 | $name =~ s/\.dir$// if $Is_VMS; |
9f637d3d |
234 | $name =~ s/\\dir$// if $Is_NT; |
a0d0e21e |
235 | &finddepthdir($wanted,$name,$nlink); |
236 | chdir '..'; |
237 | } |
238 | --$subcount; |
239 | } |
240 | } |
241 | &$wanted; |
242 | } |
243 | } |
244 | } |
245 | |
6280b799 |
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. |
10eba763 |
253 | if ($^O eq 'VMS') { |
748a9306 |
254 | $Is_VMS = 1; |
255 | $dont_use_nlink = 1; |
256 | } |
9f637d3d |
257 | if ($^O =~ m:^mswin32:i) { |
258 | $Is_NT = 1; |
259 | $dont_use_nlink = 1; |
260 | } |
748a9306 |
261 | |
55497cff |
262 | $dont_use_nlink = 1 |
263 | if $^O eq 'os2' || $^O eq 'msdos' || $^O eq 'amigaos'; |
6280b799 |
264 | |
a0d0e21e |
265 | 1; |
266 | |