Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File::Find follow_fast => 1 loses dangling symlink #7241

Closed
p5pRT opened this issue Apr 18, 2004 · 7 comments
Closed

File::Find follow_fast => 1 loses dangling symlink #7241

p5pRT opened this issue Apr 18, 2004 · 7 comments

Comments

@p5pRT
Copy link

p5pRT commented Apr 18, 2004

Migrated from rt.perl.org#28929 (status was 'resolved')

Searchable as RT28929$

@p5pRT
Copy link
Author

p5pRT commented Apr 18, 2004

From @mjdominus

Created by @mjdominus

  my $DIR = shift || "/tmp/T";
  -e $DIR or mkdir($DIR) or die;
  symlink("$DIR/NO-SUCH-FILE", "$DIR/dangling-symlink");
  die unless -l "$DIR/dangling-symlink";

  use File​::Find;
  my $ok = 0;
  find({wanted => sub { $ok = 1 if /dangling-symlink/ },
  follow_fast => 1,
  }, "/tmp/T");
  print $ok ? "ok\n" : "not ok\n";

When the 'follow_fast' option is supplied, the 'find' function fails
to find the dangling symbolic link "dangling-symlink". When
'follow_fast' is omitted, "dangling-symlink" is found, as it should
be.

Perl Info

Flags:
    category=library
    severity=medium

Site configuration information for perl v5.8.0:

Configured by mjd at Thu Apr 17 11:57:37 EDT 2003.

Summary of my perl5 (revision 5.0 version 8 subversion 0) configuration:
  Platform:
    osname=linux, osvers=2.4.2-2, archname=i586-linux
    uname='linux plover.com 2.4.2-2 #1 sun apr 8 19:37:14 edt 2001 i586 unknown '
    config_args='-des'
    hint=previous, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm',
    optimize='-O2',
    cppflags='-fno-strict-aliasing -I/usr/local/include -I/usr/include/gdbm -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm'
    ccversion='', gccversion='2.96 20000731 (Red Hat Linux 7.1 2.96-81)', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -lndbm -lgdbm -ldl -lm -lc -lcrypt -lutil
    perllibs=-lnsl -ldl -lm -lc -lcrypt -lutil
    libc=/lib/libc-2.2.2.so, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version='2.2.4'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:
    


@INC for perl v5.8.0:
    /usr/local/lib/perl5/5.8.0/i586-linux
    /usr/local/lib/perl5/5.8.0
    /usr/local/lib/perl5/site_perl/5.8.0/i586-linux
    /usr/local/lib/perl5/site_perl/5.8.0
    /usr/local/lib/perl5/site_perl/5.7.3
    /usr/local/lib/perl5/site_perl/5.7.2
    /usr/local/lib/perl5/site_perl/5.6.1
    /usr/local/lib/perl5/site_perl/5.6.0
    /usr/local/lib/perl5/site_perl
    .


Environment for perl v5.8.0:
    HOME=/home/mjd
    LANG=C
    LANGUAGE (unset)
    LD_LIBRARY_PATH=/lib:/usr/lib:/usr/X11R6/lib
    LOGDIR (unset)
    PATH=/home/mjd/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/games:/sbin:/usr/sbin:/usr/local/bin/X11R6:/usr/local/bin/mh:/data/mysql/bin:/usr/local/bin/pbm:/usr/local/bin/ezmlm:/home/mjd/TPI/bin:/usr/local/teTeX/bin:/usr/local/mysql/bin
    PERL_BADLANG (unset)
    SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Nov 12, 2004

From @smpeters

[mjd@​plover.com - Sun Apr 18 10​:44​:44 2004]​:

This is a bug report for perl from mjd@​plover.com,
generated with the help of perlbug 1.34 running under perl v5.8.0.

-----------------------------------------------------------------
[Please enter your report here]

    my $DIR = shift || "/tmp/T";
    \-e $DIR or mkdir\($DIR\) or die;
    symlink\("$DIR/NO\-SUCH\-FILE"\, "$DIR/dangling\-symlink"\);
    die unless \-l "$DIR/dangling\-symlink";

    use File​::Find;
    my $ok = 0;
    find\(\{wanted => sub \{ $ok = 1 if /dangling\-symlink/ \}\,
          follow\_fast => 1\,
         \}\, "/tmp/T"\);
    print $ok ? "ok\\n" : "not ok\\n";

When the 'follow_fast' option is supplied, the 'find' function fails
to find the dangling symbolic link "dangling-symlink". When
'follow_fast' is omitted, "dangling-symlink" is found, as it should
be.

The attached patches fix this problem. From my tests, the problem could
also be replicated when follow => 1 is set. The first patch is to
lib/File/Find.pm. It bumps up the version number, clarifies some
documentation, and provides the fix needed to solve this problem. The
second patch is for lib/File/Find/t/find.t. It repairs some tests and
removes a couple that loose their usefulness since they depended on the
behavior of the broken code.

--- /home/steve/perl-current/lib/File/Find.pm 2004-02-27
10​:48​:42.000000000
-0600
+++ ./lib/File/Find.pm 2004-11-11 20​:00​:42.000000000 -0600
@​@​ -3,7 +3,7 @​@​
use strict;
use warnings;
use warnings​::register;
-our $VERSION = '1.07';
+our $VERSION = '1.08';
require Exporter;
require Cwd;

@​@​ -126,7 +126,8 @​@​
=item *

There is a variable C<$File​::Find​::fullname> which holds the absolute
-pathname of the file with all symbolic links resolved
+pathname of the file with all symbolic links resolved. If the link is
+a dangling symbolic link, then fullname will be set to C<undef>.

=back

@​@​ -1101,7 +1102,21 @​@​
  $new_loc = Follow_SymLink($loc_pref.$FN);

  # ignore if invalid symlink
- next unless defined $new_loc;
+ unless (defined $new_loc) {
+ if ($dangling_symlinks) {
+ if (ref $dangling_symlinks eq 'CODE') {
+ $dangling_symlinks->($FN, $dir_pref);
+ } else {
+ warnings​::warnif "$dir_pref$FN is a dangling
symbolic
link\n";
+ }
+ }
+
+ $fullname = undef;
+ $name = $dir_pref . $FN;
+ $_ = ($no_chdir ? $name : $FN);
+ { $wanted_callback->() };
+ next;
+ }

  if (-d _) {
  push @​Stack,[$new_loc,$updir_loc,$dir_name,$FN,1];

--- /home/steve/perl-current/lib/File/Find/t/find.t 2004-03-31
02​:47​:28.00
0000000 -0600
+++ t/find.t 2004-11-11 20​:08​:26.000000000 -0600
@​@​ -661,6 +661,7 @​@​
  use warnings;

  %Expect_File = (File​::Spec->curdir => 1,
+ file_path('dangling_file_sl') => 1,
  file_path('fa_ord') => 1,
  file_path('fsl') => 1,
  file_path('fb_ord') => 1,
@​@​ -684,7 +685,7 @​@​
  topdir('dangling_dir_sl'), topdir('fa') );

  Check( scalar(keys %Expect_File) == 0 );
- Check( $warn_msg =~ m|dangling_dir_sl is a dangling symbolic
link| );

+ Check( $warn_msg =~ m|dangling_file_sl is a dangling symbolic
link| )
;
  unlink file_path('fa', 'dangling_file_sl'),
  file_path('dangling_dir_sl');

@​@​ -750,10 +751,7 @​@​
  File​::Find​::finddepth( {wanted => \&wanted_File_Dir, follow => 1,
  follow_skip => 1, no_chdir => 1},
  topdir('fa') );
-
- Check( scalar(keys %Expect_File) == 1 );
- # Only the file and its symlink have value 2;<
- Check( (values %Expect_File)[0] == 2);
+ Check( scalar(keys %Expect_File) == 0 );
  unlink file_path('fa', 'fa_ord_sl');

@​@​ -814,13 +812,10 @​@​
  # If we encountered the symlink first, then the entries
corresponding to
  # the real name remain, if the real name first then the symlink
  my @​names = sort keys %Expect_File;
- Check( @​names == 2 );
- # In sorted order the directory name comes first
- Check ($names[1] =~ /^$names[0]/);
+ Check( @​names == 1 );
  # Normalise both to the original name
  s/_sl// foreach @​names;
- Check ($names[0] eq file_path_name('fa', 'faa'));
- Check ($names[1] eq file_path_name('fa', 'faa', 'faa_ord'));
+ Check ($names[0] eq file_path_name('fa', 'faa', 'faa_ord'));
  unlink file_path('fa', 'faa_sl');

}

@p5pRT
Copy link
Author

p5pRT commented Nov 12, 2004

From @smpeters

find_t.patch
--- /home/steve/perl-current/lib/File/Find/t/find.t	2004-03-31 02:47:28.000000000 -0600
+++ t/find.t	2004-11-11 20:08:26.000000000 -0600
@@ -661,6 +661,7 @@
 	use warnings;
 
         %Expect_File = (File::Spec->curdir => 1,
+	                file_path('dangling_file_sl') => 1,
 			file_path('fa_ord') => 1,
                         file_path('fsl') => 1,
                         file_path('fb_ord') => 1,
@@ -684,7 +685,7 @@
                            topdir('dangling_dir_sl'), topdir('fa') );
 
         Check( scalar(keys %Expect_File) == 0 );
-        Check( $warn_msg =~ m|dangling_dir_sl is a dangling symbolic link| );  
+        Check( $warn_msg =~ m|dangling_file_sl is a dangling symbolic link| );  
         unlink file_path('fa', 'dangling_file_sl'),
                          file_path('dangling_dir_sl');
 
@@ -750,10 +751,7 @@
     File::Find::finddepth( {wanted => \&wanted_File_Dir, follow => 1,
                            follow_skip => 1, no_chdir => 1},
                            topdir('fa') );
-
-    Check( scalar(keys %Expect_File) == 1 );
-    # Only the file and its symlink have value 2;<
-    Check( (values %Expect_File)[0] == 2);
+    Check( scalar(keys %Expect_File) == 0 );
     unlink file_path('fa', 'fa_ord_sl');
 
 
@@ -814,13 +812,10 @@
     # If we encountered the symlink first, then the entries corresponding to
     # the real name remain, if the real name first then the symlink
     my @names = sort keys %Expect_File;
-    Check( @names == 2 );
-    # In sorted order the directory name comes first
-    Check ($names[1] =~ /^$names[0]/);
+    Check( @names == 1 );
     # Normalise both to the original name
     s/_sl// foreach @names;
-    Check ($names[0] eq file_path_name('fa', 'faa'));
-    Check ($names[1] eq file_path_name('fa', 'faa', 'faa_ord'));
+    Check ($names[0] eq file_path_name('fa', 'faa', 'faa_ord'));
     unlink file_path('fa', 'faa_sl');
 
 }

@p5pRT
Copy link
Author

p5pRT commented Nov 12, 2004

From @smpeters

File_Find_pm.patch
--- /home/steve/perl-current/lib/File/Find.pm	2004-02-27 10:48:42.000000000 -0600
+++ ./lib/File/Find.pm	2004-11-11 20:00:42.000000000 -0600
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 use warnings::register;
-our $VERSION = '1.07';
+our $VERSION = '1.08';
 require Exporter;
 require Cwd;
 
@@ -126,7 +126,8 @@
 =item *
 
 There is a variable C<$File::Find::fullname> which holds the absolute
-pathname of the file with all symbolic links resolved
+pathname of the file with all symbolic links resolved.  If the link is
+a dangling symbolic link, then fullname will be set to C<undef>.
 
 =back
 
@@ -1101,7 +1102,21 @@
 	    $new_loc = Follow_SymLink($loc_pref.$FN);
 
 	    # ignore if invalid symlink
-	    next unless defined $new_loc;
+            unless (defined $new_loc) {
+	        if ($dangling_symlinks) {
+	            if (ref $dangling_symlinks eq 'CODE') {
+	                $dangling_symlinks->($FN, $dir_pref);
+	            } else {
+	                warnings::warnif "$dir_pref$FN is a dangling symbolic link\n";
+	            }
+	        }
+
+	        $fullname = undef;
+	        $name = $dir_pref . $FN;
+	        $_ = ($no_chdir ? $name : $FN);
+	        { $wanted_callback->() };
+	        next;
+	    }
 
 	    if (-d _) {
 		push @Stack,[$new_loc,$updir_loc,$dir_name,$FN,1];

@p5pRT
Copy link
Author

p5pRT commented Nov 12, 2004

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Nov 17, 2004

From @rgs

Steve Peters via RT wrote​:

The attached patches fix this problem. From my tests, the problem could
also be replicated when follow => 1 is set. The first patch is to
lib/File/Find.pm. It bumps up the version number, clarifies some
documentation, and provides the fix needed to solve this problem. The
second patch is for lib/File/Find/t/find.t. It repairs some tests and
removes a couple that loose their usefulness since they depended on the
behavior of the broken code.

Thanks, applied as #23510.

@p5pRT
Copy link
Author

p5pRT commented Nov 17, 2004

@rgs - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant