line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyrights 2001-2019 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
|
|
|
2
|
|
1776
|
use vars '$VERSION'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
108
|
|
11
|
|
|
|
|
|
|
$VERSION = '3.008'; |
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
10
|
use base 'Mail::Box::Locker'; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
518
|
|
14
|
|
|
|
|
|
|
|
15
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
44
|
|
16
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
40
|
|
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
2
|
|
7
|
use IO::File; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
267
|
|
19
|
2
|
|
|
2
|
|
10
|
use File::Spec; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
40
|
|
20
|
2
|
|
|
2
|
|
8
|
use Errno qw/EEXIST/; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
210
|
|
21
|
2
|
|
|
2
|
|
12
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
1476
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub init($) |
25
|
1
|
|
|
1
|
0
|
12
|
{ my ($self, $args) = @_; |
26
|
1
|
50
|
|
|
|
3
|
$args->{file} = $args->{dotlock_file} if $args->{dotlock_file}; |
27
|
1
|
|
|
|
|
6
|
$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
|
|
|
39
|
@_ && $_[0] or return $self->SUPER::folder; |
35
|
|
|
|
|
|
|
|
36
|
1
|
|
|
|
|
3
|
my $folder = shift; |
37
|
1
|
50
|
|
|
|
5
|
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
|
|
|
|
|
4
|
$self->SUPER::folder($folder); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub _try_lock($) |
52
|
1
|
|
|
1
|
|
2
|
{ my ($self, $lockfile) = @_; |
53
|
1
|
50
|
|
|
|
13
|
return if -e $lockfile; |
54
|
|
|
|
|
|
|
|
55
|
1
|
50
|
|
|
|
4
|
my $flags = $^O eq 'MSWin32' |
56
|
|
|
|
|
|
|
? O_CREAT|O_EXCL|O_WRONLY |
57
|
|
|
|
|
|
|
: O_CREAT|O_EXCL|O_WRONLY|O_NONBLOCK; |
58
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
8
|
my $lock = IO::File->new($lockfile, $flags, 0600); |
60
|
1
|
50
|
|
|
|
131
|
if($lock) |
61
|
1
|
|
|
|
|
9
|
{ close $lock; |
62
|
1
|
|
|
|
|
13
|
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
|
259
|
{ my $self = shift; |
74
|
1
|
50
|
|
|
|
3
|
$self->hasLock |
75
|
|
|
|
|
|
|
or return $self; |
76
|
|
|
|
|
|
|
|
77
|
1
|
|
|
|
|
3
|
my $lock = $self->filename; |
78
|
|
|
|
|
|
|
|
79
|
1
|
50
|
|
|
|
47
|
unlink $lock |
80
|
|
|
|
|
|
|
or $self->log(WARNING => "Couldn't remove lockfile $lock: $!"); |
81
|
|
|
|
|
|
|
|
82
|
1
|
|
|
|
|
7
|
$self->SUPER::unlock; |
83
|
1
|
|
|
|
|
2
|
$self; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub lock() |
88
|
2
|
|
|
2
|
1
|
882
|
{ my $self = shift; |
89
|
|
|
|
|
|
|
|
90
|
2
|
|
|
|
|
6
|
my $lockfile = $self->filename; |
91
|
2
|
100
|
|
|
|
8
|
if($self->hasLock) |
92
|
1
|
|
|
|
|
10
|
{ $self->log(WARNING => "Folder already locked with file $lockfile"); |
93
|
1
|
|
|
|
|
39
|
return 1; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
1
|
|
|
|
|
5
|
my $timeout = $self->timeout; |
97
|
1
|
50
|
|
|
|
4
|
my $end = $timeout eq 'NOTIMEOUT' ? -1 : $timeout; |
98
|
1
|
|
|
|
|
4
|
my $expire = $self->expires/86400; # in days for -A |
99
|
|
|
|
|
|
|
|
100
|
1
|
|
|
|
|
1
|
while(1) |
101
|
|
|
|
|
|
|
{ |
102
|
1
|
50
|
|
|
|
4
|
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
|
|
|
|
|
|
|
|