Paranoid/IO/FileMultiplexer/Block.pm

filter: Statementstmt 88.1% 119 / 135 Branchbran 77.0% 37 / 48 Conditioncond 71.4% 15 / 21 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 85.0% 17 / 20 Podpod 100% 9 / 9 totaltotal 82.7% 202 / 244 CC 32 SCAR 20.7
CC
32
Cov
84%
bwrite
12.6
new
12.0
bread
10.5
CRAP 7.9
« Paranoid/IO/FileMultiplexer/Block/FileHeader.pm ↑ Summary Paranoid/Network.pm »
1 # Paranoid::IO::FileMultiplexer::Block -- Block-level Allocator/Accessor
2 #
3 # $Id: lib/Paranoid/IO/FileMultiplexer/Block.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;
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 Fcntl qw(:DEFAULT :flock :mode :seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 12 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
46  
47 12 ▶use constant MINBSIZE => 4_096;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 12 ▶use constant MAXBSIZE => 1_048_576;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 #####################################################################
51 #
52 # Module code follows
53 #
54 #####################################################################
55  
56 3690 ▶sub new {
Subroutine new: called CC=12 SCAR=24.9
Pod documented
57  
58 # Purpose: Creates a block object
59 # Returns: Object reference or undef
60 # Usage: $obj = Paranoid::IO::FileMultiplexer::Block->new(
61 # $filename, $bnum, $bsize);
62  
63 3690 my $class = shift;
64 3690 my $file = shift;
65 3690 my $bnum = shift;
66 3690 my $bsize = shift;
67 3690 my $self = {
68 file => $file,
69 blockNum => 0,
70 blockSize => MINBSIZE,
71 minPos => 0,
72 maxPos => MINBSIZE - 1,
73 };
74  
75 3690 subPreamble( PDLEVEL3, '$$$', $file, $bnum, $bsize );
76  
77 3690 bless $self, $class;
78  
79 # Check mandatory values
80 3690 ▶ $self = undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bnum and defined $bsize and defined $file and length $file 22 3668 3690
Condition77%7/9
defined $bnum and defined $bsize and defined $file and length $file
!ll&&!rl&&r
2203668
defined $bnum and defined $bsize and defined $file
!ll&&!rl&&r
2203668
defined $bnum and defined $bsize
!ll&&!rl&&r
11113668
MC/DC50%2/4
defined $bnum and defined $bsize and defined $file and length $file
defined $bnum defined $bsize defined $file length $file
Truth table60%3/5
A and B and C and D
A= defined $bnumB= defined $bsizeC= defined $fileD= length $file
ABCDresult
0XXX0
10XX0
110X0
11100
11111
81 unless defined $bnum
82 and defined $bsize
83 and defined $file
84 and length $file;
85 3690 ▶ pdebug( 'invalid or missing arguments', PDLEVEL1 )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 22 3668 3690
86 unless defined $self;
87  
88 3690 ▶ if ( defined $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self) 3668 22 3690
89  
90 # Make sure we only have positive values for the block number and size
91 3668 $$self{blockNum} = int $bnum;
92 3668 ▶ $$self{blockSize} = int $bsize if defined $bsize;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bsize 3668 0 3668
93 3668 $$self{minPos} = $$self{blockNum} * $$self{blockSize};
94 3668 $$self{maxPos} = $$self{minPos} + $$self{blockSize} - 1;
95  
96 # Make sure block size is in range and a multiple of MINBSIZE
97 $self = undef
98 unless $$self{blockSize} >= MINBSIZE
99 and $$self{blockSize} <= MAXBSIZE
100 3668 ▶ and $$self{blockSize} % MINBSIZE == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"blockSize"} >= 4096 and $self->{"blockSize"} <= 1048576 and $self->{"blockSize"} % 4096 == 0 99 3569 3668
Condition100%6/6
$self->{"blockSize"} >= 4096 and $self->{"blockSize"} <= 1048576 and $self->{"blockSize"} % 4096 == 0
!ll&&!rl&&r
44553569
$self->{"blockSize"} >= 4096 and $self->{"blockSize"} <= 1048576
!ll&&!rl&&r
22223624
MC/DC100%3/3
$self->{"blockSize"} >= 4096 and $self->{"blockSize"} <= 1048576 and $self->{"blockSize"} % 4096 == 0
$self->{"blockSize"} >= 4096 $self->{"blockSize"} <= 1048576 $self->{"blockSize"} % 4096 == 0
Truth table100%4/4
A and B and C
A= $self->{"blockSize"} >= 4096B= $self->{"blockSize"} <= 1048576C= $self->{"blockSize"} % 4096 == 0
ABCresult
0XX0
10X0
1100
1111
101 3668 ▶ pdebug( 'invalid block size', PDLEVEL1 ) unless defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 99 3569 3668
102 }
103  
104 3690 subPostamble( PDLEVEL3, '$', $self );
105  
106 3690 return $self;
107 }
108  
109 86 ▶sub recalibrate {
Subroutine recalibrate: called CC=1 SCAR=0.0
Pod documented
110  
111 # Purpose: Recalibrates min/max positions in the block based
112 # on block size.
113 # Returns: Boolean
114 # Usage: $rv = $obj->recalibrate;
115  
116 86 my $self = shift;
117  
118 86 $$self{minPos} = $$self{blockNum} * $$self{blockSize};
119 86 $$self{maxPos} = $$self{minPos} + $$self{blockSize} - 1;
120  
121 86 return 1;
122 }
123  
124 0 ▶sub blockNum {
Subroutine blockNum: not called CC=1 SCAR=6.9
Pod documented
125  
126 # Purpose: Returns the block number
127 # Returns: Integer
128 # Usage: $bn = $obj->blockNum;
129  
130 0 my $self = shift;
131 0 return $$self{blockNum};
132 }
133  
134 166 ▶sub blockSize {
Subroutine blockSize: called CC=1 SCAR=0.0
Pod documented
135  
136 # Purpose: Returns the block size
137 # Returns: Integer
138 # Usage: $bs = $obj->blockSize;
139  
140 166 my $self = shift;
141 166 return $$self{blockSize};
142 }
143  
144 0 ▶sub minPos {
Subroutine minPos: not called CC=1 SCAR=6.9
Pod documented
145  
146 # Purpose: Returns the min writable file position for the block
147 # Returns: Integer
148 # Usage: $minp = $obj->minPos;
149  
150 0 my $self = shift;
151 0 return $$self{minPos};
152 }
153  
154 0 ▶sub maxPos {
Subroutine maxPos: not called CC=1 SCAR=6.9
Pod documented
155  
156 # Purpose: Returns the max writable file position for the block
157 # Returns: Integer
158 # Usage: $maxp = $obj->maxPos;
159  
160 0 my $self = shift;
161 0 return $$self{maxPos};
162 }
163  
164 345 ▶sub allocate {
Subroutine allocate: called CC=3 SCAR=11.0
Pod documented
165  
166 # Purpose: Writes a new block to disk
167 # Returns: Boolean
168 # Usage: $rv = $obj->allocate;
169  
170 345 my $self = shift;
171 345 my $file = $$self{file};
172 345 my $minPos = $$self{minPos};
173 345 my $maxPos = $$self{maxPos};
174 345 my $rv = 0;
175  
176 345 subPreamble(PDLEVEL3);
177  
178 345 ▶ if ( pflock( $$self{file}, LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pflock($self->{"file"}, 2)) 345 0 345
179  
180 # Seek and write a null byte at the end of the block
181 345 pdebug( 'end of file should be at %s', PDLEVEL4, $minPos );
182 345 pseek( $file, 0, SEEK_END );
183 345 ▶ if ( ptell($file) == $minPos ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ptell($file) == $minPos) { } 323 22 345
184 323 pseek( $file, $maxPos, SEEK_SET );
185 323 $rv = pwrite( $file, pack 'x' );
186 } else {
187 22 pdebug('block already allocated');
188 }
189  
190 345 pflock( $$self{file}, LOCK_UN );
191 }
192  
193 345 subPostamble( PDLEVEL3, '$', $rv );
194  
195 345 return $rv;
196 }
197  
198 8957 ▶sub bread {
Subroutine bread: called CC=9 SCAR=23.5
Pod documented
199  
200 # Purpose: Reads the contents of the entire block. or a specified range
201 # Returns: Integer (bytes read) or undef on error
202 # Usage: $bytesRead = $obj->bread(\$content);
203 # Usage: $bytesRead = $obj->bread(\$content, $start);
204 # Usage: $bytesRead = $obj->bread(\$content, undef, $bytes);
205 # Usage: $bytesRead = $obj->bread(\$content, $start, $bytes);
206  
207 8957 my $self = shift;
208 8957 my $cref = shift;
209 8957 my $start = shift;
210 8957 my $bytes = shift;
211 8957 my $file = $$self{file};
212 8957 my $bsize = $$self{blockSize};
213 8957 my $minp = $$self{minPos};
214 8957 my $maxp = $$self{maxPos};
215 8957 my $rv = PTRUE_ZERO;
216  
217 8957 subPreamble( PDLEVEL3, '$;$$', $cref, $start, $bytes );
218  
219 # NOTE: This method intentionally allows reads of a length greater than
220 # the block size, but it will only return content from within the block
221 # boundaries.
222  
223 # Error out if we were not given a valid scalar ref
224 8957 ▶ unless ( defined $cref and ref($cref) eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cref and ref $cref eq "SCALAR") 0 8957 8957
Condition33%1/3
defined $cref and ref $cref eq "SCALAR"
!ll&&!rl&&r
008957
MC/DC0%0/2
defined $cref and ref $cref eq "SCALAR"
defined $cref ref $cref eq "SCALAR"
Truth table33%1/3
A and B
A= defined $crefB= ref $cref eq "SCALAR"
ABresult
0X0
100
111
225 0 $rv = undef;
226 0 pdebug( 'invalid argument for content ref', PDLEVEL1 );
227 }
228  
229 # Set start to beginning of block if not specified
230 8957 ▶ $start = 0 unless defined $start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start 33 8924 8957
231  
232 # Set default bytes if not specified
233 8957 ▶ $bytes = $bsize - $start unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 1390 7567 8957
234  
235 # Make sure start is in range
236 8957 ▶ if ( $minp + $start > $maxp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minp + $start > $maxp) 0 8957 8957
237 0 pdebug( 'starting position is out of range', PDLEVEL1 );
238 0 $rv = undef;
239 }
240  
241 8957 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 8957 0 8957
242  
243 # Make sure we limit read to our block
244 8957 ▶ $bytes = ( $maxp + 1 ) - ( $minp + $start )
Branch: 1 branch
BranchTrueFalseTotal
if $minp + $start + $bytes > $maxp + 1 11 8946 8957
245 if ( $minp + $start + $bytes ) > ( $maxp + 1 );
246  
247 # Perform the read
248 8957 ▶ if ( pseek( $file, $minp + $start, SEEK_SET ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pseek($file, $minp + $start, 0)) { } 8957 0 8957
249 8957 $rv = pread( $file, $$cref, $bytes );
250 } else {
251 0 $rv = undef;
252 }
253 }
254  
255 8957 subPostamble( PDLEVEL3, '$', $rv );
256  
257 8957 return $rv;
258 }
259  
260 1925 ▶sub bwrite {
Subroutine bwrite: called CC=11 SCAR=25.3
Pod documented
261  
262 # Purpose: Writes the contents of the entire block. or a specified range
263 # Returns: Integer (bytes written) or undef on error
264 # Usage: $bytesWritten = $obj->bwrite($content);
265 # Usage: $bytesWritten = $obj->bwrite($content, $start );
266 # Usage: $bytesWritten = $obj->bwrite($content, $start, $length );
267 # Usage: $bytesWritten = $obj->bwrite($content, $start, $length, $offset );
268  
269 1925 my $self = shift;
270 1925 my $content = shift;
271 1925 my $start = shift;
272 1925 my $length = shift;
273 1925 my $offset = shift;
274 1925 my $file = $$self{file};
275 1925 my $bsize = $$self{blockSize};
276 1925 my $minp = $$self{minPos};
277 1925 my $maxp = $$self{maxPos};
278 1925 my $rv = PTRUE_ZERO;
279 1925 ▶ my $cdata = defined $content ? ( length $content ) . ' bytes' : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 1925 0 1925
280 1925 my $blkLeft;
281  
282 1925 subPreamble( PDLEVEL3, '$;$$$', $cdata, $start, $length, $offset );
283  
284 # NOTE: This method intentionally allows writes of a length greater than
285 # the block size, but it will only write content from within the block
286 # boundaries.
287  
288 # Error out if we were not given a valid scalar ref
289 1925 ▶ unless ( defined $content and length $content ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content and length $content) 0 1925 1925
Condition33%1/3
defined $content and length $content
!ll&&!rl&&r
001925
MC/DC0%0/2
defined $content and length $content
defined $content length $content
Truth table33%1/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
290 0 $rv = undef;
291 0 pdebug( 'invalid argument for content', PDLEVEL1 );
292 }
293  
294 # Set start to beginning of block if not specified
295 1925 ▶ $start = 0 unless defined $start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start 220 1705 1925
296  
297 # Set offset to zero if not specified
298 1925 ▶ $offset = 0 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 1300 625 1925
299  
300 # Set length to max content length available if not defined
301 1925 ▶ $length = length($content) - $offset unless defined $length;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length 1300 625 1925
302 1925 $blkLeft = $bsize - $start;
303 1925 ▶ $length = $blkLeft if $blkLeft < $length;
Branch: 1 branch
BranchTrueFalseTotal
if $blkLeft < $length 11 1914 1925
304  
305 # Make sure start is in range
306 1925 ▶ if ( $minp + $start > $maxp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minp + $start > $maxp) 0 1925 1925
307 0 pdebug( 'starting position is out of range', PDLEVEL1 );
308 0 $rv = undef;
309 }
310  
311 1925 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 1925 0 1925
312  
313 # Perform the write
314 1925 ▶ if ( pseek( $file, $minp + $start, SEEK_SET ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pseek($file, $minp + $start, 0)) { } 1925 0 1925
315 1925 $rv = pwrite( $file, $content, $length, $offset );
316 } else {
317 0 $rv = undef;
318 }
319 }
320  
321 1925 subPostamble( PDLEVEL3, '$', $rv );
322  
323 1925 return $rv;
324 }
325  
326 12 1;
327  
328 __END__
« Paranoid/IO/FileMultiplexer/Block/FileHeader.pm ↑ Summary Paranoid/Network.pm »