| 1 | # Copyright (C) 2004 Jörg Tiedemann <joerg@stp.ling.uu.se> | ||
| 2 | # | ||
| 3 | # This program is free software; you can redistribute it and/or modify | ||
| 4 | # it under the terms of the GNU General Public License as published by | ||
| 5 | # the Free Software Foundation; either version 2 of the License, or | ||
| 6 | # (at your option) any later version. | ||
| 7 | # | ||
| 8 | # This program is distributed in the hope that it will be useful, | ||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | # GNU General Public License for more details. | ||
| 12 | # | ||
| 13 | # You should have received a copy of the GNU General Public License | ||
| 14 | # along with this program; if not, write to the Free Software | ||
| 15 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 16 | |||
| 17 | |||
| 18 | package Uplug::Web::Process::Stack; | ||
| 19 | |||
| 20 | use strict; | ||
| 21 | use Fcntl qw(:DEFAULT :flock); | ||
| 22 | |||
| 23 | use Uplug::Web::Process::Lock; | ||
| 24 | |||
| 25 | |||
| 26 | my $DEFAULTMAXFLOCKWAIT=5; | ||
| 27 | |||
| 28 | |||
| 29 | sub new{ | ||
| 30 | my $class=shift; | ||
| 31 | my $self={}; | ||
| 32 | bless $self,$class; | ||
| 33 | $self->setFile($_[0]); | ||
| 34 | $self->setFlockWait($_[1]); | ||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | sub setFile{ | ||
| 41 | my $self=shift; | ||
| 42 | my $file=shift; | ||
| 43 | if (not -e $file){ | ||
| 44 | sysopen (F,$file,O_CREAT); | ||
| 45 | close F; | ||
| 46 | # system "chmod g+w $file"; | ||
| 47 | system "chmod o+w $file"; | ||
| 48 | } | ||
| 49 | $self->{FILE}=$file; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub getFile{ | ||
| 53 | my $self=shift; | ||
| 54 | return $self->{FILE}; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub setFlockWait{ | ||
| 58 | my $self=shift; | ||
| 59 | my $wait=shift; | ||
| 60 | if ($wait){$self->{MAXFLOCKWAIT}=$wait;} | ||
| 61 | else{$self->{MAXFLOCKWAIT}=$DEFAULTMAXFLOCKWAIT;} | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | sub open{ | ||
| 66 | my $self=shift; | ||
| 67 | my $lock=shift; # open with file locking! | ||
| 68 | |||
| 69 | if (not -e $self->{FILE}){return 0;} | ||
| 70 | open $self->{FH},"+<$self->{FILE}"; | ||
| 71 | my $fh=$self->{FH}; | ||
| 72 | |||
| 73 | if ($lock){ | ||
| 74 | if (not $self->lock()){ # lock the file | ||
| 75 | close $fh; | ||
| 76 | return 0; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | $self->{STATUS}='open'; | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub lock{ | ||
| 84 | my $self=shift; | ||
| 85 | if (nflock($self->{FILE},$self->{MAXLOCKWAIT})){ | ||
| 86 | $self->{LOCKED}=1; | ||
| 87 | return 1; | ||
| 88 | } | ||
| 89 | return 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub unlock{ | ||
| 93 | my $self=shift; | ||
| 94 | nunflock($self->{FILE}); # release lock! | ||
| 95 | delete $self->{LOCKED}; | ||
| 96 | return 1; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub openFlock{ | ||
| 101 | my $self=shift; | ||
| 102 | |||
| 103 | my $fh=$self->{FH}; | ||
| 104 | if (not -e $self->{FILE}){return 0;} | ||
| 105 | open $self->{FH},"+<$self->{FILE}"; | ||
| 106 | my $sec=0; | ||
| 107 | while (not flock($self->{FH},2)){ | ||
| 108 | $sec++;sleep(1); | ||
| 109 | if ($sec>$self->{MAXFLOCKWAIT}){ | ||
| 110 | close $self->{FH}; | ||
| 111 | return 0; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | $self->{STATUS}='open'; | ||
| 115 | return 1; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub close{ | ||
| 119 | my $self=shift; | ||
| 120 | |||
| 121 | if ($self->{STATUS} eq 'open'){ | ||
| 122 | my $fh=$self->{FH}; | ||
| 123 | truncate($fh,tell($fh)); | ||
| 124 | close $fh; | ||
| 125 | $self->{STATUS}='closed'; | ||
| 126 | } | ||
| 127 | if ($self->{LOCKED}){ | ||
| 128 | nunflock($self->{FILE}); # release lock! | ||
| 129 | delete $self->{LOCKED}; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | sub read{ | ||
| 134 | my $self=shift; | ||
| 135 | |||
| 136 | my $NeedToClose=0; | ||
| 137 | if ($self->{STATUS} ne 'open'){ # if the stack is not opend yet: | ||
| 138 | if (not $self->open()){return 0;} # open it! | ||
| 139 | $NeedToClose=1; # but close it afterwards again! | ||
| 140 | } | ||
| 141 | my $fh=$self->{FH}; # get the file handle | ||
| 142 | seek ($fh,0,0); # go to the beginning of the file | ||
| 143 | my @content=<$fh>; # and read from it | ||
| 144 | if ($NeedToClose){$self->close();} # close the stack if we opened it | ||
| 145 | |||
| 146 | return wantarray ? @content : join "@content"; | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | sub write{ | ||
| 151 | my $self=shift; | ||
| 152 | my $content=shift; | ||
| 153 | |||
| 154 | my $NeedToClose=0; | ||
| 155 | if ($self->{STATUS} ne 'open'){ | ||
| 156 | if (not $self->open('lock')){return 0;} # open in lock-mode | ||
| 157 | $NeedToClose=1; | ||
| 158 | } | ||
| 159 | if (not $self->{LOCKED}){ | ||
| 160 | if (not $self->lock()){return 0;} | ||
| 161 | } | ||
| 162 | my $fh=$self->{FH}; | ||
| 163 | seek ($fh,0,0); | ||
| 164 | if (ref($content) eq 'ARRAY'){print $fh @{$content};} | ||
| 165 | else{print $fh $content;} | ||
| 166 | if ($NeedToClose){$self->close();} | ||
| 167 | return 1; | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | sub push{ | ||
| 172 | my $self=shift; | ||
| 173 | my $text=join(':',@_); | ||
| 174 | |||
| 175 | my $NeedToClose=0; | ||
| 176 | if ($self->{STATUS} ne 'open'){ | ||
| 177 | if (not $self->open('lock')){return 0;} # open in lock-mode | ||
| 178 | $NeedToClose=1; | ||
| 179 | } | ||
| 180 | if (not $self->{LOCKED}){ | ||
| 181 | if (not $self->lock()){return 0;} | ||
| 182 | } | ||
| 183 | my @content=$self->read(); | ||
| 184 | push (@content,$text."\n"); | ||
| 185 | $self->write(\@content); | ||
| 186 | if ($NeedToClose){$self->close();} | ||
| 187 | return 1; | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | sub pop{ | ||
| 192 | my $self=shift; | ||
| 193 | |||
| 194 | my $NeedToClose=0; | ||
| 195 | if ($self->{STATUS} ne 'open'){ | ||
| 196 | if (not $self->open('lock')){return undef;} # open in lock-mode | ||
| 197 | $NeedToClose=1; | ||
| 198 | } | ||
| 199 | if (not $self->{LOCKED}){ | ||
| 200 | if (not $self->lock()){return undef;} | ||
| 201 | } | ||
| 202 | my @content=$self->read(); | ||
| 203 | my $text=pop (@content); | ||
| 204 | $self->write(\@content); | ||
| 205 | if ($NeedToClose){$self->close();} | ||
| 206 | chomp($text); | ||
| 207 | return wantarray ? split(/\:/,$text) : $text; | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | |||
| 212 | sub unshift{ | ||
| 213 | my $self=shift; | ||
| 214 | my $text=join(':',@_); | ||
| 215 | |||
| 216 | my $NeedToClose=0; | ||
| 217 | if ($self->{STATUS} ne 'open'){ | ||
| 218 | if (not $self->open('lock')){return 0;} # open in lock-mode | ||
| 219 | $NeedToClose=1; | ||
| 220 | } | ||
| 221 | if (not $self->{LOCKED}){ | ||
| 222 | if (not $self->lock()){return 0;} | ||
| 223 | } | ||
| 224 | my @content=$self->read(); | ||
| 225 | unshift (@content,$text."\n"); | ||
| 226 | $self->write(\@content); | ||
| 227 | if ($NeedToClose){$self->close();} | ||
| 228 | return 1; | ||
| 229 | } | ||
| 230 | |||
| 231 | |||
| 232 | sub shift{ | ||
| 233 | my $self=shift; | ||
| 234 | |||
| 235 | my $NeedToClose=0; | ||
| 236 | if ($self->{STATUS} ne 'open'){ | ||
| 237 | if (not $self->open('lock')){return undef;} # open in lock-mode | ||
| 238 | $NeedToClose=1; | ||
| 239 | } | ||
| 240 | if (not $self->{LOCKED}){ | ||
| 241 | if (not $self->lock()){return undef;} | ||
| 242 | } | ||
| 243 | my @content=$self->read(); | ||
| 244 | my $text=shift (@content); | ||
| 245 | $self->write(\@content); | ||
| 246 | if ($NeedToClose){$self->close();} | ||
| 247 | chomp($text); | ||
| 248 | return wantarray ? split(/\:/,$text) : $text; | ||
| 249 | } | ||
| 250 | |||
| 251 | sub remove{ | ||
| 252 | my $self=shift; | ||
| 253 | my @data=@_; | ||
| 254 | map($_=quotemeta($_),@data); | ||
| 255 | my $pattern='^'.join(':',@data).'(\:|\Z)'; | ||
| 256 | |||
| 257 | my $NeedToClose=0; | ||
| 258 | if ($self->{STATUS} ne 'open'){ | ||
| 259 | if (not $self->open('lock')){return 0;} # open in lock-mode | ||
| 260 | $NeedToClose=1; | ||
| 261 | } | ||
| 262 | if (not $self->{LOCKED}){ | ||
| 263 | if (not $self->lock()){return 0;} | ||
| 264 | } | ||
| 265 | my @content=$self->read(); | ||
| 266 | @content=grep($_!~/$pattern/,@content); | ||
| 267 | $self->write(\@content); | ||
| 268 | if ($NeedToClose){$self->close();} | ||
| 269 | return 1; | ||
| 270 | } | ||
| 271 | |||
| 272 | |||
| 273 | sub find{ | ||
| 274 | my $self=shift; | ||
| 275 | my @data=@_; | ||
| 276 | map($_=quotemeta($_),@data); | ||
| 277 | |||
| 278 | my $pattern='^'.join(':',@data).'(\:|\Z)'; | ||
| 279 | my @content=$self->read(); | ||
| 280 | my @match=grep($_=~/$pattern/,@content); | ||
| 281 | chomp($match[0]); | ||
| 282 | return wantarray ? split(/\:/,$match[0]) : $match[0]; | ||
| 283 | } | ||
| 284 | |||
| 285 |