File Coverage

lib/Rex/Shared/Var/Common.pm
Criterion Covered Total %
statement 29 29 100.0
branch 10 12 83.3
condition n/a
subroutine 10 10 100.0
pod n/a
total 49 51 96.0


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Shared::Var::Common;
6              
7 63     63   818 use v5.12.5;
  63         231  
8 63     63   360 use warnings;
  63         129  
  63         2350  
9              
10             require Exporter;
11 63     63   497 use base qw/Exporter/;
  63         132  
  63         7017  
12             our @EXPORT_OK = qw/__lock __store __retrieve/;
13              
14             our $VERSION = '1.14.2.2'; # TRIAL VERSION
15              
16 63     63   553 use Fcntl qw(:DEFAULT :flock);
  63         154  
  63         32644  
17 63     63   5340 use Storable;
  63         185757  
  63         4167  
18 63     63   537 use File::Spec;
  63         147  
  63         3021  
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             our $PARENT_PID = $$;
24             our $FILE = File::Spec->catfile( File::Spec->tmpdir(), "vars.db.$PARENT_PID" );
25             our $LOCK_FILE =
26             File::Spec->catfile( File::Spec->tmpdir(), "vars.db.lock.$PARENT_PID" );
27              
28             sub __lock {
29 1120 50   1120   64552 sysopen( my $dblock, $LOCK_FILE, O_RDONLY | O_CREAT ) or die($!);
30 1120 50       32941 flock( $dblock, LOCK_EX ) or die($!);
31              
32 1120         5824 my $ret = $_[0]->();
33              
34 1120         65007 close($dblock);
35              
36 1120         15479 return $ret;
37             }
38              
39             sub __store {
40 292     292   685 my $ref = shift;
41 292         1505 store( $ref, $FILE );
42             }
43              
44             sub __retrieve {
45 1120 100   1120   16646 return {} unless -f $FILE;
46 1102         6965 return retrieve($FILE);
47              
48             }
49              
50             sub END {
51              
52             # return if we exiting a child process
53 63 100   63   52384 return unless $$ eq $PARENT_PID;
54              
55             # we are exiting the master process
56 28 100       3399 unlink $FILE if -f $FILE;
57 28 100       3101 unlink $LOCK_FILE if -f $LOCK_FILE;
58             }
59              
60             1;