Add thread cache file locking in thread.inc.php

This commit is contained in:
Retro_Guy 2021-01-09 01:31:51 -07:00
parent 4d3645b33c
commit ad4168ea7b
1 changed files with 11 additions and 1 deletions

View File

@ -72,11 +72,15 @@ function thread_cache_load($group) {
if (!file_exists($filename)) return false; if (!file_exists($filename)) return false;
if ($compress_spoolfiles) { if ($compress_spoolfiles) {
$file=gzopen("$spooldir/$group-data.dat","r"); $file=gzopen("$spooldir/$group-data.dat","r");
flock($file, LOCK_SH);
$headers=unserialize(gzread($file,1000000)); $headers=unserialize(gzread($file,1000000));
flock($file, LOCK_UN);
gzclose($file); gzclose($file);
} else { } else {
$file=fopen($filename,"r"); $file=fopen($filename,"r");
flock($file, LOCK_SH);
$headers=unserialize(fread($file,filesize($filename))); $headers=unserialize(fread($file,filesize($filename)));
flock($file, LOCK_UN);
fclose($file); fclose($file);
} }
return($headers); return($headers);
@ -90,10 +94,13 @@ function thread_cache_load($group) {
* $group: name of the newsgroup, is needed to create the filename * $group: name of the newsgroup, is needed to create the filename
*/ */
function thread_cache_save($headers,$group) { function thread_cache_save($headers,$group) {
global $spooldir,$compress_spoolfiles; global $spooldir,$compress_spoolfiles,$logdir,$config_name;
$logfile=$logdir.'/newsportal.log';
if ($compress_spoolfiles) { if ($compress_spoolfiles) {
$file=gzopen("$spooldir/$group-data.dat","w"); $file=gzopen("$spooldir/$group-data.dat","w");
$islock = flock($file, LOCK_EX);
gzputs($file,serialize($headers)); gzputs($file,serialize($headers));
flock($file, LOCK_UN);
gzclose($file); gzclose($file);
} else { } else {
$file=fopen("$spooldir/$group-data.dat","w"); $file=fopen("$spooldir/$group-data.dat","w");
@ -101,8 +108,11 @@ function thread_cache_save($headers,$group) {
die('The spool-directory is not writeable. Please change the user '. die('The spool-directory is not writeable. Please change the user '.
'permissions to give the webserver write-access to it.'); 'permissions to give the webserver write-access to it.');
} }
$islock = flock($file, LOCK_EX);
fputs($file,serialize($headers)); fputs($file,serialize($headers));
flock($file, LOCK_UN);
fclose($file); fclose($file);
file_put_contents($logfile, "\n".format_log_date()." ".$config_name." Locking status: ".$islock." for ".$group, FILE_APPEND);
} }
} }