lib/Ubic/Lockf.pm

filter: Statementstmt 87.3% 62 / 71 Branchbran 64.2% 18 / 28 Conditioncond 60.0% 9 / 15 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 85.7% 12 / 14 Podpod 100% 3 / 3 totaltotal 77.7% 108 / 139 SCAR 32.4
CC
21
Cov
78%
_lockf
21.7
lockf
7.9
DESTROY
2.0
CRAP 25.7
« blib/lib/Ubic/UA.pm ↑ Summary lib/Ubic/Watchdog.pm »
1 package Ubic::Lockf;
2 171 $Ubic::Lockf::VERSION = '1.59';
3 171 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 171 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: file locker with an automatic out-of-scope unlocking mechanism
7  
8  
9 171 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 171 ▶use Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 171 ▶use POSIX qw(:errno_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 171 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 171 ▶use Ubic::Lockf::Alarm;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 171 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 171 our @EXPORT = qw(lockf);
20  
21  
22 sub DESTROY ($) {
23 313 ▶ my ($self) = @_;
Subroutine DESTROY: called CC=2 SCAR=6.9
24 313 local $@;
25 313 my $fh = $self->{_fh};
26 313 ▶ return unless defined $fh; # already released or dissolved
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 2 311 313
27 311 flock $fh, LOCK_UN;
28 311 delete $self->{_fh}; # closes the file if opened by us
29 }
30  
31 171 my %defaults = (
32 shared => 0,
33 blocking => 1,
34 timeout => undef,
35 mode => undef,
36 );
37  
38 sub lockf ($;$) {
39 717 ▶ my ($param, $opts) = validate_pos(@_, 1, 0);
Subroutine lockf: called CC=7 SCAR=20.6
Pod documented
40 717 ▶ $opts ||= {};
Condition100%2/2
$opts ||= {}
l!l
558159
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
41 717 $opts = validate(@{ [ $opts ] }, {
42 blocking => 0,
43 shared => 0,
44 silent => 0, # deprecated option, does nothing
45 timeout => 0,
46 mode => 0,
47 });
48 717 $opts = {%defaults, %$opts};
49  
50 717 my ($fh, $fname);
51 717 ▶ if (ref $param eq "") { # filename instead of filehandle
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param eq '') { } 717 0 717
52 717 ▶ open $fh, ">>", $param or die "Can't open $param: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">>", $param or die "Can't open ${param}: $!" 0 717 717
53 717 $fname = $param;
54 } else {
55 0 $fh = $param;
56 }
57  
58 717 ▶ unless (_lockf($fh, $opts, $fname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_lockf($fh, $opts, $fname)) 364 353 717
59 364 return;
60 }
61  
62 # don't check chmod success - it can fail and it's ok
63 353 ▶ chmod ($opts->{mode}, ($fname || $fh)) if defined $opts->{mode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"mode"} 0 353 353
Condition0%0/3
$fname || $fh
l!l&&r!l&&!r
000
MC/DC0%0/2
$fname || $fh
$fname $fh
Truth table0%0/3
A || B
A= $fnameB= $fh
ABresult
1X1
011
000
64  
65 353 return bless {
66 _fh => $fh,
67 _fname => $fname,
68 };
69 }
70  
71 sub _lockf ($$;$) {
72 717 ▶ my ($fh, $opts, $fname) = @_;
Subroutine _lockf: called CC=14 SCAR=30.8
73 717 ▶ $fname ||= ''; # TODO - discover $fname from $fh, it's possible in most cases with some /proc magic
Condition50%1/2
$fname ||= ""
l!l
7170
MC/DC0%0/1
$fname ||= ""
$fname
Truth table50%1/2
A
A= $fname
Aresult
11
00
74  
75 717 ▶ my $mode = ($opts->{shared} ? LOCK_SH : LOCK_EX);
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'shared'} ? : 0 717 717
76  
77 717 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts->{"blocking"} or defined $opts->{"timeout"} and not $opts->{"timeout"}) 447 270 717
Condition100%6/6
not $opts->{"blocking"} or defined $opts->{"timeout"} and not $opts->{"timeout"}
l!l&&r!l&&!r
78434205
defined $opts->{"timeout"} and not $opts->{"timeout"}
!ll&&!rl&&r
24129369
MC/DC100%3/3
not $opts->{"blocking"} or defined $opts->{"timeout"} and not $opts->{"timeout"}
not $opts->{"blocking"} defined $opts->{"timeout"} not $opts->{"timeout"}
Truth table100%4/4
not(A) or B and not(C)
A= not $opts->{"blocking"}B= defined $opts->{"timeout"}C= not $opts->{"timeout"}
ABCresult
1XX1
0111
00X0
0100
78 not $opts->{blocking}
79 or (defined $opts->{timeout} and not $opts->{timeout}) # timeout=0
80 ) {
81 447 ▶ return 1 if flock ($fh, $mode | LOCK_NB);
Branch: 1 branch
BranchTrueFalseTotal
if flock $fh, $mode | 4 83 364 447
82 364 ▶ return 0 if ($! == EWOULDBLOCK);
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 364 0 364
83 0 ▶ croak "flock ".($fname || '')." failed: $!";
Condition0%0/2
$fname || ''
l!l
00
MC/DC0%0/1
$fname || ''
$fname
Truth table0%0/2
A
A= $fname
Aresult
11
00
84 }
85  
86 270 ▶ unless (flock ($fh, $mode | LOCK_NB)) {
Branch: 1 branch
BranchTrueFalseTotal
if (flock $fh, $mode | 4) { } 269 1 270
87 1 my $msg = "$fname already locked, wait...";
88 1 ▶ if (-t STDOUT) {
Branch: 1 branch
BranchTrueFalseTotal
if (-t STDOUT) 0 1 1
89 0 print $msg;
90 }
91 } else {
92 269 return 1;
93 }
94  
95 1 ▶ if ($opts->{timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'timeout'}) { } 0 1 1
96 0 ▶ local $SIG{ALRM} = sub { croak "flock $fname failed: timed out" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 0 my $alarm = Ubic::Lockf::Alarm->new($opts->{timeout});
98 0 ▶ flock $fh, $mode or die "flock failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, $mode or die "flock failed: $!" 0 0 0
99 } else {
100 1 ▶ flock $fh, $mode or die "flock failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, $mode or die "flock failed: $!" 0 1 1
101 }
102 1 return 1;
103 }
104  
105 sub name($)
106 {
107 0 ▶ my $self = shift;
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
108 0 return $self->{_fname};
109 }
110  
111 sub dissolve {
112 29 ▶ my $self = shift;
Subroutine dissolve: called CC=1 SCAR=0.0
Pod documented
113 29 undef $self->{_fh};
114 }
115  
116 171 1;
117  
118 __END__
« blib/lib/Ubic/UA.pm ↑ Summary lib/Ubic/Watchdog.pm »