blib/lib/File/Symlink/Atomic.pm

filter: Statementstmt 100% 29 / 29 Branchbran 50.0% 3 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 92.8% 39 / 42 CC 5 SCAR 11.2
CC
5
Cov
91%
symlink
5.1
BEGIN
1.0
BEGIN
1.0
CRAP 3.1
↑ Summary
1 package File::Symlink::Atomic;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 # ABSTRACT: an atomic drop-in replacement for CORE::symlink
5 2 our $VERSION = '0.002'; # VERSION
6  
7 2 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 our @EXPORT = qw(symlink); ## no critic(Modules::ProhibitAutomaticExportation)
11  
12
13 1 ▶sub symlink($$) { ## no critic(Subroutines::ProhibitSubroutinePrototypes Subroutines::ProhibitBuiltinHomonyms)
Subroutine symlink: called CC=5 SCAR=16.4
Pod documented
14 1 my $symlink_target = shift;
15 1 my $symlink_name = shift;
16  
17 1 my ($volume, $dirs, $file) = File::Spec->splitpath($symlink_name);
18 1 my $template = File::Spec->catpath($volume, $dirs, ".$file.$$.XXXXXX");
19 1 my $tmp_symlink_name;
20 ATTEMPT:
21 1 for (1..10) { # try 10 times
22 1 $tmp_symlink_name = mktemp($template);
23 1 ▶ symlink $symlink_target, $tmp_symlink_name and last ATTEMPT;
Branch: 1 branch
BranchTrueFalseTotal
CORE::symlink $symlink_target, $tmp_symlink_name and last ATTEMPT 1 0 1
24 }
25 1 ▶ return 0 unless -l $tmp_symlink_name; # wtf?
Branch: 1 branch
BranchTrueFalseTotal
unless -l $tmp_symlink_name 0 1 1
26
27 1 ▶ rename $tmp_symlink_name, $symlink_name or return 0; # should be atomic
Branch: 1 branch
BranchTrueFalseTotal
rename $tmp_symlink_name, $symlink_name or return 0 1 0 1
28 1 return 1;
29 }
30  
31 2 1;
32  
33  
34 __END__
↑ Summary