]> git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Lock/Exclusive.pm
d6243ec4ae3f10ea55b6a7d2f64fa15a9b730eb4
[selfforum.git] / selfforum-cgi / shared / Lock / Exclusive.pm
1 package Lock::Exclusive;
2
3 ################################################################################
4 # #
5 # File: shared/Lock/Exclusive.pm #
6 # #
7 # Authors: Andre Malo <nd@o3media.de>, 2001-05-25 #
8 # #
9 # Description: Locking and Filehandle class #
10 # using O_EXCL and lock files #
11 # #
12 ################################################################################
13
14 use strict;
15 use vars qw(
16 $VERSION
17 );
18
19 use Fcntl;
20
21 ################################################################################
22 #
23 # Version check
24 #
25 $VERSION = do { my @r =(q$Revision$ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
26
27 ### sub _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 ### sub _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 ### sub _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 ### sub 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 ### sub 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 ### sub 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 # keep 'require' happy
119 1;
120
121 #
122 #
123 ### end of Lock::Exclusive #####################################################

patrick-canterino.de