Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
There's more than one way to do things
 
PerlMonks  

perlbug: seekdir/readdir broken on win32 on 5.008009, 5.012002, 5.014001

by Anonymous Monk
on Nov 27, 2011 at 08:43 UTC ( #940245=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I just discovered seekdir, and its kinda broken on win32 (not that I'm 100% sure of how its supposed to behave )

5.006001 seems to behave the most as I expect based on my knowledge of seek

I'm using ActivePerl/mingw-perl on WinXP

#!/usr/bin/perl -- use strict; use warnings; use Data::Dumper; use File::Temp qw/ tempdir /; print $], "\n"; my $dir = tempdir; chdir $dir or die $!; touch( qw/ hi.txt there.txt you.txt / ); opendir my($d), $dir or die $!; foreach my $ix (-1 .. 6) { printf "seekdir(%3d) = %d => %s\n", $ix, seekdir($d, $ix), DD( [ readdir $d ] ); } print '#' x 33,"\n"; sub DD { scalar Data::Dumper->new([@_])->Indent(0)->Useqq(1)->Dump ; } sub touch { for my $file ( @_ ){ open my($fh), '>', $file or die $!; close $fh; } } __END__
5.014001 seekdir( -1) = 1 => $VAR1 = []; seekdir( 0) = 1 => $VAR1 = [".","..","hi.txt","there.txt","you.txt"] +; seekdir( 1) = 1 => $VAR1 = ["","..","hi.txt","there.txt","you.txt"]; seekdir( 2) = 1 => $VAR1 = ["..","hi.txt","there.txt","you.txt"]; seekdir( 3) = 1 => $VAR1 = [".","hi.txt","there.txt","you.txt"]; seekdir( 4) = 1 => $VAR1 = ["","hi.txt","there.txt","you.txt"]; seekdir( 5) = 1 => $VAR1 = ["hi.txt","there.txt","you.txt"]; seekdir( 6) = 1 => $VAR1 = ["i.txt","there.txt","you.txt"]; ################################# 5.012002 seekdir( -1) = 1 => $VAR1 = ["",".","..","hi.txt","there.txt","you.tx +t"]; seekdir( 0) = 1 => $VAR1 = [".","..","hi.txt","there.txt","you.txt"] +; seekdir( 1) = 1 => $VAR1 = ["","..","hi.txt","there.txt","you.txt"]; seekdir( 2) = 1 => $VAR1 = ["..","hi.txt","there.txt","you.txt"]; seekdir( 3) = 1 => $VAR1 = [".","hi.txt","there.txt","you.txt"]; seekdir( 4) = 1 => $VAR1 = ["","hi.txt","there.txt","you.txt"]; seekdir( 5) = 1 => $VAR1 = ["hi.txt","there.txt","you.txt"]; seekdir( 6) = 1 => $VAR1 = ["i.txt","there.txt","you.txt"]; ################################# 5.008009 seekdir( -1) = 1 => $VAR1 = ["",".","..","hi.txt","there.txt","you.tx +t"]; seekdir( 0) = 1 => $VAR1 = [".","..","hi.txt","there.txt","you.txt"] +; seekdir( 1) = 1 => $VAR1 = ["","..","hi.txt","there.txt","you.txt"]; seekdir( 2) = 1 => $VAR1 = ["..","hi.txt","there.txt","you.txt"]; seekdir( 3) = 1 => $VAR1 = [".","hi.txt","there.txt","you.txt"]; seekdir( 4) = 1 => $VAR1 = ["","hi.txt","there.txt","you.txt"]; seekdir( 5) = 1 => $VAR1 = ["hi.txt","there.txt","you.txt"]; seekdir( 6) = 1 => $VAR1 = ["i.txt","there.txt","you.txt"]; ################################# 5.006001 seekdir( -1) = 1 => $VAR1 = [".","..","hi.txt","there.txt","you.txt"] +; seekdir( 0) = 1 => $VAR1 = [".","..","hi.txt","there.txt","you.txt"] +; seekdir( 1) = 1 => $VAR1 = ["..","hi.txt","there.txt","you.txt"]; seekdir( 2) = 1 => $VAR1 = ["hi.txt","there.txt","you.txt"]; seekdir( 3) = 1 => $VAR1 = ["there.txt","you.txt"]; seekdir( 4) = 1 => $VAR1 = ["you.txt"]; seekdir( 5) = 1 => $VAR1 = []; seekdir( 6) = 1 => $VAR1 = []; #################################

Comment on perlbug: seekdir/readdir broken on win32 on 5.008009, 5.012002, 5.014001
Select or Download Code
Re: perlbug: seekdir/readdir broken on win32 on 5.008009, 5.012002, 5.014001
by ikegami (Pope) on Nov 27, 2011 at 09:17 UTC

    You did not demonstrate a bug since you're clearly misusing seekdir according to the very documentation to which you linked:

    seekdir DIRHANDLE,POS sets the current position for the readdir routine on DIRHANDLE. POS must be a value returned by telldir.

      Thanks, I should have known better (I even thought, sure, I'm the first to discover this bug since 1987)

      It appears seekdir/telldir returns length + 1

      Happy holidays

        I see now where I was confused, I was thinking in terms of readline, IO::Dir/FIRSTKEY/NEXTKEY, and I confused seek for being line-oriented

        All this, and I'm not tired or under the influence, go figure :)

Re: perlbug: seekdir/readdir broken on win32 on 5.008009, 5.012002, 5.014001
by Anonymous Monk on Nov 27, 2011 at 10:00 UTC

    So after reading Re: perlbug: seekdir/readdir broken on win32 on 5.008009, 5.012002, 5.014001 I looked up some of these system library seekdir docs

    http://www.delorie.com/gnu/docs/dirent/directory.3.html
    Telldir and seekdir are unreliable when used in conjunction with file systems that perform directory compaction or expansion or when the directory stream has been closed and reopened. It is best to avoid using telldir and seekdir altogether.

    http://www.vnode.ch/fixing_seekdir# When seekdir() Won't Seek to the Right Position | vnode.ch

    (and sorry that it took us almost twenty-five years to fix it)

    Yes, I think I will avoid seekdir/telldir as much as possible

      Caveats about a POSIX system call aren't very relevant on a Windows system. As far as I know, Windows doesn't even have an equivalent to seekdir. If I remember correct, Perl emulates seekdir on Windows by caching previously seen entries.
        I am curious about this function (seekdir).
        It just sounds like a "solution in search of a problem"?

        I do most of my programming on Windows, but some on *nix. But even so, how many directory entries would one need in order for this type of a function to be "worthwhile" in terms of performance or memory? 1K? 10K? 50K? My mind boggles at directories of such sizes.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://940245]
Approved by Eliya
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2014-04-18 13:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (468 votes), past polls