| 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::DotLock; | ||||||||||||||||||||||||||||
| 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 IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use Errno qw/EEXIST/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub init($) | ||||||||||||||||||||||||||||
| 25 | 1 | ▶ | { my ($self, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | $args->{file} = $args->{dotlock_file} if $args->{dotlock_file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 1 | $self->SUPER::init($args); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub name() {'DOTLOCK'} | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | sub folder(;$) | ||||||||||||||||||||||||||||
| 33 | 1 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
folder: called CC=6 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | @_ && $_[0] or return $self->SUPER::folder; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ and $_[0]
MC/DC0%0/2
@_ and $_[0]
@_
$_[0]
Truth table33%1/3
A and B
A= @_B= $_[0]
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | my $folder = shift; | |||||||||||||||||||||||||||
| 37 | 1 | ▶ | unless(defined $self->filename) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | { my $org = $folder->organization; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | my $filename | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 41 | = $org eq 'FILE' ? $folder->filename . '.lock' | ||||||||||||||||||||||||||||
| 42 | : $org eq 'DIRECTORY'? File::Spec->catfile($folder->directory,'.lock') | ||||||||||||||||||||||||||||
| 43 | : croak "Need lock file name for DotLock."; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | $self->filename($filename); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | $self->SUPER::folder($folder); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub _try_lock($) | ||||||||||||||||||||||||||||
| 52 | 1 | ▶ | { my ($self, $lockfile) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_try_lock: called CC=5 SCAR=19.7
| |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | return if -e $lockfile; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | my $flags = $^O eq 'MSWin32' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | ? O_CREAT|O_EXCL|O_WRONLY | ||||||||||||||||||||||||||||
| 57 | : O_CREAT|O_EXCL|O_WRONLY|O_NONBLOCK; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | my $lock = IO::File->new($lockfile, $flags, 0600); | |||||||||||||||||||||||||||
| 60 | 1 | ▶ | if($lock) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 1 | { close $lock; | |||||||||||||||||||||||||||
| 62 | 1 | return 1; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if($! != EEXIST) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | { $self->log(ERROR => "lockfile $lockfile can never be created: $!"); | |||||||||||||||||||||||||||
| 67 | 0 | return 1; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub unlock() | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
unlock: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | $self->hasLock | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | or return $self; | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my $lock = $self->filename; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | unlink $lock | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | or $self->log(WARNING => "Couldn't remove lockfile $lock: $!"); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | $self->SUPER::unlock; | |||||||||||||||||||||||||||
| 83 | 0 | $self; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | sub lock() | ||||||||||||||||||||||||||||
| 88 | 2 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
lock: called CC=8 SCAR=31.4
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2 | my $lockfile = $self->filename; | |||||||||||||||||||||||||||
| 91 | 2 | ▶ | if($self->hasLock) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 1 | { $self->log(WARNING => "Folder already locked with file $lockfile"); | |||||||||||||||||||||||||||
| 93 | 0 | return 1; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | my $timeout = $self->timeout; | |||||||||||||||||||||||||||
| 97 | 1 | ▶ | my $end = $timeout eq 'NOTIMEOUT' ? -1 : $timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 1 | my $expire = $self->expires/86400; # in days for -A | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 1 | while(1) | |||||||||||||||||||||||||||
| 101 | { | ||||||||||||||||||||||||||||
| 102 | 1 | ▶ | return $self->SUPER::lock | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | if $self->_try_lock($lockfile); | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if(-e $lockfile && -A $lockfile > $expire) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $lockfile and -A $lockfile > $expire
MC/DC0%0/2
-e $lockfile and -A $lockfile > $expire
-e $lockfile
-A $lockfile > $expire
Truth table0%0/3
A and B
A= -e $lockfileB= -A $lockfile > $expire
| |||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if(unlink $lockfile) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | { $self->log(WARNING => "Removed expired lockfile $lockfile"); | |||||||||||||||||||||||||||
| 109 | 0 | redo; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | else | ||||||||||||||||||||||||||||
| 112 | 0 | { $self->log(ERROR => | |||||||||||||||||||||||||||
| 113 | "Failed to remove expired lockfile $lockfile: $!"); | ||||||||||||||||||||||||||||
| 114 | 0 | last; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | last unless --$end; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | return 0; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub isLocked() { -e shift->filename } | ||||||||||||||||||||||||||
|
Subroutine
isLocked: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2 | 1; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||