Paranoid/IO/FileMultiplexer/Block/FileHeader.pm

filter: Statementstmt 84.3% 242 / 287 Branchbran 65.3% 51 / 78 Conditioncond 61.9% 13 / 21 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 92.3% 36 / 39 Podpod 100% 15 / 15 totaltotal 79.6% 361 / 453 CC 49 SCAR 20.9
CC
49
Cov
79%
writeStreams
42.0
readSig
11.6
readStreams
11.2
CRAP 8.1
« Paranoid/Network/IPv4.pm ↑ Summary Paranoid/IO/FileMultiplexer/Block.pm »
1 # Paranoid::IO::FileMultiplexer::Block::FileHeader -- File Header Block
2 #
3 # $Id: lib/Paranoid/IO/FileMultiplexer/Block/FileHeader.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $
4 #
5 # This software is free software. Similar to Perl, you can redistribute it
6 # and/or modify it under the terms of either:
7 #
8 # a) the GNU General Public License
9 # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the
10 # Free Software Foundation <http://www.fsf.org/>; either version 1
11 # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
12 # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
13 # b) the Artistic License 2.0
14 # <https://opensource.org/licenses/Artistic-2.0>,
15 #
16 # subject to the following additional term: No trademark rights to
17 # "Paranoid" have been or are conveyed under any of the above licenses.
18 # However, "Paranoid" may be used fairly to describe this unmodified
19 # software, in good faith, but not as a trademark.
20 #
21 # (c) 2005 - 2021, Arthur Corliss (corliss@digitalmages.com)
22 # (tm) 2008 - 2021, Paranoid Inc. (www.paranoid.com)
23 #
24 #####################################################################
25  
26 #####################################################################
27 #
28 # Environment definitions
29 #
30 #####################################################################
31  
32 package Paranoid::IO::FileMultiplexer::Block::FileHeader;
33  
34 12 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 12 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 12 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 12 ▶use Paranoid qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 12 ▶use Paranoid::IO qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 12 ▶use Paranoid::Debug qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 12 ▶use Paranoid::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 12 ▶use Fcntl qw(:DEFAULT :flock :mode :seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 12 ▶use Paranoid::IO::FileMultiplexer::Block;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 12 ▶use Paranoid::IO::FileMultiplexer::Block::StreamHeader;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 12 ▶use Paranoid::IO::FileMultiplexer::Block::BATHeader;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 12 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
50  
51 12 ▶use base qw(Paranoid::IO::FileMultiplexer::Block);
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 12 ▶use constant PIOFMVER => '1.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 # Signature format:
56 # PIOFM VER BS BC
57 # Z6 Z4 NNx NNx
58 # 28 bytes
59 #
60 # Stream record format:
61 # String BN
62 # Z21 NNx
63 # 30 bytes
64 12 ▶use constant SIGNATURE => 'Z6Z4NNxNNx';
Subroutine BEGIN: called CC=1 SCAR=0.0
65 12 ▶use constant SIG_LEN => 28;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 12 ▶use constant SIG_TYPE => 'PIOFM';
Subroutine BEGIN: called CC=1 SCAR=0.0
67 12 ▶use constant BLOCKS_POS => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 12 ▶use constant BLOCKC_POS => 19;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 12 ▶use constant STREAMS_POS => 28;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 12 ▶use constant STRMIDX => 'Z21NNx';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 12 ▶use constant STRM_LEN => 30;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 #####################################################################
74 #
75 # Module code follows
76 #
77 #####################################################################
78  
79 339 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
80  
81 # Purpose: Creates a new file header object
82 # Returns: Object reference/undef
83 # Usage: $obj =
84 # Paranoid::IO::FileMultiplexer::Block::FileHeader->new($file, $blockSize);
85  
86 339 my $class = shift;
87 339 my $file = shift;
88 339 my $bsize = shift;
89 339 my $self;
90  
91 339 subPreamble( PDLEVEL3, '$$', $file, $bsize );
92  
93 339 $self = __PACKAGE__->SUPER::new( $file, 0, $bsize );
94  
95 339 ▶ if ( defined $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self) 240 99 339
96 240 $$self{version} = PIOFMVER;
97 240 $$self{blocks} = 1;
98 240 $$self{streamidx} = {}; # name => idx of rec in streams
99 240 $$self{streams} = []; # array of [ name, blockNum ]
100 240 $$self{maxStreams} = int( ( $bsize - SIG_LEN ) / STRM_LEN );
101 }
102  
103 339 subPostamble( PDLEVEL3, '$', $self );
104  
105 339 return $self;
106 }
107  
108 279 ▶sub blocks {
Subroutine blocks: called CC=1 SCAR=0.0
Pod documented
109  
110 # Purpose: Returns the number of blocks recorded in the signature
111 # Returns: Integer
112 # Usage: $count = $obj->blocks;
113  
114 279 my $self = shift;
115 279 return $$self{blocks};
116 }
117  
118 0 ▶sub version {
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
119  
120 # Purpose: Returns the version of the file format
121 # Returns: String
122 # Usage: $ver = $obj->version;
123  
124 0 my $self = shift;
125 0 return $$self{version};
126 }
127  
128 100 ▶sub streams {
Subroutine streams: called CC=2 SCAR=6.9
Pod documented
129  
130 # Purpose: Returns a hash of stream names => blockNums
131 # Returns: Hash
132 # Usage: %streams = $obj->streams;
133  
134 100 my $self = shift;
135 100 my @streams = @{ $$self{streams} };
136 100 my ( %rv, $stream );
137  
138 100 foreach $stream (@streams) {
139 167 $rv{ $$stream[0] } = $$stream[1];
140 }
141  
142 100 return %rv;
143 }
144  
145 0 ▶sub maxStreams {
Subroutine maxStreams: not called CC=1 SCAR=6.9
Pod documented
146  
147 # Purpose: Returns the maximum number of streams supported by this file
148 # Returns: Integer
149 # Usage: $max = $obj->maxStreams;
150  
151 0 my $self = shift;
152  
153 0 return $$self{maxStreams};
154 }
155  
156 225 ▶sub _transHuman {
Subroutine _transHuman: called CC=8 SCAR=20.8
157  
158 # Purpose: Translates raw integers into human-readable values
159 # Returns: String
160 # Usage: $rv = _transHuman($n);
161  
162 225 my $n = shift;
163 225 my $u = 'B';
164  
165 225 ▶ while ( $n > 1024 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 1024) 730 150 880
166 730 ▶ $u =
Branch: 5 branches
BranchTrueFalseTotal
$u eq 'B' ? : 225 505 730
$u eq 'KB' ? : 150 355 505
$u eq 'MB' ? : 130 225 355
$u eq 'GB' ? : 75 150 225
$u eq 'TB' ? : 75 75 150
167 $u eq 'B' ? 'KB'
168 : $u eq 'KB' ? 'MB'
169 : $u eq 'MB' ? 'GB'
170 : $u eq 'GB' ? 'TB'
171 : $u eq 'TB' ? 'PB'
172 : 'EX';
173 730 $n /= 1024;
174 730 ▶ last if $u eq 'EX';
Branch: 1 branch
BranchTrueFalseTotal
if $u eq "EX" 75 655 730
175 }
176 225 $n = sprintf( '%0.2f', $n );
177  
178 225 return "$n$u";
179 }
180  
181 75 ▶sub model {
Subroutine model: called CC=3 SCAR=11.0
Pod documented
182  
183 # Purpose: Returns a hash of file statistics
184 # Returns: Hash
185 # Usage: $stats = $obj->model;
186  
187 75 my $self = shift;
188 75 my $bs = $$self{blockSize};
189 75 my $blks = $$self{blocks};
190 75 my $strms = scalar keys %{ $$self{streamidx} };
191 75 my ( $block, $maxBATs, $maxData, %rv );
192  
193 # Get reference max values
194 $block =
195 Paranoid::IO::FileMultiplexer::Block::StreamHeader->new( $$self{file},
196 75 1, $bs, 'foo' );
197 75 $maxBATs = $block->maxBATs;
198 $block =
199 75 Paranoid::IO::FileMultiplexer::Block::BATHeader->new( $$self{file}, 1,
200 $bs, 'foo', 0 );
201 75 $maxData = $block->maxData;
202  
203 # Current stats
204 75 $rv{intSize} = ( 1 << 32 ) == 1 ? 32 : 64;
205 75 $rv{curFileSize} = $bs * $blks;
206 75 $rv{curFSHuman} = _transHuman( $rv{curFileSize} );
207 75 $rv{curStreams} = $strms;
208  
209 # Predicted max
210 75 $rv{maxFileSize} = 0b11111111_11111111_11111111_11111111;
211 $rv{maxFileSize} = $rv{maxFileSize} | ( $rv{maxFileSize} << 32 )
212 75 ▶ if $rv{intSize} == 64;
Branch: 1 branch
BranchTrueFalseTotal
if $rv{"intSize"} == 64 75 0 75
213 75 $rv{maxStreams} = $$self{maxStreams};
214 75 $rv{maxStreamSize} = $bs * $maxBATs * $maxData;
215 $rv{maxStreamSize} = $rv{maxFileSize}
216 75 ▶ if $rv{maxStreamSize} > $rv{maxFileSize};
Branch: 1 branch
BranchTrueFalseTotal
if $rv{"maxStreamSize"} > $rv{"maxFileSize"} 0 75 75
217 75 $rv{maxSSHuman} = _transHuman( $rv{maxStreamSize} );
218  
219 # Provide human-readable values
220 75 $rv{maxFSHuman} = _transHuman( $rv{maxFileSize} );
221  
222 75 return %rv;
223 }
224  
225 99 ▶sub writeSig {
Subroutine writeSig: called CC=1 SCAR=0.0
Pod documented
226  
227 # Purpose: Writes the file signature to the file
228 # Returns: Boolean
229 # Usage: $rv = $obj->writeSig;
230  
231 99 my $self = shift;
232 99 my $file = $$self{file};
233 99 my $ver = $$self{version};
234 99 my $rv = 0;
235 my $sig = pack SIGNATURE, SIG_TYPE, PIOFMVER,
236 quad2Longs( $$self{blockSize} ),
237 99 quad2Longs( $$self{blocks} );
238  
239 99 pdebug( 'entering', PDLEVEL3 );
240 99 pIn();
241  
242 99 $rv = $self->bwrite($sig);
243  
244 99 pOut();
245 99 pdebug( 'leaving w/rv: %s', PDLEVEL3, $rv );
246  
247 99 return $rv;
248 }
249  
250 130 ▶sub readSig {
Subroutine readSig: called CC=10 SCAR=24.5
Pod documented
251  
252 # Purpose: Reads the block signature from the file
253 # Returns: Boolean
254 # Usage: $rv = $obj->readSig;
255  
256 130 my $self = shift;
257 130 my $file = $$self{file};
258 130 my $rv = 0;
259 130 my ( $raw, $type, $ver, $bs, $bc, $tblock );
260 130 my ( $lbs, $ubs, $lbc, $ubc );
261  
262 130 subPreamble(PDLEVEL3);
263  
264 130 ▶ if ( pflock( $file, LOCK_SH ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 1)) 130 0 130
265 130 ▶ if ( $self->bread( \$raw, 0, SIG_LEN ) == SIG_LEN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bread(\$raw, 0, 28) == 28) { } 130 0 130
266 130 $rv = 1;
267  
268 # Unpack the signature
269 130 ( $type, $ver, $lbs, $ubs, $lbc, $ubc ) = unpack SIGNATURE, $raw;
270  
271 # Validate contents
272 #
273 # Start with file type
274 130 ▶ unless ( $type eq SIG_TYPE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type eq "PIOFM") 0 130 130
275 0 $rv = 0;
276 0 pdebug( 'Invalid file header type (%s)', PDLEVEL1, $type );
277 }
278  
279 # format version
280 130 ▶ unless ( $ver eq PIOFMVER ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ver eq "1.0") 0 130 130
281 0 $rv = 0;
282 0 pdebug( 'Invalid file header version (%s)', PDLEVEL1, $ver );
283 }
284  
285 # Make sure block size is legitimate
286 130 $bs = longs2Quad( $lbs, $ubs );
287 130 ▶ if ( defined $bs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bs) { } 130 0 130
288 130 $tblock = __PACKAGE__->new( $file, $bs );
289 130 ▶ unless ( defined $tblock ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tblock) 22 108 130
290 22 $rv = 0;
291 22 pdebug( 'blockSize error in file header: %s',
292 PDLEVEL1, $bs );
293 }
294 } else {
295 0 pdebug(
296 'this platform does not support 64b values for block size',
297 PDLEVEL1
298 );
299 0 $rv = 0;
300 }
301  
302 # Validate end of file matches block count
303 130 $bc = longs2Quad( $lbc, $ubc );
304 130 ▶ if ( defined $bc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bc) { } 130 0 130
305 130 pseek( $file, 0, SEEK_END );
306 130 ▶ unless ( ptell($file) == $bc * $bs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ptell($file) == $bc * $bs) 44 86 130
307 44 $rv = 0;
308 44 pdebug(
309 'incorrect file size based on block count (%s * %s = %s)',
310 PDLEVEL1, $bc, $bs, $bc * $bs
311 );
312 }
313 } else {
314 0 pdebug(
315 'this platform does not support 64b values for block count',
316 PDLEVEL1
317 );
318 0 $rv = 0;
319 }
320  
321 # Update internal values
322 130 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 86 44 130
323 86 $$self{version} = $ver;
324 86 $$self{blockSize} = $bs;
325 86 $$self{blocks} = $bc;
326 86 $self->recalibrate;
327 } else {
328 44 pdebug( 'file signature verification failure', PDLEVEL1 );
329 }
330  
331 } else {
332 0 pdebug( 'failed to read file header signature', PDLEVEL1 );
333 }
334  
335 130 pflock( $file, LOCK_UN );
336 }
337  
338 130 subPostamble( PDLEVEL3, '$', $rv );
339  
340 130 return $rv;
341 }
342  
343 257 ▶sub writeBlocks {
Subroutine writeBlocks: called CC=4 SCAR=14.6
Pod documented
344  
345 # Purpose: Updates the blocks counter and writes it to disk
346 # Returns: Boolean
347 # Usage: $rv = $obj->writeBlocks($count);
348  
349 257 my $self = shift;
350 257 my $bcount = shift;
351 257 my ( $raw, $rv );
352  
353 257 subPreamble( PDLEVEL3, '$', $bcount );
354  
355 257 ▶ if ( defined $bcount and $bcount > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bcount and $bcount > 0) { } 257 0 257
Condition33%1/3
defined $bcount and $bcount > 0
!ll&&!rl&&r
00257
MC/DC0%0/2
defined $bcount and $bcount > 0
defined $bcount $bcount > 0
Truth table33%1/3
A and B
A= defined $bcountB= $bcount > 0
ABresult
0X0
100
111
356 257 $raw = pack 'NN', quad2Longs($bcount);
357 257 ▶ if ( $self->bwrite( $raw, BLOCKC_POS ) == 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bwrite($raw, 19) == 8) 257 0 257
358 257 $$self{blocks} = $bcount;
359 257 $rv = 1;
360 }
361 } else {
362 0 pdebug( 'invalid value for blocks (%s)', PDLEVEL1, $bcount );
363 }
364  
365 257 subPostamble( PDLEVEL3, '$', $rv );
366  
367 257 return $rv;
368 }
369  
370 3003 ▶sub readBlocks {
Subroutine readBlocks: called CC=4 SCAR=14.6
Pod documented
371  
372 # Purpose: Reads the blocks counter from disk
373 # Returns: Integer/undef on error
374 # Usage: $count = $obj->readBlocks;
375  
376 3003 my $self = shift;
377 3003 my ( $rv, $raw );
378  
379 3003 subPreamble(PDLEVEL3);
380  
381 3003 ▶ if ( $self->bread( \$raw, BLOCKC_POS, 8 ) == 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bread(\$raw, 19, 8) == 8) 3003 0 3003
382 3003 $rv = longs2Quad( unpack 'NN', $raw );
383 3003 ▶ $rv = PTRUE_ZERO if defined $rv and $rv == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rv and $rv == 0 0 3003 3003
Condition33%1/3
defined $rv and $rv == 0
!ll&&!rl&&r
030030
MC/DC0%0/2
defined $rv and $rv == 0
defined $rv $rv == 0
Truth table33%1/3
A and B
A= defined $rvB= $rv == 0
ABresult
0X0
100
111
384 }
385  
386 3003 subPostamble( PDLEVEL3, '$', $rv );
387  
388 3003 return $rv;
389 }
390  
391 191 ▶sub incrBlocks {
Subroutine incrBlocks: called CC=1 SCAR=0.0
Pod documented
392  
393 # Purpose: Increments the block count and writes the field to disk
394 # Returns: Boolean
395 # Usage: $rv = $obj->incrBlocks;
396  
397 191 my $self = shift;
398  
399 191 return $self->writeBlocks( $$self{blocks} + 1 );
400 }
401  
402 2992 ▶sub validateBlocks {
Subroutine validateBlocks: called CC=2 SCAR=6.9
Pod documented
403  
404 # Purpose: Compares in-memory block counter to what's stored in the file
405 # Returns: Boolean
406 # Usage: $rv = $obj->validateBlocks;
407  
408 2992 my $self = shift;
409 2992 my $rv = 0;
410  
411 2992 subPreamble(PDLEVEL3);
412  
413 2992 ▶ $rv = 1 if $$self{blocks} == $self->readBlocks;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"blocks"} == $self->readBlocks 2972 20 2992
414  
415 2992 subPostamble( PDLEVEL3, '$', $rv );
416  
417 2992 return $rv;
418 }
419  
420 0 ▶sub writeStreams {
Subroutine writeStreams: not called CC=6 SCAR=37.4
Pod documented
421  
422 # Purpose: Writes all the stream index records to the file
423 # Returns: Boolean
424 # Usage: $rv = $obj->writeStreams;
425  
426 0 my $self = shift;
427 0 my $file = $$self{file};
428 0 my $rv = 0;
429 0 my ( $rec, $i, $pos );
430  
431 0 subPreamble(PDLEVEL3);
432  
433 # Hold an exclusive lock for the entire transaction
434 0 ▶ if ( pflock( $file, LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 2)) 0 0 0
435 0 $rv = 1;
436 0 $i = 0;
437 0 foreach $rec ( @{ $$self{streams} } ) {
438 0 @$rec = ( $$rec[0], quad2Longs( $$rec[1] ) );
439 0 $pos = STREAMS_POS + $i * STRM_LEN;
440 0 ▶ $rv = 0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bwrite(pack("Z21NNx", @$rec), $pos) == 30 0 0 0
441 unless $self->bwrite( pack( STRMIDX, @$rec ), $pos ) ==
442 STRM_LEN;
443 0 $i++;
444 0 ▶ last unless $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 0 0
445 }
446  
447 0 pflock( $file, LOCK_UN );
448 }
449  
450 0 ▶ pdebug( 'failed to write all stream records to the file header',
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 0 0
451 PDLEVEL1 )
452 unless $rv;
453  
454 0 subPostamble( PDLEVEL3, '$', $rv );
455  
456 0 return $rv;
457 }
458  
459 53 ▶sub readStreams {
Subroutine readStreams: called CC=9 SCAR=24.1
Pod documented
460  
461 # Purpose: Reads the stream records from the file header
462 # Returns: Boolean
463 # Usage: $rv = $obj->readStreams;
464  
465 53 my $self = shift;
466 53 my $rv = 1;
467 53 my ( $raw, $sname, $bn, @sraw, $prev );
468 53 my ( %sidx, @streams, %model, $maxstreams );
469  
470 53 subPreamble(PDLEVEL3);
471  
472 # Read the streams section of the block
473 53 ▶ if ( $self->bread( \$raw, STREAMS_POS ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bread(\$raw, 28)) { } 53 0 53
474  
475 # Get the model so we know how many streams we can support
476 53 %model = $self->model;
477 53 $maxstreams = $model{maxStreams};
478  
479 53 @sraw = unpack '(' . STRMIDX . ")$maxstreams", $raw;
480 53 ▶ while (@sraw) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sraw) 115 0 115
481 115 $sname = shift @sraw;
482 115 $bn = longs2Quad( shift @sraw, shift @sraw );
483  
484 # Stop processing when it looks like we're not getting legitmate
485 # values
486 115 ▶ last unless defined $sname and length $sname and $bn > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sname and length $sname and $bn > 0 53 62 115
Condition66%4/6
defined $sname and length $sname and $bn > 0
!ll&&!rl&&r
53062
defined $sname and length $sname
!ll&&!rl&&r
05362
MC/DC33%1/3
defined $sname and length $sname and $bn > 0
defined $sname length $sname $bn > 0
Truth table50%2/4
A and B and C
A= defined $snameB= length $snameC= $bn > 0
ABCresult
0XX0
10X0
1100
1111
487  
488 # Make sure we're not getting repeated streams
489 62 ▶ if ( exists $sidx{$sname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sidx{$sname}) 0 62 62
490 0 pdebug( 'stream (%s) listed more than once',
491 PDLEVEL1, $sname );
492 0 $rv = 0;
493 0 last;
494 }
495  
496 # Error out if stream block numbers aren't ascending
497 62 ▶ unless ( !defined $prev or $bn > $prev ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $prev or $bn > $prev) 0 62 62
Condition66%2/3
not defined $prev or $bn > $prev
l!l&&r!l&&!r
31310
MC/DC0%0/2
not defined $prev or $bn > $prev
not defined $prev $bn > $prev
Truth table66%2/3
not(A) or B
A= not defined $prevB= $bn > $prev
ABresult
1X1
011
000
498 0 pdebug( 'stream block number appearing out of sequence',
499 PDLEVEL1 );
500 0 $rv = 0;
501 0 last;
502 }
503  
504 # Save entry
505 62 push @streams, [ $sname, $bn ];
506 62 $sidx{$sname} = $#streams;
507 62 $prev = $bn;
508 }
509  
510 # Save everything extracted
511 53 $$self{streamidx} = {%sidx};
512 53 $$self{streams} = [@streams];
513 53 pdebug( 'found %s streams', PDLEVEL4, scalar @streams );
514  
515 } else {
516 0 pdebug( 'failed to read list of streams from file header', PDLEVEL1 );
517 0 $rv = 0;
518 }
519  
520 53 subPostamble( PDLEVEL3, '$', $rv );
521  
522 53 return $rv;
523 }
524  
525 88 ▶sub addStream {
Subroutine addStream: called CC=9 SCAR=22.4
Pod documented
526  
527 # Purpose: Adds a stream record to the file header
528 # Returns: Boolean
529 # Usage: $rv = $obj->addStream($sname, $bn);
530  
531 88 my $self = shift;
532 88 my $sname = shift;
533 88 my $bn = shift;
534 88 my %sidx = %{ $$self{streamidx} };
535 88 my $rv = 1;
536  
537 88 subPreamble( PDLEVEL3, '$$', $sname, $bn );
538  
539 88 ▶ if ( defined $sname and length $sname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sname and length $sname) { } 77 11 88
Condition66%2/3
defined $sname and length $sname
!ll&&!rl&&r
11077
MC/DC50%1/2
defined $sname and length $sname
defined $sname length $sname
Truth table66%2/3
A and B
A= defined $snameB= length $sname
ABresult
0X0
100
111
540 77 ▶ if ( exists $sidx{$sname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sidx{$sname}) 0 77 77
541 0 pdebug( 'stream already exists (%s)', PDLEVEL1, $sname );
542 0 $rv = 0;
543 }
544  
545 77 ▶ if ( length $sname > 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $sname > 20) 0 77 77
546 0 pdebug( 'stream name is too long (%s)', PDLEVEL1, $sname );
547 0 $rv = 0;
548 }
549  
550 77 ▶ if ( !defined $bn or $bn < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $bn or $bn < 1) 22 55 77
Condition100%3/3
not defined $bn or $bn < 1
l!l&&r!l&&!r
111155
MC/DC100%2/2
not defined $bn or $bn < 1
not defined $bn $bn < 1
Truth table100%3/3
not(A) or B
A= not defined $bnB= $bn < 1
ABresult
1X1
011
000
551 22 pdebug( 'invalid stream block number (%s)', PDLEVEL1, $bn );
552 22 $rv = 0;
553 }
554  
555 77 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 55 22 77
556 55 push @{ $$self{streams} }, [ $sname, $bn ];
557 55 ${ $$self{streamidx} }{$sname} = $#{ $$self{streams} };
558 $rv = 0
559 unless $self->bwrite( pack( STRMIDX, $sname, $bn ),
560 55 ▶ STREAMS_POS + STRM_LEN * $#{ $$self{streams} } ) == STRM_LEN;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bwrite(pack("Z21NNx", $sname, $bn), 28 + 30 * $#{$$self{"streams"};}) == 30 0 55 55
561 }
562  
563 } else {
564 11 pdebug( 'invalid stream name (%s)', PDLEVEL1, $sname );
565 11 $rv = 0;
566 }
567  
568 88 subPostamble( PDLEVEL3, '$', $rv );
569  
570 88 return $rv;
571 }
572  
573 12 1;
574  
575 __END__
« Paranoid/Network/IPv4.pm ↑ Summary Paranoid/IO/FileMultiplexer/Block.pm »