line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl -w |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Copyright (C) 2002-2016 National Marrow Donor Program. All rights reserved. |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# For a description of this module, please refer to the POD documentation |
6
|
|
|
|
|
|
|
# embedded at the bottom of the file (e.g. perldoc EMDIS::ECS::LockedHash). |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
package EMDIS::ECS::LockedHash; |
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
22666
|
use Data::Dumper; |
|
2
|
|
|
|
|
14340
|
|
|
2
|
|
|
|
|
136
|
|
11
|
2
|
|
|
2
|
|
1358
|
use EMDIS::ECS qw($VERSION); |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
292
|
|
12
|
2
|
|
|
2
|
|
16
|
use Fcntl qw(:DEFAULT :flock); |
|
2
|
|
|
|
|
32
|
|
|
2
|
|
|
|
|
818
|
|
13
|
|
|
|
|
|
|
#use File::lockf; # potential alternate locking method |
14
|
2
|
|
|
2
|
|
1282
|
use SDBM_File; |
|
2
|
|
|
|
|
1084
|
|
|
2
|
|
|
|
|
86
|
|
15
|
2
|
|
|
2
|
|
16
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
50
|
|
16
|
2
|
|
|
2
|
|
10
|
use vars qw($VERSION); |
|
2
|
|
|
|
|
16
|
|
|
2
|
|
|
|
|
3624
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
19
|
|
|
|
|
|
|
# Constructor. Requires name of the database file and name of lock file |
20
|
|
|
|
|
|
|
# as parameters. Also accepts optional lock timeout parameter. |
21
|
|
|
|
|
|
|
sub new { |
22
|
7
|
|
|
7
|
0
|
2920
|
my $class = shift; |
23
|
7
|
|
|
|
|
22
|
my $dbfile = shift; |
24
|
7
|
|
|
|
|
38
|
my $lockfile = shift; |
25
|
7
|
|
|
|
|
15
|
my $lock_timeout = shift; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# validate aaarghs |
28
|
7
|
100
|
|
|
|
51
|
if(!defined $dbfile) { |
29
|
2
|
|
|
|
|
48
|
warn "EMDIS::ECS::LockedHash::new() failed: missing database file name."; |
30
|
2
|
|
|
|
|
14
|
return undef; |
31
|
|
|
|
|
|
|
} |
32
|
5
|
100
|
|
|
|
18
|
if(!defined $lockfile) { |
33
|
2
|
|
|
|
|
24
|
warn "EMDIS::ECS::LockedHash::new() failed: missing lock file name."; |
34
|
2
|
|
|
|
|
12
|
return undef; |
35
|
|
|
|
|
|
|
} |
36
|
3
|
100
|
|
|
|
51
|
$lock_timeout = 10 unless defined $lock_timeout; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# define this object |
39
|
3
|
|
|
|
|
37
|
my $this = {}; |
40
|
3
|
|
|
|
|
39
|
bless $this, $class; |
41
|
3
|
|
|
|
|
28
|
$this->{dbfile} = $dbfile; |
42
|
3
|
|
|
|
|
51
|
$this->{lockfile} = $lockfile; |
43
|
3
|
|
|
|
|
17
|
$this->{lock_timeout} = $lock_timeout; |
44
|
3
|
|
|
|
|
74
|
$this->{ERROR} = ''; |
45
|
3
|
|
|
|
|
24
|
$this->{LOCK} = 0; |
46
|
3
|
|
|
|
|
13
|
$this->{TIED} = ''; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# open lock file and retain file handle |
49
|
3
|
50
|
|
|
|
447
|
if(!sysopen($this->{FH_LOCK}, $this->{lockfile}, O_RDWR|O_CREAT)) { |
50
|
0
|
|
|
|
|
0
|
warn "EMDIS::ECS::LockedHash::new() failed: unable to access lock file " . |
51
|
|
|
|
|
|
|
"'$this->{lockfile}': $!"; |
52
|
0
|
|
|
|
|
0
|
return undef; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# tie/untie db file, to test whether it's accessible |
56
|
3
|
50
|
|
|
|
68
|
if(!$this->_tie()) { |
57
|
0
|
|
|
|
|
0
|
warn "EMDIS::ECS::LockedHash::new() failed: " . $this->ERROR; |
58
|
0
|
|
|
|
|
0
|
return undef; |
59
|
|
|
|
|
|
|
} |
60
|
3
|
|
|
|
|
12
|
$this->_untie; |
61
|
|
|
|
|
|
|
|
62
|
3
|
|
|
|
|
11
|
return $this; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
66
|
|
|
|
|
|
|
# set/get error description |
67
|
|
|
|
|
|
|
sub ERROR { |
68
|
40
|
|
|
40
|
0
|
424
|
my $this = shift; |
69
|
40
|
|
|
|
|
99
|
my $err = shift; |
70
|
40
|
100
|
|
|
|
86
|
if(defined $err) { |
71
|
33
|
|
|
|
|
88
|
$this->{ERROR} = $err; |
72
|
|
|
|
|
|
|
} |
73
|
40
|
|
|
|
|
125
|
return $this->{ERROR}; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
77
|
|
|
|
|
|
|
# set/get locked status indicator |
78
|
|
|
|
|
|
|
sub LOCK { |
79
|
59
|
|
|
59
|
0
|
114
|
my $this = shift; |
80
|
59
|
|
|
|
|
82
|
my $status = shift; |
81
|
59
|
100
|
|
|
|
145
|
if(defined $status) { |
82
|
18
|
|
|
|
|
33
|
$this->{LOCK} = $status; |
83
|
|
|
|
|
|
|
} |
84
|
59
|
|
|
|
|
191
|
return $this->{LOCK}; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
88
|
|
|
|
|
|
|
# set/get tied status indicator |
89
|
|
|
|
|
|
|
sub TIED { |
90
|
43
|
|
|
43
|
0
|
71
|
my $this = shift; |
91
|
43
|
|
|
|
|
108
|
my $status = shift; |
92
|
43
|
100
|
|
|
|
131
|
if(defined $status) { |
93
|
21
|
|
|
|
|
45
|
$this->{TIED} = $status; |
94
|
|
|
|
|
|
|
} |
95
|
43
|
|
|
|
|
143
|
return $this->{TIED}; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
99
|
|
|
|
|
|
|
# Read one key-value from the database under a shared lock |
100
|
|
|
|
|
|
|
sub read { |
101
|
5
|
|
|
5
|
0
|
311
|
my $this = shift; |
102
|
5
|
|
|
|
|
22
|
my $key = shift; |
103
|
5
|
|
|
|
|
8
|
my $value = undef; |
104
|
|
|
|
|
|
|
|
105
|
5
|
|
|
|
|
15
|
$this->ERROR(''); # reset error status |
106
|
|
|
|
|
|
|
# check lock status |
107
|
5
|
100
|
66
|
|
|
9
|
if($this->LOCK != LOCK_SH and $this->LOCK != LOCK_EX) { |
108
|
1
|
|
|
|
|
4
|
$this->ERROR( |
109
|
|
|
|
|
|
|
"EMDIS::ECS::LockedHash::read() requires shared or exclusive lock."); |
110
|
1
|
|
|
|
|
6
|
return undef; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
# read value from hash |
113
|
4
|
|
|
|
|
8
|
$value = undef; |
114
|
4
|
100
|
|
|
|
62
|
$value = $this->{hash}->{$key} if exists $this->{hash}->{$key}; |
115
|
4
|
100
|
100
|
|
|
35
|
if(defined($value) and ($value =~ /^\$\w+\s*=\s*\{.*\}\s*\;\s*$/s)) { |
116
|
|
|
|
|
|
|
# convert Dumper() string to hash ref |
117
|
1
|
|
|
|
|
13
|
$value =~ s/^\$\w+/\$value/; # convert "$VAR1 = ..." to "$value = ..." |
118
|
1
|
|
|
|
|
218
|
eval($value); # eval "$value = ..." string |
119
|
|
|
|
|
|
|
} |
120
|
4
|
|
|
|
|
17
|
return $value; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
124
|
|
|
|
|
|
|
# Write one key-value to the database under an exclusive lock |
125
|
|
|
|
|
|
|
sub write { |
126
|
3
|
|
|
3
|
0
|
7
|
my $this = shift; |
127
|
3
|
|
|
|
|
12
|
my $key = shift; |
128
|
3
|
|
|
|
|
11
|
my $value = shift; |
129
|
|
|
|
|
|
|
|
130
|
3
|
|
|
|
|
8
|
$this->ERROR(''); # reset error status |
131
|
|
|
|
|
|
|
# check lock status |
132
|
3
|
100
|
|
|
|
6
|
if($this->LOCK != LOCK_EX) { |
133
|
1
|
|
|
|
|
3
|
$this->ERROR("EMDIS::ECS::LockedHash::write() requires exclusive lock."); |
134
|
1
|
|
|
|
|
11
|
return ''; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
# write value to hash |
137
|
2
|
100
|
|
|
|
7
|
if(ref $value) { |
138
|
1
|
|
|
|
|
22
|
local $Data::Dumper::Indent = 0; |
139
|
1
|
|
|
|
|
24
|
$value = Dumper($value); # convert ref to Dumper() string |
140
|
|
|
|
|
|
|
} |
141
|
2
|
|
|
|
|
340
|
$this->{hash}->{$key} = $value; |
142
|
2
|
|
|
|
|
17
|
return 1; # successful |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
146
|
|
|
|
|
|
|
# Delete a key-value under an exclusive lock |
147
|
|
|
|
|
|
|
sub delete { |
148
|
2
|
|
|
2
|
0
|
11
|
my $this = shift; |
149
|
2
|
|
|
|
|
5
|
my $key = shift; |
150
|
2
|
|
|
|
|
4
|
my $value = undef; |
151
|
|
|
|
|
|
|
|
152
|
2
|
|
|
|
|
8
|
$this->ERROR(''); # reset error status |
153
|
|
|
|
|
|
|
# check lock status |
154
|
2
|
100
|
|
|
|
6
|
if($this->LOCK != LOCK_EX) { |
155
|
1
|
|
|
|
|
5
|
$this->ERROR("EMDIS::ECS::LockedHash::delete() requires exclusive lock."); |
156
|
1
|
|
|
|
|
12
|
return ''; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
# delete value from hash |
159
|
1
|
|
|
|
|
32
|
$value = delete $this->{hash}->{$key}; |
160
|
1
|
|
|
|
|
13
|
return 1; # successful |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
164
|
|
|
|
|
|
|
# Return a list of key values under a shared lock |
165
|
|
|
|
|
|
|
sub keys { |
166
|
3
|
|
|
3
|
0
|
6
|
my $this = shift; |
167
|
3
|
|
|
|
|
6
|
my @ks = (); |
168
|
|
|
|
|
|
|
|
169
|
3
|
|
|
|
|
10
|
$this->ERROR(''); # reset error status |
170
|
|
|
|
|
|
|
# check lock status |
171
|
3
|
100
|
66
|
|
|
12
|
if($this->LOCK != LOCK_SH and $this->LOCK != LOCK_EX) { |
172
|
1
|
|
|
|
|
3
|
$this->ERROR( |
173
|
|
|
|
|
|
|
"EMDIS::ECS::LockedHash::keys() requires shared or exclusive lock."); |
174
|
1
|
|
|
|
|
12
|
return ''; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
# get keys from hash |
177
|
2
|
|
|
|
|
11
|
@ks = keys %{$this->{hash}}; |
|
2
|
|
|
|
|
55
|
|
178
|
2
|
|
|
|
|
14
|
return @ks; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
182
|
|
|
|
|
|
|
# Obtain (advisory) lock and tie internal hash to db file. |
183
|
|
|
|
|
|
|
sub lock { |
184
|
6
|
|
|
6
|
0
|
13
|
my $this = shift; |
185
|
6
|
|
|
|
|
10
|
my $lock_type = shift; |
186
|
6
|
|
|
|
|
11
|
my $oldlock = $this->LOCK; |
187
|
6
|
50
|
|
|
|
16
|
$lock_type = LOCK_EX unless $lock_type; # default = LOCK_EX |
188
|
6
|
|
|
|
|
18
|
$this->ERROR(''); # reset error status |
189
|
6
|
50
|
|
|
|
13
|
return 1 if $oldlock == $lock_type; # already locked |
190
|
6
|
|
|
|
|
8
|
my $locked = 0; |
191
|
6
|
|
|
|
|
6
|
my $attempt = 0; |
192
|
6
|
|
66
|
|
|
46
|
while(!$locked and $attempt++ < 5) { |
193
|
6
|
50
|
|
|
|
15
|
sleep 2 if $attempt > 1; |
194
|
6
|
|
|
|
|
14
|
$this->ERROR(''); # reset error status |
195
|
6
|
|
|
|
|
14
|
$locked = $this->_lock($lock_type); |
196
|
|
|
|
|
|
|
} |
197
|
6
|
50
|
|
|
|
15
|
if(!$locked) { |
198
|
0
|
|
|
|
|
0
|
$this->ERROR("EMDIS::ECS::LockedHash::lock() failed: " . $this->ERROR); |
199
|
0
|
|
|
|
|
0
|
return ''; |
200
|
|
|
|
|
|
|
} |
201
|
6
|
50
|
33
|
|
|
12
|
if(!$this->TIED and !$this->_tie()) { |
202
|
0
|
|
|
|
|
0
|
$this->ERROR("EMDIS::ECS::LockedHash::lock() failed: " . $this->ERROR); |
203
|
0
|
|
|
|
|
0
|
return ''; |
204
|
|
|
|
|
|
|
} |
205
|
6
|
|
|
|
|
31
|
return 1; # successful |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
209
|
|
|
|
|
|
|
# Release (advisory) lock and untie internal hash. |
210
|
|
|
|
|
|
|
sub unlock { |
211
|
6
|
|
|
6
|
0
|
212
|
my $this = shift; |
212
|
6
|
|
|
|
|
19
|
$this->_untie(); |
213
|
6
|
|
|
|
|
14
|
$this->_unlock(); |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
217
|
|
|
|
|
|
|
# Quickly delete all key-values under an exclusive lock |
218
|
|
|
|
|
|
|
sub undef { |
219
|
2
|
|
|
2
|
0
|
9
|
my $this = shift; |
220
|
|
|
|
|
|
|
|
221
|
2
|
|
|
|
|
7
|
$this->ERROR(''); # reset error status |
222
|
|
|
|
|
|
|
# check lock status |
223
|
2
|
100
|
|
|
|
4
|
if($this->LOCK != LOCK_EX) { |
224
|
1
|
|
|
|
|
4
|
$this->ERROR("EMDIS::ECS::LockedHash::undef() requires exclusive lock."); |
225
|
1
|
|
|
|
|
5
|
return ''; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
# delete everything from hash |
228
|
1
|
|
|
|
|
2
|
undef %{$this->{hash}}; |
|
1
|
|
|
|
|
40
|
|
229
|
1
|
|
|
|
|
65
|
return 1; # successful |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
234
|
|
|
|
|
|
|
# untie hash and close lock file when perl object passes out of scope |
235
|
|
|
|
|
|
|
sub DESTROY { |
236
|
1
|
|
|
1
|
|
46
|
my $this = shift; |
237
|
1
|
|
|
|
|
18
|
$this->_untie(); |
238
|
|
|
|
|
|
|
close($this->{FH_LOCK}) |
239
|
1
|
50
|
|
|
|
351
|
if defined $this->{FH_LOCK}; |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
243
|
|
|
|
|
|
|
# Select UNIX or Win32 version of _lock |
244
|
|
|
|
|
|
|
sub _lock |
245
|
|
|
|
|
|
|
{ |
246
|
10
|
50
|
|
10
|
|
131
|
$^O =~ /MSWin32/ ? _lock_win32(@_) : _lock_unix(@_); |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
250
|
|
|
|
|
|
|
# Internal subroutine: obtain (advisory) lock, using time limit to |
251
|
|
|
|
|
|
|
# avoid indefinite blocking. Returns true if able to obtain lock within |
252
|
|
|
|
|
|
|
# time limit; otherwise returns false. |
253
|
|
|
|
|
|
|
sub _lock_unix { |
254
|
10
|
|
|
10
|
|
19
|
my $this = shift; |
255
|
10
|
|
|
|
|
17
|
my $lock_type = shift; |
256
|
10
|
100
|
|
|
|
24
|
$lock_type = LOCK_EX unless defined $lock_type; |
257
|
10
|
|
|
|
|
18
|
my $result = 1; |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
# set up "local" SIG_ALRM handler |
260
|
|
|
|
|
|
|
# (Note: not using "local $SIG{PIPE}" because it ignores die()) |
261
|
10
|
|
|
|
|
44
|
my $oldsigalrm = $SIG{ALRM}; |
262
|
|
|
|
|
|
|
$SIG{ALRM} = sub { |
263
|
1
|
|
|
1
|
|
74
|
die "timeout - $this->{lock_timeout} second time limit exceeded\n"; |
264
|
10
|
|
|
|
|
305
|
}; |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
# attempt to obtain lock, with time limit |
267
|
10
|
|
|
|
|
84
|
eval { |
268
|
10
|
|
|
|
|
77
|
alarm($this->{lock_timeout}); # set alarm |
269
|
|
|
|
|
|
|
die "flock() failed: $!\n" |
270
|
10
|
100
|
|
|
|
2000426
|
unless flock($this->{FH_LOCK}, $lock_type); |
271
|
|
|
|
|
|
|
# File::lockf -- potential alternate locking method: |
272
|
|
|
|
|
|
|
# my $status = File::lockf::lock($this->{FH_LOCK}, 0); |
273
|
|
|
|
|
|
|
# die "lockf failed: $status\n" |
274
|
|
|
|
|
|
|
# if $status != 0; |
275
|
9
|
|
|
|
|
53
|
alarm(0); # turn off alarm |
276
|
|
|
|
|
|
|
}; |
277
|
10
|
100
|
|
|
|
40
|
if($@) { |
278
|
1
|
|
|
|
|
48
|
alarm(0); # turn off alarm |
279
|
1
|
|
|
|
|
26
|
$this->ERROR("EMDIS::ECS::LockedHash::_lock_unix() failed: $@"); |
280
|
1
|
|
|
|
|
134
|
$this->LOCK(0); # reset status indicator |
281
|
1
|
|
|
|
|
15
|
$result = ''; |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
# restore previous SIG_ALRM handler |
284
|
10
|
50
|
|
|
|
36
|
if(defined $oldsigalrm) { $SIG{ALRM} = $oldsigalrm; } |
|
0
|
|
|
|
|
0
|
|
285
|
10
|
|
|
|
|
177
|
else { delete $SIG{ALRM}; } |
286
|
10
|
100
|
|
|
|
66
|
$this->LOCK($lock_type) # set status indicator |
287
|
|
|
|
|
|
|
if $result; |
288
|
10
|
|
|
|
|
63
|
return $result; # successful |
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
292
|
|
|
|
|
|
|
# Internal subroutine: obtain (advisory) lock, using time limit to |
293
|
|
|
|
|
|
|
# avoid indefinite blocking. Returns true if able to obtain lock within |
294
|
|
|
|
|
|
|
# time limit; otherwise returns false. |
295
|
|
|
|
|
|
|
sub _lock_win32 { |
296
|
0
|
|
|
0
|
|
0
|
my $this = shift; |
297
|
0
|
|
|
|
|
0
|
my $lock_type = shift; |
298
|
0
|
0
|
|
|
|
0
|
$lock_type = LOCK_EX unless defined $lock_type; |
299
|
0
|
|
|
|
|
0
|
my $result = 1; |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
# attempt to obtain lock, with time limit |
302
|
|
|
|
|
|
|
# (uses polling method to obtain lock -- somewhat more crude than |
303
|
|
|
|
|
|
|
# the unix method, which uses blocking with SIGALRM to enforce timeout) |
304
|
0
|
|
|
|
|
0
|
my $timeoutCount = 0; |
305
|
0
|
|
|
|
|
0
|
my $locked; |
306
|
0
|
|
0
|
|
|
0
|
while (!($locked = flock($this->{FH_LOCK}, $lock_type | LOCK_NB)) and |
307
|
|
|
|
|
|
|
($timeoutCount++ <= $this->{lock_timeout})) { |
308
|
0
|
|
|
|
|
0
|
sleep 1; |
309
|
|
|
|
|
|
|
} |
310
|
|
|
|
|
|
|
|
311
|
0
|
0
|
|
|
|
0
|
if(!$locked) { |
312
|
0
|
|
|
|
|
0
|
$this->ERROR("EMDIS::ECS::LockedHash::_lock_win32() failed: $@"); |
313
|
0
|
|
|
|
|
0
|
$this->LOCK(0); # reset status indicator |
314
|
0
|
|
|
|
|
0
|
$result = ''; |
315
|
|
|
|
|
|
|
} |
316
|
0
|
0
|
|
|
|
0
|
$this->LOCK($lock_type) # set status indicator |
317
|
|
|
|
|
|
|
if $result; |
318
|
0
|
|
|
|
|
0
|
return $result; # successful |
319
|
|
|
|
|
|
|
} |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
322
|
|
|
|
|
|
|
# Internal subroutine: tie hash to db file |
323
|
|
|
|
|
|
|
sub _tie { |
324
|
10
|
|
|
10
|
|
22
|
my $this = shift; |
325
|
10
|
|
|
|
|
814
|
$this->{db_obj} = tie(%{$this->{hash}}, 'SDBM_File', $this->{dbfile}, |
326
|
10
|
50
|
|
|
|
18
|
O_CREAT|O_RDWR, (defined $EMDIS::ECS::FILEMODE ? $EMDIS::ECS::FILEMODE : 0664)) |
|
|
50
|
|
|
|
|
|
327
|
|
|
|
|
|
|
or $this->ERROR( |
328
|
|
|
|
|
|
|
"EMDIS::ECS::LockedHash::_tie() failed ($this->{dbfile}): $!"); |
329
|
10
|
50
|
|
|
|
75
|
if($this->{db_obj}) { |
330
|
10
|
|
|
|
|
54
|
$this->TIED(1); # set status indicator |
331
|
|
|
|
|
|
|
} else { |
332
|
0
|
|
|
|
|
0
|
$this->TIED(''); # reset status indicator |
333
|
|
|
|
|
|
|
} |
334
|
10
|
|
|
|
|
24
|
return $this->TIED; |
335
|
|
|
|
|
|
|
} |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
338
|
|
|
|
|
|
|
# Internal subroutine: release (advisory) lock |
339
|
|
|
|
|
|
|
sub _unlock { |
340
|
8
|
|
|
8
|
|
19
|
my $this = shift; |
341
|
8
|
|
|
|
|
86
|
flock($this->{FH_LOCK}, LOCK_UN); |
342
|
|
|
|
|
|
|
# File::lockf -- potential alternate locking method: |
343
|
|
|
|
|
|
|
# my $status = File::lockf::ulock($this->{FH_LOCK}, 0); |
344
|
8
|
|
|
|
|
114
|
$this->LOCK(0); # reset status indicator |
345
|
|
|
|
|
|
|
} |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
348
|
|
|
|
|
|
|
# Internal subroutine: untie hash from db file |
349
|
|
|
|
|
|
|
# (mainly, insure that output is flushed to disk) |
350
|
|
|
|
|
|
|
sub _untie { |
351
|
11
|
|
|
11
|
|
27
|
my $this = shift; |
352
|
|
|
|
|
|
|
untie $this->{hash} |
353
|
11
|
100
|
|
|
|
38
|
if exists $this->{hash}; |
354
|
11
|
|
|
|
|
32
|
delete $this->{hash}; |
355
|
11
|
|
|
|
|
172
|
delete $this->{db_obj}; |
356
|
11
|
|
|
|
|
43
|
$this->TIED(''); # reset status indicator |
357
|
|
|
|
|
|
|
} |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
1; |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
__DATA__ |