blib/lib/bun-x.pl untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 60 CC 5 SCAR 29.7
CC
5
Cov
0%
extract
30.0
unnetstrings
2.0
BEGIN
2.0
CRAP 19.5
1 #! /usr/bin/perl
2  
3 use CDB_File;
4 use strict;
5  
6 sub unnetstrings {
7 my ($netstrings) = @_;
8 my @result;
9 while ( $netstrings =~ s/^([0-9]+):// ) {
10 push @result, substr( $netstrings, 0, $1, '' );
11 $netstrings =~ s/^,//;
12 }
13 return @result;
14 }
15  
16 my $chunk = 8192;
17  
18 sub extract {
19 my ( $file, $t, $b ) = @_;
20 my $head = $$b{"H$file"};
21 my ( $code, $type ) = $head =~ m/^([0-9]+)(.)/;
22 if ( $type eq "/" ) {
23 mkdir $file, 0777;
24 }
25 elsif ( $type eq "_" ) {
26 my ( $total, $now, $got, $x );
27 open OUT, ">$file" or die "open for output: $!\n";
28 exists $$b{"D$code"} or die "corrupt bun file\n";
29 my $fh = $t->handle;
30 sysseek $fh, $t->datapos, 0;
31 $total = $t->datalen;
32 while ($total) {
33 $now = ( $total > $chunk ) ? $chunk : $total;
34 $got = sysread $fh, $x, $now;
35 if ( not $got ) { die "read error\n"; }
36 $total -= $got;
37 print OUT $x;
38 }
39 close OUT;
40 }
41 else {
42 print STDERR "warning: skipping unknown file type\n";
43 }
44 }
45  
46 die "usage\n" if @ARGV != 1;
47  
48 my ( %b, $t );
49 $t = tie %b, 'CDB_File', $ARGV[0] or die "tie: $!\n";
50 map { extract $_, $t, \%b } unnetstrings $b{""};