lib/Net/BitTorrent/Storage.pm

filter: Statementstmt 80.5% 203 / 252 Branchbran 55.8% 57 / 102 Conditioncond 39.3% 13 / 33 MC/DCmcdc 18.1% 4 / 22 Subroutinesub 87.5% 35 / 40 Podpod 95.8% 23 / 24 totaltotal 70.8% 335 / 473 CC 81 SCAR 22.5
CC
81
Cov
71%
_evict_one
56.0
map_abs_offset
14.7
map_v1_piece
13.3
CRAP 9.5
« lib/Net/BitTorrent/Protocol/BEP10.pm ↑ Summary lib/Net/BitTorrent/Protocol/BEP11.pm »
1 27 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 27 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 27 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use Net::BitTorrent::Emitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 class Net::BitTorrent::Storage v2.0.1 : isa(Net::BitTorrent::Emitter) {
6 27 ▶ use Net::BitTorrent::Storage::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 27 ▶ use Digest::Merkle::SHA256; # Standalone spin-off
Subroutine BEGIN: called CC=1 SCAR=0.0
8 27 ▶ use Path::Tiny qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 27 ▶ use Digest::SHA qw[sha1];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 27 ▶ use builtin qw[refaddr];
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 field $base_path : param : reader;
12 27 field $file_tree : param : reader = undef;
13 27 field $piece_size : param : reader = 0;
14 27 field $pieces_v1 : param : reader = undef;
15 27 field %files; # pieces_root => File object
16 27 field @files_ordered; # For v1 mapping
17 0 ▶ method files_ordered () { \@files_ordered }
Subroutine files_ordered: not called CC=1 SCAR=6.9
Pod undocumented
18 27 field %piece_layers; # pieces_root => piece layer data
19  
20 # Async Disk Cache (LRU)
21 27 field %cache; # file_id => { offset => data }
22 27 field %cache_dirty; # file_id => { offset => 1 }
23 27 field @lru_list; # [[file_id, offset], ...]
24 27 field $max_cache_size = 1024 * 1024 * 16; # 16MiB cache limit
25 27 field $current_cache_size = 0;
26 75 ▶ ADJUST {
Subroutine __ANON__: called CC=2 SCAR=6.9
27 75 $base_path = Path::Tiny::path($base_path);
28 75 ▶ if ($file_tree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_tree) 68 7 75
29 68 $self->load_file_tree($file_tree);
30 }
31 }
32  
33 86 ▶ method add_file ( $rel_path, $size, $pieces_root = undef ) {
Subroutine add_file: called CC=3 SCAR=11.0
Pod documented
34 86 my $file = Net::BitTorrent::Storage::File->new(
35 path => $base_path->child($rel_path),
36 size => $size,
37 pieces_root => $pieces_root,
38 piece_size => $piece_size,
39 );
40 86 push @files_ordered, $file;
41 86 ▶ $files{$pieces_root} = $file if $pieces_root;
Branch: 1 branch
BranchTrueFalseTotal
if $pieces_root 64 22 86
42 86 return $file;
43 }
44  
45 69 ▶ method load_file_tree ($tree) {
Subroutine load_file_tree: called CC=1 SCAR=0.0
Pod documented
46 69 $self->_parse_file_tree( $tree, [] );
47 }
48  
49 72 ▶ method _parse_file_tree ( $tree, $path_stack ) {
Subroutine _parse_file_tree: called CC=3 SCAR=11.0
50 72 for my $name ( sort keys %$tree ) {
51 83 my $node = $tree->{$name};
52 83 ▶ if ( exists $node->{''} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{''}) { } 80 3 83
53 80 my $file_info = $node->{''};
54 80 $self->add_file( Path::Tiny::path( @$path_stack, $name ), $file_info->{length}, $file_info->{'pieces root'} );
55 }
56 else {
57 3 $self->_parse_file_tree( $node, [ @$path_stack, $name ] );
58 }
59 }
60 }
61  
62 3 ▶ method get_file_by_root ($root) {
Subroutine get_file_by_root: called CC=1 SCAR=0.0
Pod documented
63 3 return $files{$root};
64 }
65  
66 9 ▶ method set_piece_layer ( $root, $layer_data ) {
Subroutine set_piece_layer: called CC=1 SCAR=0.0
Pod documented
67 9 $piece_layers{$root} = $layer_data;
68 }
69  
70 0 ▶ method get_hashes ( $root, $base_layer, $index, $length ) {
Subroutine get_hashes: not called CC=2 SCAR=17.9
Pod documented
71 0 ▶ my $file = $files{$root} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $files{$root} or return undef 0 0 0
72 0 return $file->merkle->get_hashes( $base_layer, $index, $length );
73 }
74  
75 4 ▶ method verify_block ( $root, $index, $data ) {
Subroutine verify_block: called CC=2 SCAR=8.4
Pod documented
76 4 my $file = $files{$root};
77 4 ▶ if ( !$file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 4 4
78 0 $self->_emit_log( 'fatal', 'Unknown file root' );
79 0 return;
80 }
81 4 return $file->verify_block( $index, $data );
82 }
83  
84 4 ▶ method verify_block_audit ( $root, $index, $data, $audit_path ) {
Subroutine verify_block_audit: called CC=2 SCAR=8.4
Pod documented
85 4 my $file = $files{$root};
86 4 ▶ if ( !$file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 4 4
87 0 $self->_emit_log( 'fatal', 'Unknown file root' );
88 0 return;
89 }
90 4 return $file->verify_block_audit( $index, $data, $audit_path );
91 }
92  
93 4 ▶ method verify_piece_v2 ( $root, $index, $data ) {
Subroutine verify_piece_v2: called CC=3 SCAR=15.0
Pod documented
94 4 my $file = $files{$root};
95 4 ▶ if ( !$file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 4 4
96 0 $self->_emit_log( 'fatal', 'Unknown file root' );
97 0 return;
98 }
99 4 ▶ my $layer = $piece_layers{$root} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $layer = $piece_layers{$root} or return undef 0 4 4
100 0 my $expected = substr( $layer, $index * 32, 32 );
101 0 return $file->verify_piece_v2( $index, $data, $expected );
102 }
103  
104 # Writes a block to the cache
105 2 ▶ method write_block ( $root, $offset, $data ) {
Subroutine write_block: called CC=2 SCAR=8.4
Pod documented
106 2 my $file = $files{$root};
107 2 ▶ if ( !$file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 2 2
108 0 $self->_emit_log( 'fatal', 'Unknown file root' );
109 0 return;
110 }
111 2 $self->_write_to_cache( $file, $offset, $data );
112 }
113  
114 # Reads a block, checking cache first
115 1 ▶ method read_block ( $root, $offset, $length ) {
Subroutine read_block: called CC=2 SCAR=8.4
Pod documented
116 1 my $file = $files{$root};
117 1 ▶ if ( !$file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 1 1
118 0 $self->_emit_log( 'fatal', 'Unknown file root' );
119 0 return;
120 }
121 1 return $self->_read_from_cache( $file, $offset, $length );
122 }
123  
124 15 ▶ method read_global ( $offset, $length ) {
Subroutine read_global: called CC=2 SCAR=6.9
Pod documented
125 15 my $segments = $self->map_abs_offset( undef, $offset, $length );
126 15 my $full_data = '';
127 15 for my $seg (@$segments) {
128 15 $full_data .= $self->_read_from_cache( $seg->{file}, $seg->{offset}, $seg->{length} );
129 }
130 15 return $full_data;
131 }
132  
133 0 ▶ method write_global ( $offset, $data ) {
Subroutine write_global: not called CC=2 SCAR=17.9
Pod documented
134 0 my $segments = $self->map_abs_offset( undef, $offset, length($data) );
135 0 my $data_offset = 0;
136 0 for my $seg (@$segments) {
137 0 $self->_write_to_cache( $seg->{file}, $seg->{offset}, substr( $data, $data_offset, $seg->{length} ) );
138 0 $data_offset += $seg->{length};
139 }
140 }
141  
142 8 ▶ method _write_to_cache ( $file, $offset, $data ) {
Subroutine _write_to_cache: called CC=4 SCAR=16.3
143 8 my $id = refaddr($file);
144 8 $self->_emit_log( 'debug', "Adding " . length($data) . " bytes to cache for file $id at offset $offset (dirty)" );
145 8 ▶ if ( exists $cache{$id}{$offset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cache{$id}{$offset}) { } 0 8 8
146 0 $current_cache_size -= length( $cache{$id}{$offset} );
147 0 $self->_lru_bump( $id, $offset );
148 }
149 else {
150 8 push @lru_list, [ $id, $offset ];
151 }
152 8 $cache{$id}{$offset} = $data;
153 8 $cache_dirty{$id}{$offset} = 1;
154 8 $current_cache_size += length($data);
155 8 ▶ while ( $current_cache_size > $max_cache_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_cache_size > $max_cache_size) 0 8 8
156 0 ▶ last unless $self->_evict_one();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_evict_one 0 0 0
157 }
158 }
159  
160 16 ▶ method _read_from_cache ( $file, $offset, $length ) {
Subroutine _read_from_cache: called CC=9 SCAR=24.0
161 16 my $id = refaddr($file);
162 16 ▶ if ( exists $cache{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cache{$id}) 13 3 16
163 13 for my $cached_offset ( keys %{ $cache{$id} } ) {
164 13 my $cached_data = $cache{$id}{$cached_offset};
165 13 my $cached_len = length($cached_data);
166 13 ▶ if ( $offset >= $cached_offset && ( $offset + $length ) <= ( $cached_offset + $cached_len ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset >= $cached_offset and $offset + $length <= $cached_offset + $cached_len) 13 0 13
Condition33%1/3
$offset >= $cached_offset and $offset + $length <= $cached_offset + $cached_len
!ll&&!rl&&r
0013
MC/DC0%0/2
$offset >= $cached_offset and $offset + $length <= $cached_offset + $cached_len
$offset >= $cached_offset $offset + $length <= $cached_offset + $cached_len
Truth table33%1/3
A and B
A= $offset >= $cached_offsetB= $offset + $length <= $cached_offset + $cached_len
ABresult
0X0
100
111
167 13 $self->_lru_bump( $id, $cached_offset );
168 13 return substr( $cached_data, $offset - $cached_offset, $length );
169 }
170 }
171 }
172  
173 # Cache miss - read from disk and add to clean cache
174 3 my $data = $file->read( $offset, $length );
175 3 ▶ if ( defined $data && length($data) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data and length $data > 0) 3 0 3
Condition33%1/3
defined $data and length $data > 0
!ll&&!rl&&r
003
MC/DC0%0/2
defined $data and length $data > 0
defined $data length $data > 0
Truth table33%1/3
A and B
A= defined $dataB= length $data > 0
ABresult
0X0
100
111
176 3 $self->_emit_log( 'debug', "Cache miss for file $id at offset $offset, caching read" );
177 3 push @lru_list, [ $id, $offset ];
178 3 $cache{$id}{$offset} = $data;
179 3 $current_cache_size += length($data);
180 3 ▶ while ( $current_cache_size > $max_cache_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_cache_size > $max_cache_size) 0 3 3
181 0 ▶ last unless $self->_evict_one();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_evict_one 0 0 0
182 }
183 }
184 3 return $data;
185 }
186  
187 13 ▶ method _lru_bump ( $id, $offset ) {
Subroutine _lru_bump: called CC=4 SCAR=15.2
188  
189 # Move [id, offset] to end of @lru_list
190 13 for my $i ( 0 .. $#lru_list ) {
191 13 ▶ if ( $lru_list[$i][0] == $id && $lru_list[$i][1] == $offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lru_list[$i][0] == $id and $lru_list[$i][1] == $offset) 13 0 13
Condition33%1/3
$lru_list[$i][0] == $id and $lru_list[$i][1] == $offset
!ll&&!rl&&r
0013
MC/DC0%0/2
$lru_list[$i][0] == $id and $lru_list[$i][1] == $offset
$lru_list[$i][0] == $id $lru_list[$i][1] == $offset
Truth table33%1/3
A and B
A= $lru_list[$i][0] == $idB= $lru_list[$i][1] == $offset
ABresult
0X0
100
111
192 13 push @lru_list, splice( @lru_list, $i, 1 );
193 13 last;
194 }
195 }
196 }
197  
198 0 ▶ method _evict_one () {
Subroutine _evict_one: not called CC=7 SCAR=40.3
199 0 ▶ return 0 unless @lru_list;
Branch: 1 branch
BranchTrueFalseTotal
unless @lru_list 0 0 0
200 0 my $entry = shift @lru_list;
201 0 my ( $id, $offset ) = @$entry;
202 0 ▶ if ( $cache_dirty{$id} && $cache_dirty{$id}{$offset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_dirty{$id} and $cache_dirty{$id}{$offset}) 0 0 0
Condition0%0/3
$cache_dirty{$id} and $cache_dirty{$id}{$offset}
!ll&&!rl&&r
000
MC/DC0%0/2
$cache_dirty{$id} and $cache_dirty{$id}{$offset}
$cache_dirty{$id} $cache_dirty{$id}{$offset}
Truth table0%0/3
A and B
A= $cache_dirty{$id}B= $cache_dirty{$id}{$offset}
ABresult
0X0
100
111
203  
204 # Must flush before evicting
205 0 $self->_flush_one( $id, $offset );
206 }
207 0 ▶ if ( exists $cache{$id} && exists $cache{$id}{$offset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cache{$id} and exists $cache{$id}{$offset}) 0 0 0
Condition0%0/3
exists $cache{$id} and exists $cache{$id}{$offset}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $cache{$id} and exists $cache{$id}{$offset}
exists $cache{$id} exists $cache{$id}{$offset}
Truth table0%0/3
A and B
A= exists $cache{$id}B= exists $cache{$id}{$offset}
ABresult
0X0
100
111
208 0 $current_cache_size -= length( delete $cache{$id}{$offset} );
209 0 ▶ delete $cache{$id} unless keys %{ $cache{$id} }; # Clean up empty file entry
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$cache{$id};} 0 0 0
210 }
211 0 return 1;
212 }
213  
214 5 ▶ method _flush_one ( $id, $offset ) {
Subroutine _flush_one: called CC=8 SCAR=22.3
215 5 my $file;
216  
217 # Find file object by refaddr - inefficient but safe without reverse mapping
218 # In a real system we'd store file objects in a registry.
219 # Actually, let's look in %files and @files_ordered.
220 5 for my $f (@files_ordered) {
221 5 ▶ if ( refaddr($f) == $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($f) == $id) 5 0 5
222 5 $file = $f;
223 5 last;
224 }
225 }
226 5 ▶ return unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 5 5
227 5 ▶ if ( exists $cache{$id}{$offset} && delete $cache_dirty{$id}{$offset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cache{$id}{$offset} and delete $cache_dirty{$id}{$offset}) 5 0 5
Condition33%1/3
exists $cache{$id}{$offset} and delete $cache_dirty{$id}{$offset}
!ll&&!rl&&r
005
MC/DC0%0/2
exists $cache{$id}{$offset} and delete $cache_dirty{$id}{$offset}
exists $cache{$id}{$offset} delete $cache_dirty{$id}{$offset}
Truth table33%1/3
A and B
A= exists $cache{$id}{$offset}B= delete $cache_dirty{$id}{$offset}
ABresult
0X0
100
111
228 5 my $data = $cache{$id}{$offset};
229 5 $file->write( $offset, $data );
230 5 $current_cache_size -= length( delete $cache{$id}{$offset} ); # Remove from cache after flush
231 5 ▶ delete $cache{$id} unless keys %{ $cache{$id} }; # Clean up empty file entry
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$cache{$id};} 5 0 5
232 5 ▶ delete $cache_dirty{$id} unless keys %{ $cache_dirty{$id} }; # Clean up empty dirty entry
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$cache_dirty{$id};} 5 0 5
233 5 return 1;
234 }
235 0 return 0;
236 }
237  
238 43 ▶ method flush ( $count = undef ) {
Subroutine flush: called CC=6 SCAR=18.5
Pod documented
239 43 my $flushed = 0;
240 43 for my $id ( keys %cache_dirty ) {
241 5 for my $offset ( keys %{ $cache_dirty{$id} } ) {
242 5 $self->_flush_one( $id, $offset );
243 5 $flushed++;
244 5 ▶ return $flushed if defined $count && $flushed >= $count;
Branch: 1 branch
BranchTrueFalseTotal
if defined $count and $flushed >= $count 0 5 5
Condition33%1/3
defined $count and $flushed >= $count
!ll&&!rl&&r
050
MC/DC0%0/2
defined $count and $flushed >= $count
defined $count $flushed >= $count
Truth table33%1/3
A and B
A= defined $countB= $flushed >= $count
ABresult
0X0
100
111
245 }
246 }
247 43 return $flushed;
248 }
249  
250 3 ▶ method explicit_flush () {
Subroutine explicit_flush: called CC=1 SCAR=0.0
Pod documented
251 3 $self->flush();
252 }
253  
254 40 ▶ method tick ( $delta = 0.1 ) {
Subroutine tick: called CC=2 SCAR=6.9
Pod documented
255  
256 # Throttled flush: flush up to 16 items per tick
257 40 $self->flush(16);
258 }
259  
260 15 ▶ method map_abs_offset ( $root, $offset, $length ) {
Subroutine map_abs_offset: called CC=9 SCAR=26.8
Pod documented
261 15 my @segments;
262 15 ▶ if ( defined $root ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $root) { } 0 15 15
263 0 my $file = $files{$root};
264 0 ▶ if ( !$file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 0 0
265 0 $self->_emit_log( 'fatal', 'Unknown file root' );
266 0 return [];
267 }
268 0 push @segments, { file => $file, offset => $offset, length => $length };
269 }
270 else {
271 15 my $current_file_start = 0;
272 15 my $end = $offset + $length;
273 15 for my $file (@files_ordered) {
274 15 my $file_size = $file->size;
275 15 my $current_file_end = $current_file_start + $file_size;
276 15 ▶ if ( $offset < $current_file_end && $end > $current_file_start ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $current_file_end and $end > $current_file_start) 15 0 15
Condition33%1/3
$offset < $current_file_end and $end > $current_file_start
!ll&&!rl&&r
0015
MC/DC0%0/2
$offset < $current_file_end and $end > $current_file_start
$offset < $current_file_end $end > $current_file_start
Truth table33%1/3
A and B
A= $offset < $current_file_endB= $end > $current_file_start
ABresult
0X0
100
111
277 15 ▶ my $overlap_start = $offset > $current_file_start ? $offset : $current_file_start;
Branch: 1 branch
BranchTrueFalseTotal
$offset > $current_file_start ? : 0 15 15
278 15 ▶ my $overlap_end = $end < $current_file_end ? $end : $current_file_end;
Branch: 1 branch
BranchTrueFalseTotal
$end < $current_file_end ? : 0 15 15
279 15 push @segments, { file => $file, offset => $overlap_start - $current_file_start, length => $overlap_end - $overlap_start, };
280 }
281 15 $current_file_start = $current_file_end;
282 15 ▶ last if $current_file_start >= $end;
Branch: 1 branch
BranchTrueFalseTotal
if $current_file_start >= $end 15 0 15
283 }
284 }
285 15 return \@segments;
286 }
287  
288 13 ▶ method map_v1_piece ($index) {
Subroutine map_v1_piece: called CC=13 SCAR=25.9
Pod documented
289 13 ▶ if ( !$piece_size ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($piece_size) 0 13 13
290 0 $self->_emit_log( 'fatal', 'piece_size not set' );
291 0 return [];
292 }
293 13 my $piece_start = $index * $piece_size;
294 13 my $piece_end = $piece_start + $piece_size;
295 13 my @segments;
296 13 my $current_v1_offset = 0;
297 13 for my $file (@files_ordered) {
298 14 my $file_size = $file->size;
299 14 my $padded_size = $file_size;
300 14 ▶ if ( $file->merkle && ( $file_size % $piece_size != 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->merkle and $file_size % $piece_size != 0) 5 9 14
Condition100%3/3
$file->merkle and $file_size % $piece_size != 0
!ll&&!rl&&r
545
MC/DC100%2/2
$file->merkle and $file_size % $piece_size != 0
$file->merkle $file_size % $piece_size != 0
Truth table100%3/3
A and B
A= $file->merkleB= $file_size % $piece_size != 0
ABresult
0X0
100
111
301 5 $padded_size += ( $piece_size - ( $file_size % $piece_size ) );
302 }
303 14 my $current_v1_end = $current_v1_offset + $padded_size;
304 14 ▶ if ( $piece_start < $current_v1_end && $piece_end > $current_v1_offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($piece_start < $current_v1_end and $piece_end > $current_v1_offset) 10 4 14
Condition66%2/3
$piece_start < $current_v1_end and $piece_end > $current_v1_offset
!ll&&!rl&&r
4010
MC/DC50%1/2
$piece_start < $current_v1_end and $piece_end > $current_v1_offset
$piece_start < $current_v1_end $piece_end > $current_v1_offset
Truth table66%2/3
A and B
A= $piece_start < $current_v1_endB= $piece_end > $current_v1_offset
ABresult
0X0
100
111
305 10 ▶ my $overlap_start = $piece_start > $current_v1_offset ? $piece_start : $current_v1_offset;
Branch: 1 branch
BranchTrueFalseTotal
$piece_start > $current_v1_offset ? : 1 9 10
306 10 ▶ my $overlap_end = $piece_end < $current_v1_end ? $piece_end : $current_v1_end;
Branch: 1 branch
BranchTrueFalseTotal
$piece_end < $current_v1_end ? : 1 9 10
307 10 my $file_offset = $overlap_start - $current_v1_offset;
308 10 my $length = $overlap_end - $overlap_start;
309 10 ▶ if ( $file_offset < $file_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_offset < $file_size) 10 0 10
310 10 ▶ my $actual_len = ( $file_offset + $length > $file_size ) ? ( $file_size - $file_offset ) : $length;
Branch: 1 branch
BranchTrueFalseTotal
$file_offset + $length > $file_size ? : 3 7 10
311 10 ▶ push @segments, { file => $file, offset => $file_offset, length => $actual_len, } if $actual_len > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $actual_len > 0 10 0 10
312 }
313 }
314 14 $current_v1_offset = $current_v1_end;
315 14 ▶ last if $current_v1_offset >= $piece_end;
Branch: 1 branch
BranchTrueFalseTotal
if $current_v1_offset >= $piece_end 9 5 14
316 }
317 13 return \@segments;
318 }
319  
320 9 ▶ method write_piece_v1 ( $index, $data ) {
Subroutine write_piece_v1: called CC=2 SCAR=6.9
Pod documented
321 9 my $segments = $self->map_v1_piece($index);
322 9 $self->_emit_log( 'debug', "write_piece_v1: Piece $index mapped to " . scalar(@$segments) . " segments" );
323 9 my $data_offset = 0;
324 9 for my $seg (@$segments) {
325 6 $self->_write_to_cache( $seg->{file}, $seg->{offset}, substr( $data, $data_offset, $seg->{length} ) );
326 6 $data_offset += $seg->{length};
327 }
328 }
329  
330 0 ▶ method read_piece_v1 ($index) {
Subroutine read_piece_v1: not called CC=2 SCAR=17.9
Pod documented
331 0 my $segments = $self->map_v1_piece($index);
332 0 my $full_data = '';
333 0 for my $seg (@$segments) {
334 0 $full_data .= $self->_read_from_cache( $seg->{file}, $seg->{offset}, $seg->{length} );
335 }
336 0 return $full_data;
337 }
338  
339 50 ▶ method verify_piece_v1 ( $index, $data ) {
Subroutine verify_piece_v1: called CC=3 SCAR=11.0
Pod documented
340 50 ▶ return undef unless $pieces_v1;
Branch: 1 branch
BranchTrueFalseTotal
unless $pieces_v1 0 50 50
341 50 my $offset = $index * 20;
342 50 ▶ return undef if $offset + 20 > length($pieces_v1);
Branch: 1 branch
BranchTrueFalseTotal
if $offset + 20 > length $pieces_v1 40 10 50
343 10 my $expected = substr( $pieces_v1, $offset, 20 );
344 10 return sha1($data) eq $expected;
345 }
346  
347 73 ▶ method map_v2_piece ($index) {
Subroutine map_v2_piece: called CC=7 SCAR=19.7
Pod documented
348 73 ▶ if ( !$piece_size ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($piece_size) 0 73 73
349 0 $self->_emit_log( 'fatal', 'piece_size not set' );
350 0 return ( undef, undef );
351 }
352 73 my $offset = 0;
353 73 for my $file (@files_ordered) {
354 73 my $file_size = $file->size;
355 73 my $padded_size = $file_size;
356 73 ▶ if ( $file_size % $piece_size != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_size % $piece_size != 0) 2 71 73
357 2 $padded_size += ( $piece_size - ( $file_size % $piece_size ) );
358 }
359 73 my $num_pieces = int( $padded_size / $piece_size );
360 73 my $rel_index = $index - ( $offset / $piece_size );
361 73 ▶ if ( $rel_index >= 0 && $rel_index < $num_pieces ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_index >= 0 and $rel_index < $num_pieces) 10 63 73
Condition66%2/3
$rel_index >= 0 and $rel_index < $num_pieces
!ll&&!rl&&r
06310
MC/DC50%1/2
$rel_index >= 0 and $rel_index < $num_pieces
$rel_index >= 0 $rel_index < $num_pieces
Truth table66%2/3
A and B
A= $rel_index >= 0B= $rel_index < $num_pieces
ABresult
0X0
100
111
362 10 ▶ return ( $file->pieces_root, $rel_index ) if $file->pieces_root;
Branch: 1 branch
BranchTrueFalseTotal
if $file->pieces_root 6 4 10
363 4 return ( undef, undef );
364 }
365 63 $offset += $padded_size;
366 }
367 63 return ( undef, undef );
368 }
369  
370 1 ▶ method dump_state () {
Subroutine dump_state: called CC=2 SCAR=6.9
Pod documented
371 1 my %file_states;
372 1 for my $file (@files_ordered) {
373 1 my $rel = $file->path->relative($base_path)->stringify;
374 1 $file_states{$rel} = $file->dump_state();
375 }
376 1 return \%file_states;
377 }
378  
379 1 ▶ method load_state ($state) {
Subroutine load_state: called CC=3 SCAR=11.1
Pod documented
380 1 for my $file (@files_ordered) {
381 1 my $rel = $file->path->relative($base_path)->stringify;
382 1 ▶ if ( exists $state->{$rel} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{$rel}) 1 0 1
383 1 $file->load_state( $state->{$rel} );
384 }
385 }
386 }
387 27 } 1;
« lib/Net/BitTorrent/Protocol/BEP10.pm ↑ Summary lib/Net/BitTorrent/Protocol/BEP11.pm »