line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyrights 2001-2023 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.03. |
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
|
|
|
2
|
|
2191
|
use vars '$VERSION'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
142
|
|
11
|
|
|
|
|
|
|
$VERSION = '3.010'; |
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
13
|
use base 'Mail::Box::Locker'; |
|
2
|
|
|
|
|
23
|
|
|
2
|
|
|
|
|
600
|
|
14
|
|
|
|
|
|
|
|
15
|
2
|
|
|
2
|
|
34
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
44
|
|
16
|
2
|
|
|
2
|
|
15
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
55
|
|
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
2
|
|
9
|
use IO::File; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
288
|
|
19
|
2
|
|
|
2
|
|
13
|
use File::Spec; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
79
|
|
20
|
2
|
|
|
2
|
|
13
|
use Errno qw/EEXIST/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
311
|
|
21
|
2
|
|
|
2
|
|
26
|
use Carp; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1712
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub init($) |
25
|
1
|
|
|
1
|
0
|
15
|
{ my ($self, $args) = @_; |
26
|
1
|
50
|
|
|
|
6
|
$args->{file} = $args->{dotlock_file} if $args->{dotlock_file}; |
27
|
1
|
|
|
|
|
9
|
$self->SUPER::init($args); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub name() {'DOTLOCK'} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub folder(;$) |
33
|
1
|
|
|
1
|
1
|
2
|
{ my $self = shift; |
34
|
1
|
50
|
33
|
|
|
64
|
@_ && $_[0] or return $self->SUPER::folder; |
35
|
|
|
|
|
|
|
|
36
|
1
|
|
|
|
|
7
|
my $folder = shift; |
37
|
1
|
50
|
|
|
|
10
|
unless(defined $self->filename) |
38
|
0
|
|
|
|
|
0
|
{ my $org = $folder->organization; |
39
|
|
|
|
|
|
|
|
40
|
0
|
0
|
|
|
|
0
|
my $filename |
|
|
0
|
|
|
|
|
|
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
|
|
|
|
|
0
|
$self->filename($filename); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
1
|
|
|
|
|
9
|
$self->SUPER::folder($folder); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub _try_lock($) |
52
|
1
|
|
|
1
|
|
4
|
{ my ($self, $lockfile) = @_; |
53
|
1
|
50
|
|
|
|
16
|
return if -e $lockfile; |
54
|
|
|
|
|
|
|
|
55
|
1
|
50
|
|
|
|
7
|
my $flags = $^O eq 'MSWin32' |
56
|
|
|
|
|
|
|
? O_CREAT|O_EXCL|O_WRONLY |
57
|
|
|
|
|
|
|
: O_CREAT|O_EXCL|O_WRONLY|O_NONBLOCK; |
58
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
11
|
my $lock = IO::File->new($lockfile, $flags, 0600); |
60
|
1
|
50
|
|
|
|
247
|
if($lock) |
61
|
1
|
|
|
|
|
12
|
{ close $lock; |
62
|
1
|
|
|
|
|
29
|
return 1; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
0
|
0
|
|
|
|
0
|
if($! != EEXIST) |
66
|
0
|
|
|
|
|
0
|
{ $self->log(ERROR => "lockfile $lockfile can never be created: $!"); |
67
|
0
|
|
|
|
|
0
|
return 1; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub unlock() |
73
|
1
|
|
|
1
|
1
|
300
|
{ my $self = shift; |
74
|
1
|
50
|
|
|
|
5
|
$self->hasLock |
75
|
|
|
|
|
|
|
or return $self; |
76
|
|
|
|
|
|
|
|
77
|
1
|
|
|
|
|
5
|
my $lock = $self->filename; |
78
|
|
|
|
|
|
|
|
79
|
1
|
50
|
|
|
|
68
|
unlink $lock |
80
|
|
|
|
|
|
|
or $self->log(WARNING => "Couldn't remove lockfile $lock: $!"); |
81
|
|
|
|
|
|
|
|
82
|
1
|
|
|
|
|
24
|
$self->SUPER::unlock; |
83
|
1
|
|
|
|
|
3
|
$self; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub lock() |
88
|
2
|
|
|
2
|
1
|
1471
|
{ my $self = shift; |
89
|
|
|
|
|
|
|
|
90
|
2
|
|
|
|
|
6
|
my $lockfile = $self->filename; |
91
|
2
|
100
|
|
|
|
15
|
if($self->hasLock) |
92
|
1
|
|
|
|
|
11
|
{ $self->log(WARNING => "Folder already locked with file $lockfile"); |
93
|
1
|
|
|
|
|
51
|
return 1; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
1
|
|
|
|
|
6
|
my $timeout = $self->timeout; |
97
|
1
|
50
|
|
|
|
6
|
my $end = $timeout eq 'NOTIMEOUT' ? -1 : $timeout; |
98
|
1
|
|
|
|
|
7
|
my $expire = $self->expires/86400; # in days for -A |
99
|
|
|
|
|
|
|
|
100
|
1
|
|
|
|
|
3
|
while(1) |
101
|
|
|
|
|
|
|
{ |
102
|
1
|
50
|
|
|
|
5
|
return $self->SUPER::lock |
103
|
|
|
|
|
|
|
if $self->_try_lock($lockfile); |
104
|
|
|
|
|
|
|
|
105
|
0
|
0
|
0
|
|
|
|
if(-e $lockfile && -A $lockfile > $expire) |
106
|
|
|
|
|
|
|
{ |
107
|
0
|
0
|
|
|
|
|
if(unlink $lockfile) |
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
|
0
|
|
|
|
|
last unless --$end; |
119
|
0
|
|
|
|
|
|
sleep 1; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
return 0; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
0
|
1
|
|
sub isLocked() { -e shift->filename } |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
1; |
128
|
|
|
|
|
|
|
|