blib/lib/SVN/Dump/Reader.pm

filter: Statementstmt 99.0% 107 / 108 Branchbran 95.4% 42 / 44 Conditioncond 90.0% 27 / 30 MC/DCmcdc 75.0% 12 / 16 Subroutinesub 100% 15 / 15 Podpod 100% 5 / 5 totaltotal 95.4% 208 / 218 SCAR 35.6
CC
35
Cov
97%
read_record
18.0
read_property_block
7.0
read_header_block
6.0
CRAP 35.0
↑ Summary blib/lib/SVN/Dump/Record.pm »
1 package SVN::Dump::Reader;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 our @ISA = qw( IO::Handle );
9  
10 # SVN::Dump elements
11 11 ▶use SVN::Dump::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use SVN::Dump::Property;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use SVN::Dump::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use SVN::Dump::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # some useful definitions
17 11 my $NL = "\012";
18  
19 # prepare the digest checkers
20 my @digest = grep {
21 11 eval { require Digest; Digest->new(uc) }
22 } qw( md5 sha1 );
23  
24 # the object is a filehandle
25 sub new {
26 67 ▶ my ($class, $fh, $args) = @_;
Subroutine new: called CC=5 SCAR=16.1
Pod documented
27 67 ▶ croak 'SVN::Dump::Reader parameter is not a filehandle'
Branch: 1 branch
BranchTrueFalseTotal
unless $fh and ref $fh and ref $fh eq "GLOB" 3 63 66
Condition100%6/6
$fh and ref $fh and ref $fh eq "GLOB"
!ll&&!rl&&r
2263
$fh and ref $fh
!ll&&!rl&&r
1165
MC/DC100%3/3
$fh and ref $fh and ref $fh eq "GLOB"
$fh ref $fh ref $fh eq "GLOB"
Truth table100%4/4
A and B and C
A= $fhB= ref $fhC= ref $fh eq "GLOB"
ABCresult
0XX0
10X0
1100
1111
28 if !( $fh && ref $fh && ref($fh) eq 'GLOB' );
29 63 ▶ %{*$fh} = %{ $args || {} };
Branch: 1 branch
BranchTrueFalseTotal
$args or {} 26 37 63
30 63 return bless $fh, $class;
31 }
32  
33 sub read_record {
34 725 ▶ my ($fh) = @_;
Subroutine read_record: called CC=18 SCAR=28.9
Pod documented
35  
36 725 my $record = SVN::Dump::Record->new();
37  
38 # first get the headers
39 725 ▶ my $headers = $fh->read_header_block() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $headers = $fh->read_header_block or return 46 679 725
40 679 $record->set_headers_block( $headers );
41
42 # get the property block
43 $record->set_property_block( $fh->read_property_block() )
44 if (
45 exists $headers->{'Prop-content-length'} and
46 679 ▶ $headers->{'Prop-content-length'}
Branch: 1 branch
BranchTrueFalseTotal
if exists $headers->{"Prop-content-length"} and $headers->{"Prop-content-length"} 459 220 679
Condition66%2/3
exists $headers->{"Prop-content-length"} and $headers->{"Prop-content-length"}
!ll&&!rl&&r
2200459
MC/DC50%1/2
exists $headers->{"Prop-content-length"} and $headers->{"Prop-content-length"}
exists $headers->{"Prop-content-length"} $headers->{"Prop-content-length"}
Truth table66%2/3
A and B
A= exists $headers->{"Prop-content-length"}B= $headers->{"Prop-content-length"}
ABresult
0X0
100
111
47 );
48  
49 # get the text block
50 679 ▶ if ( exists $headers->{'Text-content-length'}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $headers->{"Text-content-length"} and $headers->{"Text-content-length"}) 96 583 679
Condition100%3/3
exists $headers->{"Text-content-length"} and $headers->{"Text-content-length"}
!ll&&!rl&&r
5622196
MC/DC100%2/2
exists $headers->{"Text-content-length"} and $headers->{"Text-content-length"}
exists $headers->{"Text-content-length"} $headers->{"Text-content-length"}
Truth table100%3/3
A and B
A= exists $headers->{"Text-content-length"}B= $headers->{"Text-content-length"}
ABresult
0X0
100
111
51 and $headers->{'Text-content-length'} )
52 {
53 96 my $text = $fh->read_text_block( $headers->{'Text-content-length'} );
54  
55 # verify checksums (but not in delta dumps)
56 96 ▶ if (${*$fh}{check_digest}
Branch: 1 branch
BranchTrueFalseTotal
if (do { *$fh }->{"check_digest"} and !$headers->{"Text-delta"} || $headers->{"Text-delta"} ne "true") 7 89 96
Condition66%4/6
do { *$fh }->{"check_digest"} and !$headers->{"Text-delta"} || $headers->{"Text-delta"} ne "true"
!ll&&!rl&&r
83130
not $headers->{"Text-delta"} or $headers->{"Text-delta"} ne "true"
l!l&&r!l&&!r
706
MC/DC0%0/3
do { *$fh }->{"check_digest"} and !$headers->{"Text-delta"} || $headers->{"Text-delta"} ne "true"
do { *$fh }->{"check_digest"} not $headers->{"Text-delta"} $headers->{"Text-delta"} ne "true"
Truth table50%2/4
A and not(B) or C
A= do { *$fh }->{"check_digest"}B= not $headers->{"Text-delta"}C= $headers->{"Text-delta"} ne "true"
ABCresult
0XX0
1000
11X1
1011
57 && ( !$headers->{'Text-delta'}
58 || $headers->{'Text-delta'} ne 'true' )
59 )
60 {
61 7 for my $algo ( grep { $headers->{"Text-content-$_"} } @digest ) {
62 7 my $digest = $text->digest($algo);
63 croak
64 qq{$algo checksum mismatch: got $digest, expected $headers->{"Text-content-$algo"}}
65 7 ▶ if $headers->{"Text-content-$algo"} ne $digest;
Branch: 1 branch
BranchTrueFalseTotal
if $$headers{"Text-content-$algo"} ne $digest 1 6 7
66 }
67 }
68  
69 95 $record->set_text_block($text);
70 }
71  
72 # some safety checks
73 croak "Inconsistent record size"
74 if ( $headers->{'Prop-content-length'} || 0 )
75 + ( $headers->{'Text-content-length'} || 0 )
76 678 ▶ != ( $headers->{'Content-length'} || 0 );
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->{"Prop-content-length"} || 0) + ($headers->{"Text-content-length"} || 0) != ($headers->{"Content-length"} || 0) 1 677 678
Condition100%6/6
$headers->{'Prop-content-length'} || 0
l!l
458220
$headers->{'Text-content-length'} || 0
l!l
95583
$headers->{'Content-length'} || 0
l!l
466212
MC/DC100%1/1
$headers->{'Prop-content-length'} || 0
$headers->{'Prop-content-length'}
MC/DC100%1/1
$headers->{'Text-content-length'} || 0
$headers->{'Text-content-length'}
MC/DC100%1/1
$headers->{'Content-length'} || 0
$headers->{'Content-length'}
Truth table100%2/2
A
A= $headers->{'Prop-content-length'}
Aresult
11
00
Truth table100%2/2
A
A= $headers->{'Text-content-length'}
Aresult
11
00
Truth table100%2/2
A
A= $headers->{'Content-length'}
Aresult
11
00
77  
78 # if we have a delete record with a 'Node-kind' header
79 # we have to recurse for an included record
80 677 ▶ if ( exists $headers->{'Node-action'}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $headers->{"Node-action"} and $headers->{"Node-action"} eq "delete" and exists $headers->{"Node-kind"}) 1 676 677
Condition100%6/6
exists $headers->{"Node-action"} and $headers->{"Node-action"} eq "delete" and exists $headers->{"Node-kind"}
!ll&&!rl&&r
643331
exists $headers->{"Node-action"} and $headers->{"Node-action"} eq "delete"
!ll&&!rl&&r
32132234
MC/DC100%3/3
exists $headers->{"Node-action"} and $headers->{"Node-action"} eq "delete" and exists $headers->{"Node-kind"}
exists $headers->{"Node-action"} $headers->{"Node-action"} eq "delete" exists $headers->{"Node-kind"}
Truth table100%4/4
A and B and C
A= exists $headers->{"Node-action"}B= $headers->{"Node-action"} eq "delete"C= exists $headers->{"Node-kind"}
ABCresult
0XX0
10X0
1100
1111
81 && $headers->{'Node-action'} eq 'delete'
82 && exists $headers->{'Node-kind'} )
83 {
84 1 my $included = $fh->read_record();
85 1 $record->set_included_record( $included );
86 }
87  
88 # uuid and format record only contain headers
89 677 return $record;
90 }
91  
92 sub read_header_block {
93 731 ▶ my ($fh) = @_;
Subroutine read_header_block: called CC=6 SCAR=17.9
Pod documented
94  
95 731 local $/ = $NL;
96  
97 # skip empty lines
98 731 my $line;
99 731 while(1) {
100 1459 $line = <$fh>;
101 1459 ▶ return if !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 46 1413 1459
102 1413 chop $line;
103 1413 ▶ last unless $line eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $line eq "" 685 728 1413
104 }
105  
106 685 my $headers = SVN::Dump::Headers->new();
107 685 while(1) {
108 2848 my ($key, $value) = split /: /, $line, 2;
109 2848 $headers->{$key} = $value;
110  
111 2848 $line = <$fh>;
112 2848 ▶ croak _eof() if !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 2847 2848
113 2847 chop $line;
114 2847 ▶ last if $line eq ''; # stop on empty line
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 684 2163 2847
115 }
116  
117 684 ▶ croak "Empty line found instead of a header block line $."
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$headers 0 684 684
118 if ! keys %$headers;
119  
120 684 return $headers;
121 }
122  
123 sub read_property_block {
124 466 ▶ my ($fh) = @_;
Subroutine read_property_block: called CC=7 SCAR=19.5
Pod documented
125 466 my $property = SVN::Dump::Property->new();
126  
127 466 local $/ = $NL;
128 466 my @buffer;
129 466 while(1) {
130 1252 my $line = <$fh>;
131 1252 ▶ croak _eof() if !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 2 1250 1252
132 1250 chop $line;
133  
134 # read a key/value pair
135 1250 ▶ if( $line =~ /\AK (\d+)\z/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /\AK (\d+)\z/) { } 784 466 1250
elsif ($line =~ /\AD (\d+)\z/) { } 4 462 466
elsif ($line =~ /\APROPS-END\z/) { } 461 1 462
136 784 my $key = $fh->_read_string( $1 );
137  
138 784 $line = <$fh>;
139 784 ▶ croak _eof() if !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 783 784
140 783 chop $line;
141
142 783 ▶ if( $line =~ /\AV (\d+)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\AV (\d+)\z/) { } 782 1 783
143 782 my $value = $fh->_read_string( $1 );
144  
145 782 $property->set( $key => $value );
146  
147 # FIXME what happens if we see duplicate keys?
148 }
149 else {
150 1 croak "Corrupted property"; # FIXME better error message
151 }
152 }
153 # or a deleted key (only with fs-format-version >= 3)
154 # FIXME shall we fail if fs-format-version < 3?
155 elsif( $line =~ /\AD (\d+)\z/ ) {
156 4 my $key = $fh->_read_string( $1 );
157
158 4 $property->set( $key => undef ); # undef means deleted
159 }
160 # end of properties
161 elsif( $line =~ /\APROPS-END\z/ ) {
162 461 last;
163 }
164 # inconsistent data
165 else {
166 1 croak "Corrupted property"; # FIXME better error message
167 }
168 }
169  
170 461 return $property;
171 }
172  
173 sub read_text_block {
174 97 ▶ my ($fh, $size) = @_;
Subroutine read_text_block: called CC=1 SCAR=0.0
Pod documented
175  
176 97 return SVN::Dump::Text->new( $fh->_read_string( $size ) );
177 }
178  
179 sub _read_string {
180  
181 1667 ▶ my ( $fh, $size ) = @_;
Subroutine _read_string: called CC=3 SCAR=11.1
182  
183 1667 local $/ = $NL;
184  
185 1667 my $text;
186 1667 my $characters_read = read( $fh, $text, $size );
187  
188 1667 ▶ if ( defined($characters_read) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $characters_read) { } 1667 0 1667
189 1667 ▶ if ( $characters_read != $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($characters_read != $size) 1 1666 1667
190 1 croak _eof();
191 };
192 } else {
193 0 croak $!;
194 };
195  
196 1666 <$fh>; # clear trailing newline
197  
198 1666 return $text;
199  
200 };
201  
202 # FIXME make this more explicit
203 5 ▶sub _eof { return "Unexpected EOF line $.", }
Subroutine _eof: called CC=1 SCAR=0.0
204  
205 __END__
↑ Summary blib/lib/SVN/Dump/Record.pm »