]> git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Lock/Exclusive.pm
Admin.pm: hide_posting()/recover_posting(): thread and forum XML files are now locked...
[selfforum.git] / selfforum-cgi / shared / Lock / Exclusive.pm
1 package Lock::Exclusive;
2
3 ################################################################################
4 # #
5 # File: shared/Lock/Exclusive.pm #
6 # #
7 # Authors: André Malo <nd@o3media.de> #
8 # #
9 # Description: Locking and Filehandle class #
10 # using O_EXCL and lock files #
11 # #
12 ################################################################################
13
14 use strict;
15 use Fcntl;
16
17 ################################################################################
18 #
19 # Version check
20 #
21 # last modified:
22 # $Date$ (GMT)
23 # by $Author$
24 #
25 sub VERSION {(q$Revision$ =~ /([\d.]+)\s*$/)[0] or '0.0'}
26
27 ### _simple_lock () ############################################################
28 #
29 # simple file lock
30 # (create lock file)
31 #
32 # Params: $filename - file to lock
33 # $timeout - timeout
34 #
35 # Return: success (boolean)
36 #
37 sub _simple_lock {
38 my ($self, $fh) = @_;
39
40 sysopen ($fh, $fh->filename, O_CREAT | O_EXCL | O_WRONLY)
41 and close $fh
42 and return 1;
43
44 return;
45 }
46
47 ### _simple_unlock () ##########################################################
48 #
49 # simple file unlock
50 # (unlink lock file)
51 #
52 # Params: $filename - lockfile name
53 # ^^^^^^^^
54 #
55 # Return: success (boolean)
56 #
57 sub _simple_unlock {
58 my ($self, $filename) = @_;
59
60 return 1 if (!-f $filename or unlink $filename);
61
62 # not able to unlink lock file, hmmm...
63 #
64 return;
65 }
66
67 ### _reftime () ################################################################
68 #
69 # determine reference time for violent unlock
70 #
71 # Params: ~none~
72 #
73 # Return: time or zero, if no reference file found
74 #
75 sub _reftime {
76 my $self = shift;
77 my ($time, $reffile) = 0;
78
79 if (-f ($reffile = $self -> filename)) {
80 $time = (stat $reffile)[9];}
81
82 elsif (-f ($reffile = $self -> lockfile)) {
83 $time = (stat $reffile)[9];}
84
85 $time;
86 }
87
88 ### masterlocked () ############################################################
89 #
90 # check on master lock status of the file
91 #
92 # Params: ~none~
93 #
94 # Return: status (boolean)
95 #
96 sub masterlocked {-f shift -> masterlock}
97
98 ### excl_announced () ##########################################################
99 #
100 # check on exclusive lock announced status of the file
101 #
102 # Params: ~none~
103 #
104 # Return: status (boolean)
105 #
106 sub excl_announced {-f shift -> lockfile}
107
108 ### exsh_announced () ##########################################################
109 #
110 # check on exclusive shared lock status of the file
111 #
112 # Params: ~none~
113 #
114 # Return: status (boolean)
115 #
116 sub exsh_announced {-f shift -> exshlock}
117
118 ### purge () ###################################################################
119 #
120 # cover our traces after a file was removed
121 #
122 # Params: ~none~
123 #
124 # Return: ~none~
125 #
126 sub purge {
127 shift -> release;
128 }
129
130 # keep 'require' happy
131 1;
132
133 #
134 #
135 ### end of Lock::Exclusive #####################################################

patrick-canterino.de