Paranoid/IO/FileMultiplexer.pm

filter: Statementstmt 92.6% 464 / 501 Branchbran 61.6% 132 / 214 Conditioncond 43.6% 48 / 110 MC/DCmcdc 17.7% 11 / 62 Subroutinesub 100% 46 / 46 Podpod 100% 10 / 10 totaltotal 75.4% 711 / 943 CC 147 SCAR 22.9
CC
147
Cov
78%
_getStream
29.4
_strmRead
18.4
_strmWrite
17.7
CRAP 9.8
« Paranoid/IO/Line.pm ↑ Summary Paranoid/Network/IPv6.pm »
1 # Paranoid::IO::FileMultiplexer -- File Multiplexer Object
2 #
3 # $Id: lib/Paranoid/IO/FileMultiplexer.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;
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 Carp;
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::FileHeader;
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 constant PIOFMVER => '1.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
52 12 ▶use constant PERMMASK => 0666;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 12 ▶use constant DEFBSIZE => 4096;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 12 ▶use constant ADDR_BAT => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 12 ▶use constant ADDR_BLK => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 12 ▶use constant ADDR_OFT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 #####################################################################
60 #
61 # Module code follows
62 #
63 #####################################################################
64  
65 154 ▶sub new {
Subroutine new: called CC=8 SCAR=21.0
Pod documented
66  
67 # Purpose: Creates a PIOFM object for manipulation
68 # Returns: Object reference or undef
69 # Usage: $obj = Paranoid::IO::FileMultiplexer->new(
70 # file => $fn,
71 # readOnly => 0,
72 # perms => $perms,
73 # blockSize => $bsize,
74 # );
75  
76 154 my $class = shift;
77 154 my %args = @_;
78 154 my $self = {
79 file => undef,
80 readOnly => 0,
81 perms => PERMMASK ^ umask,
82 header => undef,
83 streams => {},
84 streamPos => {},
85 blockSize => DEFBSIZE,
86 corrupted => 0,
87 %args
88 };
89  
90 pdebug( 'entering w/f: %s bs: %s p: %s ro: %s',
91 154 PDLEVEL1, @args{qw(file blockSize perms readOnly)} );
92 154 pIn();
93  
94 154 bless $self, $class;
95  
96 # Mandatory file name required
97 $self = undef
98 154 ▶ unless defined $args{file} and length $args{file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"file"} and length $args{"file"} 22 132 154
Condition100%3/3
defined $args{"file"} and length $args{"file"}
!ll&&!rl&&r
1111132
MC/DC100%2/2
defined $args{"file"} and length $args{"file"}
defined $args{"file"} length $args{"file"}
Truth table100%3/3
A and B
A= defined $args{"file"}B= length $args{"file"}
ABresult
0X0
100
111
99  
100 154 ▶ if ( defined $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self) { } 132 22 154
101  
102 # Enable the lock stack
103 132 PIOLOCKSTACK = 1;
104  
105 # Attempt to open the file
106 132 ▶ if ( $$self{ro} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ro'}) { } 0 132 132
107 0 ▶ $self = undef unless $self->_oldFile;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_oldFile 0 0 0
108 } else {
109 132 ▶ $self = undef unless $self->_newFile or $self->_oldFile;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_newFile or $self->_oldFile 44 88 132
Condition100%3/3
$self->_newFile or $self->_oldFile
l!l&&r!l&&!r
771144
MC/DC100%2/2
$self->_newFile or $self->_oldFile
$self->_newFile $self->_oldFile
Truth table100%3/3
A or B
A= $self->_newFileB= $self->_oldFile
ABresult
1X1
011
000
110 }
111  
112 } else {
113 22 pdebug( 'invalid file name: %s', PDLEVEL1, $args{file} );
114 }
115  
116 154 subPostamble( PDLEVEL1, '$', $self );
117  
118 154 return $self;
119 }
120  
121 132 ▶sub _newFile {
Subroutine _newFile: called CC=6 SCAR=18.1
122  
123 # Purpose: Attempts to open the file as a new file
124 # Returns: Boolean
125 # Usage: $rv = $obj->_newFile;
126  
127 132 my $self = shift;
128 132 my $file = $$self{file};
129 132 my $bsize = $$self{blockSize};
130 132 my $rv = 0;
131 132 my $header;
132  
133 132 subPreamble(PDLEVEL2);
134  
135 132 ▶ if ( !$$self{readOnly} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'readOnly'}) { } 132 0 132
136  
137 # Allocate the header object (it will fail on invalid block sizes)
138 132 $header =
139 Paranoid::IO::FileMultiplexer::Block::FileHeader->new( $file,
140 $bsize );
141 132 ▶ if ( defined $header ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $header) 99 33 132
142  
143 # Open the file exclusively and get an flock
144 99 $rv = popen( $file, O_CREAT | O_RDWR | O_EXCL, $$self{perms} );
145 99 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 77 22 99
146  
147 # Lock file
148 77 pflock( $file, LOCK_EX );
149  
150 # Allocate the block and write the initial signature
151 77 ▶ $rv = $header->allocate and $header->writeSig;
Branch: 1 branch
BranchTrueFalseTotal
$rv = $header->allocate and $header->writeSig 77 0 77
152 77 ▶ $$self{header} = $header if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 77 0 77
153  
154 # Release the lock
155 77 pflock( $file, LOCK_UN );
156 }
157 }
158 } else {
159 0 pdebug( 'cannot create a new file in readOnly mode', PDLEVEL1 );
160 }
161  
162 132 subPostamble( PDLEVEL2, '$', $rv );
163  
164 132 return $rv;
165 }
166  
167 55 ▶sub _oldFile {
Subroutine _oldFile: called CC=6 SCAR=18.0
168  
169 # Purpose: Attempts to open the file as a new file
170 # Returns: Boolean
171 # Usage: $rv = $obj->_newFile;
172  
173 55 my $self = shift;
174 55 my $file = $$self{file};
175 55 my $bsize = $$self{blockSize};
176 55 my $rv = 0;
177 55 my $header;
178  
179 55 subPreamble(PDLEVEL2);
180  
181 # Allocate the header object (it will fail on invalid block sizes)
182 55 $header = Paranoid::IO::FileMultiplexer::Block::FileHeader->new( $file,
183 $bsize );
184 55 ▶ if ( defined $header ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $header) 22 33 55
185  
186 # Open the file exclusively and get an flock
187 $rv = popen( $file, ( $$self{readOnly} ? O_RDONLY : O_RDWR ),
188 22 ▶ $$self{perms} );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'readOnly'} ? : 0 22 22
189 22 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 22 0 22
190  
191 # Lock file
192 22 pflock( $file, LOCK_SH );
193  
194 # Read an existing signature
195 22 ▶ $rv = $header->readSig && $header->readStreams;
Condition66%2/3
$header->readSig && $header->readStreams
!ll&&!rl&&r
11011
MC/DC50%1/2
$header->readSig && $header->readStreams
$header->readSig $header->readStreams
Truth table66%2/3
A && B
A= $header->readSigB= $header->readStreams
ABresult
0X0
100
111
196 22 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 11 11 22
197 11 $$self{header} = $header;
198 11 $$self{blockSize} = $header->blockSize;
199 }
200  
201 # Release the lock
202 22 pflock( $file, LOCK_UN );
203 }
204 }
205  
206 55 subPostamble( PDLEVEL2, '$', $rv );
207  
208 55 return $rv;
209 }
210  
211 363 ▶sub header {
Subroutine header: called CC=1 SCAR=0.0
Pod documented
212  
213 # Purpose: Returns a reference to the header object
214 # Returns: Ref
215 # Usage: $header = $obj->header;
216  
217 363 my $self = shift;
218 363 return $$self{header};
219 }
220  
221 9 ▶sub _reload {
Subroutine _reload: called CC=4 SCAR=14.9
222  
223 # Purpose: Reloads the file header information and purges the stream
224 # cache
225 # Returns: Boolean
226 # Usage: $rv = $obj->_reload;
227  
228 9 my $self = shift;
229 9 my $file = $$self{file};
230 9 my $header = $$self{header};
231 9 my $rv = 1;
232  
233 9 subPreamble(PDLEVEL4);
234  
235 9 ▶ if ( pflock( $file, LOCK_SH ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 1)) 9 0 9
236 9 ▶ if ( $header->readSig && $header->readStreams ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header->readSig and $header->readStreams) { } 9 0 9
Condition33%1/3
$header->readSig and $header->readStreams
!ll&&!rl&&r
009
MC/DC0%0/2
$header->readSig and $header->readStreams
$header->readSig $header->readStreams
Truth table33%1/3
A and B
A= $header->readSigB= $header->readStreams
ABresult
0X0
100
111
237 9 $$self{streams} = {};
238 } else {
239 0 $$self{corrupt} = 1;
240 0 $rv = 0;
241 }
242 9 pflock( $file, LOCK_UN );
243 }
244  
245 9 subPostamble( PDLEVEL4, '$', $rv );
246  
247 9 return $rv;
248 }
249  
250 2948 ▶sub _getStream {
Subroutine _getStream: called CC=15 SCAR=33.8
251  
252 # Purpose: Retrieves or creates a stream header object
253 # Returns: Ref
254 # Usage: $ref = $obj->_getStream($name);
255  
256 2948 my $self = shift;
257 2948 my $sname = shift;
258 2948 my $header = $$self{header};
259 2948 my $file = $$self{file};
260 2948 my ( $rv, %streams, $stream );
261  
262 2948 subPreamble( PDLEVEL2, '$$', $sname, $header );
263  
264 2948 ▶ if ( defined $sname and length $sname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sname and length $sname) 2948 0 2948
Condition33%1/3
defined $sname and length $sname
!ll&&!rl&&r
002948
MC/DC0%0/2
defined $sname and length $sname
defined $sname length $sname
Truth table33%1/3
A and B
A= defined $snameB= length $sname
ABresult
0X0
100
111
265  
266 # Reload if header fails validation
267 2948 ▶ $self->_reload unless $header->validateBlocks;
Branch: 1 branch
BranchTrueFalseTotal
unless $header->validateBlocks 9 2939 2948
268  
269 # Create the stream object if we don't have one cached
270 2948 ▶ unless ( exists $$self{streams}{$sname} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"streams"}{$sname}) 34 2914 2948
271 34 %streams = $header->streams;
272 34 ▶ if ( exists $streams{$sname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $streams{$sname}) { } 34 0 34
273 $stream =
274 Paranoid::IO::FileMultiplexer::Block::StreamHeader->new(
275 34 $$self{file}, $streams{$sname}, $header->blockSize,
276 $sname );
277 34 ▶ if ( pflock( $file, LOCK_SH ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 1)) 34 0 34
278 34 ▶ $$self{streams}{$sname} = $stream
Branch: 1 branch
BranchTrueFalseTotal
if $stream->readSig and $stream->readBATs 34 0 34
Condition33%1/3
$stream->readSig and $stream->readBATs
!ll&&!rl&&r
0034
MC/DC0%0/2
$stream->readSig and $stream->readBATs
$stream->readSig $stream->readBATs
Truth table33%1/3
A and B
A= $stream->readSigB= $stream->readBATs
ABresult
0X0
100
111
279 if $stream->readSig
280 and $stream->readBATs;
281 34 pflock( $file, LOCK_UN );
282 }
283 34 ▶ unless ( exists $$self{streams}{$sname} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"streams"}{$sname}) 0 34 34
284 0 pdebug( 'stream \'%s\' failed consistency checks',
285 PDLEVEL1, $sname );
286 0 $$self{corrupt} = 1;
287 }
288 } else {
289 0 pdebug( 'attempted to access a non-existent stream (%s)',
290 PDLEVEL1, $sname );
291 }
292 }
293  
294 # Retrieve a reference to the stream object
295 $stream =
296 exists $$self{streams}{$sname}
297 2948 ▶ ? $$self{streams}{$sname}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'streams'}{$sname} ? : 2948 0 2948
298 : undef;
299  
300 # Reload stream signature if EOS has changed outside of this process
301 2948 ▶ if ( defined $stream ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stream) 2948 0 2948
302 2948 ▶ unless ( $stream->validateEOS ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stream->validateEOS) 0 2948 2948
303 0 ▶ unless ( $stream->readSig and $stream->readBATs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stream->readSig and $stream->readBATs) 0 0 0
Condition0%0/3
$stream->readSig and $stream->readBATs
!ll&&!rl&&r
000
MC/DC0%0/2
$stream->readSig and $stream->readBATs
$stream->readSig $stream->readBATs
Truth table0%0/3
A and B
A= $stream->readSigB= $stream->readBATs
ABresult
0X0
100
111
304 0 $stream = undef;
305 0 pdebug( 'stream \'%s\' failed consistency checks',
306 PDLEVEL1, $sname );
307 0 $$self{corrupt} = 1;
308 }
309 }
310  
311 # Return the stream reference
312 2948 $rv = $stream;
313 }
314 }
315  
316 2948 subPostamble( PDLEVEL4, '$', $rv );
317  
318 2948 return $rv;
319 }
320  
321 1215 ▶sub _getBAT {
Subroutine _getBAT: called CC=8 SCAR=22.3
322  
323 # Purpose: Returns a BAT which has been loaded and validated
324 # Returns: Ref
325 # Usage: $ref = $obj->_getBAT($sname, $seq);
326  
327 1215 my $self = shift;
328 1215 my $sname = shift;
329 1215 my $seq = shift;
330 1215 my $file = $$self{file};
331 1215 my ( $rv, $stream, @bats, $bat );
332  
333 1215 subPreamble( PDLEVEL4, '$$', $sname, $seq );
334  
335 1215 $stream = $self->_getStream($sname);
336 1215 ▶ if ( defined $stream ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stream) 1215 0 1215
337  
338 # Get the list of BATs
339 1215 @bats = $stream->bats;
340  
341 1215 ▶ if ( $seq <= $#bats ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq <= $#bats) 1215 0 1215
342 $bat = Paranoid::IO::FileMultiplexer::Block::BATHeader->new(
343 1215 $$self{file}, $bats[$seq], $$self{blockSize}, $sname, $seq );
344 1215 ▶ if ( pflock( $file, LOCK_SH ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 1)) 1215 0 1215
345 1215 ▶ $rv = $bat
Branch: 1 branch
BranchTrueFalseTotal
if defined $bat and $bat->readSig and $bat->readData 1215 0 1215
Condition33%2/6
defined $bat and $bat->readSig and $bat->readData
!ll&&!rl&&r
001215
defined $bat and $bat->readSig
!ll&&!rl&&r
001215
MC/DC0%0/3
defined $bat and $bat->readSig and $bat->readData
defined $bat $bat->readSig $bat->readData
Truth table25%1/4
A and B and C
A= defined $batB= $bat->readSigC= $bat->readData
ABCresult
0XX0
10X0
1100
1111
346 if defined $bat
347 and $bat->readSig
348 and $bat->readData;
349 1215 pflock( $file, LOCK_UN );
350 }
351 1215 ▶ pdebug( 'BAT %s for stream \'%s\' failed consistency checks',
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 1215 1215
352 PDLEVEL1, $seq, $sname )
353 unless $rv;
354 }
355 }
356  
357 1215 subPostamble( PDLEVEL4, '$', $rv );
358  
359 1215 return $rv;
360 }
361  
362 22 ▶sub _chkData {
Subroutine _chkData: called CC=3 SCAR=11.9
363  
364 # Purpose: Checks that a data block appears to be present
365 # Returns: Boolean
366 # Usage: $rv = $obj->_chkData;
367  
368 22 my $self = shift;
369 22 my $bn = shift;
370 22 my $file = $$self{file};
371 22 my $bsize = $$self{blockSize};
372 22 my ( $rv, $block, $raw );
373  
374 22 subPreamble( PDLEVEL4, '$', $bn );
375  
376 22 $block = Paranoid::IO::FileMultiplexer::Block->new( $file, $bn, $bsize );
377 22 ▶ $rv = ( defined $block and $block->bread( \$raw, 0, 1 ) == 1 );
Condition33%1/3
defined $block && $block->bread(\$raw, 0, 1) == 1
!ll&&!rl&&r
0022
MC/DC0%0/2
defined $block && $block->bread(\$raw, 0, 1) == 1
defined $block $block->bread(\$raw, 0, 1) == 1
Truth table33%1/3
A && B
A= defined $blockB= $block->bread(\$raw, 0, 1) == 1
ABresult
0X0
100
111
378  
379 22 ▶ unless ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 0 22 22
380 0 pdebug( 'data block list at dn %s but cannot be read', PDLEVEL1,
381 $bn );
382 0 $rv = 0;
383 0 $$self{corrupted} = 1;
384 }
385  
386 22 subPostamble( PDLEVEL4, '$', $rv );
387  
388 22 return $rv;
389 }
390  
391 22 ▶sub _chkBAT {
Subroutine _chkBAT: called CC=7 SCAR=21.4
392  
393 # Purpose: Checks that a BAT appears consistent
394 # Returns: Boolean
395 # Usage: $rv = $obj->_chkBAT($bn, $snmae, $seq);
396  
397 22 my $self = shift;
398 22 my $bn = shift;
399 22 my $sname = shift;
400 22 my $seq = shift;
401 22 my $file = $$self{file};
402 22 my $bsize = $$self{blockSize};
403 22 my ( $rv, $block, @data );
404  
405 22 subPreamble( PDLEVEL4, '$$$', $bn, $sname, $seq );
406  
407 22 $block = Paranoid::IO::FileMultiplexer::Block::BATHeader->new( $file, $bn,
408 $bsize, $sname, $seq );
409 22 ▶ $rv = ( defined $block and $block->readSig and $block->readData );
Condition33%2/6
defined $block && $block->readSig && $block->readData
!ll&&!rl&&r
0022
defined $block and $block->readSig
!ll&&!rl&&r
0022
MC/DC0%0/3
defined $block && $block->readSig && $block->readData
defined $block $block->readSig $block->readData
Truth table25%1/4
A and B && C
A= defined $blockB= $block->readSigC= $block->readData
ABCresult
0XX0
10X0
1100
1111
410  
411 22 ▶ unless ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 0 22 22
412 0 pdebug( 'BAT at bn %s fails consistency checks', PDLEVEL1, $bn );
413 0 $rv = 0;
414 0 $$self{corrupted} = 1;
415 }
416  
417 22 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 22 0 22
418 22 @data = $block->dataBlocks;
419 22 ▶ foreach (@data) { $rv = 0 unless $self->_chkData($_) }
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_chkData($_) 0 22 22
420 }
421  
422 22 subPostamble( PDLEVEL4, '$', $rv );
423  
424 22 return $rv;
425 }
426  
427 44 ▶sub _chkStream {
Subroutine _chkStream: called CC=7 SCAR=19.5
428  
429 # Purpose: Checks that a stream appears consistent
430 # Returns: Boolean
431 # Usage: $rv = $obj->_chkStream($bn, $sname);
432  
433 44 my $self = shift;
434 44 my $bn = shift;
435 44 my $sname = shift;
436 44 my $file = $$self{file};
437 44 my $bsize = $$self{blockSize};
438 44 my ( $rv, $i, $block, @bats );
439  
440 44 subPreamble( PDLEVEL4, '$$', $bn, $sname );
441  
442 44 $block =
443 Paranoid::IO::FileMultiplexer::Block::StreamHeader->new( $file, $bn,
444 $bsize, $sname );
445 44 ▶ $rv = ( defined $block and $block->readSig and $block->readBATs );
Condition66%4/6
defined $block && $block->readSig && $block->readBATs
!ll&&!rl&&r
11033
defined $block and $block->readSig
!ll&&!rl&&r
01133
MC/DC33%1/3
defined $block && $block->readSig && $block->readBATs
defined $block $block->readSig $block->readBATs
Truth table50%2/4
A and B && C
A= defined $blockB= $block->readSigC= $block->readBATs
ABCresult
0XX0
10X0
1100
1111
446  
447 44 ▶ unless ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 11 33 44
448 11 pdebug( 'Stream at bn %s (%s) fails consistency checks',
449 PDLEVEL1, $bn, $sname, $sname, $sname );
450 11 $rv = 0;
451 11 $$self{corrupted} = 1;
452 }
453  
454 44 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 33 11 44
455 33 @bats = $block->bats;
456 33 $i = 0;
457 33 foreach (@bats) {
458 22 ▶ $rv = 0 unless $self->_chkBAT( $_, $sname, $i );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_chkBAT($_, $sname, $i) 0 22 22
459 22 $i++;
460 }
461 }
462  
463 44 subPostamble( PDLEVEL4, '$', $rv );
464  
465 44 return $rv;
466 }
467  
468 22 ▶sub chkConsistency {
Subroutine chkConsistency: called CC=7 SCAR=20.6
Pod documented
469  
470 # Purpose: Checks the file for consistency
471 # Returns: Boolean
472 # Usage: $rv = $obj->chkConsistency;
473  
474 22 my $self = shift;
475 22 my $file = $$self{file};
476 22 my $header = $$self{header};
477 22 my $bsize = $$self{blockSize};
478 22 my $rv = 1;
479 22 my %streams;
480  
481 22 subPreamble(PDLEVEL1);
482  
483 # TODO: There is one major flaw in this consistency check, in that is
484 # TODO: possible to list a header block as a data block in a BAT.
485 # TODO: Writes to said block will obviously introduce consistency errors
486 # TODO: and corruption in the future. Depending on the size of the file,
487 # TODO: however, doing an exhaustive search on all data blocks and making
488 # TODO: sure they're not in use as a header block could be memory
489 # TODO: intensive. We might have to bite the bullet, though.
490 #
491 # Possible solution (which isn't perfect): look for signatures and see if
492 # they load error free. I.e., any block that starts with PIOFM. If we've
493 # already passed the rest of the consistency checks, anything pointing to
494 # what looks like a header block, but doesn't pass consistency checks, we
495 # really don't care about. We might warn if it does pass, though, and
496 # then brute-force check each data block number against a full list of
497 # stream/BAT block numbers.
498  
499 # Apply a read lock for the duration
500 22 ▶ if ( pflock( $file, LOCK_SH ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 1)) { } 22 0 22
501  
502 # Check header
503 22 ▶ if ( $header->readSig && $header->readStreams ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header->readSig and $header->readStreams) { } 22 0 22
Condition33%1/3
$header->readSig and $header->readStreams
!ll&&!rl&&r
0022
MC/DC0%0/2
$header->readSig and $header->readStreams
$header->readSig $header->readStreams
Truth table33%1/3
A and B
A= $header->readSigB= $header->readStreams
ABresult
0X0
100
111
504  
505 # Check streams
506 22 %streams = $header->streams;
507 22 foreach ( sort keys %streams ) {
508 44 ▶ $rv = 0 unless $self->_chkStream( $streams{$_}, $_ );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_chkStream($streams{$_}, $_) 11 33 44
509 }
510  
511 } else {
512 0 pdebug( 'file header failed consistency checks', PDLEVEL1 );
513 0 $$self{corrupted} = 1;
514 0 $rv = 0;
515 }
516  
517 22 pflock( $file, LOCK_UN );
518  
519 } else {
520 0 pdebug( 'failed to get a read lock', PDLEVEL1 );
521 0 $rv = 0;
522 }
523  
524 22 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 11 11 22
525 11 $$self{corrupted} = 0;
526 } else {
527 11 $$self{corrupted} = 1;
528 11 pdebug( 'error - setting corrupted flag to true', PDLEVEL1 );
529 }
530  
531 22 subPostamble( PDLEVEL1, '$', $rv );
532  
533 22 return $rv;
534 }
535  
536 114 ▶sub _addBlock {
Subroutine _addBlock: called CC=4 SCAR=14.8
537  
538 # Purpose: Adds a data block to the file and updates the file header
539 # Returns: Integer (block number of new block)
540 # Usage: $bn = $self->_addBlock;
541  
542 114 my $self = shift;
543 114 my $header = $$self{header};
544 114 my ( $rv, $bn, $data );
545  
546 114 subPreamble(PDLEVEL2);
547  
548 114 $bn = $header->blocks;
549 $data =
550 Paranoid::IO::FileMultiplexer::Block->new( $$self{file}, $bn,
551 114 $$self{blockSize} );
552 114 ▶ $rv = $bn if defined $data and $data->allocate and $header->incrBlocks;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data and $data->allocate and $header->incrBlocks 114 0 114
Condition33%2/6
defined $data and $data->allocate and $header->incrBlocks
!ll&&!rl&&r
00114
defined $data and $data->allocate
!ll&&!rl&&r
00114
MC/DC0%0/3
defined $data and $data->allocate and $header->incrBlocks
defined $data $data->allocate $header->incrBlocks
Truth table25%1/4
A and B and C
A= defined $dataB= $data->allocateC= $header->incrBlocks
ABCresult
0XX0
10X0
1100
1111
553  
554 114 subPostamble( PDLEVEL2, '$', $rv );
555  
556 114 return $rv;
557 }
558  
559 44 ▶sub _addBAT {
Subroutine _addBAT: called CC=6 SCAR=19.7
560  
561 # Purpose: Adds a BAT to the file and updates the file header, and calls
562 # _addBlock
563 # Returns: Integer (block number of new BAT)
564 # Usage: $bn = $self->_addBAT($sname, $seq);
565  
566 44 my $self = shift;
567 44 my $sname = shift;
568 44 my $seq = shift;
569 44 my $header = $$self{header};
570 44 my ( $rv, $bn, $bat );
571  
572 44 subPreamble( PDLEVEL2, '$$', $sname, $seq );
573  
574 44 $bn = $header->blocks;
575 $bat =
576 Paranoid::IO::FileMultiplexer::Block::BATHeader->new( $$self{file},
577 44 $bn, $$self{blockSize}, $sname, $seq );
578 44 ▶ $rv = $bn
Branch: 1 branch
BranchTrueFalseTotal
if defined $bat and $bat->allocate and $bat->writeSig and $header->incrBlocks 44 0 44
Condition33%3/9
defined $bat and $bat->allocate and $bat->writeSig and $header->incrBlocks
!ll&&!rl&&r
0044
defined $bat and $bat->allocate and $bat->writeSig
!ll&&!rl&&r
0044
defined $bat and $bat->allocate
!ll&&!rl&&r
0044
MC/DC0%0/4
defined $bat and $bat->allocate and $bat->writeSig and $header->incrBlocks
defined $bat $bat->allocate $bat->writeSig $header->incrBlocks
Truth table20%1/5
A and B and C and D
A= defined $batB= $bat->allocateC= $bat->writeSigD= $header->incrBlocks
ABCDresult
0XXX0
10XX0
110X0
11100
11111
579 if defined $bat
580 and $bat->allocate
581 and $bat->writeSig
582 and $header->incrBlocks;
583  
584 44 ▶ $bat->addData( $self->_addBlock ) if defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rv 44 0 44
585  
586 44 subPostamble( PDLEVEL2, '$', $rv );
587  
588 44 return $rv;
589 }
590  
591 33 ▶sub _addStream {
Subroutine _addStream: called CC=6 SCAR=19.9
592  
593 # Purpose: Adds a Stream to the file and updates the file header, and calls
594 # _addBAT
595 # Returns: Integer (block number of new stream)
596 # Usage: $bn = $self->_addStream($sname);
597  
598 33 my $self = shift;
599 33 my $sname = shift;
600 33 my $header = $$self{header};
601 33 my ( $rv, $bn, $stream );
602  
603 33 subPreamble( PDLEVEL2, '$', $sname );
604  
605 33 $bn = $header->blocks;
606 $stream =
607 Paranoid::IO::FileMultiplexer::Block::StreamHeader->new( $$self{file},
608 33 $bn, $$self{blockSize}, $sname );
609 33 ▶ $rv = $bn
Branch: 1 branch
BranchTrueFalseTotal
if defined $stream and $stream->allocate and $stream->writeSig and $header->incrBlocks 33 0 33
Condition33%3/9
defined $stream and $stream->allocate and $stream->writeSig and $header->incrBlocks
!ll&&!rl&&r
0033
defined $stream and $stream->allocate and $stream->writeSig
!ll&&!rl&&r
0033
defined $stream and $stream->allocate
!ll&&!rl&&r
0033
MC/DC0%0/4
defined $stream and $stream->allocate and $stream->writeSig and $header->incrBlocks
defined $stream $stream->allocate $stream->writeSig $header->incrBlocks
Truth table20%1/5
A and B and C and D
A= defined $streamB= $stream->allocateC= $stream->writeSigD= $header->incrBlocks
ABCDresult
0XXX0
10XX0
110X0
11100
11111
610 if defined $stream
611 and $stream->allocate
612 and $stream->writeSig
613 and $header->incrBlocks;
614  
615 33 ▶ $stream->addBAT( $self->_addBAT( $sname, 0 ) ) if defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rv 33 0 33
616  
617 33 subPostamble( PDLEVEL2, '$', $rv );
618  
619 33 return $rv;
620 }
621  
622 44 ▶sub addStream {
Subroutine addStream: called CC=7 SCAR=20.7
Pod documented
623  
624 # Purpose: Adds the requested stream
625 # Returns: Boolean
626 # Usage: $rv = $obj->addStream($name);
627  
628 44 my $self = shift;
629 44 my $sname = shift;
630 44 my $file = $$self{file};
631 44 my $header = $$self{header};
632 44 ▶ my $bypass = $$self{readOnly} || $$self{corrupted};
Condition66%2/3
$self->{'readOnly'} || $self->{'corrupted'}
l!l&&r!l&&!r
01133
MC/DC50%1/2
$self->{'readOnly'} || $self->{'corrupted'}
$self->{'readOnly'} $self->{'corrupted'}
Truth table66%2/3
A || B
A= $self->{'readOnly'}B= $self->{'corrupted'}
ABresult
1X1
011
000
633 44 my $rv = 0;
634  
635 44 subPreamble( PDLEVEL1, '$', $sname );
636  
637 44 ▶ unless ($bypass) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bypass) 33 11 44
638  
639 # Get an exclusive lock
640 33 ▶ if ( pflock( $file, LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 2)) { } 33 0 33
641  
642 # Validate file header block count
643 33 $rv = 1;
644 33 ▶ $rv = $self->_reload unless $header->validateBlocks;
Branch: 1 branch
BranchTrueFalseTotal
unless $header->validateBlocks 0 33 33
645  
646 # Add the stream
647 33 ▶ $rv = $header->addStream( $sname, $header->blocks )
Branch: 1 branch
BranchTrueFalseTotal
if $rv 33 0 33
Condition33%2/6
$rv && (($rv = $header->addStream($sname, $header->blocks)) && $self->_addStream($sname))
!ll
0330
$rv = $header->addStream($sname, $header->blocks) and $self->_addStream($sname)
!ll
0330
MC/DC0%0/3
$rv && (($rv = $header->addStream($sname, $header->blocks)) && $self->_addStream($sname))
$rv $rv = $header->addStream($sname, $header->blocks) $self->_addStream($sname)
Truth table0%0/4
A && B and C
A= $rvB= $rv = $header->addStream($sname, $header->blocks)C= $self->_addStream($sname)
ABCresult
0XX0
10X0
1100
1111
648 and $self->_addStream($sname)
649 if $rv;
650  
651 # Release the lock
652 33 pflock( $file, LOCK_UN );
653  
654 } else {
655 0 pdebug( 'failed to get an exclusive lock', PDLEVEL1 );
656 }
657 }
658  
659 44 pOut();
660 44 pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
661  
662 44 return $rv;
663 }
664  
665 1408 ▶sub _calcAddr {
Subroutine _calcAddr: called CC=3 SCAR=11.0
666  
667 # Purpose: Calculates the (BAT, Data, offset) address of the stream
668 # position
669 # Returns: Array (BAT #, Data #, offset)
670 # Usage: @addr = $self->_calcAddr($pos);
671  
672 1408 my $self = shift;
673 1408 my $pos = shift;
674 1408 my $bsize = $$self{blockSize};
675 1408 my ( @rv, $bat, $max );
676  
677 1408 ▶ if ( $pos < $bsize ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $bsize) { } 553 855 1408
678 553 @rv = ( 0, 0, $pos );
679 } else {
680  
681 $bat = Paranoid::IO::FileMultiplexer::Block::BATHeader->new(
682 855 $$self{file}, 0, $bsize );
683 855 ▶ if ( defined $bat ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bat) 855 0 855
684 855 $max = $bat->maxData;
685  
686 855 $rv[ADDR_BAT] = int( $pos / ( $max * $bsize ) );
687 855 $rv[ADDR_BLK] =
688 int( ( $pos - ( $rv[ADDR_BAT] * $max * $bsize ) ) / $bsize );
689 855 $rv[ADDR_OFT] = $pos -
690 ( $rv[ADDR_BAT] * $max * $bsize + $rv[ADDR_BLK] * $bsize );
691  
692 }
693 }
694  
695 1408 return @rv;
696 }
697  
698 1702 ▶sub strmSeek {
Subroutine strmSeek: called CC=10 SCAR=24.4
Pod documented
699  
700 # Purpose: Updates the stream cursor position
701 # Returns: Integer/undef on error
702 # Usage: $rv = $obj->_strmSeek($sname, $pos, $whence);
703  
704 1702 my $self = shift;
705 1702 my $sname = shift;
706 1702 my $pos = shift;
707 1702 my $whence = shift;
708 1702 my $cur = 0;
709 1702 my $rv = 1;
710  
711 1702 subPreamble( PDLEVEL2, '$$$', $sname, $pos, $whence );
712  
713 1702 ▶ $whence = SEEK_SET unless defined $whence;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $whence 0 1702 1702
714 1702 ▶ $pos = 0 unless defined $whence;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $whence 0 1702 1702
715  
716 1702 ▶ if ( $whence == SEEK_SET ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($whence == 0) { } 1180 522 1702
717 1180 $$self{streamPos}{$sname} = $pos;
718 } else {
719 522 ▶ $cur = $$self{streamPos}{$sname} if exists $$self{streamPos}{$sname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"streamPos"}{$sname} 522 0 522
720  
721 522 ▶ if ( $whence == SEEK_CUR ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($whence == 1) { } 0 522 522
elsif ($whence == 2) { } 522 0 522
722 0 $cur += $pos;
723 } elsif ( $whence == SEEK_END ) {
724 522 $cur = $$self{streams}{$sname}->eos + $pos;
725 } else {
726 0 pdebug( 'invalid value for whence in seek (%s)',
727 PDLEVEL1, $whence );
728 0 $rv = undef;
729 }
730 522 $$self{streamPos}{$sname} = $cur;
731 }
732 1702 ▶ $$self{streamPos}{$sname} = 0 if $$self{streamPos}{$sname} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"streamPos"}{$sname} < 0 0 1702 1702
733  
734 1702 ▶ $rv = $$self{streamPos}{$sname} if defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rv 1702 0 1702
735 1702 ▶ $rv = PTRUE_ZERO if $rv == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 526 1176 1702
736  
737 1702 subPostamble( PDLEVEL2, '$', $rv );
738  
739 1702 return $rv;
740 }
741  
742 1156 ▶sub strmTell {
Subroutine strmTell: called CC=2 SCAR=6.9
Pod documented
743  
744 # Purpose: Returns the current stream cursor position
745 # Returns: Integer
746 # Usage: $rv = $obj->_strmTell($sname);
747  
748 1156 my $self = shift;
749 1156 my $sname = shift;
750 1156 my $rv;
751  
752 1156 ▶ $$self{streamPos}{$sname} = 0 unless exists $$self{streamPos}{$sname};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"streamPos"}{$sname} 32 1124 1156
753  
754 1156 return $$self{streamPos}{$sname};
755 }
756  
757 577 ▶sub _growStream {
Subroutine _growStream: called CC=9 SCAR=22.5
758  
759 # Purpose: Grows the stream as needed to accomodate the upcoming write
760 # based on the address of the write's starting position
761 # Returns: Boolean/Integer (bn of last block added)
762 # Usage: $rv = $obj->_growStream($sname, @addr);
763  
764 577 my $self = shift;
765 577 my $sname = shift;
766 577 my @addr = @_;
767 577 my $file = $$self{file};
768 577 my $rv = 1;
769 577 my ( $max, $stream, $bat, @bats, @blocks );
770  
771 577 subPreamble( PDLEVEL3, '$@', $sname, @addr );
772  
773 # Get the stream and list of bats
774 577 $stream = $self->_getStream($sname);
775 577 @bats = $stream->bats;
776  
777 # Start padding BATs
778 577 ▶ while ( $#bats <= $addr[ADDR_BAT] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#bats <= $addr[0]) 577 0 577
779  
780 # Add a BAT
781 577 ▶ if ( $#bats < $addr[ADDR_BAT] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#bats < $addr[0]) 11 566 577
782  
783 # Only add a BAT if we're still below the BAT address
784 11 $rv = $self->_addBAT( $sname, scalar @bats );
785 11 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 11 0 11
786 11 $stream->addBAT($rv);
787 11 @bats = $stream->bats;
788 } else {
789 0 last;
790 }
791 }
792  
793 # Add data blocks as needed
794 577 $bat = $self->_getBAT( $sname, $#bats );
795 577 @blocks = $bat->dataBlocks;
796 577 ▶ while (
Branch: 2 branches
BranchTrueFalseTotal
if ($#bats == $addr[0] ? $#blocks < $addr[1] : !$bat->full) 70 577 647
$#bats == $addr[0] ? : 647 0 647
797 $#bats == $addr[ADDR_BAT]
798 ? $#blocks < $addr[ADDR_BLK]
799 : !$bat->full
800 ) {
801  
802 70 $rv = $self->_addBlock;
803 70 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 70 0 70
804 70 $bat->addData($rv);
805 70 @blocks = $bat->dataBlocks;
806 } else {
807 0 last;
808 }
809 }
810  
811 577 ▶ last if $#bats == $addr[ADDR_BAT];
Branch: 1 branch
BranchTrueFalseTotal
if $#bats == $addr[0] 577 0 577
812 }
813  
814 577 ▶ pdebug( 'failed to grow the stream (%s)', PDLEVEL1, $sname ) unless $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 577 577
815  
816 577 subPostamble( PDLEVEL3, '$', $rv );
817  
818 577 return $rv;
819 }
820  
821 555 ▶sub _strmWrite {
Subroutine _strmWrite: called CC=15 SCAR=28.8
822  
823 # Purpose: Writes to the specified stream
824 # Returns: Integer/undef (bytes written/error)
825 # Usage: $bytes = $obj->_strmWrite($sname, $content);
826  
827 555 my $self = shift;
828 555 my $sname = shift;
829 555 my $content = shift;
830 555 my $file = $$self{file};
831 555 my $bsize = $$self{blockSize};
832 555 my ( $rv, $stream, $bat, $block, $pos );
833 555 my ( @addr, @blocks, $bn, $blkLeft, $offset, $clength, $chunk, $bw );
834  
835 555 subPreamble( PDLEVEL1, '$$', $sname, $content );
836  
837 555 ▶ if ( pflock( $file, LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 2)) 555 0 555
838  
839 555 $stream = $self->_getStream($sname);
840 555 ▶ if ( defined $stream and defined $content and length $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stream and defined $content and length $content) 555 0 555
Condition33%2/6
defined $stream and defined $content and length $content
!ll&&!rl&&r
00555
defined $stream and defined $content
!ll&&!rl&&r
00555
MC/DC0%0/3
defined $stream and defined $content and length $content
defined $stream defined $content length $content
Truth table25%1/4
A and B and C
A= defined $streamB= defined $contentC= length $content
ABCresult
0XX0
10X0
1100
1111
841  
842 # Get the current position
843 555 $pos = $self->strmTell($sname);
844  
845 # Get the address
846 555 @addr = $self->_calcAddr( $pos + length $content );
847  
848 # Allocate blocks as needed
849 555 ▶ if ( $self->_growStream( $sname, @addr ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_growStream($sname, @addr)) 555 0 555
850 555 @addr = $self->_calcAddr($pos);
851  
852 # Get the specified BAT and data block
853 555 $bat = $self->_getBAT( $sname, $addr[ADDR_BAT] );
854 555 @blocks = $bat->dataBlocks;
855  
856 # Get the specified block
857 555 $block =
858 Paranoid::IO::FileMultiplexer::Block->new( $file,
859 $blocks[ $addr[ADDR_BLK] ], $bsize );
860  
861 555 ▶ if ( defined $bat and defined $block ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bat and defined $block) 555 0 555
Condition33%1/3
defined $bat and defined $block
!ll&&!rl&&r
00555
MC/DC0%0/2
defined $bat and defined $block
defined $bat defined $block
Truth table33%1/3
A and B
A= defined $batB= defined $block
ABresult
0X0
100
111
862  
863 # Start writing
864 555 $offset = $rv = 0;
865 555 ▶ while ( $rv < length $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv < length $content) 625 555 1180
866  
867 # We need to know how much room is left in the block
868 625 $blkLeft = $bsize - $addr[ADDR_OFT];
869  
870 # We need to know if the remaining content will fit in
871 # that block
872 625 $clength = length($content) - $offset;
873 625 ▶ $chunk = $clength <= $blkLeft ? $clength : $blkLeft;
Branch: 1 branch
BranchTrueFalseTotal
$clength <= $blkLeft ? : 555 70 625
874  
875 # Write the chunk
876 625 $bw =
877 $block->bwrite( $content, $addr[ADDR_OFT], $chunk,
878 $offset );
879 625 $rv += $bw;
880 625 $offset += $bw;
881 625 $pos += $bw;
882  
883 # Exit if we couldn't write the full chunk
884 625 ▶ unless ( $bw == $chunk ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bw == $chunk) 0 625 625
885 0 pdebug(
886 'failed to write entire contents: %s bytes',
887 PDLEVEL1, $rv );
888 0 last;
889 }
890  
891 # Get the next block if we have bytes left
892 625 ▶ if ( $rv < length $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv < length $content) 70 555 625
893 70 @addr = $self->_calcAddr($pos);
894 70 ▶ unless ( $bat->sequence == $addr[ADDR_BAT] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bat->sequence == $addr[0]) 0 70 70
895 0 $bat =
896 $self->_getBAT( $sname, $addr[ADDR_BAT] );
897 0 @blocks = $bat->dataBlocks;
898 }
899  
900 # Get the specified block
901 $block =
902 70 Paranoid::IO::FileMultiplexer::Block->new(
903 $file, $blocks[ $addr[ADDR_BLK] ], $bsize );
904 }
905 }
906 }
907  
908 # Update stream position and EOS
909 555 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 555 0 555
910 555 $self->strmSeek( $sname, $pos, SEEK_SET );
911 555 ▶ $stream->writeEOS($pos) if $stream->eos < $pos;
Branch: 1 branch
BranchTrueFalseTotal
if $stream->eos < $pos 544 11 555
912 }
913  
914 }
915  
916 }
917 555 pflock( $file, LOCK_UN );
918 }
919  
920 555 subPostamble( PDLEVEL1, '$', $rv );
921  
922 555 return $rv;
923 }
924  
925 555 ▶sub strmWrite {
Subroutine strmWrite: called CC=4 SCAR=15.2
Pod documented
926  
927 # Purpose: Calls _strmWrite after making sure the file can be written to
928 # Returns: Integer/undef
929 # Usage: $bw = $obj->strmWrite($sname, $content);
930  
931 555 my $self = shift;
932 555 my @args = @_;
933 555 ▶ my $bypass = $$self{readOnly} || $$self{corrupted};
Condition33%1/3
$self->{'readOnly'} || $self->{'corrupted'}
l!l&&r!l&&!r
00555
MC/DC0%0/2
$self->{'readOnly'} || $self->{'corrupted'}
$self->{'readOnly'} $self->{'corrupted'}
Truth table33%1/3
A || B
A= $self->{'readOnly'}B= $self->{'corrupted'}
ABresult
1X1
011
000
934  
935 555 ▶ pdebug( 'can\'t write to files that are corrupted or read-only',
Branch: 1 branch
BranchTrueFalseTotal
if $bypass 0 555 555
936 PDLEVEL1 )
937 if $bypass;
938  
939 555 ▶ return $bypass ? undef : $self->_strmWrite(@args);
Branch: 1 branch
BranchTrueFalseTotal
$bypass ? : 0 555 555
940 }
941  
942 68 ▶sub _strmRead {
Subroutine _strmRead: called CC=15 SCAR=29.1
943  
944 # Purpose: Reads from the specified stream
945 # Returns: Integer/undef (bytes read/error)
946 # Usage: $bytes = $obj->_strmRead($sname, $content, $bytes);
947  
948 68 my $self = shift;
949 68 my $sname = shift;
950 68 my $cref = shift;
951 68 ▶ my $btr = shift || 0;
Condition50%1/2
shift() || 0
l!l
680
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
952 68 my $file = $$self{file};
953 68 my $bsize = $$self{blockSize};
954 68 my $rv = 0;
955 68 my ( $stream, $pos, $eos, @addr, $content );
956 68 my ( $bat, @blocks, $block, $ctr, $br, $offset );
957  
958 68 subPreamble( PDLEVEL1, '$$$', $sname, $cref, $btr );
959  
960 68 ▶ if ( pflock( $file, LOCK_SH ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 1)) 68 0 68
961  
962 68 $stream = $self->_getStream($sname);
963 68 ▶ if ( defined $stream and defined $cref and ref $cref eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stream and defined $cref and ref $cref eq 'SCALAR') { } 68 0 68
Condition33%2/6
defined $stream and defined $cref and ref $cref eq "SCALAR"
!ll&&!rl&&r
0068
defined $stream and defined $cref
!ll&&!rl&&r
0068
MC/DC0%0/3
defined $stream and defined $cref and ref $cref eq "SCALAR"
defined $stream defined $cref ref $cref eq "SCALAR"
Truth table25%1/4
A and B and C
A= defined $streamB= defined $crefC= ref $cref eq "SCALAR"
ABCresult
0XX0
10X0
1100
1111
964  
965 # Get the current position
966 68 $pos = $self->strmTell($sname);
967  
968 # Get the address
969 68 @addr = $self->_calcAddr($pos);
970  
971 # Get the End Of Stream position
972 68 $eos = $stream->eos;
973  
974 # Start reading
975 68 $$cref = '';
976 68 ▶ while ( $pos < $eos and $rv < $btr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $eos and $rv < $btr) 83 68 151
Condition100%3/3
$pos < $eos and $rv < $btr
!ll&&!rl&&r
462283
MC/DC100%2/2
$pos < $eos and $rv < $btr
$pos < $eos $rv < $btr
Truth table100%3/3
A and B
A= $pos < $eosB= $rv < $btr
ABresult
0X0
100
111
977  
978 # Get the specified BAT
979 83 $bat = $self->_getBAT( $sname, $addr[ADDR_BAT] );
980 83 ▶ if ( defined $bat ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bat) 83 0 83
981  
982 # Get the specified data block
983 83 @blocks = $bat->dataBlocks;
984 83 $block =
985 Paranoid::IO::FileMultiplexer::Block->new( $file,
986 $blocks[ $addr[ADDR_BLK] ], $bsize );
987 83 ▶ if ( defined $block ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $block) 83 0 83
988  
989 # Take and early out if pos equals eos
990 83 ▶ last unless $pos < $eos;
Branch: 1 branch
BranchTrueFalseTotal
unless $pos < $eos 0 83 83
991  
992 # Figure out how much of the block we have left to
993 # read
994 83 $ctr = $bsize - $addr[ADDR_OFT];
995  
996 # Reduce it if the read finishes in this block
997 83 ▶ $ctr = $btr - $rv if $ctr > $btr - $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $ctr > $btr - $rv 55 28 83
998  
999 # Reduce it further if EOS is even closer
1000 83 ▶ $ctr = $eos - $pos if $ctr > $eos - $pos;
Branch: 1 branch
BranchTrueFalseTotal
if $ctr > $eos - $pos 24 59 83
1001  
1002 # Read the chunk
1003 83 $br =
1004 $block->bread( \$content, $addr[ADDR_OFT], $ctr );
1005 83 $rv += $br;
1006 83 $pos += $br;
1007 83 @addr = $self->_calcAddr($pos);
1008 83 $$cref .= $content;
1009  
1010 83 ▶ unless ( $br == $ctr ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($br == $ctr) 0 83 83
1011 0 pdebug(
1012 'failed to read entire chunk: %s/%s bytes',
1013 PDLEVEL1, $br, $ctr );
1014 0 last;
1015 }
1016  
1017 }
1018 }
1019 }
1020  
1021 # Update stream pointer
1022 68 $self->strmSeek( $sname, $pos, SEEK_SET );
1023  
1024 } else {
1025 0 ▶ if ( defined $stream ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stream) 0 0 0
1026 0 pdebug( 'invalid value passed for the content reference: %s',
1027 PDLEVEL1, $cref );
1028 0 $rv = undef;
1029 }
1030 }
1031  
1032 68 pflock( $file, LOCK_UN );
1033 }
1034  
1035 68 subPostamble( PDLEVEL1, '$', $rv );
1036  
1037 68 return $rv;
1038 }
1039  
1040 68 ▶sub strmRead {
Subroutine strmRead: called CC=3 SCAR=11.3
Pod documented
1041  
1042 # Purpose: Calls _strmRead after making sure the file can be read from
1043 # Returns: Integer/undef
1044 # Usage: $br = $obj->strmRead($stream, \$content, $bytes);
1045  
1046 68 my $self = shift;
1047 68 my @args = @_;
1048 68 my $bypass = $$self{corrupted};
1049  
1050 68 ▶ pdebug( 'can\'t read from files that are corrupted', PDLEVEL1 )
Branch: 1 branch
BranchTrueFalseTotal
if $bypass 0 68 68
1051 if $bypass;
1052  
1053 68 ▶ return $bypass ? undef : $self->_strmRead(@args);
Branch: 1 branch
BranchTrueFalseTotal
$bypass ? : 0 68 68
1054 }
1055  
1056 511 ▶sub strmAppend {
Subroutine strmAppend: called CC=4 SCAR=14.0
Pod documented
1057  
1058 # Purpose: Seeks to the end of the stream and writes new content there
1059 # Returns: Integer/undef (bytes written/error)
1060 # Usage: $bytes = $obj->_strmAppend($sname, $content);
1061  
1062 511 my $self = shift;
1063 511 my $sname = shift;
1064 511 my $content = shift;
1065 511 my $file = $$self{file};
1066 511 my ( $rv, $stream, $pos );
1067  
1068 511 subPreamble( PDLEVEL1, '$$', $sname, $content );
1069  
1070 511 ▶ if ( pflock( $file, LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 2)) 511 0 511
1071 511 $stream = $self->_getStream($sname);
1072 511 ▶ if ( defined $stream ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stream) 511 0 511
1073 511 $pos = $self->strmTell($sname);
1074 511 ▶ if ( $self->strmSeek( $sname, 0, SEEK_END ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->strmSeek($sname, 0, 2)) 511 0 511
1075 511 $rv = $self->strmWrite( $sname, $content );
1076 511 $self->strmSeek( $sname, $pos, SEEK_SET );
1077 }
1078 }
1079 }
1080  
1081 511 subPostamble( PDLEVEL1, '$', $rv );
1082  
1083 511 return $rv;
1084 }
1085  
1086 11 ▶sub _strmTruncate {
Subroutine _strmTruncate: called CC=6 SCAR=18.6
1087  
1088 # Purpose: Truncates the stream to the specified length. This will zero
1089 # out any data written past the new EOS.
1090 # Returns: Boolean
1091 # Usage: $rv = $obj->_strmTruncate($sname, $neos);
1092  
1093 11 my $self = shift;
1094 11 my $sname = shift;
1095 11 my $neos = shift;
1096 11 my $file = $$self{file};
1097 11 my ( $rv, $stream, $eos, $zeroes, $zl );
1098  
1099 11 subPreamble( PDLEVEL1, '$$', $sname, $neos );
1100  
1101 11 ▶ if ( pflock( $file, LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($file, 2)) 11 0 11
1102 11 $stream = $self->_getStream($sname);
1103 11 ▶ if ( defined $stream ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stream) 11 0 11
1104 11 $eos = $stream->eos;
1105  
1106 11 ▶ if ( $neos < $eos ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($neos < $eos) 11 0 11
1107  
1108 # Zero out old data beyond the new EOS
1109 11 $zl = $eos - $neos;
1110 11 $zeroes = pack "x$zl";
1111 11 ▶ $rv =
Branch: 1 branch
BranchTrueFalseTotal
$rv = $self->strmSeek($sname, $neos, 0) and $self->strmWrite($sname, $zeroes) and $stream->writeEOS($neos) 11 0 11
Condition33%1/3
$rv = $self->strmSeek($sname, $neos, 0) and $self->strmWrite($sname, $zeroes)
!ll&&!rl&&r
0011
MC/DC0%0/2
$rv = $self->strmSeek($sname, $neos, 0) and $self->strmWrite($sname, $zeroes)
$rv = $self->strmSeek($sname, $neos, 0) $self->strmWrite($sname, $zeroes)
Truth table33%1/3
A and B
A= $rv = $self->strmSeek($sname, $neos, 0)B= $self->strmWrite($sname, $zeroes)
ABresult
0X0
100
111
1112 $self->strmSeek( $sname, $neos, SEEK_SET )
1113 and $self->strmWrite( $sname, $zeroes )
1114 and $stream->writeEOS($neos);
1115 }
1116 }
1117 }
1118  
1119 11 subPostamble( PDLEVEL1, '$', $rv );
1120  
1121 11 return $rv;
1122 }
1123  
1124 11 ▶sub strmTruncate {
Subroutine strmTruncate: called CC=4 SCAR=15.2
Pod documented
1125  
1126 # Purpose: Calls _strmTruncate after making sure the file can be written to
1127 # Returns: Integer/undef
1128 # Usage: $bw = $obj->strmTruncate($sname, $neos);
1129  
1130 11 my $self = shift;
1131 11 my @args = @_;
1132 11 ▶ my $bypass = $$self{readOnly} || $$self{corrupted};
Condition33%1/3
$self->{'readOnly'} || $self->{'corrupted'}
l!l&&r!l&&!r
0011
MC/DC0%0/2
$self->{'readOnly'} || $self->{'corrupted'}
$self->{'readOnly'} $self->{'corrupted'}
Truth table33%1/3
A || B
A= $self->{'readOnly'}B= $self->{'corrupted'}
ABresult
1X1
011
000
1133  
1134 11 ▶ pdebug( 'can\'t write to files that are corrupted or read-only',
Branch: 1 branch
BranchTrueFalseTotal
if $bypass 0 11 11
1135 PDLEVEL1 )
1136 if $bypass;
1137  
1138 11 ▶ return $bypass ? undef : $self->_strmTruncate(@args);
Branch: 1 branch
BranchTrueFalseTotal
$bypass ? : 0 11 11
1139 }
1140  
1141 154 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
1142  
1143 154 my $self = shift;
1144  
1145 pclose( $$self{file} )
1146 154 ▶ if defined $$self{file} and length $$self{file};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"file"} and length $self->{"file"} 132 22 154
Condition100%3/3
defined $self->{"file"} and length $self->{"file"}
!ll&&!rl&&r
1111132
MC/DC100%2/2
defined $self->{"file"} and length $self->{"file"}
defined $self->{"file"} length $self->{"file"}
Truth table100%3/3
A and B
A= defined $self->{"file"}B= length $self->{"file"}
ABresult
0X0
100
111
1147  
1148 154 return 1;
1149 }
1150  
1151 12 1;
1152  
1153 __END__
« Paranoid/IO/Line.pm ↑ Summary Paranoid/Network/IPv6.pm »