| 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::Flock; | ||||||||||||||||||||||||||||
| 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 IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use Fcntl qw/:DEFAULT :flock/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 3 | ▶ | use Errno qw/EAGAIN/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub name() {'FLOCK'} | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub _try_lock($) | ||||||||||||||||||||||||||||
| 26 | 1 | ▶ | { my ($self, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_try_lock: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | flock $file, LOCK_EX|LOCK_NB; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub _unlock($) | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | { my ($self, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_unlock: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | 0 | flock $file, LOCK_UN; | |||||||||||||||||||||||||||
| 33 | 0 | $self; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # 'r+' is require under Solaris and AIX, other OSes are satisfied with 'r'. | ||||||||||||||||||||||||||||
| 39 | 3 | ▶ | my $lockfile_access_mode = ($^O eq 'solaris' || $^O eq 'aix') ? 'r+' : 'r'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$^O eq "solaris" or $^O eq "aix"
MC/DC0%0/2
$^O eq "solaris" or $^O eq "aix"
$^O eq "solaris"
$^O eq "aix"
Truth table33%1/3
A or B
A= $^O eq "solaris"B= $^O eq "aix"
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub lock() | ||||||||||||||||||||||||||||
| 42 | 2 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
lock: called CC=7 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 2 | my $folder = $self->folder; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | if($self->hasLock) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 1 | { $self->log(WARNING => "Folder $folder already flocked."); | |||||||||||||||||||||||||||
| 47 | 0 | return 1; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | my $filename = $self->filename; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | my $file = IO::File->new($filename, $lockfile_access_mode); | |||||||||||||||||||||||||||
| 53 | 1 | ▶ | unless($file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | { $self->log(ERROR => | |||||||||||||||||||||||||||
| 55 | "Unable to open flock file $filename for $folder: $!"); | ||||||||||||||||||||||||||||
| 56 | 0 | return 0; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | my $timeout = $self->timeout; | |||||||||||||||||||||||||||
| 60 | 1 | ▶ | my $end = $timeout eq 'NOTIMEOUT' ? -1 : $timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | while(1) | |||||||||||||||||||||||||||
| 63 | 1 | ▶ | { if($self->_try_lock($file)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 1 | { $self->{MBLF_filehandle} = $file; | |||||||||||||||||||||||||||
| 65 | 1 | return $self->SUPER::lock; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if($! != EAGAIN) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | { $self->log(ERROR => | |||||||||||||||||||||||||||
| 70 | "Will never get a flock on $filename for $folder: $!"); | ||||||||||||||||||||||||||||
| 71 | 0 | last; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | last unless --$end; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | return 0; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub isLocked() | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
isLocked: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 0 | my $filename = $self->filename; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | my $file = IO::File->new($filename, $lockfile_access_mode); | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | unless($file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | { my $folder = $self->folder; | |||||||||||||||||||||||||||
| 89 | 0 | $self->log(ERROR => | |||||||||||||||||||||||||||
| 90 | "Unable to check lock file $filename for $folder: $!"); | ||||||||||||||||||||||||||||
| 91 | 0 | return 0; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $self->_try_lock($file) or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | $self->_unlock($file); | |||||||||||||||||||||||||||
| 96 | 0 | $file->close; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | $self->SUPER::unlock; | |||||||||||||||||||||||||||
| 99 | 0 | 1; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub unlock() | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
unlock: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | $self->_unlock(delete $self->{MBLF_filehandle}) | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if $self->hasLock; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | $self->SUPER::unlock; | |||||||||||||||||||||||||||
| 109 | 0 | $self; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 3 | 1; | |||||||||||||||||||||||||||