blib/lib/File/BasicFlock.pm

filter: Statementstmt 95.0% 58 / 61 Branchbran 59.0% 13 / 22 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 2 totaltotal 77.7% 84 / 108 CC 16 SCAR 19.9
CC
16
Cov
81%
lock
13.1
unlock
5.3
END
2.1
CRAP 7.3
↑ Summary
1 # Copyright (C) 1996, David Muir Sharnoff
2  
3 package File::BasicFlock;
4  
5 9 require Exporter;
6 9 @ISA = qw(Exporter);
7 9 @EXPORT = qw(lock unlock);
8  
9 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #
12 # It would be nice if I could use fcntl.ph and
13 # errno.ph, but alas, that isn't safe.
14 #
15 9 ▶use POSIX qw(EAGAIN ENOENT EEXIST O_RDWR);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Fcntl qw(LOCK_SH LOCK_EX LOCK_NB LOCK_UN);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 ▶use vars qw($VERSION %locks %lockHandle %shared $debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 $VERSION = 98.120200;
22 9 $debug = 0;
23 }
24  
25 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 9 ▶no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 9 my $gensym = "sym0000";
29  
30 sub lock
31 743 ▶{
Subroutine lock: called CC=11 SCAR=25.8
Pod undocumented
32 743 my ($file, $shared, $nonblocking) = @_;
33 #my $f = new FileHandle;
34  
35 743 $gensym++;
36 743 my $f = "File::BasicFlock::$gensym";
37  
38 743 my $previous = exists $locks{$file};
39  
40 743 ▶ unless (sysopen($f, $file, O_RDWR)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $f, $file, 2) 0 743 743
41 0 croak "open $file: $!";
42 }
43 743 ▶ $locks{$file} = $locks{$file} || 0;
Condition50%1/2
$File::BasicFlock::locks{$file} || 0
l!l
0743
MC/DC0%0/1
$File::BasicFlock::locks{$file} || 0
$File::BasicFlock::locks{$file}
Truth table50%1/2
A
A= $File::BasicFlock::locks{$file}
Aresult
11
00
44 743 $shared{$file} = $shared;
45
46 743 $lockHandle{$file} = $f;
47  
48 743 my $flags;
49  
50 743 ▶ $flags = $shared ? LOCK_SH : LOCK_EX;
Branch: 1 branch
BranchTrueFalseTotal
$shared ? : 0 743 743
51 743 ▶ $flags |= LOCK_NB
Branch: 1 branch
BranchTrueFalseTotal
if $nonblocking 699 44 743
52 if $nonblocking;
53
54 743 my $r = flock($f, $flags);
55  
56 743 ▶ print " ($$ " if $debug and $r;
Branch: 1 branch
BranchTrueFalseTotal
if $File::BasicFlock::debug and $r 0 743 743
Condition33%1/3
$File::BasicFlock::debug and $r
!ll&&!rl&&r
74300
MC/DC0%0/2
$File::BasicFlock::debug and $r
$File::BasicFlock::debug $r
Truth table33%1/3
A and B
A= $File::BasicFlock::debugB= $r
ABresult
0X0
100
111
57  
58 743 ▶ return 1 if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 65 678 743
59 678 ▶ if ($nonblocking and $! == EAGAIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nonblocking and $! == 11) 678 0 678
Condition33%1/3
$nonblocking and $! == 11
!ll&&!rl&&r
00678
MC/DC0%0/2
$nonblocking and $! == 11
$nonblocking $! == 11
Truth table33%1/3
A and B
A= $nonblockingB= $! == 11
ABresult
0X0
100
111
60 678 ▶ if (! $previous) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($previous) 678 0 678
61 678 delete $locks{$file};
62 678 my $f = $lockHandle{$file};
63 678 close($f);
64 678 delete $lockHandle{$file};
65 678 delete $shared{$file};
66 }
67 678 return 0;
68 }
69 0 croak "flock $f $flags: $!";
70 }
71  
72 sub unlock
73 65 ▶{
Subroutine unlock: called CC=5 SCAR=16.6
Pod undocumented
74 65 my ($file) = @_;
75  
76 65 ▶ croak "no lock on $file" unless exists $locks{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $File::BasicFlock::locks{$file} 0 65 65
77  
78 65 delete $locks{$file};
79 65 my $f = $lockHandle{$file};
80 65 delete $lockHandle{$file};
81  
82 65 ▶ return 0 unless defined $f;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f 0 65 65
83  
84 65 ▶ print " $$) " if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $File::BasicFlock::debug 0 65 65
85 65 ▶ flock($f, LOCK_UN)
Branch: 1 branch
BranchTrueFalseTotal
flock $f, 8 or croak("flock $f UN: $!") 65 0 65
86 or croak "flock $f UN: $!";
87  
88 65 close($f);
89 65 return 1;
90 }
91  
92 9 ▶END {
Subroutine END: called CC=2 SCAR=7.6
93 9 my $f;
94 9 for $f (keys %locks) {
95 0 &unlock($f);
96 }
97 }
98  
99 __DATA__
↑ Summary