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