line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright (C) 1996, David Muir Sharnoff |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package File::BasicFlock; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
require Exporter; |
6
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
7
|
|
|
|
|
|
|
@EXPORT = qw(lock unlock); |
8
|
|
|
|
|
|
|
|
9
|
9
|
|
|
9
|
|
5625
|
use Carp; |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
774
|
|
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
|
|
|
9
|
|
12402
|
use POSIX qw(EAGAIN ENOENT EEXIST O_RDWR); |
|
9
|
|
|
|
|
92844
|
|
|
9
|
|
|
|
|
81
|
|
16
|
9
|
|
|
9
|
|
16704
|
use Fcntl qw(LOCK_SH LOCK_EX LOCK_NB LOCK_UN); |
|
9
|
|
|
|
|
63
|
|
|
9
|
|
|
|
|
531
|
|
17
|
|
|
|
|
|
|
|
18
|
9
|
|
|
9
|
|
45
|
use vars qw($VERSION %locks %lockHandle %shared $debug); |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
927
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
BEGIN { |
21
|
9
|
|
|
9
|
|
18
|
$VERSION = 98.120200; |
22
|
9
|
|
|
|
|
126
|
$debug = 0; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
9
|
|
|
9
|
|
36
|
use strict; |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
1404
|
|
26
|
9
|
|
|
9
|
|
54
|
no strict qw(refs); |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
5859
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my $gensym = "sym0000"; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub lock |
31
|
|
|
|
|
|
|
{ |
32
|
370
|
|
|
370
|
0
|
1483117
|
my ($file, $shared, $nonblocking) = @_; |
33
|
|
|
|
|
|
|
#my $f = new FileHandle; |
34
|
|
|
|
|
|
|
|
35
|
370
|
|
|
|
|
1989
|
$gensym++; |
36
|
370
|
|
|
|
|
8021
|
my $f = "File::BasicFlock::$gensym"; |
37
|
|
|
|
|
|
|
|
38
|
370
|
|
|
|
|
1682
|
my $previous = exists $locks{$file}; |
39
|
|
|
|
|
|
|
|
40
|
370
|
50
|
|
|
|
119416
|
unless (sysopen($f, $file, O_RDWR)) { |
41
|
0
|
|
|
|
|
0
|
croak "open $file: $!"; |
42
|
|
|
|
|
|
|
} |
43
|
370
|
|
50
|
|
|
22713
|
$locks{$file} = $locks{$file} || 0; |
44
|
370
|
|
|
|
|
1750
|
$shared{$file} = $shared; |
45
|
|
|
|
|
|
|
|
46
|
370
|
|
|
|
|
2164
|
$lockHandle{$file} = $f; |
47
|
|
|
|
|
|
|
|
48
|
370
|
|
|
|
|
608
|
my $flags; |
49
|
|
|
|
|
|
|
|
50
|
370
|
50
|
|
|
|
1697
|
$flags = $shared ? LOCK_SH : LOCK_EX; |
51
|
370
|
100
|
|
|
|
1551
|
$flags |= LOCK_NB |
52
|
|
|
|
|
|
|
if $nonblocking; |
53
|
|
|
|
|
|
|
|
54
|
370
|
|
|
|
|
407617
|
my $r = flock($f, $flags); |
55
|
|
|
|
|
|
|
|
56
|
370
|
50
|
33
|
|
|
2370
|
print " ($$ " if $debug and $r; |
57
|
|
|
|
|
|
|
|
58
|
370
|
100
|
|
|
|
2121
|
return 1 if $r; |
59
|
305
|
50
|
33
|
|
|
7015
|
if ($nonblocking and $! == EAGAIN) { |
60
|
305
|
50
|
|
|
|
1786
|
if (! $previous) { |
61
|
305
|
|
|
|
|
1116
|
delete $locks{$file}; |
62
|
305
|
|
|
|
|
771
|
my $f = $lockHandle{$file}; |
63
|
305
|
|
|
|
|
5125
|
close($f); |
64
|
305
|
|
|
|
|
832
|
delete $lockHandle{$file}; |
65
|
305
|
|
|
|
|
1096
|
delete $shared{$file}; |
66
|
|
|
|
|
|
|
} |
67
|
305
|
|
|
|
|
1563
|
return 0; |
68
|
|
|
|
|
|
|
} |
69
|
0
|
|
|
|
|
0
|
croak "flock $f $flags: $!"; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub unlock |
73
|
|
|
|
|
|
|
{ |
74
|
65
|
|
|
65
|
0
|
208760
|
my ($file) = @_; |
75
|
|
|
|
|
|
|
|
76
|
65
|
50
|
|
|
|
1298
|
croak "no lock on $file" unless exists $locks{$file}; |
77
|
|
|
|
|
|
|
|
78
|
65
|
|
|
|
|
232
|
delete $locks{$file}; |
79
|
65
|
|
|
|
|
206
|
my $f = $lockHandle{$file}; |
80
|
65
|
|
|
|
|
122
|
delete $lockHandle{$file}; |
81
|
|
|
|
|
|
|
|
82
|
65
|
50
|
|
|
|
906
|
return 0 unless defined $f; |
83
|
|
|
|
|
|
|
|
84
|
65
|
50
|
|
|
|
682
|
print " $$) " if $debug; |
85
|
65
|
50
|
|
|
|
4005
|
flock($f, LOCK_UN) |
86
|
|
|
|
|
|
|
or croak "flock $f UN: $!"; |
87
|
|
|
|
|
|
|
|
88
|
65
|
|
|
|
|
3868
|
close($f); |
89
|
65
|
|
|
|
|
364
|
return 1; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
END { |
93
|
9
|
|
|
9
|
|
373980
|
my $f; |
94
|
9
|
|
|
|
|
0
|
for $f (keys %locks) { |
95
|
0
|
|
|
|
|
0
|
&unlock($f); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
__DATA__ |