| 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 | # dump perl-hashs to config-files | ||
| 19 | # | ||
| 20 | |||
| 21 | package Uplug::Web::Config; | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | use Data::Dumper; | ||
| 25 | use Uplug::Web::Process::Lock; | ||
| 26 | |||
| 27 | our $DEFAULTMAXFLOCKWAIT=5; | ||
| 28 | |||
| 29 | sub DESTROY{ | ||
| 30 | $_[0]->close(); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub new{ | ||
| 34 | my $class=shift; | ||
| 35 | my $self={}; | ||
| 36 | bless $self,$class; | ||
| 37 | $self->setFile($_[0]); | ||
| 38 | $self->setFlockWait($_[1]); | ||
| 39 | return $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | sub setFile{ | ||
| 45 | my $self=shift; | ||
| 46 | my $file=shift; | ||
| 47 | if (not -e $file){ | ||
| 48 | open F,">$file"; | ||
| 49 | close F; | ||
| 50 | system "chmod g+w $file"; | ||
| 51 | } | ||
| 52 | $self->{FILE}=$file; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub getFile{ | ||
| 56 | my $self=shift; | ||
| 57 | return $self->{FILE}; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub setFlockWait{ | ||
| 61 | my $self=shift; | ||
| 62 | my $wait=shift; | ||
| 63 | if ($wait){$self->{MAXFLOCKWAIT}=$wait;} | ||
| 64 | else{$self->{MAXFLOCKWAIT}=$DEFAULTMAXFLOCKWAIT;} | ||
| 65 | } | ||
| 66 | |||
| 67 | sub open{ | ||
| 68 | my $self=shift; | ||
| 69 | |||
| 70 | my $fh=$self->{FH}; | ||
| 71 | if (not -e $self->{FILE}){return 0;} | ||
| 72 | |||
| 73 | ##------------------------------------------------------ | ||
| 74 | ## file locking with flock | ||
| 75 | ## | ||
| 76 | # open $self->{FH},"+<$self->{FILE}"; | ||
| 77 | # my $sec=0; | ||
| 78 | # while (not flock($self->{FH},2)){ | ||
| 79 | # $sec++;sleep(1); | ||
| 80 | # if ($sec>$self->{MAXFLOCKWAIT}){ | ||
| 81 | # close $self->{FH}; | ||
| 82 | # return 0; | ||
| 83 | # } | ||
| 84 | # } | ||
| 85 | ##------------------------------------------------------ | ||
| 86 | ## | ||
| 87 | ## file locking with nflock in Uplug::Web::Process::Lock | ||
| 88 | ## | ||
| 89 | |||
| 90 | if (not &nflock($self->{FILE},$self->{MAXFLOCKWAIT})){ | ||
| 91 | print STDERR "# Uplug::Web::Config - can't get exclusive lock for $self->{FILE}!\n"; | ||
| 92 | return 0; | ||
| 93 | } | ||
| 94 | open $self->{FH},"+<$self->{FILE}"; | ||
| 95 | |||
| 96 | ##------------------------------------------------------ | ||
| 97 | |||
| 98 | $self->{STATUS}='open'; | ||
| 99 | return 1; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub close{ | ||
| 103 | my $self=shift; | ||
| 104 | |||
| 105 | if ($self->{STATUS} eq 'open'){ | ||
| 106 | my $fh=$self->{FH}; | ||
| 107 | truncate($fh,tell($fh)); | ||
| 108 | close $fh; | ||
| 109 | ## | ||
| 110 | ## unlocking the file if nflock was used! | ||
| 111 | ## | ||
| 112 | &nunflock($self->{FILE}); | ||
| 113 | $self->{STATUS}='closed'; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | sub read{ | ||
| 118 | my $self=shift; | ||
| 119 | my $config; | ||
| 120 | |||
| 121 | if ($self->{STATUS} ne 'open'){ | ||
| 122 | if (not $self->open()){return undef;} | ||
| 123 | } | ||
| 124 | if ($self->{STATUS} eq 'open'){ | ||
| 125 | my $fh=$self->{FH}; | ||
| 126 | my @content=<$fh>; | ||
| 127 | $config=eval join ('',@content); | ||
| 128 | } | ||
| 129 | if (ref($config) ne 'HASH'){$config={};} | ||
| 130 | return $config; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub write{ | ||
| 134 | my $self=shift; | ||
| 135 | my $content=shift; | ||
| 136 | |||
| 137 | if (ref($content) ne 'HASH'){return 0;} | ||
| 138 | if ($self->{STATUS} eq 'open'){ | ||
| 139 | my $fh=$self->{FH}; | ||
| 140 | seek ($fh,0,0); | ||
| 141 | print $fh Dumper($content); | ||
| 142 | truncate($fh,tell($fh)); | ||
| 143 | return 1; | ||
| 144 | } | ||
| 145 | return 0; | ||
| 146 | } | ||
| 147 |