X-Git-Url: https://git.p6c8.net/selfforum.git/blobdiff_plain/cee4397796b2a1015b88addca2de54fe50dbc3f8..7cfc9e17caf89a9b009e8fa2c65351255687706e:/selfforum-cgi/shared/Posting/Cache.pm?ds=inline diff --git a/selfforum-cgi/shared/Posting/Cache.pm b/selfforum-cgi/shared/Posting/Cache.pm index d937b9d..5e27d0b 100644 --- a/selfforum-cgi/shared/Posting/Cache.pm +++ b/selfforum-cgi/shared/Posting/Cache.pm @@ -4,19 +4,31 @@ package Posting::Cache; # # # File: shared/Posting/Cache.pm # # # -# Authors: André Malo , 2001-04-21 # +# Authors: André Malo # # # # Description: Views/Voting Cache class # # # ################################################################################ use strict; - use Fcntl; use File::Path; -use Lock qw(:ALL); +use Lock; -my $O_BINARY = eval "O_BINARY"; +################################################################################ +# +# Version check +# +# last modified: +# $Date$ (GMT) +# by $Author$ +# +sub VERSION {(q$Revision$ =~ /([\d.]+)\s*$/)[0] or '0.0'} + +my $O_BINARY = eval q{ + local $SIG{__DIE__}; + O_BINARY; +}; $O_BINARY = 0 if ($@); ### sub new #################################################################### @@ -148,6 +160,8 @@ sub r_garbage_collection { local $/; local $\; + return; # no GC yet + seek $handle, 0, 0 or return; read ($handle, $buf, $len) or return; for (0..$num) { @@ -220,6 +234,8 @@ sub r_add_view { read ($handle, $buf, $reclen) == $reclen or return; my ($posting, $thread, $views, $votings) = unpack 'L4' => $buf; + $thread == 0xFFFFFFFF and $thread = $param->{thread}; + $param->{thread} == $thread or return; $param->{posting} == $posting or return; @@ -262,6 +278,8 @@ sub r_pick { read ($handle, $buf, $reclen) == $reclen or return; my ($posting, $thread, $views, $votings) = unpack 'L4' => $buf; + $thread == 0xFFFFFFFF and $thread = $param->{thread}; + $param->{thread} == $thread or return; $param->{posting} == $posting or return; @@ -279,7 +297,7 @@ sub r_pick { time => $_->[0] || 0, IP => $_->[1] || 0 } - } [split ' '] + } [split ' ' => $_,3] } @records } }; @@ -344,7 +362,6 @@ sub add_voting { $self -> vote_wrap ( \&r_add_voting, - $self->cachefile($param), $param ); } @@ -359,6 +376,8 @@ sub r_add_voting { read ($handle, $buf, $reclen) == $reclen or return; my ($posting, $thread, $views, $votings) = unpack 'L4' => $buf; + $thread == 0xFFFFFFFF and $thread = $param->{thread}; + $param->{thread} == $thread or return; { @@ -396,18 +415,14 @@ sub add_posting { } sub r_add_posting { my ($self, $handle, $param) = @_; - local *FILE; + my $newfile = new Lock ($self->cachefile($param)); local $\; unless (-d $self -> threaddir($param)) { - mkdir $self->threaddir($param) or return; + mkdir $self->threaddir($param), 0777 or return; } - sysopen ( - FILE, - $self->cachefile($param), - O_WRONLY | O_CREAT | O_TRUNC - ) or return; - close FILE or return; + $newfile -> open (O_WRONLY | O_CREAT | O_TRUNC) or return; + $newfile -> close or return; my $z; if (-s $handle) { @@ -420,7 +435,7 @@ sub r_add_posting { while (++$z < $param->{posting}) { seek $handle, 0, 2 or return; print $handle pack( - 'L4' => $z, 0, 0, 0 + 'L4' => $z, 0xFFFFFFFF, 0, 0 ) or return; } $z = undef; @@ -442,7 +457,7 @@ sub r_add_posting { 'L4' => $param->{posting}, $param->{thread}, 0, 0 ) or return; - release_file ($self->cachefile($param)); + $newfile -> release; 1; } @@ -460,29 +475,28 @@ sub r_add_posting { sub add_wrap { my ($self, $gosub, @param) = @_; my $status; + my $summary = new Lock ($self -> summaryfile); - unless (write_lock_file ($self->summaryfile)) { - violent_unlock_file ($self->summaryfile); - $self->set_error ('could not write-lock summary file '.$self->summaryfile); + unless ($summary -> lock (LH_EXCL)) { + $self->set_error ('could not write-lock summary file '.$summary -> filename); } else { - local *SUM; - unless (sysopen (SUM, $self->summaryfile, $O_BINARY | O_APPEND | O_CREAT | O_RDWR)) { + unless ($summary -> open($O_BINARY | O_APPEND | O_CREAT | O_RDWR)) { $self->set_error - ('could not open to read/write/append summary file '.$self->summaryfile); + ('could not open to read/write/append summary file '.$summary->filename); } else { $status = $gosub -> ( $self, - \*SUM, + $summary, @param ); - unless (close SUM) { + unless ($summary -> close) { $status=0; - $self->set_error('could not close summary file '.$self->summaryfile); + $self->set_error('could not close summary file '.$summary -> filename); } } - violent_unlock_file ($self->summaryfile) unless (write_unlock_file ($self->summaryfile)); + $summary -> unlock; } # return @@ -500,30 +514,50 @@ sub add_wrap { # Return: Status code (Bool) # sub vote_wrap { - my ($self, $gosub, $filename, @param) = @_; + my ($self, $gosub, $param) = @_; my $status; + my $summary = new Lock ($self -> summaryfile); - unless (write_lock_file ($filename)) { - violent_unlock_file ($filename); - $self->set_error ('could not write-lock cache file '.$filename); + unless ($summary -> lock (LH_EXCL)) { + $self->set_error ('could not write-lock summary file '.$summary -> filename); } else { - local *CACHE; - unless (sysopen (CACHE, $filename, O_APPEND | O_CREAT | O_RDWR)) { - $self->set_error ('could not open to read/write/append cache file '.$filename); + unless ($summary -> open (O_RDWR | $O_BINARY)) { + $self->set_error ('could not open to read/write summary file '.$summary -> filename); } else { - $status = $self -> mod_wrap ( - $gosub, - \*CACHE, - @param - ); - unless (close CACHE) { + unless (-d $self->threaddir($param)) { + mkdir $self->threaddir($param), 0777 or return; + } + my $cache = new Lock ($self->cachefile($param)); + + unless ($cache -> lock (LH_EXCL)) { + $self->set_error ('could not write-lock cache file '.$cache -> filename); + } + else { + unless ($cache -> open (O_APPEND | O_CREAT | O_RDWR)) { + $self->set_error ('could not open to read/write/append cache file '.$cache -> filename); + } + else { + $status = $gosub -> ( + $self, + $summary, + $cache, + $param + ); + unless ($cache -> close) { + $status=0; + $self->set_error('could not close cache file '.$cache -> filename); + } + } + $cache -> unlock; + } + unless ($summary -> close) { $status=0; - $self->set_error('could not close cache file '.$filename); + $self->set_error('could not close summary file '.$summary -> filename); } } - violent_unlock_file ($filename) unless (write_unlock_file ($filename)); + $summary -> unlock; } # return @@ -543,46 +577,48 @@ sub vote_wrap { sub purge_wrap { my ($self, $gosub, @param) = @_; my $status; - my $filename = $self -> summaryfile . '.temp'; + my $summary = new Lock ($self -> summaryfile); - unless (write_lock_file ($self->summaryfile)) { - violent_unlock_file ($self->summaryfile); - $self->set_error ('could not write-lock summary file '.$self->summaryfile); + unless ($summary -> lock (LH_EXSH)) { + $self->set_error ('could not write-lock summary file '.$summary -> filename); } else { - local *TEMP; - unless (sysopen (TEMP, $filename, O_CREAT | O_WRONLY | O_TRUNC | $O_BINARY)) { - $self->set_error ('could not open to write temp summary file '.$filename); + my $temp = new Lock::Handle ($summary -> filename . '.temp'); + unless ($temp -> open (O_CREAT | O_WRONLY | O_TRUNC | $O_BINARY)) { + $self->set_error ('could not open to write temp summary file '.$temp -> filename); } else { - local *S; - unless (sysopen (S, $self->summaryfile, O_RDONLY | $O_BINARY)) { - $self->set_error ('could not open to read summary file '.$self->summaryfile); + unless ($summary -> open (O_RDONLY | $O_BINARY)) { + $self->set_error ('could not open to read summary file '.$summary -> filename); } else { $status = $gosub -> ( $self, - \*S, - \*TEMP, + $summary, + $temp, @param ); - unless (close S) { + unless ($summary -> close) { $status = 0; - $self->set_error('could not close summary file '.$self->summaryfile); + $self->set_error('could not close summary file '.$summary -> filename); } } - unless (close TEMP) { + unless ($temp -> close) { + $status=0; + $self->set_error('could not close temp summary file '.$temp -> filename); + } + unless ($summary -> lock (LH_EXCL)) { $status=0; - $self->set_error('could not close temp summary file '.$filename); + $self->set_error ('could not write-lock summary file '.$summary -> filename); } if ($status) { - unless (rename $filename => $self->summaryfile) { + unless (rename $temp -> filename => $summary -> filename) { $status=0; - $self->set_error('could not rename temp summary file '.$filename); + $self->set_error('could not rename temp summary file '.$temp -> filename); } } } - violent_unlock_file ($self->summaryfile) unless (write_unlock_file ($self->summaryfile)); + $summary -> unlock; } # return @@ -602,28 +638,27 @@ sub purge_wrap { sub pick_wrap { my ($self, $gosub, $filename, @param) = @_; my $status; + my $cache = new Lock ($filename); - unless (lock_file ($filename)) { - violent_unlock_file ($filename); - $self->set_error ('could not lock cache file '.$filename); + unless ($cache -> lock (LH_SHARED)) { + $self->set_error ('could not lock cache file '.$cache -> filename); } else { - local *CACHE; - unless (sysopen (CACHE, $filename, O_RDONLY)) { - $self->set_error ('could not open to read cache file '.$filename); + unless ($cache -> open (O_RDONLY)) { + $self->set_error ('could not open to read cache file '.$cache -> filename); } else { $status = $self -> read_wrap ( $gosub, - \*CACHE, + $cache, @param ); - unless (close CACHE) { + unless ($cache -> close) { $status=0; - $self->set_error('could not close cache file '.$filename); + $self->set_error('could not close cache file '.$cache -> filename); } } - violent_unlock_file ($filename) unless (unlock_file ($filename)); + $cache -> unlock; } # return @@ -643,28 +678,27 @@ sub pick_wrap { sub read_wrap { my ($self, $gosub, @param) = @_; my $status; + my $summary = new Lock ($self -> summaryfile); - unless (lock_file ($self->summaryfile)) { - violent_unlock_file ($self->summaryfile); - $self->set_error ('could not read-lock summary file '.$self->summaryfile); + unless ($summary -> lock (LH_SHARED)) { + $self->set_error ('could not read-lock summary file '.$summary -> filename); } else { - local *S; - unless (sysopen (S, $self->summaryfile, O_RDONLY | $O_BINARY)) { - $self->set_error ('could not open to read summary file '.$self->summaryfile); + unless ($summary -> open (O_RDONLY | $O_BINARY)) { + $self->set_error ('could not open to read summary file '.$summary -> filename); } else { $status = $gosub -> ( $self, - \*S, + $summary, @param ); - unless (close S) { + unless ($summary -> close) { $status=0; - $self->set_error('could not close summary file '.$self->summaryfile); + $self->set_error('could not close summary file '.$summary -> filename); } } - violent_unlock_file ($self->summaryfile) unless (unlock_file ($self->summaryfile)); + $summary -> unlock; } # return @@ -684,28 +718,27 @@ sub read_wrap { sub mod_wrap { my ($self, $gosub, @param) = @_; my $status; + my $summary = new Lock ($self -> summaryfile); - unless (write_lock_file ($self->summaryfile)) { - violent_unlock_file ($self->summaryfile); - $self->set_error ('could not write-lock summary file '.$self->summaryfile); + unless ($summary -> lock (LH_EXCL)) { + $self->set_error ('could not write-lock summary file '.$summary -> filename); } else { - local *S; - unless (sysopen (S, $self->summaryfile, O_RDWR | $O_BINARY)) { - $self->set_error ('could not open to read/write summary file '.$self->summaryfile); + unless ($summary -> open (O_RDWR | $O_BINARY)) { + $self->set_error ('could not open to read/write summary file '.$summary -> filename); } else { $status = $gosub -> ( $self, - \*S, + $summary, @param ); - unless (close S) { + unless ($summary -> close) { $status=0; - $self->set_error('could not close summary file '.$self->summaryfile); + $self->set_error('could not close summary file '.$summary -> filename); } } - violent_unlock_file ($self->summaryfile) unless (write_unlock_file ($self->summaryfile)); + $summary -> unlock; } # return