Box/Locker/POSIX.pm

filter: Statementstmt 60.8% 45 / 74 Branchbran 31.8% 7 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 76.9% 10 / 13 Podpod 75.0% 3 / 4 totaltotal 55.9% 66 / 118 CC 12 SCAR 18.1
CC
12
Cov
54%
isLocked
12.0
lock
11.7
unlock
6.0
CRAP 6.1
« Box/Locker/Flock.pm ↑ Summary Box/Tie/ARRAY.pm »
1 # Copyrights 2001-2020 by [Mark Overmeer].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution Mail-Box. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package Mail::Box::Locker::POSIX;
10 2 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 $VERSION = '3.009';
12  
13 2 ▶use base 'Mail::Box::Locker';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Errno qw/EAGAIN/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # fcntl() should not be used without XS: the below is sensitive
23 # for changes in the structure. However, at the moment it seems
24 # there are only two options: either SysV-style or BSD-style
25  
26 2 ▶my $pack_pattern = $^O =~ /bsd|darwin/i ? '@20 s @256' : 's @256';
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /bsd|darwin/i ? : 0 2 2
27  
28  
29 sub init($)
30 1 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=2 SCAR=7.1
Pod undocumented
31 1 ▶ $args->{file} = $args->{posix_file} if $args->{posix_file};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"posix_file"} 0 1 1
32 1 $self->SUPER::init($args);
33 }
34  
35 sub name() {'POSIX'}
36  
37 sub _try_lock($)
38 1 ▶{ my ($self, $file) = @_;
Subroutine _try_lock: called CC=2 SCAR=7.4
39 1 my $p = pack $pack_pattern, F_WRLCK;
40 1 ▶ $? = fcntl($file, F_SETLK, $p) || ($!+0);
Condition33%1/3
fcntl($file, 6, $p) || $! + 0
l!l&&r!l&&!r
100
MC/DC0%0/2
fcntl($file, 6, $p) || $! + 0
fcntl($file, 6, $p) $! + 0
Truth table33%1/3
A || B
A= fcntl($file, 6, $p)B= $! + 0
ABresult
1X1
011
000
41 1 $?==0;
42 }
43  
44 sub _unlock($)
45 0 ▶{ my ($self, $file) = @_;
Subroutine _unlock: not called CC=1 SCAR=6.9
46 0 my $p = pack $pack_pattern, F_UNLCK;
47 0 fcntl $file, F_SETLK, $p;
48 0 $self;
49 }
50  
51  
52  
53 sub lock()
54 2 ▶{ my $self = shift;
Subroutine lock: called CC=7 SCAR=24.6
Pod documented
55  
56 2 ▶ if($self->hasLock)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hasLock) 1 1 2
57 1 { my $folder = $self->folder;
58 1 $self->log(WARNING => "Folder $folder already lockf'd");
59 0 return 1;
60 }
61  
62 1 my $filename = $self->filename;
63 1 my $folder = $self->folder;
64  
65 1 my $file = IO::File->new($filename, 'r+');
66 1 ▶ unless(defined $file)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 1 1
67 0 { $self->log(ERROR =>
68 "Unable to open POSIX lock file $filename for $folder: $!");
69 0 return 0;
70 }
71  
72 1 my $timeout = $self->timeout;
73 1 ▶ my $end = $timeout eq 'NOTIMEOUT' ? -1 : $timeout;
Branch: 1 branch
BranchTrueFalseTotal
$timeout eq 'NOTIMEOUT' ? : 0 1 1
74  
75 1 while(1)
76 1 ▶ { if($self->_try_lock($file))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_try_lock($file)) 1 0 1
77 1 { $self->{MBLF_filehandle} = $file;
78 1 return $self->SUPER::lock;
79 }
80  
81 0 ▶ unless($!==EAGAIN)
Branch: 1 branch
BranchTrueFalseTotal
unless ($! == 11) 0 0 0
82 0 { $self->log(ERROR =>
83 "Will never get a POSIX lock on $filename for $folder: $!");
84 0 last;
85 }
86  
87 0 ▶ last unless --$end;
Branch: 1 branch
BranchTrueFalseTotal
unless --$end 0 0 0
88 0 sleep 1;
89 }
90  
91 0 return 0;
92 }
93  
94  
95 sub isLocked()
96 0 ▶{ my $self = shift;
Subroutine isLocked: not called CC=3 SCAR=24.8
Pod documented
97 0 my $filename = $self->filename;
98  
99 0 my $file = IO::File->new($filename, "r");
100 0 ▶ unless($file)
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 0 0
101 0 { my $folder = $self->folder;
102 0 $self->log(ERROR => "Unable to check lock file $filename for $folder: $!");
103 0 return 0;
104 }
105  
106 0 ▶ $self->_try_lock($file)==0 or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_try_lock($file) == 0 or return 0 0 0 0
107 0 $self->_unlock($file);
108 0 $file->close;
109  
110 0 $self->SUPER::unlock;
111 0 1;
112 }
113  
114 sub unlock()
115 0 ▶{ my $self = shift;
Subroutine unlock: not called CC=2 SCAR=17.9
Pod documented
116  
117 $self->_unlock(delete $self->{MBLF_filehandle})
118 0 ▶ if $self->hasLock;
Branch: 1 branch
BranchTrueFalseTotal
if $self->hasLock 0 0 0
119  
120 0 $self->SUPER::unlock;
121 0 $self;
122 }
123  
124 2 1;
« Box/Locker/Flock.pm ↑ Summary Box/Tie/ARRAY.pm »