Box/Locker/NFS.pm

filter: Statementstmt 66.6% 48 / 72 Branchbran 30.7% 8 / 26 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 78.5% 11 / 14 Podpod 100% 3 / 3 totaltotal 57.6% 72 / 125 CC 16 SCAR 21.0
CC
16
Cov
56%
lock
17.5
isLocked
12.0
_unlock
6.0
CRAP 8.2
« Box/Mbox/Message.pm ↑ Summary Box/Collection.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::NFS;
10 3 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 $VERSION = '3.009';
12  
13 3 ▶use base 'Mail::Box::Locker';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 sub name() {'NFS'}
24  
25 #-------------------------------------------
26  
27 # METHOD nfs
28 # This hack is copied from the Mail::Folder packages, as written
29 # by Kevin Jones. Cited from his code:
30 # Whhheeeee!!!!!
31 # In NFS, the O_CREAT|O_EXCL isn't guaranteed to be atomic.
32 # So we create a temp file that is probably unique in space
33 # and time ($folder.lock.$time.$pid.$host).
34 # Then we use link to create the real lock file. Since link
35 # is atomic across nfs, this works.
36 # It loses if it's on a filesystem that doesn't do long filenames.
37  
38 3 my $hostname = hostname;
39  
40 sub _tmpfilename()
41 1 ▶{ my $self = shift;
Subroutine _tmpfilename: called CC=2 SCAR=8.1
42 1 ▶ $self->{MBLN_tmp} ||= $self->filename . $$;
Condition33%1/3
$self->{"MBLN_tmp"} ||= $self->filename . $$
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"MBLN_tmp"} ||= $self->filename . $$
$self->{"MBLN_tmp"} $self->filename . $$
Truth table33%1/3
A ||= B
A= $self->{"MBLN_tmp"}B= $self->filename . $$
ABresult
1X1
011
000
43 }
44  
45 sub _construct_tmpfile()
46 1 ▶{ my $self = shift;
Subroutine _construct_tmpfile: called CC=2 SCAR=7.0
47 1 my $tmpfile = $self->_tmpfilename;
48  
49 1 ▶ my $fh = IO::File->new($tmpfile, O_CREAT|O_WRONLY, 0600)
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($tmpfile, 65, 384) or return undef 0 1 1
50 or return undef;
51  
52 1 $fh->close;
53 1 $tmpfile;
54 }
55  
56 sub _try_lock($$)
57 1 ▶{ my ($self, $tmpfile, $lockfile) = @_;
Subroutine _try_lock: called CC=2 SCAR=7.0
58  
59 return undef
60 1 ▶ unless link $tmpfile, $lockfile;
Branch: 1 branch
BranchTrueFalseTotal
unless link $tmpfile, $lockfile 0 1 1
61  
62 1 my $linkcount = (stat $tmpfile)[3];
63  
64 1 unlink $tmpfile;
65 1 $linkcount == 2;
66 }
67  
68 sub _unlock($$)
69 0 ▶{ my ($self, $tmpfile, $lockfile) = @_;
Subroutine _unlock: not called CC=2 SCAR=17.9
70  
71 0 ▶ unlink $lockfile
Branch: 1 branch
BranchTrueFalseTotal
unlink $lockfile or warn "Couldn't remove lockfile ${lockfile}: $!\n" 0 0 0
72 or warn "Couldn't remove lockfile $lockfile: $!\n";
73  
74 0 unlink $tmpfile;
75  
76 0 $self;
77 }
78  
79 #-------------------------------------------
80  
81  
82 sub lock()
83 2 ▶{ my $self = shift;
Subroutine lock: called CC=9 SCAR=28.6
Pod documented
84 2 my $folder = $self->folder;
85  
86 2 ▶ if($self->hasLock)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hasLock) 1 1 2
87 1 { $self->log(WARNING => "Folder $folder already locked over nfs");
88 0 return 1;
89 }
90  
91 1 my $lockfile = $self->filename;
92 1 ▶ my $tmpfile = $self->_construct_tmpfile or return;
Branch: 1 branch
BranchTrueFalseTotal
my $tmpfile = $self->_construct_tmpfile or return 0 1 1
93 1 my $timeout = $self->timeout;
94 1 ▶ my $end = $timeout eq 'NOTIMEOUT' ? -1 : $timeout;
Branch: 1 branch
BranchTrueFalseTotal
$timeout eq 'NOTIMEOUT' ? : 0 1 1
95 1 my $expires = $self->expires / 86400; # in days for -A
96  
97 1 ▶ if(-e $lockfile && -A $lockfile > $expires)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $lockfile and -A $lockfile > $expires) 0 1 1
Condition33%1/3
-e $lockfile and -A $lockfile > $expires
!ll&&!rl&&r
100
MC/DC0%0/2
-e $lockfile and -A $lockfile > $expires
-e $lockfile -A $lockfile > $expires
Truth table33%1/3
A and B
A= -e $lockfileB= -A $lockfile > $expires
ABresult
0X0
100
111
98 0 ▶ { if(unlink $lockfile)
Branch: 1 branch
BranchTrueFalseTotal
if (unlink $lockfile) { } 0 0 0
99 0 { $self->log(WARNING => "Removed expired lockfile $lockfile.") }
100 0 else { $self->log(ERROR =>
101 "Unable to remove expired lockfile $lockfile: $!") }
102 }
103  
104 1 while(1)
105 1 ▶ { return $self->SUPER::lock
Branch: 1 branch
BranchTrueFalseTotal
if $self->_try_lock($tmpfile, $lockfile) 1 0 1
106 if $self->_try_lock($tmpfile, $lockfile);
107  
108 0 ▶ last unless --$end;
Branch: 1 branch
BranchTrueFalseTotal
unless --$end 0 0 0
109 0 sleep 1;
110 }
111  
112 0 return 0;
113 }
114  
115 #-------------------------------------------
116  
117 sub isLocked()
118 0 ▶{ my $self = shift;
Subroutine isLocked: not called CC=3 SCAR=24.8
Pod documented
119 0 ▶ my $tmpfile = $self->_construct_tmpfile or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $tmpfile = $self->_construct_tmpfile or return 0 0 0 0
120 0 my $lockfile = $self->filename;
121  
122 0 ▶ my $fh = $self->_try_lock($tmpfile, $lockfile) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->_try_lock($tmpfile, $lockfile) or return 0 0 0 0
123  
124 0 close $fh;
125 0 $self->_unlock($tmpfile, $lockfile);
126 0 $self->SUPER::unlock;
127  
128 0 1;
129 }
130  
131 #-------------------------------------------
132  
133 sub unlock($)
134 0 ▶{ my $self = shift;
Subroutine unlock: not called CC=2 SCAR=17.9
Pod documented
135 0 ▶ return $self unless $self->hasLock;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->hasLock 0 0 0
136  
137 0 $self->_unlock($self->_tmpfilename, $self->filename);
138 0 $self->SUPER::unlock;
139 0 $self;
140 }
141  
142 #-------------------------------------------
143  
144 3 1;
« Box/Mbox/Message.pm ↑ Summary Box/Collection.pm »