| 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
Condition66%2/3
defined $target and length $target
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
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | unless defined $target && length $target; | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 290 | ▶ | my $suffix = $target =~ /(\.[A-Za-z0-9]+(?:\.gz)?)\z/ ? $1 : '.tmp'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 289 | my $mode = 0666 & ~umask(); | |||||||||||||||||||||||||||||||||||||||||
| 32 | 289 | ▶ | if ( -e $target ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 85 | my @stat = stat $target; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 85 | ▶ | $mode = $stat[2] & 07777 if @stat; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 289 | ▶ | unless ( chmod $mode, $staged ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $staged and -e $staged
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
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 6 | ▶ | unlink $staged or die "Could not remove staged output <$staged>: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $staged and -f $staged
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 85 | my @stat = stat $target; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 85 | ▶ | if (@stat) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 85 | my $mode = $stat[2] & 07777; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 85 | ▶ | chmod $mode, $staged | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
$^O eq "MSWin32" and -f $target and not -l $target
$^O eq "MSWin32" and -f $target
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
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my $backup = create_staged_path($target); | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | unlink $backup | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | or die "Could not prepare backup path for <$target>: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | rename $target, $backup | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | or die "Could not preserve existing output <$target>: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ( rename $staged, $target ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | warn "Could not remove output backup <$backup>: $!\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | unless unlink $backup; | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | my $replacement_error = $!; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | rename $backup, $target | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||||