lib/Rex/Shared/Var/Common.pm

filter: Statementstmt 100% 36 / 36 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 96.5% 56 / 58 CC 7 SCAR 8.2
CC
7
Cov
96%
END
4.0
__lock
3.1
__retrieve
2.0
CRAP 2.3
« lib/Rex/Interface/Connection/Local.pm ↑ Summary lib/Rex/Shared/Var.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Shared::Var::Common;
6  
7 63 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 63 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 63 require Exporter;
11 63 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 63 our @EXPORT_OK = qw/__lock __store __retrieve/;
13  
14 63 our $VERSION = '1.16.1'; # VERSION
15  
16 63 ▶use Fcntl qw(:DEFAULT :flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 63 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 63 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # $PARENT_PID gets set when Rex starts. This value remains the same after the
21 # process forks. So $PARENT_PID is always the pid of the parent process. $$
22 # however is always the pid of the current process.
23 63 our $PARENT_PID = $$;
24 63 our $FILE = File::Spec->catfile( File::Spec->tmpdir(), "vars.db.$PARENT_PID" );
25 63 our $LOCK_FILE =
26 File::Spec->catfile( File::Spec->tmpdir(), "vars.db.lock.$PARENT_PID" );
27  
28 1120 ▶sub __lock {
Subroutine __lock: called CC=3 SCAR=11.3
29 1120 ▶ sysopen( my $dblock, $LOCK_FILE, O_RDWR | O_CREAT ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $dblock, $Rex::Shared::Var::Common::LOCK_FILE, 66 or die $! 1120 0 1120
30 1120 ▶ flock( $dblock, LOCK_EX ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
flock $dblock, 2 or die $! 1120 0 1120
31  
32 1120 my $ret = $_[0]->();
33  
34 1120 close($dblock);
35  
36 1120 return $ret;
37 }
38  
39 292 ▶sub __store {
Subroutine __store: called CC=1 SCAR=0.0
40 292 my $ref = shift;
41 292 store( $ref, $FILE );
42 }
43  
44 1120 ▶sub __retrieve {
Subroutine __retrieve: called CC=2 SCAR=6.9
45 1120 ▶ return {} unless -f $FILE;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $Rex::Shared::Var::Common::FILE 18 1102 1120
46 1102 return retrieve($FILE);
47  
48 }
49  
50 63 ▶sub END {
Subroutine END: called CC=4 SCAR=13.9
51  
52 # return if we exiting a child process
53 63 ▶ return unless $$ eq $PARENT_PID;
Branch: 1 branch
BranchTrueFalseTotal
unless $$ eq $Rex::Shared::Var::Common::PARENT_PID 35 28 63
54  
55 # we are exiting the master process
56 28 ▶ unlink $FILE if -f $FILE;
Branch: 1 branch
BranchTrueFalseTotal
if -f $Rex::Shared::Var::Common::FILE 9 19 28
57 28 ▶ unlink $LOCK_FILE if -f $LOCK_FILE;
Branch: 1 branch
BranchTrueFalseTotal
if -f $Rex::Shared::Var::Common::LOCK_FILE 9 19 28
58 }
59  
60 63 1;
« lib/Rex/Interface/Connection/Local.pm ↑ Summary lib/Rex/Shared/Var.pm »