| 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::POSIX;{ | ||||||||||||||||||||
| 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 Fcntl qw/F_WRLCK F_UNLCK F_SETLK/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 2 | ▶ | use Errno qw/EAGAIN/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | # fcntl() should not be used without XS: the below is sensitive | ||||||||||||||||||||
| 27 | # for changes in the structure. However, at the moment it seems | ||||||||||||||||||||
| 28 | # there are only two options: either SysV-style or BSD-style | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 2 | ▶ | my $pack_pattern = $^O =~ /bsd|darwin/i ? '@20 s @256' : 's @256'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | #-------------------- | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | sub init($) | ||||||||||||||||||||
| 35 | 1 | ▶ | { my ($self, $args) = @_; | ||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 36 | 1 | ▶ | $args->{file} = $args->{posix_file} if $args->{posix_file}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 1 | $self->SUPER::init($args); | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | sub name() { 'POSIX' } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | #-------------------- | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | sub _try_lock($) | ||||||||||||||||||||
| 45 | 1 | ▶ | { my ($self, $file) = @_; | ||||||||||||||||||
|
Subroutine
_try_lock: called CC=2 SCAR=7.4
| |||||||||||||||||||||
| 46 | 1 | my $p = pack $pack_pattern, F_WRLCK; | |||||||||||||||||||
| 47 | 1 | ▶ | $? = fcntl($file, F_SETLK, $p) || ($!+0); | ||||||||||||||||||
|
Condition33%1/3
fcntl($file, 6, $p) || $! + 0
MC/DC0%0/2
fcntl($file, 6, $p) || $! + 0
fcntl($file, 6, $p)
$! + 0
Truth table33%1/3
A || B
A= fcntl($file, 6, $p)B= $! + 0
| |||||||||||||||||||||
| 48 | 1 | $?==0; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | sub _unlock($) | ||||||||||||||||||||
| 52 | 1 | ▶ | { my ($self, $file) = @_; | ||||||||||||||||||
|
Subroutine
_unlock: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 53 | 1 | my $p = pack $pack_pattern, F_UNLCK; | |||||||||||||||||||
| 54 | 1 | fcntl $file, F_SETLK, $p; | |||||||||||||||||||
| 55 | 1 | $self; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | sub lock() | ||||||||||||||||||||
| 60 | 2 | ▶ | { my $self = shift; | ||||||||||||||||||
|
Subroutine
lock: called CC=7 SCAR=23.7
Pod
documented
| |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 2 | ▶ | $self->hasLock | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | and warning(__x"folder {name} already lockf'd.", name => $self->folder), return 1; | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 1 | my $file = $self->filename; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 1 | ▶ | open my $fh, '+<:raw', $file | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | or fault __x"unable to open POSIX lock file {file} for {folder}", file => $file, $self->folder; | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 1 | my $timeout = $self->timeout; | |||||||||||||||||||
| 71 | 1 | ▶ | my $end = $timeout eq 'NOTIMEOUT' ? -1 : $timeout; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 1 | while(1) | |||||||||||||||||||
| 74 | 1 | ▶ | { if($self->_try_lock($fh)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 1 | { $self->{MBLF_filehandle} = $fh; | |||||||||||||||||||
| 76 | 1 | return $self->SUPER::lock; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 0 | ▶ | $!==EAGAIN | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | or fault __x"will never get a POSIX lock on {file} for {folder}", file => $file, folder => $self->folder; | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | ▶ | --$end or last; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 0 | sleep 1; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | return 0; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | sub isLocked() | ||||||||||||||||||||
| 91 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||
|
Subroutine
isLocked: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 92 | 0 | my $file = $self->filename; | |||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | ▶ | open my $fh, '<:raw', $file | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | or fault __x"unable to check lock file {file} for {folder}", file => $file, folder => $self->folder; | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | ▶ | $self->_try_lock($fh)==0 or return 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 0 | $self->_unlock($fh); | |||||||||||||||||||
| 99 | 0 | $fh->close; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | $self->SUPER::unlock; | |||||||||||||||||||
| 102 | 0 | 1; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | sub unlock() | ||||||||||||||||||||
| 106 | 1 | ▶ | { my $self = shift; | ||||||||||||||||||
|
Subroutine
unlock: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | $self->_unlock(delete $self->{MBLF_filehandle}) | ||||||||||||||||||||
| 109 | 1 | ▶ | if $self->hasLock; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 1 | $self->SUPER::unlock; | |||||||||||||||||||
| 112 | 1 | $self; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 2 | 1; | |||||||||||||||||||