blib/lib/LockFile/NetLock.pm

filter: Statementstmt 30.1% 35 / 116 Branchbran 2.1% 1 / 46 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 60.0% 9 / 15 Podpod 80.0% 4 / 5 totaltotal 20.5% 49 / 239 CC 39 SCAR 46.4
CC
39
Cov
18%
lock
182.0
unlock
182.0
new
68.0
CRAP 103.3
↑ Summary netlock.cfg »
1 ######################################################################
2 # LockFile::NetLock
3 #
4 # Use ftp and directory creation to create mutual exclusion/locking
5 # available cross platform and on a network. Based on an article
6 # by Sean M. Burke in the Summer 2002 Perl Journal.
7 #
8 # Basic idea is based on mutually exclusive property of creating
9 # directories via FTP. The first process that asks to create the
10 # directory succeeds and later attempts are notified of failure
11 # because directory already exists. FTP session is maintained
12 # by separate program called 'netlock' that automatically removes
13 # the directory if the creating program dies or requests removal.
14 # Communication between this module and netlock program is via an
15 # interprocess pipe. On win32 systems some communication is also
16 # done via a mutex because pipes block too quickly and problems
17 # were encountered when trying other solutions.
18 #
19 # Implemented by Ronald Schmidt.
20 ######################################################################
21  
22 package LockFile::NetLock;
23  
24 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 require 5.006; # goes back a ways but does not seem to like 5.005
27 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use POSIX qw(sys_wait_h signal_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 our @ISA = qw/ Exporter /;
34 1 our @EXPORT_OK = qw(lock unlock);
35  
36 1 our $VERSION = '0.32';
37  
38 1 our $errstr;
39  
40 1 my %named_lock;
41 1 my $mx_id = 0;
42 1 my $is_win32;
43  
44 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
45 1 $is_win32 = ($^O =~ /win32/i);
46 1 ▶ require Win32::Mutex if ($is_win32);
Branch: 1 branch
BranchTrueFalseTotal
if $is_win32 0 1 1
47 }
48  
49 ######################################################################
50 # new - Constructor method.
51 #
52 # Not much going on here - just setup of parameters that will be
53 # used by lock process. Caller is allowed to pass up to four initial
54 # un-named parameters that are interpreted as ftp host, lock directory
55 # ftp user and password respectively.
56 ######################################################################
57 0 ▶sub new {
Subroutine new: not called CC=9 SCAR=42.2
Pod documented
58 0 my $self = shift;
59 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
60  
61 1 ▶ use constant KNOWN_OPT => {
Subroutine BEGIN: called CC=1 SCAR=0.0
62 map( ($_ => 1) , qw(
63 -dir -disconnect -ftp_heartbeat -heartbeat -host -idle
64 -password -sleep -timeout -user
65 ))
66 1 };
67  
68 0 my %ivar = (
69 -host => 'localhost',
70 -dir => 'lockdir'
71 );
72  
73 # allow host, directory user and password to be passed unlabeled
74 0 foreach (qw (-host -dir -user -password)) {
75 0 ▶ last if ($_[0] && ($_[0] =~ /^\-/));
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] =~ /^\-/ 0 0 0
Condition0%0/3
$_[0] and $_[0] =~ /^\-/
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $_[0] =~ /^\-/
$_[0] $_[0] =~ /^\-/
Truth table0%0/3
A and B
A= $_[0]B= $_[0] =~ /^\-/
ABresult
0X0
100
111
76 0 $ivar{$_} = shift;
77 }
78 0 %ivar = (%ivar, @_);
79  
80 # empty parameters may cause trouble with ./netlock program
81 0 ▶ grep( $_ ne '-disconnect' && (! $ivar{$_}) && delete($ivar{$_}),
Condition0%0/6
$_ ne '-disconnect' && !$ivar{$_} && delete $ivar{$_}
!ll&&!rl&&r
000
$_ ne "-disconnect" and not $ivar{$_}
!ll&&!rl&&r
000
MC/DC0%0/3
$_ ne '-disconnect' && !$ivar{$_} && delete $ivar{$_}
$_ ne "-disconnect" not $ivar{$_} delete $ivar{$_}
Truth table0%0/4
A and not(B) && C
A= $_ ne "-disconnect"B= not $ivar{$_}C= delete $ivar{$_}
ABCresult
0XX0
10X0
1100
1111
82 (keys %ivar));
83  
84 0 foreach my $opt (keys %ivar) {
85 0 ▶ carp("Unknown option: $opt") unless (KNOWN_OPT->{$opt});
Branch: 1 branch
BranchTrueFalseTotal
unless {"-dir" => 1,"-disconnect" => 1,"-ftp_heartbeat" => 1,"-heartbeat" => 1,"-host" => 1,"-idle" => 1,"-password" => 1,"-sleep" => 1,"-timeout" => 1,"-user" => 1}->{$opt} 0 0 0
86 }
87
88 0 return bless \%ivar, $class;
89 }
90  
91 ######################################################################
92 ######################################################################
93 0 ▶sub set_error {
Subroutine set_error: not called CC=1 SCAR=6.9
Pod undocumented
94 0 my $self = shift;
95 0 $errstr = $self->{error} = shift;
96 }
97  
98 ######################################################################
99 # lock
100 #
101 # Call netlock program to use FTP to create a directory on a
102 # mutually exclusive basis if one has not been created. Can
103 # be called using an existing LockFile::NetLock object reference
104 # or the parameters needed to create a new object can be passed
105 # to this method.
106 ######################################################################
107 0 ▶sub lock {
Subroutine lock: not called CC=13 SCAR=52.0
Pod documented
108 0 ▶ my $self = ref($_[0]) && shift;
Condition0%0/3
ref $_[0] && shift()
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] && shift()
ref $_[0] shift()
Truth table0%0/3
A && B
A= ref $_[0]B= shift()
ABresult
0X0
100
111
109 0 my $lock_key;
110  
111 0 ▶ unless ($self) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self) 0 0 0
112 0 $self = LockFile::NetLock->new(@_);
113 0 $lock_key = "$self->{-host},$self->{-dir}";
114 0 ▶ if ($named_lock{$lock_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($named_lock{$lock_key}) 0 0 0
115 0 $errstr = "Already locking $lock_key";
116 0 return;
117 }
118 0 $named_lock{$lock_key} = $self;
119 }
120  
121 0 ▶ my ($cmd) = grep { -r && ! -d }
Condition0%0/3
-r $_ && !(-d $_)
!ll&&!rl&&r
000
MC/DC0%0/2
-r $_ && !(-d $_)
-r $_ !(-d $_)
Truth table0%0/3
A && not(B)
A= -r $_B= !(-d $_)
ABresult
0X0
100
111
122 ( './netlock', "$Config{bin}/netlock",
123 "$Config{installsitescript}/netlock"
124 );
125 0 my $cmd_line = $Config{perlpath};
126 0 ▶ $cmd_line =~ s!/!\\!g if ($is_win32);
Branch: 1 branch
BranchTrueFalseTotal
if $is_win32 0 0 0
127 0 $cmd_line .= " $cmd";
128  
129 0 ▶ $cmd_line .= ' -d' if $self->{-disconnect};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"-disconnect"} 0 0 0
130  
131 # first character after - in package option is Getopt::Std prog option
132 0 foreach my $opt (grep($_ !~ /^(-d|-host|-reader)/, keys %$self)) {
133 0 $cmd_line .= ' ' . substr($opt, 0, 2) . $self->{$opt};
134 }
135  
136 0 ▶ if ($is_win32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_win32) 0 0 0
137 0 $self->{mutex_name} = "netlock:$$:" . $mx_id++;
138 0 ▶ unless ($self->{mutex} =
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"mutex"} = "Win32::Mutex"->new(1, $self->{"mutex_name"})) 0 0 0
139 Win32::Mutex->new(1, $self->{mutex_name})) {
140 0 $self->set_error("Could not create mutex: $^E");
141 0 return;
142 }
143 0 $cmd_line .= " -m $self->{mutex_name}";
144 }
145  
146 0 $cmd_line .= " $self->{-host} $self->{-dir}";
147 0 my $fh = new FileHandle "$cmd_line |";
148  
149 0 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
150 0 $self->set_error("Could not start netlock process: $!");
151 0 ▶ delete $named_lock{$lock_key} if ($lock_key);
Branch: 1 branch
BranchTrueFalseTotal
if $lock_key 0 0 0
152 0 return;
153 }
154  
155 0 $self->{filehandle} = $fh;
156 0 my $from_netlock = <$fh>;
157 0 ▶ if ($from_netlock !~ /^\.*OK/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($from_netlock =~ /^\.*OK/) 0 0 0
158 0 $self->set_error("Failed to acquire net lock: $from_netlock");
159 0 return;
160 }
161 0 $self->{is_locked} = 1;
162  
163 0 return $self;
164 }
165  
166  
167 ######################################################################
168 # unlock
169 #
170 # Close the handle to the running netlock process holding the lock
171 # uniquely identified by the host and directory or object containing
172 # those unique identifiers. If we are not currently the locker, or
173 # an error happens on close, or we get a child exist code of -1
174 # indicating a netlock lock removal error we return an error code.
175 ######################################################################
176 0 ▶sub unlock {
Subroutine unlock: not called CC=13 SCAR=52.0
Pod documented
177 0 ▶ my $self = ref($_[0]) && shift;
Condition0%0/3
ref $_[0] && shift()
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] && shift()
ref $_[0] shift()
Truth table0%0/3
A && B
A= ref $_[0]B= shift()
ABresult
0X0
100
111
178 0 my $lock_key;
179  
180 0 ▶ unless ($self) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self) 0 0 0
181 # bit of a hack - just want a standard way to get host and dir
182 0 $self = LockFile::NetLock->new(@_);
183 0 $lock_key = "$self->{-host},$self->{-dir}";
184 0 $self = $named_lock{$lock_key};
185  
186 # once you try to unlock you can always try to lock again
187 0 delete $named_lock{$lock_key};
188 }
189  
190 0 ▶ unless ($self && $self->{filehandle}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self and $self->{"filehandle"}) 0 0 0
Condition0%0/3
$self and $self->{"filehandle"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self and $self->{"filehandle"}
$self $self->{"filehandle"}
Truth table0%0/3
A and B
A= $selfB= $self->{"filehandle"}
ABresult
0X0
100
111
191 0 $errstr = "Cannot unlock: not currently locking";
192 0 ▶ if ($lock_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lock_key) { } 0 0 0
193 0 $errstr .= " $lock_key";
194 }
195 else {
196 0 $errstr .= ' object';
197 }
198 0 ▶ $self->{error} = $errstr if ($self);
Branch: 1 branch
BranchTrueFalseTotal
if $self 0 0 0
199 0 return;
200 }
201  
202 0 ▶ if ($is_win32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_win32) 0 0 0
203 0 ▶ unless ($self->{mutex}->release) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"mutex"}->release) 0 0 0
204 0 $self->set_error("Could not release mutex: $^E");
205 }
206 }
207  
208 0 my $close_rc;
209 0 ▶ unless ($close_rc = close($self->{filehandle})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($close_rc = close $self->{"filehandle"}) 0 0 0
210 0 $self->set_error("Failed to close lock process: $!");
211 }
212  
213 0 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 0 0
214 0 my $reaped_rc = $? >> 8;
215 0 ▶ if ($reaped_rc & 255 == 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reaped_rc & !0) { } 0 0 0
216 0 $self->set_error("Failed to remove lock directory.");
217 }
218 else {
219 0 $self->set_error("Unlock failure code: $reaped_rc");
220 }
221 }
222 0 ▶ return unless($close_rc);
Branch: 1 branch
BranchTrueFalseTotal
unless $close_rc 0 0 0
223 0 $self->{is_locked} = 0;
224  
225 0 return 1;
226 }
227  
228 ######################################################################
229 # Return most recent error on object if passed an object reference
230 # otherwise most recent error against module.
231 ######################################################################
232 0 ▶sub errstr {
Subroutine errstr: not called CC=4 SCAR=30.0
Pod documented
233 0 my $self = shift;
234  
235 0 ▶ return ($self && ref($self) && $self->{error}) || $errstr;
Condition0%0/9
$self && ref $self && $self->{'error'} || $LockFile::NetLock::errstr
l!l&&r!l&&!r
000
$self and ref $self and $self->{"error"}
!ll&&!rl&&r
000
$self and ref $self
!ll&&!rl&&r
000
MC/DC0%0/4
$self && ref $self && $self->{'error'} || $LockFile::NetLock::errstr
$self ref $self $self->{"error"} $LockFile::NetLock::errstr
Truth table0%0/7
A and B and C || D
A= $selfB= ref $selfC= $self->{"error"}D= $LockFile::NetLock::errstr
ABCDresult
111X1
0XX11
10X11
11011
0XX00
10X00
11000
236 }
237  
238 ######################################################################
239 # Needed for win32. Without this exiting scope could
240 # attempt to close the process handle without releasing the mutex
241 # leading to deadlock.
242 ######################################################################
243 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
244 0 my $self = shift;
245  
246 $self->{mutex}->release if (
247 $is_win32 && $self->{is_locked}
248 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if $is_win32 and $self->{"is_locked"} 0 0 0
Condition0%0/3
$is_win32 and $self->{"is_locked"}
!ll&&!rl&&r
000
MC/DC0%0/2
$is_win32 and $self->{"is_locked"}
$is_win32 $self->{"is_locked"}
Truth table0%0/3
A and B
A= $is_win32B= $self->{"is_locked"}
ABresult
0X0
100
111
249 }
250  
251 1 1;
252 __END__
↑ Summary netlock.cfg »