blib/lib/File/AtomicWrite.pm

filter: Statementstmt 71.4% 160 / 224 Branchbran 54.4% 74 / 136 Conditioncond 45.8% 22 / 48 MC/DCmcdc 31.2% 10 / 32 Subroutinesub 85.7% 24 / 28 Podpod 100% 8 / 8 totaltotal 62.6% 298 / 476 CC 85 SCAR 37.4
CC
85
Cov
63%
_set_ownership
156.0
_resolve
66.0
write_file
39.7
CRAP 42.2
↑ Summary
1 # -*- Perl -*-
2 #
3 # uses File::Temp to create the temporary file, and offers various
4 # degrees of more paranoid write handling, and means to set Unix file
5 # permissions and ownerships on the resulting file. note however that
6 # rename() may not be safe depending on the filesystem and what
7 # exactly fails. run perldoc(1) on this file for more information
8  
9 package File::AtomicWrite;
10  
11 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Fcntl qw(:seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 # for olden versions of perl
20 4 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 our $VERSION = '1.21';
23  
24 # Default options
25 4 my %default_params = ( MKPATH => 0, template => ".tmp.XXXXXXXXXX" );
26  
27 ######################################################################
28 #
29 # Class methods
30  
31 # accepts output filename, perhaps optional tmp file template, and a
32 # filehandle or scalar ref, and handles all the details in a single shot
33 23 ▶sub write_file {
Subroutine write_file: called CC=14 SCAR=36.8
Pod documented
34 23 my ( $class, $user_params ) = @_;
35 23 ▶ $user_params = {} unless defined $user_params;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user_params 1 22 23
36  
37 23 ▶ if ( !exists $user_params->{input} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $user_params->{"input"}) 2 21 23
38 2 croak "missing 'input' option";
39 }
40  
41 21 my ( $tmp_fh, $tmp_filename, $params_ref, $digest ) = _init($user_params);
42  
43 # attempt cleanup if things go awry (use the OO interface and custom
44 # signal handlers of your own if this is a problem)
45 0 ▶ local $SIG{TERM} = sub { _cleanup( $tmp_fh, $tmp_filename ); exit };
Subroutine __ANON__: not called CC=1 SCAR=2.6
46 0 ▶ local $SIG{INT} = sub { _cleanup( $tmp_fh, $tmp_filename ); exit };
Subroutine __ANON__: not called CC=1 SCAR=2.6
47 19 ▶ local $SIG{__DIE__} = sub { _cleanup( $tmp_fh, $tmp_filename ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
48  
49 19 my $input_ref = ref $params_ref->{input};
50 19 ▶ unless ( $input_ref eq 'SCALAR' or $input_ref eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($input_ref eq "SCALAR" or $input_ref eq "GLOB") 0 19 19
Condition33%1/3
$input_ref eq "SCALAR" or $input_ref eq "GLOB"
l!l&&r!l&&!r
1900
MC/DC0%0/2
$input_ref eq "SCALAR" or $input_ref eq "GLOB"
$input_ref eq "SCALAR" $input_ref eq "GLOB"
Truth table33%1/3
A or B
A= $input_ref eq "SCALAR"B= $input_ref eq "GLOB"
ABresult
1X1
011
000
51 0 croak "invalid type for input option: " . ref $input_ref;
52 }
53  
54 19 my $input = $params_ref->{input};
55 19 ▶ if ( $input_ref eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($input_ref eq 'SCALAR') { } 19 0 19
elsif ($input_ref eq 'GLOB') { } 0 0 0
56 19 ▶ unless ( print $tmp_fh $$input ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $tmp_fh $$input) 0 19 19
57 0 my $save_errstr = $!;
58 0 _cleanup( $tmp_fh, $tmp_filename );
59 0 croak "error printing to temporary file: $save_errstr";
60 }
61 19 ▶ if ( exists $params_ref->{CHECKSUM}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params_ref->{"CHECKSUM"} and not exists $params_ref->{"checksum"}) 1 18 19
Condition100%3/3
exists $params_ref->{"CHECKSUM"} and not exists $params_ref->{"checksum"}
!ll&&!rl&&r
1711
MC/DC100%2/2
exists $params_ref->{"CHECKSUM"} and not exists $params_ref->{"checksum"}
exists $params_ref->{"CHECKSUM"} not exists $params_ref->{"checksum"}
Truth table100%3/3
A and not(B)
A= exists $params_ref->{"CHECKSUM"}B= not exists $params_ref->{"checksum"}
ABresult
0X0
100
111
62 and !exists $params_ref->{checksum} ) {
63 1 $digest->add($$input);
64 }
65 } elsif ( $input_ref eq 'GLOB' ) {
66 0 ▶ while ( my $line = readline $input ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $input)) 0 0 0
67 0 ▶ unless ( print $tmp_fh $line ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $tmp_fh $line) 0 0 0
68 0 my $save_errstr = $!;
69 0 _cleanup( $tmp_fh, $tmp_filename );
70 0 croak "error printing to temporary file: $save_errstr";
71 }
72 0 ▶ if ( exists $params_ref->{CHECKSUM}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params_ref->{"CHECKSUM"} and not exists $params_ref->{"checksum"}) 0 0 0
Condition0%0/3
exists $params_ref->{"CHECKSUM"} and not exists $params_ref->{"checksum"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params_ref->{"CHECKSUM"} and not exists $params_ref->{"checksum"}
exists $params_ref->{"CHECKSUM"} not exists $params_ref->{"checksum"}
Truth table0%0/3
A and not(B)
A= exists $params_ref->{"CHECKSUM"}B= not exists $params_ref->{"checksum"}
ABresult
0X0
100
111
73 and !exists $params_ref->{checksum} ) {
74 0 $digest->add($$input);
75 }
76 }
77 }
78  
79 19 _resolve( $tmp_fh, $tmp_filename, $params_ref, $digest );
80 }
81  
82 8 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
83 8 my ( $class, $user_param ) = @_;
84 8 ▶ $user_param = {} unless defined $user_param;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user_param 1 7 8
85  
86 croak "option 'input' only for write_file class method"
87 8 ▶ if exists $user_param->{input};
Branch: 1 branch
BranchTrueFalseTotal
if exists $user_param->{"input"} 0 8 8
88  
89 8 my $self = {};
90  
91 8 @{$self}{qw/_tmp_fh _tmp_filename _params _digest/} = _init($user_param);
92  
93 7 bless $self, $class;
94 7 return $self;
95 }
96  
97 0 ▶sub safe_level {
Subroutine safe_level: not called CC=2 SCAR=17.9
Pod documented
98 0 my ( $class, $level ) = @_;
99 0 ▶ croak 'safe_level() requires a value' unless defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 0 0 0
100 0 File::Temp->safe_level($level);
101 }
102  
103 1 ▶sub set_template {
Subroutine set_template: called CC=2 SCAR=7.0
Pod documented
104 1 my ( $class, $template ) = @_;
105 1 ▶ croak 'set_template() requires a template' unless defined $template;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $template 0 1 1
106 1 $default_params{template} = $template;
107 1 return;
108 }
109  
110 ######################################################################
111 #
112 # Instance methods
113  
114 1 ▶sub checksum {
Subroutine checksum: called CC=3 SCAR=11.2
Pod documented
115 1 my ( $self, $csum ) = @_;
116 1 ▶ croak 'checksum requires an argument' unless defined $csum;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $csum 0 1 1
117 1 $self->{_params}->{checksum} = $csum;
118  
119 1 ▶ if ( !$self->{_digest} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_digest"}) 1 0 1
120 1 $self->{_params}->{CHECKSUM} = 1;
121 1 $self->{_digest} = _init_checksum( $self->{_params} );
122 }
123  
124 1 return $self;
125 }
126  
127 4 ▶sub commit { _resolve( @{ $_[0] }{qw/_tmp_fh _tmp_filename _params _digest/} ) }
Subroutine commit: called CC=1 SCAR=0.0
Pod documented
128  
129 7 ▶sub DESTROY { _cleanup( @{ $_[0] }{qw/_tmp_fh _tmp_filename/} ) }
Subroutine DESTROY: called CC=1 SCAR=0.0
130  
131 4 ▶sub fh { $_[0]->{_tmp_fh} }
Subroutine fh: called CC=1 SCAR=0.0
Pod documented
132  
133 4 ▶sub filename { $_[0]->{_tmp_filename} }
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
134  
135 # for when things go awry
136 10 ▶sub _cleanup {
Subroutine _cleanup: called CC=3 SCAR=11.7
137 10 my ( $tmp_fh, $tmp_filename ) = @_;
138 # recommended by perlport(1) prior to unlink/rename calls
139 10 ▶ close $tmp_fh if defined $tmp_fh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmp_fh 10 0 10
140 10 ▶ unlink $tmp_filename if defined $tmp_filename;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmp_filename 10 0 10
141 }
142  
143 29 ▶sub _init {
Subroutine _init: called CC=15 SCAR=29.0
144 29 my ($user_params) = @_;
145 29 ▶ $user_params = {} unless defined $user_params;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user_params 0 29 29
146 29 my $params_ref = { %default_params, %$user_params };
147  
148 29 ▶ if ( !exists $params_ref->{file}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params_ref->{"file"} and defined $params_ref->{"file"}) 1 28 29
Condition66%2/3
exists $params_ref->{"file"} and defined $params_ref->{"file"}
!ll&&!rl&&r
1028
MC/DC50%1/2
exists $params_ref->{"file"} and defined $params_ref->{"file"}
exists $params_ref->{"file"} defined $params_ref->{"file"}
Truth table66%2/3
A and B
A= exists $params_ref->{"file"}B= defined $params_ref->{"file"}
ABresult
0X0
100
111
149 or !defined $params_ref->{file} ) {
150 1 croak q{missing 'file' option};
151 }
152  
153 28 my $digest = _init_checksum($params_ref);
154  
155 28 $params_ref->{_dir} = dirname( $params_ref->{file} );
156 28 ▶ if ( !-d $params_ref->{_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $params_ref->{"_dir"}) 2 26 28
157 2 _mkpath( $params_ref->{MKPATH}, $params_ref->{_dir} );
158 }
159  
160 27 ▶ if ( exists $params_ref->{tmpdir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params_ref->{'tmpdir'}) { } 2 25 27
161 2 ▶ if ( !-d $params_ref->{tmpdir}
Branch: 1 branch
BranchTrueFalseTotal
if (not -d $params_ref->{"tmpdir"} and $params_ref->{"tmpdir"} ne $params_ref->{"_dir"}) 1 1 2
Condition66%2/3
not -d $params_ref->{"tmpdir"} and $params_ref->{"tmpdir"} ne $params_ref->{"_dir"}
!ll&&!rl&&r
101
MC/DC50%1/2
not -d $params_ref->{"tmpdir"} and $params_ref->{"tmpdir"} ne $params_ref->{"_dir"}
not -d $params_ref->{"tmpdir"} $params_ref->{"tmpdir"} ne $params_ref->{"_dir"}
Truth table66%2/3
not(A) and B
A= not -d $params_ref->{"tmpdir"}B= $params_ref->{"tmpdir"} ne $params_ref->{"_dir"}
ABresult
0X0
100
111
162 and $params_ref->{tmpdir} ne $params_ref->{_dir} ) {
163 1 _mkpath( $params_ref->{MKPATH}, $params_ref->{tmpdir} );
164  
165 # partition sanity check
166 1 my @dev_ids = map { ( stat $params_ref->{$_} )[0] } qw/_dir tmpdir/;
167 1 ▶ if ( $dev_ids[0] != $dev_ids[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dev_ids[0] != $dev_ids[1]) 0 1 1
168 0 croak 'tmpdir and file directory on different partitions';
169 }
170 }
171 } else {
172 25 $params_ref->{tmpdir} = $params_ref->{_dir};
173 }
174  
175 27 ▶ if ( exists $params_ref->{safe_level} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params_ref->{"safe_level"}) 0 27 27
176 0 File::Temp->safe_level( $params_ref->{safe_level} );
177 }
178  
179 my ( $tmp_fh, $tmp_filename ) = tempfile(
180 $params_ref->{template},
181 DIR => $params_ref->{tmpdir},
182 27 UNLINK => 0
183 );
184 26 ▶ if ( !defined $tmp_fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp_fh) 0 26 26
185 0 die "unable to obtain temporary filehandle\n";
186 }
187  
188 26 ▶ if ( exists $params_ref->{binmode_layer}
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params_ref->{'binmode_layer'} and defined $params_ref->{'binmode_layer'}) { } 0 26 26
elsif (exists $params_ref->{'BINMODE'} and $params_ref->{'BINMODE'}) { } 1 50 51
Condition33%1/3
exists $params_ref->{"binmode_layer"} and defined $params_ref->{"binmode_layer"}
!ll&&!rl&&r
2600
MC/DC0%0/2
exists $params_ref->{"binmode_layer"} and defined $params_ref->{"binmode_layer"}
exists $params_ref->{"binmode_layer"} defined $params_ref->{"binmode_layer"}
Truth table33%1/3
A and B
A= exists $params_ref->{"binmode_layer"}B= defined $params_ref->{"binmode_layer"}
ABresult
0X0
100
111
189 and defined $params_ref->{binmode_layer} ) {
190 0 binmode( $tmp_fh, $params_ref->{binmode_layer} );
191 ▶ } elsif ( exists $params_ref->{BINMODE} and $params_ref->{BINMODE} ) {
Condition66%2/3
exists $params_ref->{"BINMODE"} and $params_ref->{"BINMODE"}
!ll&&!rl&&r
2501
MC/DC50%1/2
exists $params_ref->{"BINMODE"} and $params_ref->{"BINMODE"}
exists $params_ref->{"BINMODE"} $params_ref->{"BINMODE"}
Truth table66%2/3
A and B
A= exists $params_ref->{"BINMODE"}B= $params_ref->{"BINMODE"}
ABresult
0X0
100
111
192 1 binmode($tmp_fh);
193 }
194  
195 26 return $tmp_fh, $tmp_filename, $params_ref, $digest;
196 }
197  
198 29 ▶sub _init_checksum {
Subroutine _init_checksum: called CC=4 SCAR=14.1
199 29 my ($params_ref) = @_;
200 29 my $digest = 0;
201  
202 29 ▶ if ( exists $params_ref->{CHECKSUM} and $params_ref->{CHECKSUM} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params_ref->{'CHECKSUM'} and $params_ref->{'CHECKSUM'}) { } 3 26 29
Condition66%2/3
exists $params_ref->{"CHECKSUM"} and $params_ref->{"CHECKSUM"}
!ll&&!rl&&r
2603
MC/DC50%1/2
exists $params_ref->{"CHECKSUM"} and $params_ref->{"CHECKSUM"}
exists $params_ref->{"CHECKSUM"} $params_ref->{"CHECKSUM"}
Truth table66%2/3
A and B
A= exists $params_ref->{"CHECKSUM"}B= $params_ref->{"CHECKSUM"}
ABresult
0X0
100
111
203 3 eval { require Digest::SHA1; };
204 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
205 0 croak 'cannot checksum as lack Digest::SHA1';
206 }
207 3 $digest = Digest::SHA1->new;
208 } else {
209 # so can rely on 'exists' test elsewhere hereafter
210 26 delete $params_ref->{CHECKSUM};
211 }
212  
213 29 return $digest;
214 }
215  
216 23 ▶sub _resolve {
Subroutine _resolve: called CC=29 SCAR=41.9
217 23 my ( $tmp_fh, $tmp_filename, $params_ref, $digest ) = @_;
218  
219 23 ▶ if ( exists $params_ref->{CHECKSUM}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params_ref->{"CHECKSUM"} and not exists $params_ref->{"checksum"}) 1 22 23
Condition100%3/3
exists $params_ref->{"CHECKSUM"} and not exists $params_ref->{"checksum"}
!ll&&!rl&&r
2021
MC/DC100%2/2
exists $params_ref->{"CHECKSUM"} and not exists $params_ref->{"checksum"}
exists $params_ref->{"CHECKSUM"} not exists $params_ref->{"checksum"}
Truth table100%3/3
A and not(B)
A= exists $params_ref->{"CHECKSUM"}B= not exists $params_ref->{"checksum"}
ABresult
0X0
100
111
220 and !exists $params_ref->{checksum} ) {
221 1 $params_ref->{checksum} = $digest->hexdigest;
222 }
223  
224 # help the bits reach the disk?
225 23 ▶ $tmp_fh->flush() or die "flush() error: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$tmp_fh->flush or die "flush() error: $!\n" 23 0 23
226 # TODO may need eval or exclude on other platforms
227 23 ▶ if ( $^O !~ m/Win32/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O =~ /Win32/) 23 0 23
228 23 ▶ $tmp_fh->sync() or die "sync() error: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$tmp_fh->sync or die "sync() error: $!\n" 23 0 23
229 }
230  
231 23 eval {
232 23 ▶ if ( exists $params_ref->{min_size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params_ref->{"min_size"}) 2 21 23
233 2 _check_min_size( $tmp_fh, $params_ref->{min_size} );
234 }
235 22 ▶ if ( exists $params_ref->{CHECKSUM} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params_ref->{"CHECKSUM"}) 3 19 22
236 3 _check_checksum( $tmp_fh, $params_ref->{checksum} );
237 }
238 };
239 23 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 22 23
240 1 _cleanup( $tmp_fh, $tmp_filename );
241 1 die $@;
242 }
243  
244 # recommended by perlport(1) prior to unlink/rename calls
245 #
246 # TODO I've seen false positives from close() calls (from a very old
247 # version of XML::LibXML) though certain file systems only report
248 # errors at close() time. if someone can document a false positive,
249 # instead create an option and let the caller decide...
250 22 ▶ close($tmp_fh) or die "problem closing filehandle: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $tmp_fh or die "problem closing filehandle: $!\n" 22 0 22
251  
252 # spare subsequent useless close attempts, if any
253 22 undef $tmp_fh;
254  
255 22 ▶ if ( exists $params_ref->{mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params_ref->{"mode"}) 4 18 22
256 4 my $mode = $params_ref->{mode};
257 4 ▶ croak 'invalid mode data'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode and $mode =~ /^[0-9]+$/ 0 4 4
Condition33%1/3
defined $mode and $mode =~ /^[0-9]+$/
!ll&&!rl&&r
004
MC/DC0%0/2
defined $mode and $mode =~ /^[0-9]+$/
defined $mode $mode =~ /^[0-9]+$/
Truth table33%1/3
A and B
A= defined $modeB= $mode =~ /^[0-9]+$/
ABresult
0X0
100
111
258 if !defined $mode
259 or $mode !~ m/^[0-9]+$/;
260  
261 4 ▶ my $int_mode = substr( $mode, 0, 1 ) eq '0' ? oct($mode) : ( $mode + 0 );
Branch: 1 branch
BranchTrueFalseTotal
substr($mode, 0, 1) eq '0' ? : 2 2 4
262  
263 4 my $count = chmod( $int_mode, $tmp_filename );
264 4 ▶ if ( $count != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count != 1) 0 4 4
265 0 my $save_errstr = $!;
266 0 _cleanup( $tmp_fh, $tmp_filename );
267 0 die "unable to chmod temporary file: $save_errstr\n";
268 }
269 }
270  
271 22 ▶ if ( exists $params_ref->{owner} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params_ref->{"owner"}) 0 22 22
272 0 eval { _set_ownership( $tmp_filename, $params_ref->{owner} ); };
273 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
274 0 _cleanup( $tmp_fh, $tmp_filename );
275 0 die $@;
276 }
277 }
278  
279 22 ▶ if ( exists $params_ref->{mtime} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params_ref->{"mtime"}) 1 21 22
280 croak 'invalid mtime data'
281 if !defined $params_ref->{mtime}
282 1 ▶ or $params_ref->{mtime} !~ m/^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params_ref->{"mtime"} and $params_ref->{"mtime"} =~ /^[0-9]+$/ 0 1 1
Condition33%1/3
defined $params_ref->{"mtime"} and $params_ref->{"mtime"} =~ /^[0-9]+$/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $params_ref->{"mtime"} and $params_ref->{"mtime"} =~ /^[0-9]+$/
defined $params_ref->{"mtime"} $params_ref->{"mtime"} =~ /^[0-9]+$/
Truth table33%1/3
A and B
A= defined $params_ref->{"mtime"}B= $params_ref->{"mtime"} =~ /^[0-9]+$/
ABresult
0X0
100
111
283  
284 1 my ($file_atime) = ( stat $tmp_filename )[8];
285 1 my $count = utime( $file_atime, $params_ref->{mtime}, $tmp_filename );
286 1 ▶ if ( $count != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count != 1) 0 1 1
287 0 my $save_errstr = $!;
288 0 _cleanup( $tmp_fh, $tmp_filename );
289 0 die "unable to utime temporary file: $save_errstr\n";
290 }
291 }
292  
293 # If the file does not exist, but the backup does;
294 # the backup is left unmodified
295 22 ▶ if ( exists $params_ref->{backup} && -f $params_ref->{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params_ref->{"backup"} and -f $params_ref->{"file"}) 1 21 22
Condition100%3/3
exists $params_ref->{"backup"} and -f $params_ref->{"file"}
!ll&&!rl&&r
2011
MC/DC100%2/2
exists $params_ref->{"backup"} and -f $params_ref->{"file"}
exists $params_ref->{"backup"} -f $params_ref->{"file"}
Truth table100%3/3
A and B
A= exists $params_ref->{"backup"}B= -f $params_ref->{"file"}
ABresult
0X0
100
111
296 croak 'invalid backup suffix'
297 if !defined $params_ref->{backup}
298 1 ▶ or $params_ref->{backup} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $params_ref->{"backup"} or $params_ref->{"backup"} eq "" 0 1 1
Condition33%1/3
not defined $params_ref->{"backup"} or $params_ref->{"backup"} eq ""
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $params_ref->{"backup"} or $params_ref->{"backup"} eq ""
not defined $params_ref->{"backup"} $params_ref->{"backup"} eq ""
Truth table33%1/3
not(A) or B
A= not defined $params_ref->{"backup"}B= $params_ref->{"backup"} eq ""
ABresult
1X1
011
000
299  
300 # The backup file will be hardlinked in same directory as original
301 1 my $backup_filename = $params_ref->{file} . $params_ref->{backup};
302 1 ▶ if ( -f $backup_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $backup_filename) 1 0 1
303 1 my $count = unlink($backup_filename);
304 1 ▶ if ( $count != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count != 1) 0 1 1
305 0 my $save_errstr = $!;
306 0 _cleanup( $tmp_fh, $tmp_filename );
307 0 die "unable to unlink existing backup file: $save_errstr\n";
308 }
309 }
310  
311 # make hardlink -- Haiku OS does not appear to support these;
312 # http://www.cpantesters.org/cpan/report/7c2a3994-bc30-11e8-83ca-8681f4fbe649
313 # which is warned about in the perlport POD
314 1 ▶ if ( !link( $params_ref->{file}, $backup_filename ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (link $params_ref->{"file"}, $backup_filename) 0 1 1
315 0 my $save_errstr = $!;
316 0 _cleanup( $tmp_fh, $tmp_filename );
317 0 die "unable to link existing file to backup file: $save_errstr\n";
318 }
319 }
320  
321 22 ▶ unless ( rename( $tmp_filename, $params_ref->{file} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $tmp_filename, $params_ref->{"file"}) 0 22 22
322 0 my $save_errstr = $!;
323 0 _cleanup( $tmp_fh, $tmp_filename );
324 0 croak "unable to rename file: $save_errstr";
325 }
326  
327 # spare subsequent useless unlink attempts, if any
328 22 undef $tmp_filename;
329  
330 22 return 1;
331 }
332  
333 3 ▶sub _mkpath {
Subroutine _mkpath: called CC=3 SCAR=11.0
334 3 my ( $mkpath, $directory ) = @_;
335  
336 3 ▶ if ($mkpath) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mkpath) { } 2 1 3
337 2 mkpath($directory);
338 2 ▶ croak "could not create parent directory" unless -d $directory;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $directory 0 2 2
339 } else {
340 1 croak "parent directory does not exist";
341 }
342  
343 2 return 1;
344 }
345  
346 3 ▶sub _check_checksum {
Subroutine _check_checksum: called CC=3 SCAR=11.4
347 3 my ( $tmp_fh, $checksum ) = @_;
348  
349 3 ▶ seek( $tmp_fh, 0, SEEK_SET )
Branch: 1 branch
BranchTrueFalseTotal
seek $tmp_fh, 0, 0 or die "tmp fh seek() error: $!\n" 3 0 3
350 or die("tmp fh seek() error: $!\n");
351  
352 3 my $digest = Digest::SHA1->new;
353 3 $digest->addfile($tmp_fh);
354  
355 3 my $on_disk_checksum = $digest->hexdigest;
356  
357 3 ▶ if ( $on_disk_checksum ne $checksum ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_disk_checksum ne $checksum) 0 3 3
358 0 croak 'temporary file SHA1 hexdigest does not match supplied checksum';
359 }
360  
361 3 return 1;
362 }
363  
364 2 ▶sub _check_min_size {
Subroutine _check_min_size: called CC=4 SCAR=14.3
365 2 my ( $tmp_fh, $min_size ) = @_;
366  
367 # must seek, as OO method allows the fh or filename to be passed off
368 # and used by who knows what first
369 2 ▶ seek( $tmp_fh, 0, SEEK_END )
Branch: 1 branch
BranchTrueFalseTotal
seek $tmp_fh, 0, 2 or die "tmp fh seek() error: $!\n" 2 0 2
370 or die("tmp fh seek() error: $!\n");
371  
372 2 my $written = tell($tmp_fh);
373 2 ▶ if ( $written == -1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($written == -1) { } 0 2 2
elsif ($written < $min_size) { } 1 1 2
374 0 die("tmp fh tell() error: $!\n");
375 } elsif ( $written < $min_size ) {
376 1 croak 'bytes written failed to exceed min_size required';
377 }
378  
379 1 return 1;
380 }
381  
382 # accepts "0" or "user:group" type ownership details and a filename,
383 # attempts to set ownership rights on that filename. croak()s if
384 # anything goes awry
385 0 ▶sub _set_ownership {
Subroutine _set_ownership: not called CC=12 SCAR=50.5
386 0 my ( $filename, $owner ) = @_;
387  
388 0 ▶ croak 'invalid owner data' if !defined $owner or length $owner < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $owner or length $owner < 1 0 0 0
Condition0%0/3
not defined $owner or length $owner < 1
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $owner or length $owner < 1
not defined $owner length $owner < 1
Truth table0%0/3
not(A) or B
A= not defined $ownerB= length $owner < 1
ABresult
1X1
011
000
389  
390 # defaults if nothing comes of the subsequent parsing
391 0 my ( $uid, $gid ) = ( -1, -1 );
392  
393 0 my ( $user_name, $group_name ) = split /[:.]/, $owner, 2;
394  
395 0 my ( $login, $pass, $user_uid, $user_gid );
396  
397 # only customize user if have something from caller
398 0 ▶ if ( defined $user_name and $user_name ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user_name and $user_name ne "") 0 0 0
Condition0%0/3
defined $user_name and $user_name ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $user_name and $user_name ne ""
defined $user_name $user_name ne ""
Truth table0%0/3
A and B
A= defined $user_nameB= $user_name ne ""
ABresult
0X0
100
111
399 0 ▶ if ( $user_name =~ m/^([0-9]+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_name =~ /^([0-9]+)$/) { } 0 0 0
400 0 $uid = $1;
401 } else {
402 0 ▶ ( $login, $pass, $user_uid, $user_gid ) = getpwnam($user_name)
Branch: 1 branch
BranchTrueFalseTotal
() = getpwnam $user_name or croak("user not in password database") 0 0 0
403 or croak 'user not in password database';
404 0 $uid = $user_uid;
405 }
406 }
407  
408 # only customize group if have something from caller
409 0 ▶ if ( defined $group_name and $group_name ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $group_name and $group_name ne "") 0 0 0
Condition0%0/3
defined $group_name and $group_name ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $group_name and $group_name ne ""
defined $group_name $group_name ne ""
Truth table0%0/3
A and B
A= defined $group_nameB= $group_name ne ""
ABresult
0X0
100
111
410 0 ▶ if ( $group_name =~ m/^([0-9]+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_name =~ /^([0-9]+)$/) { } 0 0 0
411 0 $gid = $1;
412 } else {
413 0 ▶ my ( $group_name, $pass, $group_gid ) = getgrnam($group_name)
Branch: 1 branch
BranchTrueFalseTotal
() = getgrnam $group_name or croak("group not in group database") 0 0 0
414 or croak 'group not in group database';
415 0 $gid = $group_gid;
416 }
417 }
418  
419 0 my $count = chown( $uid, $gid, $filename );
420 0 ▶ if ( $count != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count != 1) 0 0 0
421 0 die "unable to chown temporary file\n";
422 }
423  
424 0 return 1;
425 }
426  
427 4 1;
428 __END__
↑ Summary