Paranoid/IO.pm

filter: Statementstmt 88.8% 342 / 385 Branchbran 66.3% 142 / 214 Conditioncond 53.3% 32 / 60 MC/DCmcdc 26.3% 10 / 38 Subroutinesub 90.2% 37 / 41 Podpod 100% 18 / 18 totaltotal 76.8% 581 / 756 CC 131 SCAR 25.0
CC
131
Cov
78%
plockstat
30.0
_plsflock
24.3
preopen
20.0
CRAP 12.2
« Paranoid/Data/AVLTree.pm ↑ Summary Paranoid/IO/Line.pm »
1 # Paranoid::IO -- Paranoid IO support
2 #
3 # $Id: lib/Paranoid/IO.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;
33  
34 58 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 58 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 58 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 58 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 58 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 58 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 58 ▶use Fcntl qw(:DEFAULT :flock :mode :seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 58 ▶use Paranoid;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 58 ▶use Paranoid::Debug qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 58 ▶use Paranoid::Input;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 58 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 58 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
48  
49 58 @EXPORT = qw(pclose pcloseAll popen preopen ptell pseek pflock pread
50 pnlread pwrite pnlwrite pappend pnlappend ptruncate pnltruncate);
51 58 @EXPORT_OK = ( @EXPORT, qw(PIOBLKSIZE PIOMAXFSIZE PIOLOCKSTACK) );
52 58 %EXPORT_TAGS = ( all => [@EXPORT_OK] );
53  
54 58 ▶use constant PDEFPERM => 0666;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 58 ▶use constant PDEFMODE => O_CREAT | O_RDWR;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 58 ▶use constant PDEFBLKSZ => 4096;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 58 ▶use constant PDEFFILESZ => 65536;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 58 ▶use constant PFLMASK => LOCK_SH | LOCK_EX | LOCK_UN;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 58 ▶use constant PIGNMFLAGS => O_TRUNC | O_CREAT | O_EXCL;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 #####################################################################
62 #
63 # Module code follows
64 #
65 #####################################################################
66  
67 {
68  
69 58 my $mblksz = PDEFBLKSZ;
70  
71 840 ▶ sub PIOBLKSIZE : lvalue {
Subroutine PIOBLKSIZE: called CC=1 SCAR=0.0
Pod documented
72  
73 # Purpose: Gets/sets default block size for I/O
74 # Returns: $mblksz
75 # Usage: PIOBLKSIZE = $bytes;
76  
77 840 $mblksz;
78 }
79  
80 58 my $mfsz = PDEFFILESZ;
81  
82 518 ▶ sub PIOMAXFSIZE : lvalue {
Subroutine PIOMAXFSIZE: called CC=1 SCAR=0.0
Pod documented
83  
84 # Purpose: Gets/sets default max file size for I/O
85 # Returns: $mfsz
86 # Usage: PIOMAXFSIZE = bytes;
87  
88 518 $mfsz;
89 }
90  
91 58 my %lstack;
92 58 my $lsflag = 0;
93  
94 32886 ▶ sub PIOLOCKSTACK : lvalue {
Subroutine PIOLOCKSTACK: called CC=1 SCAR=0.0
Pod documented
95  
96 # Purpose: Enables/disables the flock lock stack
97 # Returns: $lsflag
98 # Usage: PIOLOCKSTACK = 1;
99  
100 32886 $lsflag;
101 }
102  
103 # %files: {name} => {
104 # pid => $pid,
105 # mode => $mode,
106 # perms => $perms,
107 # fh => $fh,
108 # real => $realpath,
109 # ltype => $lock,
110 # }
111 58 my %files;
112  
113 246 ▶ sub _pfFhind ($) {
Subroutine _pfFhind: called CC=6 SCAR=19.1
114  
115 # Purpose: Searches for a filename based on the
116 # current file handle
117 # Returns: String/undefined
118 # Usage: $rv = _pfFhind($fh);
119  
120 246 my $fh = shift;
121 246 my $rv;
122  
123 246 subPreamble( PDLEVEL4, '$', $fh );
124  
125 246 ▶ if ( defined $fh and ref $fh eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh and ref $fh eq "GLOB") 246 0 246
Condition33%1/3
defined $fh and ref $fh eq "GLOB"
!ll&&!rl&&r
00246
MC/DC0%0/2
defined $fh and ref $fh eq "GLOB"
defined $fh ref $fh eq "GLOB"
Truth table33%1/3
A and B
A= defined $fhB= ref $fh eq "GLOB"
ABresult
0X0
100
111
126 246 foreach ( keys %files ) {
127 136 ▶ if ( $files{$_}{fh} eq $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($files{$_}{"fh"} eq $fh) 136 0 136
128 136 ▶ $rv = $_ and last;
Branch: 1 branch
BranchTrueFalseTotal
$rv = $_ and last 136 0 136
129 }
130 }
131 }
132  
133 246 subPostamble( PDLEVEL4, '$', $rv );
134  
135 246 return $rv;
136 }
137  
138 268 ▶ sub pclose ($) {
Subroutine pclose: called CC=8 SCAR=20.8
Pod documented
139  
140 # Purpose: Closes a cached file handle
141 # Returns: Boolean
142 # Usage: $rv = plcose($filename)
143 # Usage: $rv = plcose($fh)
144  
145 268 my $filename = shift;
146 268 my $rv = 1;
147 268 my $fh;
148  
149 268 subPreamble( PDLEVEL1, '$', $filename );
150  
151 268 ▶ if ( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 268 0 268
152  
153 # Get the missing variable
154 268 ▶ if ( ref $filename eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename eq 'GLOB') { } 10 258 268
155 10 $fh = $filename;
156 10 $filename = _pfFhind($fh);
157 } else {
158 258 ▶ $fh = $files{$filename}{fh} if exists $files{$filename};
Branch: 1 branch
BranchTrueFalseTotal
if exists $files{$filename} 215 43 258
159 }
160  
161 # Close the filehandle
162 268 ▶ if ( defined $fh and fileno $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh and fileno $fh) 221 47 268
Condition100%3/3
defined $fh and fileno $fh
!ll&&!rl&&r
434221
MC/DC100%2/2
defined $fh and fileno $fh
defined $fh fileno $fh
Truth table100%3/3
A and B
A= defined $fhB= fileno $fh
ABresult
0X0
100
111
163 221 flock $fh, LOCK_UN;
164 221 $rv = close $fh;
165 }
166  
167 # Clean up internal data structures
168 268 ▶ if ( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 262 6 268
169 262 delete $files{$filename};
170 262 delete $lstack{$filename};
171 }
172  
173 Paranoid::ERROR =
174 268 ▶ pdebug( 'error closing file handle: %s', PDLEVEL1, $! )
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 268 268
175 unless $rv;
176 }
177  
178 268 subPostamble( PDLEVEL1, '$', $rv );
179  
180 268 return $rv;
181 }
182  
183 58 ▶ sub pcloseAll {
Subroutine pcloseAll: called CC=4 SCAR=14.0
Pod documented
184  
185 # Purpose: Closes all filehandles
186 # Returns: Boolean
187 # Usage: $rv = pcloseAll();
188  
189 58 my @files = @_;
190 58 my $rv = 1;
191  
192 58 subPreamble( PDLEVEL1, '@', @files );
193  
194 58 ▶ @files = keys %files unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 58 0 58
195 58 foreach (@files) {
196 16 ▶ $rv = 0 unless pclose($_);
Branch: 1 branch
BranchTrueFalseTotal
unless &pclose($_) 0 16 16
197 }
198  
199 58 subPostamble( PDLEVEL1, '$', $rv );
200  
201 58 return $rv;
202 }
203  
204 259 ▶ sub _open {
Subroutine _open: called CC=10 SCAR=24.1
205  
206 # Purpose: Performs the sysopen call
207 # Returns: rv of sysopen
208 # Usage: $rv = _open($filename);
209 # Usage: $rv = _open($filename, $mode);
210 # Usage: $rv = _open($filename, $mode, $perms);
211  
212 259 my $filename = shift;
213 259 my $mode = shift;
214 259 my $perms = shift;
215 259 my ( %tmp, $f, $fh, $rv );
216  
217 259 subPreamble( PDLEVEL3, '$;$$', $filename, $mode, $perms );
218  
219 259 ▶ if ( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 259 0 259
220  
221 # Detaint mode/perms
222 259 $rv = 1;
223 259 ▶ $mode = PDEFMODE unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 2 257 259
224 259 ▶ $perms = PDEFPERM unless defined $perms;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $perms 108 151 259
225 259 ▶ unless ( detaint( $mode, 'int' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&detaint(\$mode, "int")) 0 259 259
226 0 $rv = 0;
227 0 Paranoid::ERROR =
228 pdebug( 'invalid mode passed: %s', PDLEVEL1, $mode );
229 }
230 259 ▶ unless ( detaint( $perms, 'int' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&detaint(\$perms, "int")) 0 259 259
231 0 $rv = 0;
232 0 Paranoid::ERROR =
233 pdebug( 'invalid perm passed: %s', PDLEVEL1, $perms );
234 }
235  
236 # Prep file record
237 %tmp = (
238 259 mode => $mode,
239 perms => $perms,
240 pid => $$,
241 ltype => LOCK_UN,
242 );
243  
244 # Detaint filename
245 259 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 259 0 259
246 259 ▶ if ( detaint( $filename, 'filename', $f ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&detaint(\$filename, 'filename', \$f)) { } 259 0 259
247  
248 # Attempt to open the fila
249 $rv =
250 ( $tmp{mode} & O_CREAT )
251 ? sysopen $fh, $f, $tmp{mode}, $tmp{perms}
252 : sysopen $fh,
253 259 ▶ $f, $tmp{mode};
Branch: 1 branch
BranchTrueFalseTotal
$tmp{'mode'} & 64 ? : 138 121 259
254 259 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 219 40 259
255 219 $tmp{fh} = $fh;
256 219 $tmp{real} = realpath($filename);
257 219 $files{$filename} = {%tmp};
258 } else {
259 40 Paranoid::ERROR = pdebug( 'failed to open %s: %s',
260 PDLEVEL1, $filename, $! );
261 }
262  
263 } else {
264 0 Paranoid::ERROR =
265 pdebug( 'failed to detaint %s', PDLEVEL1, $filename );
266 }
267 }
268 }
269  
270 259 subPostamble( PDLEVEL3, '$', $rv );
271  
272 259 return $rv;
273 }
274  
275 22 ▶ sub _reopen {
Subroutine _reopen: called CC=8 SCAR=21.5
276  
277 # Purpose: Reopens an open file handle
278 # Returns: rv of _open
279 # Usage: $rv = _reopen($filename);
280 # Usage: $rv = _reopen($fh);
281  
282 22 my $filename = shift;
283 22 my ( %tmp, $fh, $pos, $rv, $af );
284  
285 22 subPreamble( PDLEVEL3, '$', $filename );
286  
287 22 ▶ if ( defined $filename and exists $files{$filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename and exists $files{$filename}) 22 0 22
Condition33%1/3
defined $filename and exists $files{$filename}
!ll&&!rl&&r
0022
MC/DC0%0/2
defined $filename and exists $files{$filename}
defined $filename exists $files{$filename}
Truth table33%1/3
A and B
A= defined $filenameB= exists $files{$filename}
ABresult
0X0
100
111
288  
289 # Get a copy of the file record
290 22 %tmp = %{ $files{$filename} };
291 22 $fh = $tmp{fh};
292  
293 # Get the current cursor position
294 22 ▶ $pos = fileno $fh ? sysseek $fh, 0, SEEK_CUR : 0;
Branch: 1 branch
BranchTrueFalseTotal
fileno $fh ? : 22 0 22
295 22 $af = $fh->autoflush;
296  
297 # Close the old file handle
298 22 $tmp{fh} = $fh = undef;
299 22 ▶ if ( pclose($filename) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&pclose($filename)) 22 0 22
300  
301 # Reopen should ignore O_TRUNC, O_CREAT, and O_EXCL on reopens
302 22 ▶ $tmp{mode} &= ~PIGNMFLAGS if $tmp{mode} & PIGNMFLAGS;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp{"mode"} & 704 22 0 22
303  
304 # Open the file and move the cursor back where it was
305 22 $rv = _open( @tmp{qw(real mode perms)} );
306 22 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 22 0 22
307  
308 # Move the cursor back to where it was
309 22 $fh = $files{ $tmp{real} }{fh};
310 22 $fh->autoflush($af);
311 22 $rv = sysseek $fh, $pos, SEEK_SET;
312  
313 # Move the record over to the original file name
314 22 $files{$filename} = { %{ $files{ $tmp{real} } } };
315 22 ▶ delete $files{ $tmp{real} } if $filename ne $tmp{real};
Branch: 1 branch
BranchTrueFalseTotal
if $filename ne $tmp{"real"} 22 0 22
316  
317 # Delete any existing lock stack
318 22 delete $lstack{$filename};
319 }
320 }
321 }
322  
323 22 subPostamble( PDLEVEL3, '$', $rv );
324  
325 22 return $rv;
326 }
327  
328 57663 ▶ sub popen {
Subroutine popen: called CC=15 SCAR=28.2
Pod documented
329  
330 # Purpose: Performs a sysopen with file descriptor caching
331 # Returns: file handle
332 # Usage: $fh = popen($filename, $mode, $perms);
333  
334 57663 my $filename = shift;
335 57663 my $mode = shift;
336 57663 my $perms = shift;
337 57663 my ( %tmp, $fh, $f, $pos, $rv );
338  
339 57663 subPreamble( PDLEVEL2, '$;$$', $filename, $mode, $perms );
340  
341 # Make sure we weren't passed a file handle, but if we
342 # were attempt to find the actual filename
343 57663 ▶ if ( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 57663 0 57663
344 57663 ▶ if ( ref $filename eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename eq 'GLOB') { } 180 57483 57663
345 180 $fh = $filename;
346 180 $filename = _pfFhind($filename);
347 } else {
348 57483 ▶ $fh = $files{$filename}{fh} if exists $files{$filename};
Branch: 1 branch
BranchTrueFalseTotal
if exists $files{$filename} 57246 237 57483
349 }
350 }
351  
352 57663 ▶ if ( defined $filename and exists $files{$filename} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $filename and exists $files{$filename}) { } 57370 293 57663
elsif (defined $filename) { } 237 56 293
elsif (not defined $filename and defined $fh) { } 56 0 56
Condition100%3/3
defined $filename and exists $files{$filename}
!ll&&!rl&&r
5623757370
MC/DC100%2/2
defined $filename and exists $files{$filename}
defined $filename exists $files{$filename}
Truth table100%3/3
A and B
A= defined $filenameB= exists $files{$filename}
ABresult
0X0
100
111
353  
354 # Make sure pid is the same
355 57370 ▶ if ( $files{$filename}{pid} == $$ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($files{$filename}{'pid'} == $$) { } 57348 22 57370
356  
357 57348 ▶ if ( fileno $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (fileno $fh) { } 57348 0 57348
358  
359 # Return existing filehandle
360 57348 pdebug( 'returning cached file handle', PDLEVEL2 );
361 57348 $rv = $fh;
362  
363 } else {
364  
365 # Reopen a filehandle that was closed outside
366 # of this module
367 0 pdebug( 'reopening closed file handle', PDLEVEL2 );
368 0 ▶ $rv = $files{$filename}{fh} if _reopen($filename);
Branch: 1 branch
BranchTrueFalseTotal
if _reopen($filename) 0 0 0
369 }
370  
371 } else {
372  
373 22 pdebug( 'reopening inherited file handle in child',
374 PDLEVEL2 );
375 22 ▶ $rv = $files{$filename}{fh} if _reopen($filename);
Branch: 1 branch
BranchTrueFalseTotal
if _reopen($filename) 22 0 22
376  
377 }
378  
379 } elsif ( defined $filename ) {
380  
381 237 pdebug( 'opening new file handle', PDLEVEL2 );
382 237 ▶ $rv = $files{$filename}{fh} if _open( $filename, $mode, $perms );
Branch: 1 branch
BranchTrueFalseTotal
if _open($filename, $mode, $perms) 197 40 237
383  
384 ▶ } elsif ( !defined $filename and defined $fh ) {
Condition33%1/3
not defined $filename and defined $fh
!ll&&!rl&&r
0056
MC/DC0%0/2
not defined $filename and defined $fh
not defined $filename defined $fh
Truth table33%1/3
not(A) and B
A= not defined $filenameB= defined $fh
ABresult
0X0
100
111
385 56 Paranoid::ERROR =
386 pdebug( 'popen called with an unmanaged file handle',
387 PDLEVEL1 );
388 56 ▶ $rv = fileno $fh ? $fh : undef;
Branch: 1 branch
BranchTrueFalseTotal
fileno $fh ? : 52 4 56
389 } else {
390 0 Paranoid::ERROR =
391 pdebug( 'attempted to open a file with an undefined name',
392 PDLEVEL1 );
393 }
394  
395 57663 subPostamble( PDLEVEL2, '$', $rv );
396  
397 57663 return $rv;
398 }
399  
400 0 ▶ sub preopen {
Subroutine preopen: not called CC=4 SCAR=30.0
Pod documented
401  
402 # Purpose: Reopens either the named files or all
403 # Returns: Boolean
404 # Usage: $rv = preopen();
405 # Usage: $rv = preopen(@filenames);
406  
407 0 my @files = @_;
408 0 my $rv = 1;
409  
410 0 subPreamble( PDLEVEL2, '@', @files );
411  
412 0 ▶ @files = keys %files unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 0 0
413 0 ▶ foreach (@files) { $rv = 0 unless _reopen($_) }
Branch: 1 branch
BranchTrueFalseTotal
unless _reopen($_) 0 0 0
414  
415 0 subPostamble( PDLEVEL2, '$', $rv );
416  
417 0 return $rv;
418 }
419  
420 4144 ▶ sub _pflock {
Subroutine _pflock: called CC=9 SCAR=22.8
421  
422 # Purpose: Performs file-locking operations on the passed filename
423 # Returns: Boolean
424 # Usage: $rv = _pflock($filename, LOCK_EX);
425  
426 4144 my $filename = shift;
427 4144 my $lock = shift;
428 4144 my ( $rv, $fh, $rl );
429 4144 local $!;
430  
431 4144 subPreamble( PDLEVEL3, '$$', $filename, $lock );
432  
433 4144 ▶ if ( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 4144 0 4144
434  
435 # Get the missing variable
436 4144 ▶ if ( ref $filename eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename eq 'GLOB') { } 36 4108 4144
437 36 $fh = $filename;
438 36 $filename = _pfFhind($fh);
439 } else {
440 4108 ▶ $fh = $files{$filename}{fh} if exists $files{$filename};
Branch: 1 branch
BranchTrueFalseTotal
if exists $files{$filename} 4108 0 4108
441 }
442  
443 4144 ▶ if ( defined $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) 4144 0 4144
444  
445 # Apply the lock
446 4144 $rl = $lock & PFLMASK;
447 4144 $rv = flock $fh, $lock;
448  
449 # Record change to internal state if we're tracking this file
450 4144 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 4144 0 4144
451 4144 ▶ if ( defined $filename and exists $files{$filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename and exists $files{$filename}) { } 4112 32 4144
Condition66%2/3
defined $filename and exists $files{$filename}
!ll&&!rl&&r
3204112
MC/DC50%1/2
defined $filename and exists $files{$filename}
defined $filename exists $files{$filename}
Truth table66%2/3
A and B
A= defined $filenameB= exists $files{$filename}
ABresult
0X0
100
111
452 4112 $files{$filename}{ltype} = $rl;
453 } else {
454 32 pdebug(
455 'flock succeeded on file opened outside of the'
456 . ' Paranoid::IO framework (%s)',
457 PDLEVEL1, $filename
458 );
459 }
460 } else {
461 0 ▶ pdebug(
Branch: 1 branch
BranchTrueFalseTotal
$lock & 4 ? : 0 0 0
462 ( ( $lock & LOCK_NB ) ? 'non-blocking' : '' )
463 . 'flock attempt failed on %s',
464 PDLEVEL1, $filename
465 );
466 }
467 }
468 }
469  
470 4144 subPostamble( PDLEVEL3, '$', $rv );
471  
472 4144 return $rv;
473 }
474  
475 31152 ▶ sub _plsflock {
Subroutine _plsflock: called CC=23 SCAR=31.9
476  
477 31152 my $filename = shift;
478 31152 my $lock = shift;
479 31152 my ( $fh, $stack, $rl, $ll, $lsl, $rv );
480  
481 31152 subPreamble( PDLEVEL3, '$$', $filename, $lock );
482  
483 # Var Key:
484 # lock: lock passed to function (can include LOCK_NB)
485 # rl: real lock (stripping LOCK_NB)
486 # ll: last lock (as performed by last _pflock()
487 # lsl: last lock recorded in the lock stack
488  
489 # Translate glob to filename for lock stack tracking purposes
490 31152 $fh = $filename;
491 31152 ▶ $filename = _pfFhind($filename) if ref $filename eq 'GLOB';
Branch: 1 branch
BranchTrueFalseTotal
if ref $filename eq "GLOB" 20 31132 31152
492  
493 # Get the current lock state
494 $ll = $files{$filename}{ltype}
495 31152 ▶ if defined $filename and exists $files{$filename};
Branch: 1 branch
BranchTrueFalseTotal
if defined $filename and exists $files{$filename} 31136 16 31152
Condition66%2/3
defined $filename and exists $files{$filename}
!ll&&!rl&&r
16031136
MC/DC50%1/2
defined $filename and exists $files{$filename}
defined $filename exists $files{$filename}
Truth table66%2/3
A and B
A= defined $filenameB= exists $files{$filename}
ABresult
0X0
100
111
496 31152 ▶ if ( defined $ll ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ll) { } 31136 16 31152
497  
498 # Get the real lock level for comparison
499 31136 $rl = $lock & PFLMASK;
500  
501 # File has been opened, at least, with popen, and has a locktype
502 # entry
503 31136 ▶ $lstack{$filename} = [] unless exists $lstack{$filename};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $lstack{$filename} 1276 29860 31136
504 31136 $stack = $lstack{$filename};
505 31136 $lsl = $$stack[-1];
506  
507 #warn "lock: $lock\nrl: $rl\nll: $ll\nlsl: $lsl\n";
508 31136 ▶ pdebug(
Branch: 1 branch
BranchTrueFalseTotal
if defined $lsl and $lsl != $ll 0 31136 31136
Condition66%2/3
defined $lsl and $lsl != $ll
!ll&&!rl&&r
1276298600
MC/DC0%0/2
defined $lsl and $lsl != $ll
defined $lsl $lsl != $ll
Truth table66%2/3
A and B
A= defined $lslB= $lsl != $ll
ABresult
0X0
100
111
509 'something has gone awry during lock tracking.'
510 . 'll: %s lsl: %s',
511 PDLEVEL1, $ll, $lsl
512 )
513 if defined $lsl
514 and $lsl != $ll;
515  
516 # Adjust as necessary
517 31136 ▶ if ( $rl == LOCK_UN ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rl == 8) { } 15305 15831 31136
elsif ($rl == 1) { } 11986 3845 15831
elsif ($rl == 2) { } 3845 0 3845
518  
519 # Remove a lock from the stack
520 15305 pop @$stack;
521  
522 15305 ▶ if ( scalar @$stack ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$stack) { } 14051 1254 15305
523  
524 # Still have locks in the stack that must not be degraded
525 14051 $rv = 1;
526 14051 ▶ if ( $ll != $$stack[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ll != $stack->[-1]) 0 14051 14051
527  
528 # Apply the new level
529 0 $rv = _pflock( $filename, $$stack[-1] );
530 }
531  
532 } else {
533  
534 # No locks in the stack to preserve, so go ahead and
535 # release the lock
536 1254 $rv = _pflock( $filename, LOCK_UN );
537  
538 }
539  
540 } elsif ( $rl == LOCK_SH ) {
541  
542 # Upgrade lock to preserve previous exclusive lock on the
543 # stack, if necessary
544 11986 ▶ if ( defined $lsl and $lsl == LOCK_EX ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lsl and $lsl == 2) 10284 1702 11986
Condition100%3/3
defined $lsl and $lsl == 2
!ll&&!rl&&r
632107010284
MC/DC100%2/2
defined $lsl and $lsl == 2
defined $lsl $lsl == 2
Truth table100%3/3
A and B
A= defined $lslB= $lsl == 2
ABresult
0X0
100
111
545 10284 $lock = ( LOCK_EX | ( $lock & LOCK_NB ) );
546 10284 $rl = LOCK_EX;
547 }
548  
549 11986 ▶ $rv = $ll == $rl ? 1 : _pflock( $filename, $lock );
Branch: 1 branch
BranchTrueFalseTotal
$ll == $rl ? : 11354 632 11986
550 11986 ▶ push @$stack, $rl if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 11986 0 11986
551  
552 } elsif ( $rl == LOCK_EX ) {
553 3845 push @$stack, $rl;
554 3845 ▶ $rv = $ll == $rl ? 1 : _pflock( $filename, $lock );
Branch: 1 branch
BranchTrueFalseTotal
$ll == $rl ? : 3201 644 3845
555 } else {
556 0 pdebug( 'unknown lock type: %x', PDLEVEL1, $lock );
557 }
558  
559 # Report some diagnostics
560 31136 ▶ if ( scalar @$stack ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$stack) { } 29882 1254 31136
561 29882 pdebug( 'lock stack depth: %s', PDLEVEL4, scalar @$stack );
562 29882 ▶ if ( $ll == $$stack[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ll == $stack->[-1]) { } 28606 1276 29882
563 28606 pdebug( 'preserved lock at %s', PDLEVEL4, $ll );
564 } else {
565 1276 pdebug( 'switched lock from %s to %s',
566 PDLEVEL4, $ll, $$stack[-1] );
567 }
568 } else {
569 1254 pdebug( 'no locks remaining', PDLEVEL4 );
570 }
571  
572 # Delete empty stacks to avoid memory leaks
573 31136 ▶ delete $lstack{$filename} unless scalar @$stack;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$stack 1254 29882 31136
574  
575 } else {
576 16 ▶ if ( defined $fh and !defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh and not defined $filename) { } 16 0 16
Condition33%1/3
defined $fh and not defined $filename
!ll&&!rl&&r
0016
MC/DC0%0/2
defined $fh and not defined $filename
defined $fh not defined $filename
Truth table33%1/3
A and not(B)
A= defined $fhB= not defined $filename
ABresult
0X0
100
111
577 16 $rv = _pflock( $fh, $lock );
578 } else {
579 0 pdebug( 'file %s is unknown to Paranoid::IO so far',
580 PDLEVEL1, $filename );
581 }
582 }
583  
584 31152 subPostamble( PDLEVEL3, '$', $rv );
585  
586 31152 return $rv;
587 }
588  
589 32750 ▶ sub pflock {
Subroutine pflock: called CC=3 SCAR=11.0
Pod documented
590  
591 # Purpose: Performs file-locking operations on the passed filename
592 # Returns: Boolean
593 # Usage: $rv = pflock($filename, LOCK_EX);
594  
595 32750 my $filename = shift;
596 32750 my $lock = shift;
597 32750 my ( $rv, $fh );
598  
599 32750 subPreamble( PDLEVEL2, '$$', $filename, $lock );
600  
601 # NOTE: retrieving the file handle might seem silly, but if a process
602 # is forked, and the first thing they do on a file is apply an flock,
603 # the first I/O operation will close and reopen the file to avoid
604 # confusion with the parent process and, therefore, losing the lock.
605 #
606 # End sum, this is a necessary evil in order to preserve locks a
607 # before any effective I/O is done in the child.
608 32750 ▶ if ( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 32750 0 32750
609 32750 $fh = popen($filename);
610 32750 ▶ $rv =
Branch: 1 branch
BranchTrueFalseTotal
PIOLOCKSTACK() ? : 31152 1598 32750
611 PIOLOCKSTACK()
612 ? _plsflock( $filename, $lock )
613 : _pflock( $filename, $lock );
614 }
615  
616 32750 subPostamble( PDLEVEL2, '$', $rv );
617  
618 32750 return $rv;
619 }
620  
621 0 ▶ sub plockstat {
Subroutine plockstat: not called CC=5 SCAR=34.0
Pod documented
622  
623 # Purpose: Returns the the status of the last lock applied via
624 # pflock()
625 # Returns: LOCK_*
626 # Usage: $lock = plockstat($filename);
627  
628 0 my $filename = shift;
629 0 my $rv;
630  
631 0 subPreamble( PDLEVEL2, '$', $filename );
632  
633 0 ▶ if ( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 0 0 0
634  
635 # Get the missing variable
636 0 ▶ $filename = _pfFhind($filename) if ref $filename eq 'GLOB';
Branch: 1 branch
BranchTrueFalseTotal
if ref $filename eq "GLOB" 0 0 0
637 0 ▶ if ( defined $filename and exists $files{$filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename and exists $files{$filename}) { } 0 0 0
Condition0%0/3
defined $filename and exists $files{$filename}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $filename and exists $files{$filename}
defined $filename exists $files{$filename}
Truth table0%0/3
A and B
A= defined $filenameB= exists $files{$filename}
ABresult
0X0
100
111
638 0 $rv = $files{$filename}{ltype};
639 } else {
640 0 pdebug(
641 'attempted to retrieve lock status for file not opened'
642 . ' with the Paranoid::IO framework (%s)',
643 PDLEVEL1, $filename
644 );
645 }
646 }
647  
648 0 subPostamble( PDLEVEL2, '$', $rv );
649  
650 0 return $rv;
651 }
652 }
653  
654 58 ▶sub ptell {
Subroutine ptell: called CC=4 SCAR=13.9
Pod documented
655  
656 # Purpose: Returns the cursor position in the file handle
657 # Returns: Integer
658 # Usage: $pos = ptell($filename);
659  
660 596 my $filename = shift;
661 596 my ( $rv, $fh );
662 596 local $!;
663  
664 596 subPreamble( PDLEVEL2, '$', $filename );
665  
666 596 ▶ if ( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 596 0 596
667  
668 596 $fh = popen( $filename, O_RDWR );
669 596 ▶ if ( defined $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) 592 4 596
670 592 $rv = sysseek $fh, 0, SEEK_CUR;
671 592 ▶ Paranoid::ERROR =
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 592 592
672 pdebug( 'error attempting to ptell: %s', PDLEVEL1, $! )
673 unless $rv;
674 }
675 }
676  
677 596 subPostamble( PDLEVEL2, '$', $rv );
678  
679 596 return $rv;
680 }
681  
682 11761 ▶sub pseek {
Subroutine pseek: called CC=5 SCAR=16.1
Pod documented
683  
684 # Purpose: Performs a sysseek
685 # Returns: Integer/undef
686 # Usage: $cur = pseek($filename, $curpos, $whence);
687  
688 11761 my $filename = shift;
689 11761 my $setpos = shift;
690 11761 my $whence = shift;
691 11761 my ( $rv, $fh );
692 11761 local $!;
693  
694 11761 subPreamble( PDLEVEL2, '$$;$', $filename, $setpos, $whence );
695  
696 11761 ▶ if ( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 11761 0 11761
697  
698 11761 $fh = popen( $filename, O_RDWR );
699 11761 ▶ if ( defined $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) 11757 4 11761
700 11757 ▶ $whence = SEEK_SET unless defined $whence;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $whence 0 11757 11757
701 11757 $rv = sysseek $fh, $setpos, $whence;
702 11757 ▶ Paranoid::ERROR =
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 2 11755 11757
703 pdebug( 'error attempting to pseek: %s', PDLEVEL1, $! )
704 unless $rv;
705 }
706 }
707  
708 11761 subPostamble( PDLEVEL2, '$', $rv );
709  
710 11761 return $rv;
711 }
712  
713 2329 ▶sub pwrite {
Subroutine pwrite: called CC=12 SCAR=25.5
Pod documented
714  
715 # Purpose: Performs a syswrite w/locking
716 # Returns: Integer/undef
717 # Usage: $bytes = pwrite($filename, $text);
718 # Usage: $bytes = pwrite($filename, $text, $length);
719 # Usage: $bytes = pwrite($filename, $text, $length, $offset);
720 # Usage: $bytes = pwrite($filename, $text, $length, $offset, $nolock);
721  
722 2329 my $filename = shift;
723 2329 my $out = shift;
724 2329 my $wlen = shift;
725 2329 my $offset = shift;
726 2329 my $nolock = shift;
727 2329 ▶ my $bytes = defined $out ? length $out : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $out ? : 2325 4 2329
728 2329 my ( $fh, $rv );
729  
730 2329 subPreamble( PDLEVEL2, '$$;$$$', $filename, $bytes, $wlen, $offset,
731 $nolock );
732  
733 2329 ▶ if ( defined $filename and defined $out and length $out ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename and defined $out and length $out) 2321 8 2329
Condition83%5/6
defined $filename and defined $out and length $out
!ll&&!rl&&r
442321
defined $filename and defined $out
!ll&&!rl&&r
042325
MC/DC66%2/3
defined $filename and defined $out and length $out
defined $filename defined $out length $out
Truth table75%3/4
A and B and C
A= defined $filenameB= defined $outC= length $out
ABCresult
0XX0
10X0
1100
1111
734  
735 # Opportunistically open a file handle if needed,
736 # otherwise, just retrieve the existing file handle
737 2321 $fh = popen( $filename, O_WRONLY | O_CREAT );
738  
739 # Smoke 'em if you got'em...
740 2321 ▶ if ( defined $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) 2321 0 2321
741 2321 ▶ if ( $nolock or pflock( $filename, LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nolock or pflock($filename, 2)) 2321 0 2321
Condition33%1/3
$nolock or pflock($filename, 2)
l!l&&r!l&&!r
023210
MC/DC0%0/2
$nolock or pflock($filename, 2)
$nolock pflock($filename, 2)
Truth table33%1/3
A or B
A= $nolockB= pflock($filename, 2)
ABresult
1X1
011
000
742 2321 ▶ $wlen = length $out unless defined $wlen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wlen 388 1933 2321
743 2321 ▶ $offset = 0 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 392 1929 2321
744 2321 $rv = syswrite $fh, $out, $wlen, $offset;
745 2321 ▶ if ( defined $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) { } 2321 0 2321
746 2321 pdebug( 'wrote %d bytes', PDLEVEL2, $rv );
747 } else {
748 0 Paranoid::ERROR =
749 pdebug( 'failed to write to file handle: %s',
750 PDLEVEL1, $! );
751 }
752 2321 ▶ pflock( $filename, LOCK_UN ) unless $nolock;
Branch: 1 branch
BranchTrueFalseTotal
unless $nolock 2321 0 2321
753 }
754 }
755 }
756  
757 2329 subPostamble( PDLEVEL2, '$', $rv );
758  
759 2329 return $rv;
760 }
761  
762 0 ▶sub pnlwrite {
Subroutine pnlwrite: not called CC=1 SCAR=6.9
Pod documented
763  
764 # Purpose: Wrapper for pwrite w/o internal flocking
765 # Returns: RV of pwrite
766 # Usage: $bytes = pnlwrite($filename, $text, $length);
767 # Usage: $bytes = pnlwrite($filename, $text, $length, $offset);
768  
769 0 my $filename = shift;
770 0 my $out = shift;
771 0 my $wlen = shift;
772 0 my $offset = shift;
773  
774 0 return pwrite( $filename, $out, $wlen, $offset, 1 );
775 }
776  
777 520 ▶sub pappend {
Subroutine pappend: called CC=13 SCAR=29.6
Pod documented
778  
779 # Purpose: Appends the data to the end of the file,
780 # but does not move the file cursor
781 # Returns: Integer/undef
782 # Usage: $rv = pappend($filename, $content);
783 # Usage: $rv = pappend($filename, $content, $length);
784 # Usage: $rv = pappend($filename, $content, $length, $offset);
785  
786 520 my $filename = shift;
787 520 my $out = shift;
788 520 my $wlen = shift;
789 520 my $offset = shift;
790 520 my $nolock = shift;
791 520 my ( $fh, $pos, $rv );
792  
793 520 subPreamble( PDLEVEL2, '$$;$$', $filename, $out, $wlen, $offset,
794 $nolock );
795  
796 520 ▶ if ( defined $filename and defined $out and length $out ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename and defined $out and length $out) 520 0 520
Condition33%2/6
defined $filename and defined $out and length $out
!ll&&!rl&&r
00520
defined $filename and defined $out
!ll&&!rl&&r
00520
MC/DC0%0/3
defined $filename and defined $out and length $out
defined $filename defined $out length $out
Truth table25%1/4
A and B and C
A= defined $filenameB= defined $outC= length $out
ABCresult
0XX0
10X0
1100
1111
797  
798 # Opportunistically open a file handle in append mode
799 520 $fh = popen( $filename, O_WRONLY | O_CREAT | O_APPEND );
800  
801 # Smoke 'em if you got'em...
802 520 ▶ if ( defined $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) 520 0 520
803  
804 # Lock the file
805 520 ▶ if ( $nolock or pflock( $filename, LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nolock or pflock($filename, 2)) 520 0 520
Condition33%1/3
$nolock or pflock($filename, 2)
l!l&&r!l&&!r
05200
MC/DC0%0/2
$nolock or pflock($filename, 2)
$nolock pflock($filename, 2)
Truth table33%1/3
A or B
A= $nolockB= pflock($filename, 2)
ABresult
1X1
011
000
806  
807 # Save the current position
808 520 $pos = sysseek $fh, 0, SEEK_CUR;
809  
810 # Seek to the end of the file
811 520 ▶ if ( $pos and sysseek $fh, 0, SEEK_END ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos and sysseek $fh, 0, 2) 520 0 520
Condition33%1/3
$pos and sysseek $fh, 0, 2
!ll&&!rl&&r
00520
MC/DC0%0/2
$pos and sysseek $fh, 0, 2
$pos sysseek $fh, 0, 2
Truth table33%1/3
A and B
A= $posB= sysseek $fh, 0, 2
ABresult
0X0
100
111
812  
813 # write the content
814 520 ▶ $wlen = length $out unless defined $wlen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wlen 520 0 520
815 520 ▶ $offset = 0 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 520 0 520
816 520 $rv = syswrite $fh, $out, $wlen, $offset;
817 520 ▶ if ( defined $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) { } 520 0 520
818 520 pdebug( 'wrote %d bytes', PDLEVEL2, $rv );
819 } else {
820 0 Paranoid::ERROR =
821 pdebug( 'failed to write to file handle: %s',
822 PDLEVEL1, $! );
823 }
824 }
825  
826 # Seek back to original position
827 520 sysseek $fh, $pos, SEEK_SET;
828  
829 # Unlock the file handle
830 520 ▶ pflock( $filename, LOCK_UN ) unless $nolock;
Branch: 1 branch
BranchTrueFalseTotal
unless $nolock 520 0 520
831 }
832 }
833 }
834  
835 520 subPostamble( PDLEVEL2, '$', $rv );
836  
837 520 return $rv;
838 }
839  
840 0 ▶sub pnlappend {
Subroutine pnlappend: not called CC=1 SCAR=6.9
Pod documented
841  
842 # Purpose: Wrapper for pappend w/o internal flocking
843 # Returns: RV of pappend
844 # Usage: $bytes = pnlappend($filename, $text, $length);
845 # Usage: $bytes = pnlappend($filename, $text, $length, $offset);
846  
847 0 my $filename = shift;
848 0 my $out = shift;
849 0 my $wlen = shift;
850 0 my $offset = shift;
851  
852 0 return pappend( $filename, $out, $wlen, $offset, 1 );
853 }
854  
855 9423 ▶sub pread ($\$;@) {
Subroutine pread: called CC=9 SCAR=22.1
Pod documented
856  
857 # Purpose: Performs a sysread w/locking
858 # Returns: Integer/undef
859 # Usage: $bytes = pread($filename, $text, $length);
860 # Usage: $bytes = pread($filename, $text, $length, $offset);
861  
862 9423 my $filename = shift;
863 9423 my $sref = shift;
864 9423 my $rlen = shift;
865 9423 my $offset = shift;
866 9423 my $nolock = shift;
867 9423 my ( $fh, $rv );
868  
869 9423 subPreamble( PDLEVEL2, '$\$;$$$', $filename, $sref, $rlen, $offset,
870 $nolock );
871  
872 9423 ▶ if ( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 9423 0 9423
873  
874 # Opportunistically open a file handle if needed,
875 # otherwise, just retrieve the existing file handle
876 9423 $fh = popen( $filename, O_RDONLY );
877  
878 # Smoke 'em if you got'em...
879 9423 ▶ if ( defined $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) 9417 6 9423
880 9417 ▶ if ( $nolock or pflock( $filename, LOCK_SH ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nolock or pflock($filename, 1)) 9417 0 9417
Condition66%2/3
$nolock or pflock($filename, 1)
l!l&&r!l&&!r
294150
MC/DC0%0/2
$nolock or pflock($filename, 1)
$nolock pflock($filename, 1)
Truth table66%2/3
A or B
A= $nolockB= pflock($filename, 1)
ABresult
1X1
011
000
881 9417 ▶ $rlen = PIOBLKSIZE unless defined $rlen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rlen 419 8998 9417
882 9417 ▶ $offset = 0 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 9417 0 9417
883 9417 $rv = sysread $fh, $$sref, $rlen, $offset;
884 9417 ▶ if ( defined $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) { } 9413 4 9417
885 9413 pdebug( 'read %d bytes', PDLEVEL2, $rv );
886 } else {
887 4 Paranoid::ERROR =
888 pdebug( 'failed to read from file handle: %s',
889 PDLEVEL1, $! );
890 }
891 9417 ▶ pflock( $filename, LOCK_UN ) unless $nolock;
Branch: 1 branch
BranchTrueFalseTotal
unless $nolock 9415 2 9417
892 }
893 }
894 }
895  
896 9423 subPostamble( PDLEVEL2, '$', $rv );
897  
898 9423 return $rv;
899 }
900  
901 2 ▶sub pnlread ($\$;@) {
Subroutine pnlread: called CC=1 SCAR=0.0
Pod documented
902  
903 # Purpose: Wrapper for pread w/o internal flocking
904 # Returns: RV of pread
905 # Usage: $bytes = pnlread($filename, $text, $length);
906 # Usage: $bytes = pnlread($filename, $text, $length, $offset);
907  
908 2 my $filename = shift;
909 2 my $sref = shift;
910 2 my $rlen = shift;
911 2 my $offset = shift;
912  
913 2 return pread( $filename, $$sref, $rlen, $offset, 1 );
914 }
915  
916 3 ▶sub ptruncate {
Subroutine ptruncate: called CC=9 SCAR=23.9
Pod documented
917  
918 # Purpose: Truncates the specified file
919 # Returns: RV of truncate
920 # Usage: $rv = ptruncate($filename);
921 # Usage: $rv = ptruncate($filename, $pos);
922 # Usage: $rv = ptruncate($filename, $pos, 1);
923  
924 3 my $filename = shift;
925 3 my $pos = shift;
926 3 my $nolock = shift;
927 3 my ( $rv, $fh, $cpos );
928  
929 3 subPreamble( PDLEVEL2, '$;$$', $filename, $pos, $nolock );
930  
931 3 ▶ if ( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 3 0 3
932 3 ▶ $pos = 0 unless defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 3 0 3
933 3 $fh = popen( $filename, O_RDWR | O_CREAT );
934  
935 # Smoke 'em if you got'em...
936 3 ▶ if ( defined $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) 3 0 3
937 3 ▶ if ( $nolock or pflock( $filename, LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nolock or pflock($filename, 2)) 3 0 3
Condition33%1/3
$nolock or pflock($filename, 2)
l!l&&r!l&&!r
030
MC/DC0%0/2
$nolock or pflock($filename, 2)
$nolock pflock($filename, 2)
Truth table33%1/3
A or B
A= $nolockB= pflock($filename, 2)
ABresult
1X1
011
000
938 3 $cpos = sysseek $fh, 0, SEEK_CUR;
939 3 $rv = truncate $fh, $pos;
940 3 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 3 0 3
941 3 ▶ sysseek $fh, $pos, SEEK_SET if $cpos > $pos;
Branch: 1 branch
BranchTrueFalseTotal
if $cpos > $pos 0 3 3
942 } else {
943 0 Paranoid::ERROR =
944 pdebug( 'failed to truncate file: %s', PDLEVEL1, $! );
945 }
946 3 ▶ pflock( $filename, LOCK_UN ) unless $nolock;
Branch: 1 branch
BranchTrueFalseTotal
unless $nolock 3 0 3
947 }
948 }
949 }
950  
951 3 subPostamble( PDLEVEL2, '$', $rv );
952  
953 3 return $rv;
954 }
955  
956 58 ▶END {
Subroutine END: called CC=1 SCAR=0.0
957  
958 # Attempt to clean close all filehandles
959 58 pcloseAll();
960 }
961  
962 58 1;
963  
964 __END__
« Paranoid/Data/AVLTree.pm ↑ Summary Paranoid/IO/Line.pm »