| 1 | # This code is part of Perl distribution Mail-Box version 4.02. | ||||||||||||||||||||||||||||
| 2 | # The POD got stripped from this file by OODoc version 3.06. | ||||||||||||||||||||||||||||
| 3 | # For contributors see file ChangeLog. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # This software is copyright (c) 2001-2026 by Mark Overmeer. | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 8 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 9 | # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | package Mail::Box::Locker::DotLock;{ | ||||||||||||||||||||||||||||
| 13 | 2 | our $VERSION = '4.02'; | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use parent 'Mail::Box::Locker'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use Log::Report 'mail-box', import => [ qw/__x error fault warning/ ]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use File::Spec::Functions qw/catfile/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use Errno qw/EEXIST/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use Fcntl qw/O_CREAT O_EXCL O_WRONLY O_NONBLOCK/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 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->{dotlock_file} if $args->{dotlock_file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 1 | $self->SUPER::init($args); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub name() { 'DOTLOCK' } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | #-------------------- | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub folder(;$) | ||||||||||||||||||||||||||||
| 40 | 1 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
folder: called CC=6 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 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]
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | my $folder = shift; | |||||||||||||||||||||||||||
| 44 | 1 | ▶ | unless(defined $self->filename) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | { my $org = $folder->organization; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my $filename | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 48 | = $org eq 'FILE' ? $folder->filename . '.lock' | ||||||||||||||||||||||||||||
| 49 | : $org eq 'DIRECTORY'? catfile($folder->directory, '.lock') | ||||||||||||||||||||||||||||
| 50 | : error __x"Dotlock requires a lock file name."; | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | $self->filename($filename); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | $self->SUPER::folder($folder); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | #-------------------- | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub unlock() | ||||||||||||||||||||||||||||
| 61 | 1 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
unlock: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | $self->hasLock | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | or return $self; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | my $lock = $self->filename; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | unlink $lock | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | or warning __x"couldn't remove lockfile {file}: {rc}", file => $lock, rc => $!; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | $self->SUPER::unlock; | |||||||||||||||||||||||||||
| 71 | 1 | $self; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub _try_lock($) | ||||||||||||||||||||||||||||
| 76 | 1 | ▶ | { my ($self, $lockfile) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_try_lock: called CC=5 SCAR=20.2
| |||||||||||||||||||||||||||||
| 77 | 1 | ▶ | return if -e $lockfile; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | my $flags = $^O eq 'MSWin32' ? O_CREAT|O_EXCL|O_WRONLY : O_CREAT|O_EXCL|O_WRONLY|O_NONBLOCK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | my $lock; | |||||||||||||||||||||||||||
| 81 | 1 | ▶ | sysopen $lock, $lockfile, $flags, 0600 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | and $lock->close, return 1; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $! == EEXIST | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | or fault __x"lockfile {file} can never be created", file => $lockfile; | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | 1; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub lock() | ||||||||||||||||||||||||||||
| 91 | 2 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
lock: called CC=8 SCAR=30.8
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 2 | my $lockfile = $self->filename; | |||||||||||||||||||||||||||
| 94 | 2 | ▶ | $self->hasLock | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | and warning(__x"folder already locked with file {file}.", file => $lockfile), return 1; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 1 | my $timeout = $self->timeout; | |||||||||||||||||||||||||||
| 98 | 1 | ▶ | my $end = $timeout eq 'NOTIMEOUT' ? -1 : $timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 1 | my $expire = $self->expires/86400; # in days for -A | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | while(1) | |||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||
| 103 | 1 | ▶ | return $self->SUPER::lock | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | if $self->_try_lock($lockfile); | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 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
| |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | { unlink $lockfile | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | or fault __x"failed to remove expired lockfile {file}", file => $lockfile; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | warning __x"removed expired lockfile {file}.", file => $lockfile; | |||||||||||||||||||||||||||
| 111 | 0 | redo; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | --$end or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | return 0; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub isLocked() { -e shift->filename } | ||||||||||||||||||||||||||
|
Subroutine
isLocked: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2 | 1; | |||||||||||||||||||||||||||