mhlock.pl untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 153 CC 19 SCAR 35.4
CC
19
Cov
0%
flock_file
72.0
create_lock_dir
42.0
set_lock_mode
20.0
CRAP 34.5
1 ##---------------------------------------------------------------------------##
2 ## File:
3 ## $Id: mhlock.pl,v 1.3 2001/09/17 16:10:40 ehood Exp $
4 ## Author:
5 ## Earl Hood mhonarc@mhonarc.org
6 ## Description:
7 ## Lock functions for MHonArc.
8 ##---------------------------------------------------------------------------##
9 ## MHonArc -- Internet mail-to-HTML converter
10 ## Copyright (C) 1997-1999 Earl Hood, mhonarc@mhonarc.org
11 ##
12 ## This program is free software; you can redistribute it and/or modify
13 ## it under the terms of the GNU General Public License as published by
14 ## the Free Software Foundation; either version 2 of the License, or
15 ## (at your option) any later version.
16 ##
17 ## This program is distributed in the hope that it will be useful,
18 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ## GNU General Public License for more details.
21 ##
22 ## You should have received a copy of the GNU General Public License
23 ## along with this program; if not, write to the Free Software
24 ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 ## 02111-1307, USA
26 ##---------------------------------------------------------------------------##
27  
28 package mhonarc;
29  
30 #############################################################################
31 ## Constants
32 #############################################################################
33  
34 sub MHA_LOCK_MODE_DIR () { 0; }
35 ## -- Directory method: Works on all platforms, but lock dir can be
36 ## left around if abnormal termination.
37 sub MHA_LOCK_MODE_FLOCK () { 1; }
38 ## -- flock() method: Works on select platforms. Can have problems
39 ## if writing to an NFS mount depending on how perl is built.
40 ## If available, and not writing to NFS (or reliable over NFS)
41 ## this method is better than directory method.
42  
43 #############################################################################
44 ## Variables
45 #############################################################################
46  
47 my $_lock_file = undef;
48 my $_flock_fh = undef;
49  
50 $LockFunc = undef;
51 $UnlockFunc = undef;
52  
53 #############################################################################
54 ## Functions
55 #############################################################################
56  
57 ##---------------------------------------------------------------------------
58 ## set_lock_mode(): Set locking method used by MHonArc.
59 ##
60 sub set_lock_mode {
61 my $mode = shift;
62 if ($mode =~ /\D/) {
63 STR2NUM: {
64 if ($mode =~ /^\s*flock/) {
65 $mode = &MHA_LOCK_MODE_FLOCK;
66 last STR2NUM;
67 }
68 $mode = &MHA_LOCK_MODE_DIR;
69 last STR2NUM;
70 }
71 }
72 if ($mode == &MHA_LOCK_MODE_FLOCK) {
73 $LockFunc = \&flock_file;
74 $UnlockFunc = \&unflock_file;
75 return;
76 }
77 $mode = &MHA_LOCK_MODE_DIR;
78 $LockFunc = \&create_lock_dir;
79 $UnlockFunc = \&remove_lock_dir;
80  
81 $mode;
82 }
83  
84 #############################################################################
85 ## Directory Method of Locking Functions
86 #############################################################################
87  
88 ##---------------------------------------------------------------------------
89 ## create_lock_dir() creates a directory to act as a lock.
90 ##
91 sub create_lock_dir {
92 my ($file, $tries, $sleep, $force) = @_;
93 my $prtry = 0;
94 my $ret = 0;
95 $_lock_file = $file;
96 while ($tries > 0) {
97 if (mkdir($file, 0777)) { $ISLOCK = 1; $ret = 1; last; }
98 sleep($sleep) if $sleep > 0;
99 $tries--;
100 if (!$prtry && ($tries > 0)) {
101 print STDOUT qq/Trying to create lock ...\n/ unless $QUIET;
102 $prtry = 1;
103 }
104 }
105 if ($force) { $ISLOCK = 1; $ret = 1; }
106 $ret;
107 }
108  
109 ##---------------------------------------------------------------------------
110 ## remove_lock_dir removes the lock directory
111 ##
112 sub remove_lock_dir {
113 if ($ISLOCK) {
114 if (!rmdir($_lock_file)) {
115 warn "Warning: Unable to remove $LOCKFILE: $!\n";
116 return 0;
117 }
118 $ISLOCK = 0;
119 }
120 1;
121 }
122  
123 #############################################################################
124 ## Flock Functions
125 #############################################################################
126  
127 ##---------------------------------------------------------------------------
128 ## flock_file(): Create archive lock using flock(2).
129 ##
130 sub flock_file {
131 my ($file, $tries, $sleep, $force) = @_;
132  
133 eval {
134 require Symbol;
135 require Fcntl;
136 Fcntl->import(':DEFAULT', ':flock');
137 };
138 if ($@) {
139 warn qq/Warning: Unable to require modules for flock() lock method: /,
140 qq/$@\n/,
141 qq/\tFalling back to directory method.\n/;
142 set_lock_mode(MHA_LOCK_MODE_DIR);
143 return &$LockFunc(@_);
144 }
145  
146 $_lock_file = $file;
147 $_flock_fh = Symbol::gensym;
148  
149 if (!sysopen($_flock_fh, $file, (&O_WRONLY | &O_CREAT), 0666)) {
150 warn(qq/ERROR: Unable to create "$file": $!\n/);
151 return 0;
152 }
153  
154 my $prtry = 0;
155 my $ret = 0;
156 while ($tries > 0) {
157 if (flock($_flock_fh, &LOCK_EX | &LOCK_NB)) {
158 $ISLOCK = 1;
159 $ret = 1;
160 last;
161 }
162 sleep($sleep) if $sleep > 0;
163 $tries--;
164 if (!$prtry && ($tries > 0)) {
165 print STDOUT qq/Trying to create lock ...\n/ unless $QUIET;
166 $prtry = 1;
167 }
168 }
169 if (!$ISLOCK && $force) { $_flock_fh = undef; $ISLOCK = 1; $ret = 1; }
170  
171 $ret;
172 }
173  
174 ##---------------------------------------------------------------------------
175  
176 sub unflock_file {
177 if (defined($_flock_fh)) {
178 flock($_flock_fh, &LOCK_UN);
179 close($_flock_fh);
180 $_flock_fh = undef;
181 }
182 $ISLOCK = 0;
183 }
184  
185 ##---------------------------------------------------------------------------
186  
187 #############################################################################
188  
189 BEGIN {
190 set_lock_mode(MHA_LOCK_MODE_DIR);
191 }
192  
193 1;