Paranoid/IO/FileMultiplexer/Block/StreamHeader.pm

filter: Statementstmt 81.0% 171 / 211 Branchbran 50.0% 26 / 52 Conditioncond 55.5% 10 / 18 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 93.7% 30 / 32 Podpod 100% 14 / 14 totaltotal 75.4% 255 / 338 CC 34 SCAR 20.5
CC
34
Cov
74%
writeBATs
42.0
readSig
8.5
readBATs
8.2
CRAP 7.8
« Paranoid/Debug.pm ↑ Summary Paranoid/Module.pm »
1 # Paranoid::IO::FileMultiplexer::Block::StreamHeader -- Stream Header Block
2 #
3 # $Id: lib/Paranoid/IO/FileMultiplexer/Block/StreamHeader.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::StreamHeader;
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  
46 12 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
47  
48 12 ▶use base qw(Paranoid::IO::FileMultiplexer::Block);
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 # Signature format:
51 # PIOFMSTRM Name EOS
52 # Z10 Z21 NNx
53 # 40 bytes
54 #
55 # BAT record format:
56 # BlockNum
57 # NN
58 # 8 bytes
59 12 ▶use constant SIGNATURE => 'Z10Z21NNx';
Subroutine BEGIN: called CC=1 SCAR=0.0
60 12 ▶use constant SIG_LEN => 40;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 12 ▶use constant SIG_TYPE => 'PIOFMSTRM';
Subroutine BEGIN: called CC=1 SCAR=0.0
62 12 ▶use constant EOS_POS => 31;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 12 ▶use constant BATS_POS => 40;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 12 ▶use constant BATIDX => 'NN';
Subroutine BEGIN: called CC=1 SCAR=0.0
65 12 ▶use constant BAT_LEN => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 #####################################################################
68 #
69 # Module code follows
70 #
71 #####################################################################
72  
73 208 ▶sub new {
Subroutine new: called CC=5 SCAR=16.9
Pod documented
74  
75 # Purpose: Creates a new stream header object
76 # Returns: Object reference/undef
77 # Usage: $obj =
78 # Paranoid::IO::FileMultiplexer::Block::StreamHeader->new($file,
79 # $blockNo, $blockSize, $strmName);
80  
81 208 my $class = shift;
82 208 my $file = shift;
83 208 my $bnum = shift;
84 208 my $bsize = shift;
85 208 my $sname = shift;
86 208 my $self;
87  
88 208 subPreamble( PDLEVEL3, '$$$$', $file, $bnum, $bsize, $sname );
89  
90 208 ▶ if ( defined $sname and length $sname and length $sname <= 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sname and length $sname and length $sname <= 20) { } 208 0 208
Condition33%2/6
defined $sname and length $sname and length $sname <= 20
!ll&&!rl&&r
00208
defined $sname and length $sname
!ll&&!rl&&r
00208
MC/DC0%0/3
defined $sname and length $sname and length $sname <= 20
defined $sname length $sname length $sname <= 20
Truth table25%1/4
A and B and C
A= defined $snameB= length $snameC= length $sname <= 20
ABCresult
0XX0
10X0
1100
1111
91 208 $self = __PACKAGE__->SUPER::new( $file, $bnum, $bsize );
92 } else {
93 0 pdebug( 'invalid stream name (%s)', PDLEVEL1, $sname );
94 }
95  
96 208 ▶ if ( defined $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self) 208 0 208
97 208 $$self{streamName} = $sname;
98 208 $$self{bats} = []; # array of blockNum
99 208 $$self{eos} = 0; # address of stream EOF
100 208 $$self{maxBATs} = int( ( $$self{blockSize} - SIG_LEN ) / BAT_LEN );
101 }
102  
103 208 subPostamble( PDLEVEL3, '$', $self );
104  
105 208 return $self;
106 }
107  
108 11 ▶sub streamName {
Subroutine streamName: called CC=1 SCAR=0.0
Pod documented
109  
110 # Purpose: Returns the current stream name
111 # Returns: String
112 # Usage: $name = $obj->streamName;
113  
114 11 my $self = shift;
115  
116 11 return $$self{streamName};
117 }
118  
119 75 ▶sub maxBATs {
Subroutine maxBATs: called CC=1 SCAR=0.0
Pod documented
120  
121 # Purpose: Returns the max BAT blocks for the stream
122 # Returns: Integer
123 # Usage: $max = $obj->maxBATs;
124  
125 75 my $self = shift;
126  
127 75 return $$self{maxBATs};
128 }
129  
130 1178 ▶sub eos {
Subroutine eos: called CC=1 SCAR=0.0
Pod documented
131  
132 # Purpose: Returns the current stream EOS
133 # Returns: Integer
134 # Usage: $eos = $obj->eos;
135  
136 1178 my $self = shift;
137  
138 1178 return $$self{eos};
139 }
140  
141 1836 ▶sub bats {
Subroutine bats: called CC=1 SCAR=0.0
Pod documented
142  
143 # Purpose: Returns an array of bat nums
144 # Returns: Hash
145 # Usage: @bats = $obj->bats;
146  
147 1836 my $self = shift;
148  
149 1836 return @{ $$self{bats} };
150 }
151  
152 0 ▶sub full {
Subroutine full: not called CC=1 SCAR=6.9
Pod documented
153  
154 # Purpose: Returns whether the streams's array of BAT blocks is full
155 # Returns: Boolean
156 # Usage: $rv = $obj->full;
157  
158 0 my $self = shift;
159  
160 0 return $self->maxBATs == scalar $self->bats;
161 }
162  
163 55 ▶sub writeSig {
Subroutine writeSig: called CC=1 SCAR=0.0
Pod documented
164  
165 # Purpose: Writes the stream signature to the file
166 # Returns: Boolean
167 # Usage: $rv = $obj->writeSig;
168  
169 55 my $self = shift;
170 55 my $file = $$self{file};
171 55 my $sname = $$self{streamName};
172 55 my $eos = $$self{eos};
173 55 my $rv = 0;
174 55 my $sig = pack SIGNATURE, SIG_TYPE, $sname, quad2Longs($eos);
175  
176 55 subPreamble(PDLEVEL3);
177  
178 55 $rv = $self->bwrite($sig);
179  
180 55 subPostamble( PDLEVEL3, '$', $rv );
181  
182 55 return $rv;
183 }
184  
185 89 ▶sub readSig {
Subroutine readSig: called CC=7 SCAR=21.5
Pod documented
186  
187 # Purpose: Reads the block signature from the file
188 # Returns: Boolean
189 # Usage: $rv = $obj->readSig;
190  
191 89 my $self = shift;
192 89 my $file = $$self{file};
193 89 my $rv = 0;
194 89 my ( $raw, $type, $sname, $eos, $leos, $ueos );
195  
196 89 subPreamble(PDLEVEL3);
197  
198 89 ▶ if ( pflock( $file, LOCK_SH ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 1)) 89 0 89
199 89 ▶ if ( $self->bread( \$raw, 0, SIG_LEN ) == SIG_LEN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bread(\$raw, 0, 40) == 40) { } 67 22 89
200 67 $rv = 1;
201  
202 # Unpack the signature
203 67 ( $type, $sname, $leos, $ueos ) = unpack SIGNATURE, $raw;
204  
205 # Validate contents
206 #
207 # Start with file type
208 67 ▶ unless ( $type eq SIG_TYPE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type eq "PIOFMSTRM") 0 67 67
209 0 $rv = 0;
210 0 pdebug( 'Invalid stream header type (%s)', PDLEVEL1, $type );
211 }
212  
213 # stream name
214 67 ▶ unless ( $sname eq $$self{streamName} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sname eq $self->{"streamName"}) 0 67 67
215 0 $rv = 0;
216 0 pdebug( 'Invalid stream name (%s)', PDLEVEL1, $sname );
217 }
218  
219 # Make sure eos is legitimate
220 67 $eos = longs2Quad( $leos, $ueos );
221 67 ▶ unless ( defined $eos ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $eos) 0 67 67
222 0 pdebug( 'this platform does not support 64b values for eos',
223 PDLEVEL1 );
224 0 $rv = 0;
225 }
226  
227 # Update internal values
228 67 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 67 0 67
229 67 $$self{eos} = $eos;
230 } else {
231 0 pdebug( 'stream signature verification failure', PDLEVEL1 );
232 }
233  
234 } else {
235 22 pdebug( 'failed to read stream header signature', PDLEVEL1 );
236 }
237  
238 89 pflock( $file, LOCK_UN );
239 }
240  
241 89 subPostamble( PDLEVEL3, '$', $rv );
242  
243 89 return $rv;
244 }
245  
246 566 ▶sub writeEOS {
Subroutine writeEOS: called CC=3 SCAR=11.2
Pod documented
247  
248 # Purpose: Updates the EOS counter and writes it to disk
249 # Returns: Boolean
250 # Usage: $rv = $obj->writeEOS($pos);
251  
252 566 my $self = shift;
253 566 my $eos = shift;
254 566 my ( $raw, $rv );
255  
256 566 subPreamble( PDLEVEL3, '$', $eos );
257  
258 566 ▶ if ( defined $eos ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $eos) { } 566 0 566
259 566 $raw = pack 'NN', quad2Longs($eos);
260 566 ▶ if ( $self->bwrite( $raw, EOS_POS ) == 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bwrite($raw, 31) == 8) 566 0 566
261 566 $$self{eos} = $eos;
262 566 $rv = 1;
263 }
264 } else {
265 0 pdebug( 'invalid value for eos (%s)', PDLEVEL1, $eos );
266 }
267  
268 566 subPostamble( PDLEVEL3, '$', $rv );
269  
270 566 return $rv;
271 }
272  
273 2959 ▶sub readEOS {
Subroutine readEOS: called CC=4 SCAR=14.0
Pod documented
274  
275 # Purpose: Reads the EOS counter from disk
276 # Returns: Integer/undef on error
277 # Usage: $pos = $obj->readEOS;
278  
279 2959 my $self = shift;
280 2959 my ( $rv, $raw );
281  
282 2959 subPreamble(PDLEVEL3);
283  
284 2959 ▶ if ( $self->bread( \$raw, EOS_POS, 8 ) == 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bread(\$raw, 31, 8) == 8) 2959 0 2959
285 2959 $rv = longs2Quad( unpack 'NN', $raw );
286 2959 ▶ $rv = PTRUE_ZERO if defined $rv and $rv == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rv and $rv == 0 65 2894 2959
Condition66%2/3
defined $rv and $rv == 0
!ll&&!rl&&r
0289465
MC/DC50%1/2
defined $rv and $rv == 0
defined $rv $rv == 0
Truth table66%2/3
A and B
A= defined $rvB= $rv == 0
ABresult
0X0
100
111
287 }
288  
289 2959 subPostamble( PDLEVEL3, '$', $rv );
290  
291 2959 return $rv;
292 }
293  
294 2959 ▶sub validateEOS {
Subroutine validateEOS: called CC=2 SCAR=7.0
Pod documented
295  
296 # Purpose: Compares in-memory EOS counter to what's stored in the file
297 # Returns: Boolean
298 # Usage: $rv = $obj->validateBlocks;
299  
300 2959 my $self = shift;
301 2959 my $rv = 0;
302  
303 2959 subPreamble(PDLEVEL3);
304  
305 2959 ▶ $rv = 1 if $$self{eos} == $self->readEOS;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eos"} == $self->readEOS 2959 0 2959
306  
307 2959 subPostamble( PDLEVEL3, '$', $rv );
308  
309 2959 return $rv;
310 }
311  
312 0 ▶sub writeBATs {
Subroutine writeBATs: not called CC=6 SCAR=37.4
Pod documented
313  
314 # Purpose: Writes all the BAT block numbers to the file
315 # Returns: Boolean
316 # Usage: $rv = $obj->writeBATs;
317  
318 0 my $self = shift;
319 0 my $file = $$self{file};
320 0 my $rv = 0;
321 0 my ( $rec, $i, $pos );
322  
323 0 subPreamble(PDLEVEL3);
324  
325 # Hold an exclusive lock for the entire transaction
326 0 ▶ if ( pflock( $file, LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 2)) 0 0 0
327  
328 0 $rv = 1;
329 0 $i = 0;
330 0 foreach $rec ( @{ $$self{bats} } ) {
331 0 $pos = BATS_POS + $i * BAT_LEN;
332 0 ▶ $rv = 0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bwrite(pack("NN", quad2Longs($rec)), $pos) == 8 0 0 0
333 unless $self->bwrite( pack( BATIDX, quad2Longs($rec) ), $pos )
334 == BAT_LEN;
335 0 $i++;
336 0 ▶ last unless $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 0 0
337 }
338  
339 0 pflock( $file, LOCK_UN );
340 }
341  
342 0 ▶ pdebug( 'failed to write all BAT block numbers to the stream header',
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 0 0
343 PDLEVEL1 )
344 unless $rv;
345  
346 0 subPostamble( PDLEVEL3, '$', $rv );
347  
348 0 return $rv;
349 }
350  
351 67 ▶sub readBATs {
Subroutine readBATs: called CC=7 SCAR=21.0
Pod documented
352  
353 # Purpose: Reads the BAT records from the stream header
354 # Returns: Boolean
355 # Usage: $rv = $obj->readBATs;
356  
357 67 my $self = shift;
358 67 my $rv = 1;
359 67 my ( $raw, @sraw, $bn, $lbn, $ubn, $prev );
360 67 my @bats;
361  
362 67 subPreamble(PDLEVEL3);
363  
364 # Read the BATs section of the block
365 67 ▶ if ( $self->bread( \$raw, BATS_POS ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bread(\$raw, 40)) { } 67 0 67
366  
367 67 @sraw = unpack '(' . BATIDX . ")$$self{maxBATs}", $raw;
368 67 ▶ while (@sraw) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sraw) 112 0 112
369  
370 112 $lbn = shift @sraw;
371 112 $ubn = shift @sraw;
372 112 $bn = longs2Quad( $lbn, $ubn );
373  
374 # Stop processing when it looks like we're not getting legitmate
375 # values
376 112 ▶ last unless defined $bn and $bn > $$self{blockNum};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bn and $bn > $self->{"blockNum"} 67 45 112
Condition66%2/3
defined $bn and $bn > $self->{"blockNum"}
!ll&&!rl&&r
06745
MC/DC50%1/2
defined $bn and $bn > $self->{"blockNum"}
defined $bn $bn > $self->{"blockNum"}
Truth table66%2/3
A and B
A= defined $bnB= $bn > $self->{"blockNum"}
ABresult
0X0
100
111
377  
378 # Error out if block numbers aren't ascending
379 45 ▶ unless ( !defined $prev or $bn > $prev ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $prev or $bn > $prev) 0 45 45
Condition33%1/3
not defined $prev or $bn > $prev
l!l&&r!l&&!r
4500
MC/DC0%0/2
not defined $prev or $bn > $prev
not defined $prev $bn > $prev
Truth table33%1/3
not(A) or B
A= not defined $prevB= $bn > $prev
ABresult
1X1
011
000
380 0 pdebug( 'BAT block number appearing out of sequence',
381 PDLEVEL1 );
382 0 $rv = 0;
383 0 last;
384 }
385  
386 # Save entry
387 45 push @bats, $bn;
388 45 $prev = $bn;
389 }
390  
391 # Save everything extracted
392 67 $$self{bats} = [@bats];
393 67 pdebug( 'found %s bats', PDLEVEL4, scalar @bats );
394  
395 } else {
396 0 pdebug( 'failed to read list of BATs from stream header', PDLEVEL1 );
397 0 $rv = 0;
398 }
399  
400 67 subPostamble( PDLEVEL3, '$', $rv );
401  
402 67 return $rv;
403 }
404  
405 77 ▶sub addBAT {
Subroutine addBAT: called CC=7 SCAR=20.8
Pod documented
406  
407 # Purpose: Adds a BAT block number to the stream header
408 # Returns: Boolean
409 # Usage: $rv = $obj->addBAT($bn);
410  
411 77 my $self = shift;
412 77 my $bn = shift;
413 77 my $rv = 1;
414  
415 77 subPreamble( PDLEVEL3, '$', $bn );
416  
417 77 ▶ if ( defined $bn and $bn > $$self{blockNum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bn and $bn > $self->{'blockNum'}) { } 55 22 77
Condition100%3/3
defined $bn and $bn > $self->{"blockNum"}
!ll&&!rl&&r
111155
MC/DC100%2/2
defined $bn and $bn > $self->{"blockNum"}
defined $bn $bn > $self->{"blockNum"}
Truth table100%3/3
A and B
A= defined $bnB= $bn > $self->{"blockNum"}
ABresult
0X0
100
111
418  
419 # Make sure we're not adding redundant entries
420 0 ▶ if ( scalar grep { $_ eq $bn } @{ $$self{bats} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({$_ eq $bn;} @{$$self{"bats"};})) 0 55 55
421 0 $rv = 0;
422 0 pdebug( 'redundant entry for an existing BAT', PDLEVEL1 );
423 }
424  
425 # Make sure new BAT is a higher block number than all previous BATs
426 0 ▶ if ( scalar grep { $_ > $bn } @{ $$self{bats} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({$_ > $bn;} @{$$self{"bats"};})) 0 55 55
427 0 $rv = 0;
428 0 pdebug( 'BAT block number is lower than previous BATs',
429 PDLEVEL1 );
430 }
431  
432 55 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 55 0 55
433 55 push @{ $$self{bats} }, $bn;
434 $rv = 0
435 unless $self->bwrite(
436 pack( BATIDX, quad2Longs($bn) ),
437 55 ▶ BATS_POS + BAT_LEN * $#{ $$self{bats} } ) == BAT_LEN;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bwrite(pack("NN", quad2Longs($bn)), 40 + 8 * $#{$$self{"bats"};}) == 8 0 55 55
438 }
439  
440 } else {
441 22 pdebug( 'invalid BAT block number (%s)', PDLEVEL1, $bn );
442 22 $rv = 0;
443 }
444  
445 77 subPostamble( PDLEVEL3, '$', $rv );
446  
447 77 return $rv;
448 }
449  
450 12 1;
451  
452 __END__
« Paranoid/Debug.pm ↑ Summary Paranoid/Module.pm »