1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
|
package File::Find;
use 5.006;
use strict;
use warnings;
use warnings::register;
our $VERSION = '1.07';
require Exporter;
require Cwd;
#
# Modified to ensure sub-directory traversal order is not inverded by stack
# push and pops. That is remains in the same order as in the directory file,
# or user pre-processing (EG:sorted).
#
=head1 NAME
File::Find - Traverse a directory tree.
=head1 SYNOPSIS
use File::Find;
find(\&wanted, @directories_to_search);
sub wanted { ... }
use File::Find;
finddepth(\&wanted, @directories_to_search);
sub wanted { ... }
use File::Find;
find({ wanted => \&process, follow => 1 }, '.');
=head1 DESCRIPTION
These are functions for searching through directory trees doing work
on each file found similar to the Unix I<find> command. File::Find
exports two functions, C<find> and C<finddepth>. They work similarly
but have subtle differences.
=over 4
=item B<find>
find(\&wanted, @directories);
find(\%options, @directories);
C<find()> does a depth-first search over the given C<@directories> in
the order they are given. For each file or directory found, it calls
the C<&wanted> subroutine. (See below for details on how to use the
C<&wanted> function). Additionally, for each directory found, it will
C<chdir()> into that directory and continue the search, invoking the
C<&wanted> function on each file or subdirectory in the directory.
=item B<finddepth>
finddepth(\&wanted, @directories);
finddepth(\%options, @directories);
C<finddepth()> works just like C<find()> except that is invokes the
C<&wanted> function for a directory I<after> invoking it for the
directory's contents. It does a postorder traversal instead of a
preorder traversal, working from the bottom of the directory tree up
where C<find()> works from the top of the tree down.
=back
=head2 %options
The first argument to C<find()> is either a code reference to your
C<&wanted> function, or a hash reference describing the operations
to be performed for each file. The
code reference is described in L<The wanted function> below.
Here are the possible keys for the hash:
=over 3
=item C<wanted>
The value should be a code reference. This code reference is
described in L<The wanted function> below.
=item C<bydepth>
Reports the name of a directory only AFTER all its entries
have been reported. Entry point C<finddepth()> is a shortcut for
specifying C<<{ bydepth => 1 }>> in the first argument of C<find()>.
=item C<preprocess>
The value should be a code reference. This code reference is used to
preprocess the current directory. The name of the currently processed
directory is in C<$File::Find::dir>. Your preprocessing function is
called after C<readdir()>, but before the loop that calls the C<wanted()>
function. It is called with a list of strings (actually file/directory
names) and is expected to return a list of strings. The code can be
used to sort the file/directory names alphabetically, numerically,
or to filter out directory entries based on their name alone. When
I<follow> or I<follow_fast> are in effect, C<preprocess> is a no-op.
=item C<postprocess>
The value should be a code reference. It is invoked just before leaving
the currently processed directory. It is called in void context with no
arguments. The name of the current directory is in C<$File::Find::dir>. This
hook is handy for summarizing a directory, such as calculating its disk
usage. When I<follow> or I<follow_fast> are in effect, C<postprocess> is a
no-op.
=item C<follow>
Causes symbolic links to be followed. Since directory trees with symbolic
links (followed) may contain files more than once and may even have
cycles, a hash has to be built up with an entry for each file.
This might be expensive both in space and time for a large
directory tree. See I<follow_fast> and I<follow_skip> below.
If either I<follow> or I<follow_fast> is in effect:
=over 6
=item *
It is guaranteed that an I<lstat> has been called before the user's
C<wanted()> function is called. This enables fast file checks involving S< _>.
=item *
There is a variable C<$File::Find::fullname> which holds the absolute
pathname of the file with all symbolic links resolved
=back
=item C<follow_fast>
This is similar to I<follow> except that it may report some files more
than once. It does detect cycles, however. Since only symbolic links
have to be hashed, this is much cheaper both in space and time. If
processing a file more than once (by the user's C<wanted()> function)
is worse than just taking time, the option I<follow> should be used.
=item C<follow_skip>
C<follow_skip==1>, which is the default, causes all files which are
neither directories nor symbolic links to be ignored if they are about
to be processed a second time. If a directory or a symbolic link
are about to be processed a second time, File::Find dies.
C<follow_skip==0> causes File::Find to die if any file is about to be
processed a second time.
C<follow_skip==2> causes File::Find to ignore any duplicate files and
directories but to proceed normally otherwise.
=item C<dangling_symlinks>
If true and a code reference, will be called with the symbolic link
name and the directory it lives in as arguments. Otherwise, if true
and warnings are on, warning "symbolic_link_name is a dangling
symbolic link\n" will be issued. If false, the dangling symbolic link
will be silently ignored.
=item C<no_chdir>
Does not C<chdir()> to each directory as it recurses. The C<wanted()>
function will need to be aware of this, of course. In this case,
C<$_> will be the same as C<$File::Find::name>.
=item C<untaint>
If find is used in taint-mode (-T command line switch or if EUID != UID
or if EGID != GID) then internally directory names have to be untainted
before they can be chdir'ed to. Therefore they are checked against a regular
expression I<untaint_pattern>. Note that all names passed to the user's
I<wanted()> function are still tainted. If this option is used while
not in taint-mode, C<untaint> is a no-op.
=item C<untaint_pattern>
See above. This should be set using the C<qr> quoting operator.
The default is set to C<qr|^([-+@\w./]+)$|>.
Note that the parentheses are vital.
=item C<untaint_skip>
If set, a directory which fails the I<untaint_pattern> is skipped,
including all its sub-directories. The default is to 'die' in such a case.
=back
=head2 The wanted function
The C<wanted()> function does whatever verifications you want on
each file and directory. Note that despite its name, the C<wanted()>
function is a generic callback function, and does B<not> tell
File::Find if a file is "wanted" or not. In fact, its return value
is ignored.
The wanted function takes no arguments but rather does its work
through a collection of variables.
=over 4
=item C<$File::Find::dir> is the current directory name,
=item C<$_> is the current filename within that directory
=item C<$File::Find::name> is the complete pathname to the file.
=back
Don't modify these variables.
For example, when examining the file F</some/path/foo.ext> you will have:
$File::Find::dir = /some/path/
$_ = foo.ext
$File::Find::name = /some/path/foo.ext
You are chdir()'d toC<$File::Find::dir> when the function is called,
unless C<no_chdir> was specified. Note that when changing to
directories is in effect the root directory (F</>) is a somewhat
special case inasmuch as the concatenation of C<$File::Find::dir>,
C<'/'> and C<$_> is not literally equal to C<$File::Find::name>. The
table below summarizes all variants:
$File::Find::name $File::Find::dir $_
default / / .
no_chdir=>0 /etc / etc
/etc/x /etc x
no_chdir=>1 / / /
/etc / /etc
/etc/x /etc /etc/x
When <follow> or <follow_fast> are in effect, there is
also a C<$File::Find::fullname>. The function may set
C<$File::Find::prune> to prune the tree unless C<bydepth> was
specified. Unless C<follow> or C<follow_fast> is specified, for
compatibility reasons (find.pl, find2perl) there are in addition the
following globals available: C<$File::Find::topdir>,
C<$File::Find::topdev>, C<$File::Find::topino>,
C<$File::Find::topmode> and C<$File::Find::topnlink>.
This library is useful for the C<find2perl> tool, which when fed,
find2perl / -name .nfs\* -mtime +7 \
-exec rm -f {} \; -o -fstype nfs -prune
produces something like:
sub wanted {
/^\.nfs.*\z/s &&
(($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_)) &&
int(-M _) > 7 &&
unlink($_)
||
($nlink || (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_))) &&
$dev < 0 &&
($File::Find::prune = 1);
}
Notice the C<_> in the above C<int(-M _)>: the C<_> is a magical
filehandle that caches the information from the preceding
C<stat()>, C<lstat()>, or filetest.
Here's another interesting wanted function. It will find all symbolic
links that don't resolve:
sub wanted {
-l && !-e && print "bogus link: $File::Find::name\n";
}
See also the script C<pfind> on CPAN for a nice application of this
module.
=head1 WARNINGS
If you run your program with the C<-w> switch, or if you use the
C<warnings> pragma, File::Find will report warnings for several weird
situations. You can disable these warnings by putting the statement
no warnings 'File::Find';
in the appropriate scope. See L<perllexwarn> for more info about lexical
warnings.
=head1 CAVEAT
=over 2
=item $dont_use_nlink
You can set the variable C<$File::Find::dont_use_nlink> to 1, if you want to
force File::Find to always stat directories. This was used for file systems
that do not have an C<nlink> count matching the number of sub-directories.
Examples are ISO-9660 (CD-ROM), AFS, HPFS (OS/2 file system), FAT (DOS file
system) and a couple of others.
You shouldn't need to set this variable, since File::Find should now detect
such file systems on-the-fly and switch itself to using stat. This works even
for parts of your file system, like a mounted CD-ROM.
If you do set C<$File::Find::dont_use_nlink> to 1, you will notice slow-downs.
=item symlinks
Be aware that the option to follow symbolic links can be dangerous.
Depending on the structure of the directory tree (including symbolic
links to directories) you might traverse a given (physical) directory
more than once (only if C<follow_fast> is in effect).
Furthermore, deleting or changing files in a symbolically linked directory
might cause very unpleasant surprises, since you delete or change files
in an unknown directory.
=back
=head1 NOTES
=over 4
=item *
Mac OS (Classic) users should note a few differences:
=over 4
=item *
The path separator is ':', not '/', and the current directory is denoted
as ':', not '.'. You should be careful about specifying relative pathnames.
While a full path always begins with a volume name, a relative pathname
should always begin with a ':'. If specifying a volume name only, a
trailing ':' is required.
=item *
C<$File::Find::dir> is guaranteed to end with a ':'. If C<$_>
contains the name of a directory, that name may or may not end with a
':'. Likewise, C<$File::Find::name>, which contains the complete
pathname to that directory, and C<$File::Find::fullname>, which holds
the absolute pathname of that directory with all symbolic links resolved,
may or may not end with a ':'.
=item *
The default C<untaint_pattern> (see above) on Mac OS is set to
C<qr|^(.+)$|>. Note that the parentheses are vital.
=item *
The invisible system file "Icon\015" is ignored. While this file may
appear in every directory, there are some more invisible system files
on every volume, which are all located at the volume root level (i.e.
"MacintoshHD:"). These system files are B<not> excluded automatically.
Your filter may use the following code to recognize invisible files or
directories (requires Mac::Files):
use Mac::Files;
# invisible() -- returns 1 if file/directory is invisible,
# 0 if it's visible or undef if an error occurred
sub invisible($) {
my $file = shift;
my ($fileCat, $fileInfo);
my $invisible_flag = 1 << 14;
if ( $fileCat = FSpGetCatInfo($file) ) {
if ($fileInfo = $fileCat->ioFlFndrInfo() ) {
return (($fileInfo->fdFlags & $invisible_flag) && 1);
}
}
return undef;
}
Generally, invisible files are system files, unless an odd application
decides to use invisible files for its own purposes. To distinguish
such files from system files, you have to look at the B<type> and B<creator>
file attributes. The MacPerl built-in functions C<GetFileInfo(FILE)> and
C<SetFileInfo(CREATOR, TYPE, FILES)> offer access to these attributes
(see MacPerl.pm for details).
Files that appear on the desktop actually reside in an (hidden) directory
named "Desktop Folder" on the particular disk volume. Note that, although
all desktop files appear to be on the same "virtual" desktop, each disk
volume actually maintains its own "Desktop Folder" directory.
=back
=back
=head1 BUGS AND CAVEATS
Despite the name of the C<finddepth()> function, both C<find()> and
C<finddepth()> perform a depth-first search of the directory
hierarchy.
=head1 HISTORY
File::Find used to produce incorrect results if called recursively.
During the development of perl 5.8 this bug was fixed.
The first fixed version of File::Find was 1.01.
=cut
our @ISA = qw(Exporter);
our @EXPORT = qw(find finddepth);
use strict;
my $Is_VMS;
my $Is_MacOS;
require File::Basename;
require File::Spec;
# Should ideally be my() not our() but local() currently
# refuses to operate on lexicals
our %SLnkSeen;
our ($wanted_callback, $avoid_nlink, $bydepth, $no_chdir, $follow,
$follow_skip, $full_check, $untaint, $untaint_skip, $untaint_pat,
$pre_process, $post_process, $dangling_symlinks);
sub contract_name {
my ($cdir,$fn) = @_;
return substr($cdir,0,rindex($cdir,'/')) if $fn eq $File::Find::current_dir;
$cdir = substr($cdir,0,rindex($cdir,'/')+1);
$fn =~ s|^\./||;
my $abs_name= $cdir . $fn;
if (substr($fn,0,3) eq '../') {
1 while $abs_name =~ s!/[^/]*/\.\./!/!;
}
return $abs_name;
}
# return the absolute name of a directory or file
sub contract_name_Mac {
my ($cdir,$fn) = @_;
my $abs_name;
if ($fn =~ /^(:+)(.*)$/) { # valid pathname starting with a ':'
my $colon_count = length ($1);
if ($colon_count == 1) {
$abs_name = $cdir . $2;
return $abs_name;
}
else {
# need to move up the tree, but
# only if it's not a volume name
for (my $i=1; $i<$colon_count; $i++) {
unless ($cdir =~ /^[^:]+:$/) { # volume name
$cdir =~ s/[^:]+:$//;
}
else {
return undef;
}
}
$abs_name = $cdir . $2;
return $abs_name;
}
}
else {
# $fn may be a valid path to a directory or file or (dangling)
# symlink, without a leading ':'
if ( (-e $fn) || (-l $fn) ) {
if ($fn =~ /^[^:]+:/) { # a volume name like DataHD:*
return $fn; # $fn is already an absolute path
}
else {
$abs_name = $cdir . $fn;
return $abs_name;
}
}
else { # argh!, $fn is not a valid directory/file
return undef;
}
}
}
sub PathCombine($$) {
my ($Base,$Name) = @_;
my $AbsName;
if ($Is_MacOS) {
# $Name is the resolved symlink (always a full path on MacOS),
# i.e. there's no need to call contract_name_Mac()
$AbsName = $Name;
# (simple) check for recursion
if ( ( $Base =~ /^$AbsName/) && (-d $AbsName) ) { # recursion
return undef;
}
}
else {
if (substr($Name,0,1) eq '/') {
$AbsName= $Name;
}
else {
$AbsName= contract_name($Base,$Name);
}
# (simple) check for recursion
my $newlen= length($AbsName);
if ($newlen <= length($Base)) {
if (($newlen == length($Base) || substr($Base,$newlen,1) eq '/')
&& $AbsName eq substr($Base,0,$newlen))
{
return undef;
}
}
}
return $AbsName;
}
sub Follow_SymLink($) {
my ($AbsName) = @_;
my ($NewName,$DEV, $INO);
($DEV, $INO)= lstat $AbsName;
while (-l _) {
if ($SLnkSeen{$DEV, $INO}++) {
if ($follow_skip < 2) {
die "$AbsName is encountered a second time";
}
else {
return undef;
}
}
$NewName= PathCombine($AbsName, readlink($AbsName));
unless(defined $NewName) {
if ($follow_skip < 2) {
die "$AbsName is a recursive symbolic link";
}
else {
return undef;
}
}
else {
$AbsName= $NewName;
}
($DEV, $INO) = lstat($AbsName);
return undef unless defined $DEV; # dangling symbolic link
}
if ($full_check && defined $DEV && $SLnkSeen{$DEV, $INO}++) {
if ( ($follow_skip < 1) || ((-d _) && ($follow_skip < 2)) ) {
die "$AbsName encountered a second time";
}
else {
return undef;
}
}
return $AbsName;
}
our($dir, $name, $fullname, $prune);
sub _find_dir_symlnk($$$);
sub _find_dir($$$);
# check whether or not a scalar variable is tainted
# (code straight from the Camel, 3rd ed., page 561)
sub is_tainted_pp {
my $arg = shift;
my $nada = substr($arg, 0, 0); # zero-length
local $@;
eval { eval "# $nada" };
return length($@) != 0;
}
sub _find_opt {
my $wanted = shift;
die "invalid top directory" unless defined $_[0];
# This function must local()ize everything because callbacks may
# call find() or finddepth()
local %SLnkSeen;
local ($wanted_callback, $avoid_nlink, $bydepth, $no_chdir, $follow,
$follow_skip, $full_check, $untaint, $untaint_skip, $untaint_pat,
$pre_process, $post_process, $dangling_symlinks);
local($dir, $name, $fullname, $prune);
local *_ = \my $a;
my $cwd = $wanted->{bydepth} ? Cwd::fastcwd() : Cwd::getcwd();
my $cwd_untainted = $cwd;
my $check_t_cwd = 1;
$wanted_callback = $wanted->{wanted};
$bydepth = $wanted->{bydepth};
$pre_process = $wanted->{preprocess};
$post_process = $wanted->{postprocess};
$no_chdir = $wanted->{no_chdir};
$full_check = $wanted->{follow};
$follow = $full_check || $wanted->{follow_fast};
$follow_skip = $wanted->{follow_skip};
$untaint = $wanted->{untaint};
$untaint_pat = $wanted->{untaint_pattern};
$untaint_skip = $wanted->{untaint_skip};
$dangling_symlinks = $wanted->{dangling_symlinks};
# for compatibility reasons (find.pl, find2perl)
local our ($topdir, $topdev, $topino, $topmode, $topnlink);
# a symbolic link to a directory doesn't increase the link count
$avoid_nlink = $follow || $File::Find::dont_use_nlink;
my ($abs_dir, $Is_Dir);
Proc_Top_Item:
foreach my $TOP (@_) {
my $top_item = $TOP;
if ($Is_MacOS) {
($topdev,$topino,$topmode,$topnlink) = $follow ? stat $top_item : lstat $top_item;
$top_item = ":$top_item"
if ( (-d _) && ( $top_item !~ /:/ ) );
}
else {
$top_item =~ s|/\z|| unless $top_item eq '/';
($topdev,$topino,$topmode,$topnlink) = $follow ? stat $top_item : lstat $top_item;
}
$Is_Dir= 0;
if ($follow) {
if ($Is_MacOS) {
$cwd = "$cwd:" unless ($cwd =~ /:$/); # for safety
if ($top_item eq $File::Find::current_dir) {
$abs_dir = $cwd;
}
else {
$abs_dir = contract_name_Mac($cwd, $top_item);
unless (defined $abs_dir) {
warnings::warnif "Can't determine absolute path for $top_item (No such file or directory)\n";
next Proc_Top_Item;
}
}
}
else {
if (substr($top_item,0,1) eq '/') {
$abs_dir = $top_item;
}
elsif ($top_item eq $File::Find::current_dir) {
$abs_dir = $cwd;
}
else { # care about any ../
$abs_dir = contract_name("$cwd/",$top_item);
}
}
$abs_dir= Follow_SymLink($abs_dir);
unless (defined $abs_dir) {
if ($dangling_symlinks) {
if (ref $dangling_symlinks eq 'CODE') {
$dangling_symlinks->($top_item, $cwd);
} else {
warnings::warnif "$top_item is a dangling symbolic link\n";
}
}
next Proc_Top_Item;
}
if (-d _) {
_find_dir_symlnk($wanted, $abs_dir, $top_item);
$Is_Dir= 1;
}
}
else { # no follow
$topdir = $top_item;
unless (defined $topnlink) {
warnings::warnif "Can't stat $top_item: $!\n";
next Proc_Top_Item;
}
if (-d _) {
$top_item =~ s/\.dir\z//i if $Is_VMS;
_find_dir($wanted, $top_item, $topnlink);
$Is_Dir= 1;
}
else {
$abs_dir= $top_item;
}
}
unless ($Is_Dir) {
unless (($_,$dir) = File::Basename::fileparse($abs_dir)) {
if ($Is_MacOS) {
($dir,$_) = (':', $top_item); # $File::Find::dir, $_
}
else {
($dir,$_) = ('./', $top_item);
}
}
$abs_dir = $dir;
if (( $untaint ) && (is_tainted($dir) )) {
( $abs_dir ) = $dir =~ m|$untaint_pat|;
unless (defined $abs_dir) {
if ($untaint_skip == 0) {
die "directory $dir is still tainted";
}
else {
next Proc_Top_Item;
}
}
}
unless ($no_chdir || chdir $abs_dir) {
warnings::warnif "Couldn't chdir $abs_dir: $!\n";
next Proc_Top_Item;
}
$name = $abs_dir . $_; # $File::Find::name
$_ = $name if $no_chdir;
{ $wanted_callback->() }; # protect against wild "next"
}
unless ( $no_chdir ) {
if ( ($check_t_cwd) && (($untaint) && (is_tainted($cwd) )) ) {
( $cwd_untainted ) = $cwd =~ m|$untaint_pat|;
unless (defined $cwd_untainted) {
die "insecure cwd in find(depth)";
}
$check_t_cwd = 0;
}
unless (chdir $cwd_untainted) {
die "Can't cd to $cwd: $!\n";
}
}
}
}
# API:
# $wanted
# $p_dir : "parent directory"
# $nlink : what came back from the stat
# preconditions:
# chdir (if not no_chdir) to dir
sub _find_dir($$$) {
my ($wanted, $p_dir, $nlink) = @_;
my ($CdLvl,$Level) = (0,0);
my @Stack;
my @filenames;
my ($subcount,$sub_nlink);
my $SE= [];
my $dir_name= $p_dir;
my $dir_pref;
my $dir_rel = $File::Find::current_dir;
my $tainted = 0;
my $no_nlink;
if ($Is_MacOS) {
$dir_pref= ($p_dir =~ /:$/) ? $p_dir : "$p_dir:"; # preface
}
else {
$dir_pref= ( $p_dir eq '/' ? '/' : "$p_dir/" );
}
local ($dir, $name, $prune, *DIR);
unless ( $no_chdir || ($p_dir eq $File::Find::current_dir)) {
my $udir = $p_dir;
if (( $untaint ) && (is_tainted($p_dir) )) {
( $udir ) = $p_dir =~ m|$untaint_pat|;
unless (defined $udir) {
if ($untaint_skip == 0) {
die "directory $p_dir is still tainted";
}
else {
return;
}
}
}
unless (chdir ($Is_VMS && $udir !~ /[\/\[<]+/ ? "./$udir" : $udir)) {
warnings::warnif "Can't cd to $udir: $!\n";
return;
}
}
# push the starting directory
push @Stack,[$CdLvl,$p_dir,$dir_rel,-1] if $bydepth;
if ($Is_MacOS) {
$p_dir = $dir_pref; # ensure trailing ':'
}
while (defined $SE) {
unless ($bydepth) {
$dir= $p_dir; # $File::Find::dir
$name= $dir_name; # $File::Find::name
$_= ($no_chdir ? $dir_name : $dir_rel ); # $_
# prune may happen here
$prune= 0;
{ $wanted_callback->() }; # protect against wild "next"
next if $prune;
}
# change to that directory
unless ($no_chdir || ($dir_rel eq $File::Find::current_dir)) {
my $udir= $dir_rel;
if ( ($untaint) && (($tainted) || ($tainted = is_tainted($dir_rel) )) ) {
( $udir ) = $dir_rel =~ m|$untaint_pat|;
unless (defined $udir) {
if ($untaint_skip == 0) {
if ($Is_MacOS) {
die "directory ($p_dir) $dir_rel is still tainted";
}
else {
die "directory (" . ($p_dir ne '/' ? $p_dir : '') . "/) $dir_rel is still tainted";
}
} else { # $untaint_skip == 1
next;
}
}
}
unless (chdir ($Is_VMS && $udir !~ /[\/\[<]+/ ? "./$udir" : $udir)) {
if ($Is_MacOS) {
warnings::warnif "Can't cd to ($p_dir) $udir: $!\n";
}
else {
warnings::warnif "Can't cd to (" .
($p_dir ne '/' ? $p_dir : '') . "/) $udir: $!\n";
}
next;
}
$CdLvl++;
}
if ($Is_MacOS) {
$dir_name = "$dir_name:" unless ($dir_name =~ /:$/);
}
$dir= $dir_name; # $File::Find::dir
# Get the list of files in the current directory.
unless (opendir DIR, ($no_chdir ? $dir_name : $File::Find::current_dir)) {
warnings::warnif "Can't opendir($dir_name): $!\n";
next;
}
@filenames = readdir DIR;
closedir(DIR);
@filenames = $pre_process->(@filenames) if $pre_process;
push @Stack,[$CdLvl,$dir_name,"",-2] if $post_process;
# default: use whatever was specifid
# (if $nlink >= 2, and $avoid_nlink == 0, this will switch back)
$no_nlink = $avoid_nlink;
# if dir has wrong nlink count, force switch to slower stat method
$no_nlink = 1 if ($nlink < 2);
if ($nlink == 2 && !$no_nlink) {
# This dir has no subdirectories.
for my $FN (@filenames) {
next if $FN =~ $File::Find::skip_pattern;
$name = $dir_pref . $FN; # $File::Find::name
$_ = ($no_chdir ? $name : $FN); # $_
{ $wanted_callback->() }; # protect against wild "next"
}
}
else {
# This dir has subdirectories.
$subcount = $nlink - 2;
# HACK: insert directories at this position. so as to preserve
# the user pre-processed ordering of files.
# EG: directory traversal is in user sorted order, not at random.
my $stack_top = @Stack;
for my $FN (@filenames) {
next if $FN =~ $File::Find::skip_pattern;
if ($subcount > 0 || $no_nlink) {
# Seen all the subdirs?
# check for directoriness.
# stat is faster for a file in the current directory
$sub_nlink = (lstat ($no_chdir ? $dir_pref . $FN : $FN))[3];
if (-d _) {
--$subcount;
$FN =~ s/\.dir\z//i if $Is_VMS;
# HACK: replace push to preserve dir traversal order
#push @Stack,[$CdLvl,$dir_name,$FN,$sub_nlink];
splice @Stack, $stack_top, 0,
[$CdLvl,$dir_name,$FN,$sub_nlink];
}
else {
$name = $dir_pref . $FN; # $File::Find::name
$_= ($no_chdir ? $name : $FN); # $_
{ $wanted_callback->() }; # protect against wild "next"
}
}
else {
$name = $dir_pref . $FN; # $File::Find::name
$_= ($no_chdir ? $name : $FN); # $_
{ $wanted_callback->() }; # protect against wild "next"
}
}
}
}
continue {
while ( defined ($SE = pop @Stack) ) {
($Level, $p_dir, $dir_rel, $nlink) = @$SE;
if ($CdLvl > $Level && !$no_chdir) {
my $tmp;
if ($Is_MacOS) {
$tmp = (':' x ($CdLvl-$Level)) . ':';
}
else {
$tmp = join('/',('..') x ($CdLvl-$Level));
}
die "Can't cd to $dir_name" . $tmp
unless chdir ($tmp);
$CdLvl = $Level;
}
if ($Is_MacOS) {
# $pdir always has a trailing ':', except for the starting dir,
# where $dir_rel eq ':'
$dir_name = "$p_dir$dir_rel";
$dir_pref = "$dir_name:";
}
else {
$dir_name = ($p_dir eq '/' ? "/$dir_rel" : "$p_dir/$dir_rel");
$dir_pref = "$dir_name/";
}
if ( $nlink == -2 ) {
$name = $dir = $p_dir; # $File::Find::name / dir
$_ = $File::Find::current_dir;
$post_process->(); # End-of-directory processing
}
elsif ( $nlink < 0 ) { # must be finddepth, report dirname now
$name = $dir_name;
if ($Is_MacOS) {
if ($dir_rel eq ':') { # must be the top dir, where we started
$name =~ s|:$||; # $File::Find::name
$p_dir = "$p_dir:" unless ($p_dir =~ /:$/);
}
$dir = $p_dir; # $File::Find::dir
$_ = ($no_chdir ? $name : $dir_rel); # $_
}
else {
if ( substr($name,-2) eq '/.' ) {
substr($name, length($name) == 2 ? -1 : -2) = '';
}
$dir = $p_dir;
$_ = ($no_chdir ? $dir_name : $dir_rel );
if ( substr($_,-2) eq '/.' ) {
substr($_, length($_) == 2 ? -1 : -2) = '';
}
}
{ $wanted_callback->() }; # protect against wild "next"
}
else {
push @Stack,[$CdLvl,$p_dir,$dir_rel,-1] if $bydepth;
last;
}
}
}
}
# API:
# $wanted
# $dir_loc : absolute location of a dir
# $p_dir : "parent directory"
# preconditions:
# chdir (if not no_chdir) to dir
sub _find_dir_symlnk($$$) {
my ($wanted, $dir_loc, $p_dir) = @_; # $dir_loc is the absolute directory
my @Stack;
my @filenames;
my $new_loc;
my $updir_loc = $dir_loc; # untainted parent directory
my $SE = [];
my $dir_name = $p_dir;
my $dir_pref;
my $loc_pref;
my $dir_rel = $File::Find::current_dir;
my $byd_flag; # flag for pending stack entry if $bydepth
my $tainted = 0;
my $ok = 1;
if ($Is_MacOS) {
$dir_pref = ($p_dir =~ /:$/) ? "$p_dir" : "$p_dir:";
$loc_pref = ($dir_loc =~ /:$/) ? "$dir_loc" : "$dir_loc:";
} else {
$dir_pref = ( $p_dir eq '/' ? '/' : "$p_dir/" );
$loc_pref = ( $dir_loc eq '/' ? '/' : "$dir_loc/" );
}
local ($dir, $name, $fullname, $prune, *DIR);
unless ($no_chdir) {
# untaint the topdir
if (( $untaint ) && (is_tainted($dir_loc) )) {
( $updir_loc ) = $dir_loc =~ m|$untaint_pat|; # parent dir, now untainted
# once untainted, $updir_loc is pushed on the stack (as parent directory);
# hence, we don't need to untaint the parent directory every time we chdir
# to it later
unless (defined $updir_loc) {
if ($untaint_skip == 0) {
die "directory $dir_loc is still tainted";
}
else {
return;
}
}
}
$ok = chdir($updir_loc) unless ($p_dir eq $File::Find::current_dir);
unless ($ok) {
warnings::warnif "Can't cd to $updir_loc: $!\n";
return;
}
}
push @Stack,[$dir_loc,$updir_loc,$p_dir,$dir_rel,-1] if $bydepth;
if ($Is_MacOS) {
$p_dir = $dir_pref; # ensure trailing ':'
}
while (defined $SE) {
unless ($bydepth) {
# change (back) to parent directory (always untainted)
unless ($no_chdir) {
unless (chdir $updir_loc) {
warnings::warnif "Can't cd to $updir_loc: $!\n";
next;
}
}
$dir= $p_dir; # $File::Find::dir
$name= $dir_name; # $File::Find::name
$_= ($no_chdir ? $dir_name : $dir_rel ); # $_
$fullname= $dir_loc; # $File::Find::fullname
# prune may happen here
$prune= 0;
lstat($_); # make sure file tests with '_' work
{ $wanted_callback->() }; # protect against wild "next"
next if $prune;
}
# change to that directory
unless ($no_chdir || ($dir_rel eq $File::Find::current_dir)) {
$updir_loc = $dir_loc;
if ( ($untaint) && (($tainted) || ($tainted = is_tainted($dir_loc) )) ) {
# untaint $dir_loc, what will be pushed on the stack as (untainted) parent dir
( $updir_loc ) = $dir_loc =~ m|$untaint_pat|;
unless (defined $updir_loc) {
if ($untaint_skip == 0) {
die "directory $dir_loc is still tainted";
}
else {
next;
}
}
}
unless (chdir $updir_loc) {
warnings::warnif "Can't cd to $updir_loc: $!\n";
next;
}
}
if ($Is_MacOS) {
$dir_name = "$dir_name:" unless ($dir_name =~ /:$/);
}
$dir = $dir_name; # $File::Find::dir
# Get the list of files in the current directory.
unless (opendir DIR, ($no_chdir ? $dir_loc : $File::Find::current_dir)) {
warnings::warnif "Can't opendir($dir_loc): $!\n";
next;
}
@filenames = readdir DIR;
closedir(DIR);
for my $FN (@filenames) {
next if $FN =~ $File::Find::skip_pattern;
# follow symbolic links / do an lstat
$new_loc = Follow_SymLink($loc_pref.$FN);
# ignore if invalid symlink
next unless defined $new_loc;
if (-d _) {
push @Stack,[$new_loc,$updir_loc,$dir_name,$FN,1];
}
else {
$fullname = $new_loc; # $File::Find::fullname
$name = $dir_pref . $FN; # $File::Find::name
$_ = ($no_chdir ? $name : $FN); # $_
{ $wanted_callback->() }; # protect against wild "next"
}
}
}
continue {
while (defined($SE = pop @Stack)) {
($dir_loc, $updir_loc, $p_dir, $dir_rel, $byd_flag) = @$SE;
if ($Is_MacOS) {
# $p_dir always has a trailing ':', except for the starting dir,
# where $dir_rel eq ':'
$dir_name = "$p_dir$dir_rel";
$dir_pref = "$dir_name:";
$loc_pref = ($dir_loc =~ /:$/) ? $dir_loc : "$dir_loc:";
}
else {
$dir_name = ($p_dir eq '/' ? "/$dir_rel" : "$p_dir/$dir_rel");
$dir_pref = "$dir_name/";
$loc_pref = "$dir_loc/";
}
if ( $byd_flag < 0 ) { # must be finddepth, report dirname now
unless ($no_chdir || ($dir_rel eq $File::Find::current_dir)) {
unless (chdir $updir_loc) { # $updir_loc (parent dir) is always untainted
warnings::warnif "Can't cd to $updir_loc: $!\n";
next;
}
}
$fullname = $dir_loc; # $File::Find::fullname
$name = $dir_name; # $File::Find::name
if ($Is_MacOS) {
if ($dir_rel eq ':') { # must be the top dir, where we started
$name =~ s|:$||; # $File::Find::name
$p_dir = "$p_dir:" unless ($p_dir =~ /:$/);
}
$dir = $p_dir; # $File::Find::dir
$_ = ($no_chdir ? $name : $dir_rel); # $_
}
else {
if ( substr($name,-2) eq '/.' ) {
substr($name, length($name) == 2 ? -1 : -2) = ''; # $File::Find::name
}
$dir = $p_dir; # $File::Find::dir
$_ = ($no_chdir ? $dir_name : $dir_rel); # $_
if ( substr($_,-2) eq '/.' ) {
substr($_, length($_) == 2 ? -1 : -2) = '';
}
}
lstat($_); # make sure file tests with '_' work
{ $wanted_callback->() }; # protect against wild "next"
}
else {
push @Stack,[$dir_loc, $updir_loc, $p_dir, $dir_rel,-1] if $bydepth;
last;
}
}
}
}
sub wrap_wanted {
my $wanted = shift;
if ( ref($wanted) eq 'HASH' ) {
if ( $wanted->{follow} || $wanted->{follow_fast}) {
$wanted->{follow_skip} = 1 unless defined $wanted->{follow_skip};
}
if ( $wanted->{untaint} ) {
$wanted->{untaint_pattern} = $File::Find::untaint_pattern
unless defined $wanted->{untaint_pattern};
$wanted->{untaint_skip} = 0 unless defined $wanted->{untaint_skip};
}
return $wanted;
}
else {
return { wanted => $wanted };
}
}
sub find {
my $wanted = shift;
_find_opt(wrap_wanted($wanted), @_);
}
sub finddepth {
my $wanted = wrap_wanted(shift);
$wanted->{bydepth} = 1;
_find_opt($wanted, @_);
}
# default
$File::Find::skip_pattern = qr/^\.{1,2}\z/;
$File::Find::untaint_pattern = qr|^([-+@\w./]+)$|;
# These are hard-coded for now, but may move to hint files.
if ($^O eq 'VMS') {
$Is_VMS = 1;
$File::Find::dont_use_nlink = 1;
}
elsif ($^O eq 'MacOS') {
$Is_MacOS = 1;
$File::Find::dont_use_nlink = 1;
$File::Find::skip_pattern = qr/^Icon\015\z/;
$File::Find::untaint_pattern = qr|^(.+)$|;
}
# this _should_ work properly on all platforms
# where File::Find can be expected to work
$File::Find::current_dir = File::Spec->curdir || '.';
$File::Find::dont_use_nlink = 1
if $^O eq 'os2' || $^O eq 'dos' || $^O eq 'amigaos' || $^O eq 'MSWin32' ||
$^O eq 'cygwin' || $^O eq 'epoc' || $^O eq 'qnx' ||
$^O eq 'nto';
# Set dont_use_nlink in your hint file if your system's stat doesn't
# report the number of links in a directory as an indication
# of the number of files.
# See, e.g. hints/machten.sh for MachTen 2.2.
unless ($File::Find::dont_use_nlink) {
require Config;
$File::Find::dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
}
# We need a function that checks if a scalar is tainted. Either use the
# Scalar::Util module's tainted() function or our (slower) pure Perl
# fallback is_tainted_pp()
{
local $@;
eval { require Scalar::Util };
*is_tainted = $@ ? \&is_tainted_pp : \&Scalar::Util::tainted;
}
1;
|