| 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::NFS;{ | ||||||||||||||||||||||||||||
| 13 | 3 | our $VERSION = '4.02'; | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use parent 'Mail::Box::Locker'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use Log::Report 'mail-box', import => [ qw/__x fault warning/ ]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use Sys::Hostname qw/hostname/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 3 | ▶ | use Fcntl qw/O_CREAT O_WRONLY/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | #-------------------- | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub name() { 'NFS' } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | #-------------------- | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # METHOD nfs | ||||||||||||||||||||||||||||
| 33 | # This hack is copied from the Mail::Folder packages, as written | ||||||||||||||||||||||||||||
| 34 | # by Kevin Jones. Cited from his code: | ||||||||||||||||||||||||||||
| 35 | # Whhheeeee!!!!! | ||||||||||||||||||||||||||||
| 36 | # In NFS, the O_CREAT|O_EXCL isn't guaranteed to be atomic. | ||||||||||||||||||||||||||||
| 37 | # So we create a temp file that is probably unique in space | ||||||||||||||||||||||||||||
| 38 | # and time ($folder.lock.$time.$pid.$host). | ||||||||||||||||||||||||||||
| 39 | # Then we use link to create the real lock file. Since link | ||||||||||||||||||||||||||||
| 40 | # is atomic across nfs, this works. | ||||||||||||||||||||||||||||
| 41 | # It loses if it's on a filesystem that doesn't do long filenames. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 3 | my $hostname = hostname; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub _tmpfilename() | ||||||||||||||||||||||||||||
| 46 | 4 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_tmpfilename: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 47 | 4 | ▶ | $self->{MBLN_tmp} ||= $self->filename . $$; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"MBLN_tmp"} ||= $self->filename . $$
MC/DC0%0/2
$self->{"MBLN_tmp"} ||= $self->filename . $$
$self->{"MBLN_tmp"}
$self->filename . $$
Truth table66%2/3
A ||= B
A= $self->{"MBLN_tmp"}B= $self->filename . $$
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub _construct_tmpfile() | ||||||||||||||||||||||||||||
| 51 | 2 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_construct_tmpfile: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 52 | 2 | my $tmpfile = $self->_tmpfilename; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | ▶ | sysopen my $fh, $tmpfile, O_CREAT|O_WRONLY, 0600 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | or return undef; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 2 | $fh->close; | |||||||||||||||||||||||||||
| 58 | 2 | $tmpfile; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub _try_lock($$) | ||||||||||||||||||||||||||||
| 62 | 2 | ▶ | { my ($self, $tmpfile, $lockfile) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_try_lock: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | ▶ | link $tmpfile, $lockfile | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | or return undef; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 2 | my $linkcount = (stat $tmpfile)[3]; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 2 | unlink $tmpfile; | |||||||||||||||||||||||||||
| 70 | 2 | $linkcount == 2; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub lock() | ||||||||||||||||||||||||||||
| 75 | 3 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
lock: called CC=9 SCAR=28.1
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 3 | my $folder = $self->folder; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 3 | ▶ | $self->hasLock | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | and warning(__x"folder {name} already locked over NFS.", name => $folder), return 1; | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | my $lockfile = $self->filename; | |||||||||||||||||||||||||||
| 82 | 2 | ▶ | my $tmpfile = $self->_construct_tmpfile or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 2 | my $timeout = $self->timeout; | |||||||||||||||||||||||||||
| 84 | 2 | ▶ | my $end = $timeout eq 'NOTIMEOUT' ? -1 : $timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 2 | my $expires = $self->expires / 86400; # in days for -A | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | ▶ | 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
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | { unlink $lockfile | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | or fault __x"Unable to remove expired lockfile {file}", file => $lockfile; | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | warning __x"removed expired lockfile {file}.", file => $lockfile; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2 | while(1) | |||||||||||||||||||||||||||
| 95 | 2 | ▶ | { return $self->SUPER::lock | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | if $self->_try_lock($tmpfile, $lockfile); | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | --$end or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | return 0; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | sub isLocked() | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
isLocked: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | my $tmpfile = $self->_construct_tmpfile or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | my $lockfile = $self->filename; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my $fh = $self->_try_lock($tmpfile, $lockfile) or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | close $fh; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | $self->_unlock($tmpfile, $lockfile); | |||||||||||||||||||||||||||
| 114 | 0 | $self->SUPER::unlock; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | 1; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | sub _unlock($$) | ||||||||||||||||||||||||||||
| 121 | 2 | ▶ | { my ($self, $tmpfile, $lockfile) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_unlock: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2 | ▶ | unlink $lockfile | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | or fault __x"couldn't remove lockfile {file}", file => $lockfile; | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 2 | unlink $tmpfile; | |||||||||||||||||||||||||||
| 127 | 2 | $self; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | sub unlock($) | ||||||||||||||||||||||||||||
| 131 | 2 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
unlock: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 2 | ▶ | $self->hasLock or return $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | $self->_unlock($self->_tmpfilename, $self->filename); | |||||||||||||||||||||||||||
| 135 | 2 | $self->SUPER::unlock; | |||||||||||||||||||||||||||
| 136 | 2 | $self; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 3 | 1; | |||||||||||||||||||||||||||