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

Re^3: failing to use getdents system call on Linux

by glasswalk3r (Friar)
on Nov 07, 2016 at 18:54 UTC ( [id://1175456]=note: print w/replies, xml ) Need Help??


in reply to Re^2: failing to use getdents system call on Linux
in thread failing to use getdents system call on Linux

Oh yes, infinite loop is a problem here, since I'm getting 1 or zero from $len. Here is the updated code:

#!/usr/bin/env perl use warnings; use strict; use File::Spec; use Getopt::Std; use Fcntl; use constant BUF_SIZE => 1024 * 1024 * 5; require 'syscall.ph'; my %opts; getopts( 'd:', \%opts ); die 'option -d <DIRECTORY> is required' unless ( ( exists( $opts{d} ) ) and ( defined( $opts{d} ) ) ); sysopen( my $fd, $opts{d}, O_RDONLY | O_DIRECTORY ); open( my $out, '>:raw', 'perldata.bin' ) or die $!; while (1) { my $buf = "\0" x BUF_SIZE; my $read = syscall( &SYS_getdents, fileno($fd), $buf, BUF_SIZE ); if ( ( $read == -1 ) and ( $! != 0 ) ) { die "failed to syscal getdents: $!"; } print $out($buf); close($out); last; =pod last if ( $read == 0 ); while ( $read != 0 ) { my ( $ino, $off, $len, $name ) = unpack( "LLSZ*", $buf ); unless ( ( $name eq '.' ) or ( $name eq '..' ) ) { my $path = File::Spec->catfile( $opts{d}, $name ); print $path,"\n"; } substr( $buf, 0, $len ) = ''; $read -= $len; } =cut }

The part changed to Pod is exactly the one that put me in a "infinite" loop (probably not infinite, but long enough to create a very large file, but I killed the process once it reached GB).

As you can see in the code, I used the same buffer size as in the C code... but results are the same:

[me@localhost ~]$ echo 'C output'; head hardlist.txt; echo 'Perl outpu +t'; head perldata.txt C output 00000000 42 ed 09 00 00 00 00 00 01 00 00 00 00 00 00 00 |B........ +.......| 00000010 18 00 2e 00 00 00 00 04 e2 6c 08 00 00 00 00 00 |......... +l......| 00000020 0d 8e 0e 00 00 00 00 00 18 00 2e 2e 00 00 00 04 |......... +.......| 00000030 cb ed 09 00 00 00 00 00 a1 07 1e 00 00 00 00 00 |......... +.......| 00000040 e2 6c 08 00 00 00 00 00 0d 8e 0e 00 00 00 00 00 |.l....... +.......| 00000050 18 00 2e 2e 00 00 00 04 cb ed 09 00 00 00 00 00 |......... +.......| 00000060 a1 07 1e 00 00 00 00 00 20 00 78 61 61 61 61 61 |........ +.xaaaaa| 00000070 61 61 61 66 66 00 00 08 54 f0 09 00 00 00 00 00 |aaaff...T +.......| 00000080 cb ed 09 00 00 00 00 00 a1 07 1e 00 00 00 00 00 |......... +.......| 00000090 20 00 78 61 61 61 61 61 61 61 61 66 66 00 00 08 | .xaaaaaa +aaff...| Perl output 00000000 42 ed 09 00 00 00 00 00 01 00 00 00 00 00 00 00 |B........ +.......| 00000010 18 00 2e 00 00 00 00 04 e2 6c 08 00 00 00 00 00 |......... +l......| 00000020 0d 8e 0e 00 00 00 00 00 18 00 2e 2e 00 00 00 04 |......... +.......| 00000030 cb ed 09 00 00 00 00 00 a1 07 1e 00 00 00 00 00 |......... +.......| 00000040 20 00 78 61 61 61 61 61 61 61 61 66 66 00 00 08 | .xaaaaaa +aaff...| 00000050 54 f0 09 00 00 00 00 00 f3 a6 28 00 00 00 00 00 |T........ +.(.....| 00000060 20 00 78 61 61 61 61 61 61 61 62 64 75 00 00 08 | .xaaaaaa +abdu...| 00000070 f7 f0 09 00 00 00 00 00 c8 13 67 00 00 00 00 00 |......... +.g.....| 00000080 20 00 78 61 61 61 61 61 61 61 62 6a 77 00 00 08 | .xaaaaaa +abjw...| 00000090 c5 ee 09 00 00 00 00 00 6e 5c 6d 00 00 00 00 00 |........n +\m.....|
Alceu Rodrigues de Freitas Junior
---------------------------------
"You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1175456]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-18 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found