Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
5.6 KiB

  1. package File::Find;
  2. require 5.000;
  3. require Exporter;
  4. require Cwd;
  5. =head1 NAME
  6. find - traverse a file tree
  7. finddepth - traverse a directory structure depth-first
  8. =head1 SYNOPSIS
  9. use File::Find;
  10. find(\&wanted, '/foo','/bar');
  11. sub wanted { ... }
  12. use File::Find;
  13. finddepth(\&wanted, '/foo','/bar');
  14. sub wanted { ... }
  15. =head1 DESCRIPTION
  16. The first argument to find() is either a hash reference describing the
  17. operations to be performed for each file, or a code reference. If it
  18. is a hash reference, then the value for the key C<wanted> should be a
  19. code reference. This code reference is called I<the wanted()
  20. function> below.
  21. Currently the only other supported key for the above hash is
  22. C<bydepth>, in presense of which the walk over directories is
  23. performed depth-first. Entry point finddepth() is a shortcut for
  24. specifying C<{ bydepth => 1}> in the first argument of find().
  25. The wanted() function does whatever verifications you want.
  26. $File::Find::dir contains the current directory name, and $_ the
  27. current filename within that directory. $File::Find::name contains
  28. C<"$File::Find::dir/$_">. You are chdir()'d to $File::Find::dir when
  29. the function is called. The function may set $File::Find::prune to
  30. prune the tree.
  31. File::Find assumes that you don't alter the $_ variable. If you do then
  32. make sure you return it to its original value before exiting your function.
  33. This library is useful for the C<find2perl> tool, which when fed,
  34. find2perl / -name .nfs\* -mtime +7 \
  35. -exec rm -f {} \; -o -fstype nfs -prune
  36. produces something like:
  37. sub wanted {
  38. /^\.nfs.*$/ &&
  39. (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  40. int(-M _) > 7 &&
  41. unlink($_)
  42. ||
  43. ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
  44. $dev < 0 &&
  45. ($File::Find::prune = 1);
  46. }
  47. Set the variable $File::Find::dont_use_nlink if you're using AFS,
  48. since AFS cheats.
  49. C<finddepth> is just like C<find>, except that it does a depth-first
  50. search.
  51. Here's another interesting wanted function. It will find all symlinks
  52. that don't resolve:
  53. sub wanted {
  54. -l && !-e && print "bogus link: $File::Find::name\n";
  55. }
  56. =head1 BUGS
  57. There is no way to make find or finddepth follow symlinks.
  58. =cut
  59. @ISA = qw(Exporter);
  60. @EXPORT = qw(find finddepth);
  61. sub find_opt {
  62. my $wanted = shift;
  63. my $bydepth = $wanted->{bydepth};
  64. my $cwd = $bydepth ? Cwd::fastcwd() : Cwd::cwd();
  65. # Localize these rather than lexicalizing them for backwards
  66. # compatibility.
  67. local($topdir,$topdev,$topino,$topmode,$topnlink);
  68. foreach $topdir (@_) {
  69. (($topdev,$topino,$topmode,$topnlink) =
  70. ($Is_VMS ? stat($topdir) : lstat($topdir)))
  71. || (warn("Can't stat $topdir: $!\n"), next);
  72. if (-d _) {
  73. if (chdir($topdir)) {
  74. $prune = 0;
  75. unless ($bydepth) {
  76. ($dir,$_) = ($topdir,'.');
  77. $name = $topdir;
  78. $wanted->{wanted}->();
  79. }
  80. next if $prune;
  81. my $fixtopdir = $topdir;
  82. $fixtopdir =~ s,/$,, ;
  83. $fixtopdir =~ s/\.dir$// if $Is_VMS;
  84. &finddir($wanted,$fixtopdir,$topnlink, $bydepth);
  85. if ($bydepth) {
  86. ($dir,$_) = ($fixtopdir,'.');
  87. $name = $fixtopdir;
  88. $wanted->{wanted}->();
  89. }
  90. }
  91. else {
  92. warn "Can't cd to $topdir: $!\n";
  93. }
  94. }
  95. else {
  96. require File::Basename;
  97. unless (($_,$dir) = File::Basename::fileparse($topdir)) {
  98. ($dir,$_) = ('.', $topdir);
  99. }
  100. if (chdir($dir)) {
  101. $name = $topdir;
  102. $wanted->{wanted}->();
  103. }
  104. else {
  105. warn "Can't cd to $dir: $!\n";
  106. }
  107. }
  108. chdir $cwd;
  109. }
  110. }
  111. sub finddir {
  112. my($wanted, $nlink, $bydepth);
  113. local($dir, $name);
  114. ($wanted, $dir, $nlink, $bydepth) = @_;
  115. my($dev, $ino, $mode, $subcount);
  116. # Get the list of files in the current directory.
  117. opendir(DIR,'.') || (warn("Can't open $dir: $!\n"), $bydepth || return);
  118. my(@filenames) = readdir(DIR);
  119. closedir(DIR);
  120. if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
  121. for (@filenames) {
  122. next if $_ eq '.';
  123. next if $_ eq '..';
  124. $name = "$dir/$_";
  125. $nlink = 0;
  126. $wanted->{wanted}->();
  127. }
  128. }
  129. else { # This dir has subdirectories.
  130. $subcount = $nlink - 2;
  131. for (@filenames) {
  132. next if $_ eq '.';
  133. next if $_ eq '..';
  134. $nlink = 0;
  135. $prune = 0 unless $bydepth;
  136. $name = "$dir/$_";
  137. $wanted->{wanted}->() unless $bydepth;
  138. if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
  139. # Get link count and check for directoriness.
  140. ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
  141. # unless ($nlink || $dont_use_nlink);
  142. if (-d _) {
  143. # It really is a directory, so do it recursively.
  144. --$subcount;
  145. next if $prune;
  146. if (chdir $_) {
  147. $name =~ s/\.dir$// if $Is_VMS;
  148. &finddir($wanted,$name,$nlink, $bydepth);
  149. chdir '..';
  150. }
  151. else {
  152. warn "Can't cd to $_: $!\n";
  153. }
  154. }
  155. }
  156. $wanted->{wanted}->() if $bydepth;
  157. }
  158. }
  159. }
  160. sub wrap_wanted {
  161. my $wanted = shift;
  162. defined &$wanted ? {wanted => $wanted} : $wanted;
  163. }
  164. sub find {
  165. my $wanted = shift;
  166. find_opt(wrap_wanted($wanted), @_);
  167. }
  168. sub finddepth {
  169. my $wanted = wrap_wanted(shift);
  170. $wanted->{bydepth} = 1;
  171. find_opt($wanted, @_);
  172. }
  173. # These are hard-coded for now, but may move to hint files.
  174. if ($^O eq 'VMS') {
  175. $Is_VMS = 1;
  176. $dont_use_nlink = 1;
  177. }
  178. $dont_use_nlink = 1
  179. if $^O eq 'os2' || $^O eq 'dos' || $^O eq 'amigaos' || $^O eq 'MSWin32';
  180. # Set dont_use_nlink in your hint file if your system's stat doesn't
  181. # report the number of links in a directory as an indication
  182. # of the number of files.
  183. # See, e.g. hints/machten.sh for MachTen 2.2.
  184. unless ($dont_use_nlink) {
  185. require Config;
  186. $dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
  187. }
  188. 1;