Paranoid/IO/FileMultiplexer/Block/BATHeader.pm

filter: Statementstmt 79.4% 147 / 185 Branchbran 47.6% 20 / 42 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 92.8% 26 / 28 Podpod 100% 10 / 10 totaltotal 76.0% 213 / 280 CC 26 SCAR 21.9
CC
26
Cov
74%
writeData
42.0
readSig
10.5
readData
7.9
CRAP 8.9
« Paranoid/Data.pm ↑ Summary Paranoid/Network/IPv4.pm »
1 # Paranoid::IO::FileMultiplexer::Block::BATHeader -- BAT Header Block
2 #
3 # $Id: lib/Paranoid/IO/FileMultiplexer/Block/BATHeader.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::BATHeader;
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;
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 # PIOFMBAT Name Sequence
52 # Z9 Z21 NNxx
53 # 40 bytes
54 #
55 # Data record format:
56 # BlockNum
57 # NN
58 # 8 bytes
59 12 ▶use constant SIGNATURE => 'Z9Z21NNxx';
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 => 'PIOFMBAT';
Subroutine BEGIN: called CC=1 SCAR=0.0
62 12 ▶use constant SEQ_POS => 30;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 12 ▶use constant DATA_POS => 40;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 12 ▶use constant DATAIDX => 'NN';
Subroutine BEGIN: called CC=1 SCAR=0.0
65 12 ▶use constant DATA_LEN => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 #####################################################################
68 #
69 # Module code follows
70 #
71 #####################################################################
72  
73 2222 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
74  
75 # Purpose: Creates a new BAT header object
76 # Returns: Object reference/undef
77 # Usage: $obj =
78 # Paranoid::IO::FileMultiplexer::Block::BATHeader->new($file,
79 # $blockNo, $blockSize, $strmName, $sequenceNo);
80  
81 2222 my $class = shift;
82 2222 my $file = shift;
83 2222 my $bnum = shift;
84 2222 my $bsize = shift;
85 2222 my $sname = shift;
86 2222 my $seq = shift;
87 2222 my $self;
88  
89 2222 subPreamble( PDLEVEL3, '$$$$$', $file, $bnum, $bsize, $sname, $seq );
90  
91 2222 $self = __PACKAGE__->SUPER::new( $file, $bnum, $bsize );
92 2222 ▶ if ( defined $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self) 2222 0 2222
93 2222 $$self{streamName} = $sname;
94 2222 $$self{data} = []; # array of data blockNums
95 2222 $$self{sequence} = 0; # sequence no of BAT
96 2222 $$self{maxData} = int( ( $$self{blockSize} - SIG_LEN ) / DATA_LEN );
97 }
98  
99 2222 subPostamble( PDLEVEL3, '$', $self );
100  
101 2222 return $self;
102 }
103  
104 930 ▶sub maxData {
Subroutine maxData: called CC=1 SCAR=0.0
Pod documented
105  
106 # Purpose: Returns the max data blocks for the BAT
107 # Returns: Integer
108 # Usage: $max = $obj->maxData;
109  
110 930 my $self = shift;
111  
112 930 return $$self{maxData};
113 }
114  
115 70 ▶sub sequence {
Subroutine sequence: called CC=1 SCAR=0.0
Pod documented
116  
117 # Purpose: Returns the current BAT sequence number
118 # Returns: Integer
119 # Usage: $seq = $obj->sequence;
120  
121 70 my $self = shift;
122  
123 70 return $$self{sequence};
124 }
125  
126 1307 ▶sub dataBlocks {
Subroutine dataBlocks: called CC=1 SCAR=0.0
Pod documented
127  
128 # Purpose: Returns an array of data block nums
129 # Returns: Array
130 # Usage: @data = $obj->dataBlocks;
131  
132 1307 my $self = shift;
133  
134 1307 return @{ $$self{data} };
135 }
136  
137 0 ▶sub full {
Subroutine full: not called CC=1 SCAR=6.9
Pod documented
138  
139 # Purpose: Returns whether the BAT's array of data blocks is full
140 # Returns: Boolean
141 # Usage: $rv = $obj->full;
142  
143 0 my $self = shift;
144  
145 0 return $self->maxData == scalar $self->dataBlocks;
146 }
147  
148 55 ▶sub writeSig {
Subroutine writeSig: called CC=1 SCAR=0.0
Pod documented
149  
150 # Purpose: Writes the BAT signature to the file
151 # Returns: Boolean
152 # Usage: $rv = $obj->writeSig;
153  
154 55 my $self = shift;
155 55 my $file = $$self{file};
156 55 my $sname = $$self{streamName};
157 55 my $seq = $$self{sequence};
158 55 my $rv = 0;
159 55 my $sig = pack SIGNATURE, SIG_TYPE, $sname, quad2Longs($seq);
160  
161 55 subPreamble(PDLEVEL3);
162  
163 55 $rv = $self->bwrite($sig);
164  
165 55 subPostamble( PDLEVEL3, '$', $rv );
166  
167 55 return $rv;
168 }
169  
170 1248 ▶sub readSig {
Subroutine readSig: called CC=8 SCAR=23.6
Pod documented
171  
172 # Purpose: Reads the block signature from the file
173 # Returns: Boolean
174 # Usage: $rv = $obj->readSig;
175  
176 1248 my $self = shift;
177 1248 my $file = $$self{file};
178 1248 my $rv = 0;
179 1248 my ( $raw, $type, $sname, $seq, $lseq, $useq );
180  
181 1248 subPreamble(PDLEVEL3);
182  
183 1248 ▶ if ( pflock( $file, LOCK_SH ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 1)) 1248 0 1248
184 1248 ▶ if ( $self->bread( \$raw, 0, SIG_LEN ) == SIG_LEN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bread(\$raw, 0, 40) == 40) { } 1237 11 1248
185 1237 $rv = 1;
186  
187 # Unpack the signature
188 1237 ( $type, $sname, $lseq, $useq ) = unpack SIGNATURE, $raw;
189  
190 # Validate contents
191 #
192 # Start with file type
193 1237 ▶ unless ( $type eq SIG_TYPE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type eq "PIOFMBAT") 0 1237 1237
194 0 $rv = 0;
195 0 pdebug( 'Invalid BAT header type (%s)', PDLEVEL1, $type );
196 }
197  
198 # stream name
199 1237 ▶ unless ( $sname eq $$self{streamName} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sname eq $self->{"streamName"}) 0 1237 1237
200 0 $rv = 0;
201 0 pdebug( 'Invalid stream name (%s)', PDLEVEL1, $sname );
202 }
203  
204 # Make sure seq is legitimate
205 1237 $seq = longs2Quad( $lseq, $useq );
206 1237 ▶ unless ( defined $seq ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $seq) 0 1237 1237
207 0 pdebug(
208 'this platform does not support 64b values for sequence',
209 PDLEVEL1
210 );
211 0 $rv = 0;
212 }
213 1237 ▶ unless ( $seq == $$self{sequence} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seq == $self->{"sequence"}) 0 1237 1237
214 0 pdebug( 'Invalid sequence number for BAT (%s)',
215 PDLEVEL1, $seq );
216 0 $rv = 0;
217 }
218  
219 # Update internal values
220 1237 ▶ pdebug( 'BAT signature verification failure', PDLEVEL1 )
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 1237 1237
221 unless $rv;
222  
223 } else {
224 11 pdebug( 'failed to read BAT header signature', PDLEVEL1 );
225 }
226  
227 1248 pflock( $file, LOCK_UN );
228 }
229  
230 1248 subPostamble( PDLEVEL3, '$', $rv );
231  
232 1248 return $rv;
233 }
234  
235 0 ▶sub writeData {
Subroutine writeData: not called CC=6 SCAR=37.4
Pod documented
236  
237 # Purpose: Writes all the data block numbers to the file
238 # Returns: Boolean
239 # Usage: $rv = $obj->writeData;
240  
241 0 my $self = shift;
242 0 my $file = $$self{file};
243 0 my $rv = 0;
244 0 my ( $rec, $i, $pos, $maxbats );
245  
246 0 subPreamble(PDLEVEL3);
247  
248 # Hold an exclusive lock for the entire transaction
249 0 ▶ if ( pflock( $file, LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 2)) 0 0 0
250  
251 # Calculate the maximum possible number of BATs
252 0 $maxbats = int( ( $$self{blockSize} - SIG_LEN ) / DATA_LEN );
253  
254 0 $rv = 1;
255 0 $i = 0;
256 0 foreach $rec ( @{ $$self{data} } ) {
257 0 $pos = DATA_POS + $i * DATA_LEN;
258 0 ▶ $rv = 0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bwrite(pack("NN", quad2Longs($rec)), $pos) == 8 0 0 0
259 unless $self->bwrite( pack( DATAIDX, quad2Longs($rec) ),
260 $pos ) == DATA_LEN;
261 0 $i++;
262 0 ▶ last unless $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 0 0
263 }
264  
265 0 pflock( $file, LOCK_UN );
266 }
267  
268 0 ▶ pdebug( 'failed to write all data block numbers to the BAT header',
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 0 0
269 PDLEVEL1 )
270 unless $rv;
271  
272 0 subPostamble( PDLEVEL3, '$', $rv );
273  
274 0 return $rv;
275 }
276  
277 1237 ▶sub readData {
Subroutine readData: called CC=7 SCAR=20.7
Pod documented
278  
279 # Purpose: Reads the data block numbers from the BAT header
280 # Returns: Boolean
281 # Usage: $rv = $obj->readData;
282  
283 1237 my $self = shift;
284 1237 my $rv = 1;
285 1237 my ( $raw, @sraw, $bn, $lbn, $ubn, $prev );
286 1237 my @data;
287  
288 1237 subPreamble(PDLEVEL3);
289  
290 # Read the BATs section of the block
291 1237 ▶ if ( $self->bread( \$raw, DATA_POS ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bread(\$raw, 40)) { } 1237 0 1237
292  
293 1237 @sraw = unpack '(' . DATAIDX . ")$$self{maxData}", $raw;
294 1237 ▶ while (@sraw) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sraw) 3882 0 3882
295  
296 3882 $lbn = shift @sraw;
297 3882 $ubn = shift @sraw;
298 3882 $bn = longs2Quad( $lbn, $ubn );
299  
300 # Stop processing when it looks like we're not getting legitmate
301 # values
302 3882 ▶ last unless defined $bn and $bn > $$self{blockNum};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bn and $bn > $self->{"blockNum"} 1237 2645 3882
Condition66%2/3
defined $bn and $bn > $self->{"blockNum"}
!ll&&!rl&&r
012372645
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
303  
304 # Error out if block numbers aren't ascending
305 2645 ▶ unless ( !defined $prev or $bn > $prev ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $prev or $bn > $prev) 0 2645 2645
Condition66%2/3
not defined $prev or $bn > $prev
l!l&&r!l&&!r
123714080
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
306 0 pdebug( 'data block number appearing out of sequence',
307 PDLEVEL1 );
308 0 $rv = 0;
309 0 last;
310 }
311  
312 # Save entry
313 2645 push @data, $bn;
314 2645 $prev = $bn;
315 }
316  
317 # Save everything extracted
318 1237 $$self{data} = [@data];
319 1237 pdebug( 'found %s data blocks', PDLEVEL4, scalar @data );
320  
321 } else {
322 0 pdebug( 'failed to read list of data blocks from BAT header',
323 PDLEVEL1 );
324 0 $rv = 0;
325 }
326  
327 1237 subPostamble( PDLEVEL3, '$', $rv );
328  
329 1237 return $rv;
330 }
331  
332 147 ▶sub addData {
Subroutine addData: called CC=7 SCAR=20.1
Pod documented
333  
334 # Purpose: Adds a data block number to the BAT header
335 # Returns: Boolean
336 # Usage: $rv = $obj->addData($bn);
337  
338 147 my $self = shift;
339 147 my $bn = shift;
340 147 my $rv = 1;
341 147 my $n;
342  
343 147 subPreamble( PDLEVEL3, '$', $bn );
344  
345 147 ▶ if ( defined $bn and $bn > $$self{blockNum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bn and $bn > $self->{'blockNum'}) { } 125 22 147
Condition100%3/3
defined $bn and $bn > $self->{"blockNum"}
!ll&&!rl&&r
1111125
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
346  
347 # Make sure we're not adding redundant entries
348 125 ▶ if ( scalar grep { $_ eq $bn } @{ $$self{data} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({$_ eq $bn;} @{$$self{"data"};})) 0 125 125
349 0 $rv = 0;
350 0 pdebug( 'redundant entry for an existing data block', PDLEVEL1 );
351 }
352  
353 # Make sure new data block is a higher block number than all previous
354 # data blocks
355 125 ▶ if ( scalar grep { $_ > $bn } @{ $$self{data} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({$_ > $bn;} @{$$self{"data"};})) 0 125 125
356 0 $rv = 0;
357 0 pdebug( 'data block number is lower than previous blocks',
358 PDLEVEL1 );
359 }
360  
361 125 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 125 0 125
362  
363 # Write the block to the header
364 125 push @{ $$self{data} }, $bn;
365 $rv = 0
366 unless $self->bwrite(
367 pack( DATAIDX, quad2Longs($bn) ),
368 125 ▶ DATA_POS + DATA_LEN * $#{ $$self{data} } ) == DATA_LEN;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bwrite(pack("NN", quad2Longs($bn)), 40 + 8 * $#{$$self{"data"};}) == 8 0 125 125
369 }
370  
371 } else {
372 22 pdebug( 'invalid data block number (%s)', PDLEVEL1, $bn );
373 22 $rv = 0;
374 }
375  
376 147 subPostamble( PDLEVEL3, '$', $rv );
377  
378 147 return $rv;
379 }
380  
381 12 1;
382  
383 __END__
« Paranoid/Data.pm ↑ Summary Paranoid/Network/IPv4.pm »