| 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 . $$
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 . $$
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 93 | 1 | my $timeout = $self->timeout; | |||||||||||||||||||||||||||
| 94 | 1 | ▶ | my $end = $timeout eq 'NOTIMEOUT' ? -1 : $timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 1 | my $expires = $self->expires / 86400; # in days for -A | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 1 | ▶ | if(-e $lockfile && -A $lockfile > $expires) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-e $lockfile and -A $lockfile > $expires
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
| |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | { if(unlink $lockfile) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 106 | if $self->_try_lock($tmpfile, $lockfile); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | last unless --$end; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 120 | 0 | my $lockfile = $self->filename; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my $fh = $self->_try_lock($tmpfile, $lockfile) or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | $self->_unlock($self->_tmpfilename, $self->filename); | |||||||||||||||||||||||||||
| 138 | 0 | $self->SUPER::unlock; | |||||||||||||||||||||||||||
| 139 | 0 | $self; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | #------------------------------------------- | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 3 | 1; | |||||||||||||||||||||||||||