lib/Convert/Pheno/IO/Atomic.pm

filter: Statementstmt 83.5% 61 / 73 Branchbran 61.3% 27 / 44 Conditioncond 53.3% 8 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 4 totaltotal 70.1% 108 / 154 CC 28 SCAR 31.4
CC
28
Cov
73%
commit_staged_path
48.4
create_staged_path
8.9
discard_staged_path
4.1
CRAP 23.2
« lib/Convert/Pheno/ClinicalCDM/I2B2.pm ↑ Summary lib/Convert/Pheno/OpenEHR/ToBFF.pm »
1 package Convert::Pheno::IO::Atomic;
2  
3 136 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 136 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 136 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 136 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 136 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 136 our @EXPORT_OK = qw(
11 commit_staged_path
12 create_staged_path
13 discard_staged_path
14 write_atomically
15 );
16  
17 291 ▶sub create_staged_path {
Subroutine create_staged_path: called CC=8 SCAR=21.9
Pod undocumented
18 291 my ($target) = @_;
19 291 ▶ die "Atomic output target is required\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target and length $target 1 290 291
Condition66%2/3
defined $target and length $target
!ll&&!rl&&r
10290
MC/DC50%1/2
defined $target and length $target
defined $target length $target
Truth table66%2/3
A and B
A= defined $targetB= length $target
ABresult
0X0
100
111
20 unless defined $target && length $target;
21  
22 290 ▶ my $suffix = $target =~ /(\.[A-Za-z0-9]+(?:\.gz)?)\z/ ? $1 : '.tmp';
Branch: 1 branch
BranchTrueFalseTotal
$target =~ /(\.[A-Za-z0-9]+(?:\.gz)?)\z/ ? : 289 1 290
23 290 my ( $fh, $staged ) = tempfile(
24 '.convert-pheno-XXXXXX',
25 DIR => dirname($target),
26 SUFFIX => $suffix,
27 UNLINK => 0,
28 );
29 289 ▶ close $fh or die "Could not close staged output <$staged>: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Could not close staged output <$staged>: $!\n" 289 0 289
30  
31 289 my $mode = 0666 & ~umask();
32 289 ▶ if ( -e $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $target) 85 204 289
33 85 my @stat = stat $target;
34 85 ▶ $mode = $stat[2] & 07777 if @stat;
Branch: 1 branch
BranchTrueFalseTotal
if @stat 85 0 85
35 }
36 289 ▶ unless ( chmod $mode, $staged ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chmod $mode, $staged) 0 289 289
37 0 my $error = $!;
38 0 unlink $staged;
39 0 die "Could not set permissions on staged output <$staged>: $error\n";
40 }
41 289 return $staged;
42 }
43  
44 7 ▶sub discard_staged_path {
Subroutine discard_staged_path: called CC=4 SCAR=14.1
Pod undocumented
45 7 my ($staged) = @_;
46 7 ▶ return 1 unless defined $staged && -e $staged;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $staged and -e $staged 1 6 7
Condition66%2/3
defined $staged and -e $staged
!ll&&!rl&&r
106
MC/DC50%1/2
defined $staged and -e $staged
defined $staged -e $staged
Truth table66%2/3
A and B
A= defined $stagedB= -e $staged
ABresult
0X0
100
111
47 6 ▶ unlink $staged or die "Could not remove staged output <$staged>: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
unlink $staged or die "Could not remove staged output <$staged>: $!\n" 6 0 6
48 6 return 1;
49 }
50  
51 286 ▶sub commit_staged_path {
Subroutine commit_staged_path: called CC=15 SCAR=38.8
Pod undocumented
52 286 my ( $staged, $target ) = @_;
53 286 ▶ die "Staged output file is missing\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $staged and -f $staged 1 285 286
Condition66%2/3
defined $staged and -f $staged
!ll&&!rl&&r
01285
MC/DC50%1/2
defined $staged and -f $staged
defined $staged -f $staged
Truth table66%2/3
A and B
A= defined $stagedB= -f $staged
ABresult
0X0
100
111
54 unless defined $staged && -f $staged;
55  
56 # Some writers replace the staged inode instead of truncating it. Reapply
57 # the destination mode immediately before publication in that case.
58 285 ▶ if ( -e $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $target) 85 200 285
59 85 my @stat = stat $target;
60 85 ▶ if (@stat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stat) 85 0 85
61 85 my $mode = $stat[2] & 07777;
62 85 ▶ chmod $mode, $staged
Branch: 1 branch
BranchTrueFalseTotal
chmod $mode, $staged or die "Could not preserve permissions for staged output <$staged>: $!\n" 85 0 85
63 or die "Could not preserve permissions for staged output <$staged>: $!\n";
64 }
65 }
66  
67 285 ▶ return 1 if rename $staged, $target;
Branch: 1 branch
BranchTrueFalseTotal
if rename $staged, $target 283 2 285
68 2 my $rename_error = $!;
69  
70 # Windows cannot replace an existing path with rename(). Keep a recoverable
71 # backup until the fully written staged file has taken its place.
72 # The Windows replacement fallback is valid only for an existing regular
73 # file. Never move a directory or symbolic link out of the way.
74 2 ▶ if ( $^O eq 'MSWin32' && -f $target && !-l $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" and -f $target and not -l $target) 0 2 2
Condition33%2/6
$^O eq "MSWin32" and -f $target and not -l $target
!ll&&!rl&&r
200
$^O eq "MSWin32" and -f $target
!ll&&!rl&&r
200
MC/DC0%0/3
$^O eq "MSWin32" and -f $target and not -l $target
$^O eq "MSWin32" -f $target not -l $target
Truth table25%1/4
A and B and not(C)
A= $^O eq "MSWin32"B= -f $targetC= not -l $target
ABCresult
0XX0
10X0
1100
1111
75 0 my $backup = create_staged_path($target);
76 0 ▶ unlink $backup
Branch: 1 branch
BranchTrueFalseTotal
unlink $backup or die "Could not prepare backup path for <$target>: $!\n" 0 0 0
77 or die "Could not prepare backup path for <$target>: $!\n";
78 0 ▶ rename $target, $backup
Branch: 1 branch
BranchTrueFalseTotal
rename $target, $backup or die "Could not preserve existing output <$target>: $!\n" 0 0 0
79 or die "Could not preserve existing output <$target>: $!\n";
80  
81 0 ▶ if ( rename $staged, $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rename $staged, $target) 0 0 0
82 0 ▶ warn "Could not remove output backup <$backup>: $!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless unlink $backup 0 0 0
83 unless unlink $backup;
84 0 return 1;
85 }
86  
87 0 my $replacement_error = $!;
88 0 ▶ rename $backup, $target
Branch: 1 branch
BranchTrueFalseTotal
rename $backup, $target or die "Could not replace <$target> ($replacement_error) or restore it ($!)\n" 0 0 0
89 or die "Could not replace <$target> ($replacement_error) or restore it ($!)\n";
90 0 die "Could not replace output <$target>: $replacement_error\n";
91 }
92  
93 2 die "Could not replace output <$target>: $rename_error\n";
94 }
95  
96 279 ▶sub write_atomically {
Subroutine write_atomically: called CC=4 SCAR=13.9
Pod undocumented
97 279 my ( $target, $writer ) = @_;
98 279 ▶ die "Atomic output writer must be a code reference\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $writer eq "CODE" 1 278 279
99 unless ref($writer) eq 'CODE';
100  
101 278 my $staged = create_staged_path($target);
102 278 my $ok = eval {
103 278 $writer->($staged);
104 277 1;
105 };
106 278 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 277 278
107 1 my $error = $@;
108 1 discard_staged_path($staged);
109 1 die $error;
110 }
111  
112 277 my $committed = eval {
113 277 commit_staged_path( $staged, $target );
114 276 1;
115 };
116 277 ▶ unless ($committed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($committed) 1 276 277
117 1 my $error = $@;
118 1 discard_staged_path($staged);
119 1 die $error;
120 }
121  
122 276 return 1;
123 }
124  
125 136 1;
« lib/Convert/Pheno/ClinicalCDM/I2B2.pm ↑ Summary lib/Convert/Pheno/OpenEHR/ToBFF.pm »