Flock.pm

filter: Statementstmt 85.9% 171 / 199 Branchbran 54.8% 57 / 104 Conditioncond 54.2% 19 / 35 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 7 totaltotal 70.6% 270 / 382 CC 68 SCAR 34.5
CC
68
Cov
73%
final_cleanup_flock
84.4
lock_flock
35.2
unlock_flock
14.4
CRAP 31.5
↑ Summary Flock/Subprocess.pm »
1 package File::Flock;
2  
3 44 require Exporter;
4 44 @ISA = qw(Exporter);
5 44 @EXPORT = qw(lock unlock lock_rename forget_locks);
6  
7 44 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 44 ▶use POSIX qw(EAGAIN EACCES EWOULDBLOCK ENOENT EEXIST O_EXCL O_CREAT O_RDWR);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 44 ▶use Fcntl qw(LOCK_SH LOCK_EX LOCK_NB LOCK_UN);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 44 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 44 ▶use Data::Structure::Util qw(unbless);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 44 ▶use vars qw($VERSION $debug $av0debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 44 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 44 $VERSION = 2014.01;
17 44 $debug = 0;
18 44 $av0debug = 0;
19 }
20  
21 44 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 44 ▶no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 44 my %locks; # did we create the file?
25 my %lockHandle;
26 44 my %shared;
27 44 my %pid;
28 44 my %rm;
29  
30 74 ▶sub new_flock {
Subroutine new_flock: called CC=2 SCAR=7.1
Pod undocumented
31 74 my ($pkg, $file, $shared, $nonblocking) = @_;
32 74 ▶ lock_flock($file, $shared, $nonblocking) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
lock_flock($file, $shared, $nonblocking) or return undef 74 0 74
33 74 return bless [$file], $pkg;
34 }
35  
36 sub DESTROY
37 64 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
38 64 my ($this) = @_;
39 64 unlock_flock($this->[0]);
40 }
41  
42 sub lock_flock
43 749 ▶{
Subroutine lock_flock: called CC=27 SCAR=35.6
Pod undocumented
44 749 my ($file, $shared, $nonblocking) = @_;
45  
46 749 my $f = new IO::File;
47  
48 749 my $created = 0;
49 749 my $previous = exists $locks{$file};
50  
51 # the file may be springing in and out of existence...
52 OPEN:
53 749 for(;;) {
54 762 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 704 58 762
55 704 ▶ unless (sysopen($f, $file, O_RDWR)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $f, $file, 2) 1 703 704
56 1 ▶ redo OPEN if $! == ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 2 1 0 1
57 0 croak "open $file: $!";
58 }
59 } else {
60 58 ▶ unless (sysopen($f, $file, O_CREAT|O_EXCL|O_RDWR)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $f, $file, 194) 12 46 58
61 12 ▶ redo OPEN if $! == EEXIST;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 17 12 0 12
62 0 croak "open >$file: $!";
63 }
64 46 ▶ print STDERR " {$$ " if $debug; # }
Branch: 1 branch
BranchTrueFalseTotal
if $File::Flock::debug 0 46 46
65 46 $created = 1;
66 }
67 749 last;
68 }
69 749 ▶ $locks{$file} = $created || $locks{$file} || 0;
Condition80%4/5
$created || $locks{$file} || 0
l!l
46703
$created or $locks{$file}
l!l&&r!l&&!r
460703
MC/DC50%1/2
$created || $locks{$file} || 0
$created $locks{$file}
Truth table66%2/3
A or B
A= $createdB= $locks{$file}
ABresult
1X1
011
000
70 749 $shared{$file} = $shared;
71 749 $pid{$file} = $$;
72
73 749 $lockHandle{$file} = $f;
74  
75 749 my $flags;
76  
77 749 ▶ $flags = $shared ? LOCK_SH : LOCK_EX;
Branch: 1 branch
BranchTrueFalseTotal
$shared ? : 13 736 749
78 749 ▶ $flags |= LOCK_NB
Branch: 1 branch
BranchTrueFalseTotal
if $nonblocking 529 220 749
79 if $nonblocking;
80
81 749 ▶ local($0) = "$0 - locking $file" if $av0debug && ! $nonblocking;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Flock::av0debug and not $nonblocking 0 749 749
Condition33%1/3
$File::Flock::av0debug and not $nonblocking
!ll&&!rl&&r
74900
MC/DC0%0/2
$File::Flock::av0debug and not $nonblocking
$File::Flock::av0debug not $nonblocking
Truth table33%1/3
A and not(B)
A= $File::Flock::av0debugB= not $nonblocking
ABresult
0X0
100
111
82 749 my $r = flock($f, $flags);
83  
84 749 ▶ print STDERR " ($$ " if $debug and $r;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Flock::debug and $r 0 749 749
Condition33%1/3
$File::Flock::debug and $r
!ll&&!rl&&r
74900
MC/DC0%0/2
$File::Flock::debug and $r
$File::Flock::debug $r
Truth table33%1/3
A and B
A= $File::Flock::debugB= $r
ABresult
0X0
100
111
85  
86 749 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 281 468 749
87 # let's check to make sure the file wasn't
88 # removed on us!
89  
90 281 my $ifile = (stat($file))[1];
91 281 my $ihandle;
92 281 eval { $ihandle = (stat($f))[1] };
93 281 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 281 281
94  
95 281 ▶ return 1 if defined $ifile
Branch: 1 branch
BranchTrueFalseTotal
if defined $ifile and defined $ihandle and $ifile == $ihandle 269 12 281
Condition83%5/6
defined $ifile and defined $ihandle and $ifile == $ihandle
!ll&&!rl&&r
102269
defined $ifile and defined $ihandle
!ll&&!rl&&r
100271
MC/DC66%2/3
defined $ifile and defined $ihandle and $ifile == $ihandle
defined $ifile defined $ihandle $ifile == $ihandle
Truth table75%3/4
A and B and C
A= defined $ifileB= defined $ihandleC= $ifile == $ihandle
ABCresult
0XX0
10X0
1100
1111
96 and defined $ihandle
97 and $ifile == $ihandle;
98  
99 # oh well, try again
100 12 flock($f, LOCK_UN);
101 12 close($f);
102 12 return lock_flock($file);
103 }
104  
105 468 ▶ return 1 if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 468 468
106 468 ▶ if ($nonblocking and
Branch: 1 branch
BranchTrueFalseTotal
if ($nonblocking and $! == 11 || $! == 13 || $! == 11) 468 0 468
Condition33%3/9
$nonblocking and $! == 11 || $! == 13 || $! == 11
!ll&&!rl&&r
00468
$! == 11 or $! == 13 or $! == 11
l!l&&r!l&&!r
46800
$! == 11 or $! == 13
l!l&&r!l&&!r
46800
MC/DC0%0/4
$nonblocking and $! == 11 || $! == 13 || $! == 11
$nonblocking $! == 11 $! == 13 $! == 11
Truth table20%1/5
A and B or C or D
A= $nonblockingB= $! == 11C= $! == 13D= $! == 11
ABCDresult
0XXX0
10000
11XX1
101X1
10011
107 (($! == EAGAIN)
108 or ($! == EACCES)
109 or ($! == EWOULDBLOCK)))
110 {
111 468 ▶ if (! $previous) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($previous) 468 0 468
112 468 delete $locks{$file};
113 468 delete $lockHandle{$file};
114 468 delete $shared{$file};
115 468 delete $pid{$file};
116 }
117 468 ▶ if ($created) {
Branch: 1 branch
BranchTrueFalseTotal
if ($created) 1 467 468
118 # oops, a bad thing just happened.
119 # We don't want to block, but we made the file.
120 1 &background_remove($f, $file);
121 }
122 468 close($f);
123 468 return 0;
124 }
125 0 croak "flock $f $flags: $!";
126 }
127  
128 #
129 # get a lock on a file and remove it if it's empty. This is to
130 # remove files that were created just so that they could be locked.
131 #
132 # To do this without blocking, defer any files that are locked to the
133 # the END block.
134 #
135 sub background_remove
136 2 ▶{
Subroutine background_remove: called CC=4 SCAR=18.7
Pod undocumented
137 2 my ($f, $file) = @_;
138  
139 2 ▶ if (flock($f, LOCK_EX|LOCK_NB)) {
Branch: 1 branch
BranchTrueFalseTotal
if (flock $f, 6) { } 0 2 2
140 0 ▶ unlink($file)
Branch: 1 branch
BranchTrueFalseTotal
if -s $file == 0 0 0 0
141 if -s $file == 0;
142 0 flock($f, LOCK_UN);
143 0 return 1;
144 } else {
145 $rm{$file} = 1
146 2 ▶ unless exists $rm{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rm{$file} 2 0 2
147 2 return 0;
148 }
149 }
150  
151 sub unlock_flock
152 257 ▶{
Subroutine unlock_flock: called CC=13 SCAR=26.7
Pod undocumented
153 257 my ($file) = @_;
154  
155 257 ▶ if (ref $file eq 'File::Flock') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq "File::Flock") 10 247 257
156 10 unbless $file; # avoid destructor later
157 10 $file = $file->[0];
158 }
159  
160 257 ▶ croak "no lock on $file" unless exists $locks{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $locks{$file} 0 257 257
161 257 my $created = $locks{$file};
162 257 my $unlocked = 0;
163  
164  
165 257 my $size = -s $file;
166 257 ▶ if ($created && defined($size) && $size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($created and defined $size and $size == 0) 35 222 257
Condition66%4/6
$created and defined $size and $size == 0
!ll&&!rl&&r
222035
$created and defined $size
!ll&&!rl&&r
222035
MC/DC33%1/3
$created and defined $size and $size == 0
$created defined $size $size == 0
Truth table50%2/4
A and B and C
A= $createdB= defined $sizeC= $size == 0
ABCresult
0XX0
10X0
1100
1111
167 35 ▶ if ($shared{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shared{$file}) { } 1 34 35
168 $unlocked =
169 1 &background_remove($lockHandle{$file}, $file);
170 } else {
171 # {
172 34 ▶ print STDERR " $$} " if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Flock::debug 0 34 34
173 34 ▶ unlink($file)
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or croak("unlink ${file}: $!") 34 0 34
174 or croak "unlink $file: $!";
175 }
176 }
177 257 delete $locks{$file};
178 257 delete $pid{$file};
179  
180 257 my $f = $lockHandle{$file};
181  
182 257 delete $lockHandle{$file};
183  
184 257 ▶ return 0 unless defined $f;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f 0 257 257
185  
186 257 ▶ print STDERR " $$) " if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Flock::debug 0 257 257
187 257 ▶ $unlocked or flock($f, LOCK_UN)
Branch: 1 branch
BranchTrueFalseTotal
$unlocked or flock $f, 8 or croak("flock $file UN: $!") 257 0 257
Condition33%1/3
$unlocked or flock $f, 8
l!l&&r!l&&!r
02570
MC/DC0%0/2
$unlocked or flock $f, 8
$unlocked flock $f, 8
Truth table33%1/3
A or B
A= $unlockedB= flock $f, 8
ABresult
1X1
011
000
188 or croak "flock $file UN: $!";
189  
190 257 close($f);
191 257 return 1;
192 }
193  
194 sub lock_rename_flock
195 3 ▶{
Subroutine lock_rename_flock: called CC=9 SCAR=22.9
Pod undocumented
196 3 ▶ croak "arguments to lock_rename" unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 3 3
197 3 my ($oldfile, $newfile) = @_;
198  
199 3 ▶ if (ref $oldfile eq 'File::Flock') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $oldfile eq "File::Flock") 3 0 3
200 3 my $obj = $oldfile;
201 3 $oldfile = $obj->[0];
202 3 $obj->[0] = $newfile;
203 }
204 3 ▶ if (exists $locks{$newfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $locks{$newfile}) 0 3 3
205 0 unlock_flock($newfile);
206 }
207 3 delete $locks{$newfile};
208 3 delete $shared{$newfile};
209 3 delete $pid{$newfile};
210 3 delete $lockHandle{$newfile};
211 3 delete $rm{$newfile};
212  
213 3 ▶ $locks{$newfile} = $locks{$oldfile} if exists $locks{$oldfile};
Branch: 1 branch
BranchTrueFalseTotal
if exists $locks{$oldfile} 3 0 3
214 3 ▶ $shared{$newfile} = $shared{$oldfile} if exists $shared{$oldfile};
Branch: 1 branch
BranchTrueFalseTotal
if exists $shared{$oldfile} 3 0 3
215 3 ▶ $pid{$newfile} = $pid{$oldfile} if exists $pid{$oldfile};
Branch: 1 branch
BranchTrueFalseTotal
if exists $pid{$oldfile} 3 0 3
216 3 ▶ $lockHandle{$newfile} = $lockHandle{$oldfile} if exists $lockHandle{$oldfile};
Branch: 1 branch
BranchTrueFalseTotal
if exists $lockHandle{$oldfile} 3 0 3
217 3 ▶ $rm{$newfile} = $rm{$oldfile} if exists $rm{$oldfile};
Branch: 1 branch
BranchTrueFalseTotal
if exists $rm{$oldfile} 0 3 3
218  
219 3 delete $locks{$oldfile};
220 3 delete $shared{$oldfile};
221 3 delete $pid{$oldfile};
222 3 delete $lockHandle{$oldfile};
223 3 delete $rm{$oldfile};
224  
225 3 return 1;
226 }
227  
228 sub forget_locks_flock
229 1 ▶{
Subroutine forget_locks_flock: called CC=1 SCAR=0.0
Pod undocumented
230 1 %locks = ();
231 1 %shared = ();
232 1 %pid = ();
233 1 %lockHandle = ();
234 1 %rm = ();
235 }
236  
237 #
238 # Unlock any files that are still locked and remove any files
239 # that were created just so that they could be locked.
240 #
241  
242 sub final_cleanup_flock
243 14 ▶{
Subroutine final_cleanup_flock: called CC=17 SCAR=44.3
Pod undocumented
244 14 my $f;
245 14 for $f (keys %locks) {
246 unlock_flock($f)
247 16 ▶ if $pid{$f} == $$;
Branch: 1 branch
BranchTrueFalseTotal
if $pid{$f} == $$ 7 9 16
248 }
249  
250 14 my %bgrm;
251 14 for my $file (keys %rm) {
252 2 my $f = new IO::File;
253 2 ▶ if (sysopen($f, $file, O_RDWR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen $f, $file, 2) 1 1 2
254 1 ▶ if (flock($f, LOCK_EX|LOCK_NB)) {
Branch: 1 branch
BranchTrueFalseTotal
if (flock $f, 6) { } 1 0 1
255 1 ▶ unlink($file)
Branch: 1 branch
BranchTrueFalseTotal
if -s $file == 0 1 0 1
256 if -s $file == 0;
257 1 flock($f, LOCK_UN);
258 } else {
259 0 $bgrm{$file} = 1;
260 }
261 1 close($f);
262 }
263 }
264 14 ▶ if (%bgrm) {
Branch: 1 branch
BranchTrueFalseTotal
if (%bgrm) 0 14 14
265 0 my $ppid = fork;
266 0 ▶ croak "cannot fork" unless defined $ppid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ppid 0 0 0
267 0 my $pppid = $$;
268 0 my $b0 = $0;
269 0 $0 = "$b0: waiting for child ($ppid) to fork()";
270 0 ▶ unless ($ppid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ppid) 0 0 0
271 0 my $pid = fork;
272 0 ▶ croak "cannot fork" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
273 0 ▶ unless ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 0 0
274 0 for my $file (keys %bgrm) {
275 0 my $f = new IO::File;
276 0 ▶ if (sysopen($f, $file, O_RDWR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysopen $f, $file, 2) 0 0 0
277 0 ▶ if (flock($f, LOCK_EX)) {
Branch: 1 branch
BranchTrueFalseTotal
if (flock $f, 2) 0 0 0
278 0 ▶ unlink($file)
Branch: 1 branch
BranchTrueFalseTotal
if -s $file == 0 0 0 0
279 if -s $file == 0;
280 0 flock($f, LOCK_UN);
281 }
282 0 close($f);
283 }
284 }
285 0 ▶ print STDERR " $pppid] $pppid)" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Flock::debug 0 0 0
286 }
287 0 kill(9, $$); # exit w/o END or anything else
288 }
289 0 waitpid($ppid, 0);
290 0 kill(9, $$); # exit w/o END or anything else
291 }
292  
293 14 %locks = ();
294 14 %lockHandle = ();
295 14 %shared = ();
296 14 %pid = ();
297 14 %rm = ();
298 14 %bgrm = ();
299 }
300  
301 44 ▶END {
Subroutine END: called CC=1 SCAR=0.0
302 44 final_cleanup();
303 }
304  
305 44 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
306 44 ▶ if ($File::Flock::Forking::SubprocessEnabled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Flock::Forking::SubprocessEnabled) { } 30 14 44
307 30 require File::Flock::Subprocess;
308 30 *new = *File::Flock::Subprocess::new;
309 30 *final_cleanup = *File::Flock::Subprocess::final_cleanup;
310 30 *lock = *File::Flock::Subprocess::lock;
311 30 *unlock = *File::Flock::Subprocess::unlock;
312 30 *lock_rename = *File::Flock::Subprocess::lock_rename;
313 30 *forget_locks = *File::Flock::Subprocess::forget_locks;
314 } else {
315 14 *new = *new_flock;
316 14 *final_cleanup = *final_cleanup_flock;
317 14 *lock = *lock_flock;
318 14 *unlock = *unlock_flock;
319 14 *lock_rename = *lock_rename_flock;
320 14 *forget_locks = *forget_locks_flock;
321 }
322 }
323  
324 44 1;
325  
326 __END__
↑ Summary Flock/Subprocess.pm »