fix: reset cached resources when the file is missing

This commit is contained in:
Emmanuel BENOîT 2024-12-31 13:39:53 +01:00
parent 4772e7cdc4
commit 077f97d5fc
Signed by: Emmanuel BENOîT
SSH key fingerprint: SHA256:l7PFUUF5TCDsvYeQC9OnTNz08dFY7Fvf4Hv3neIqYpg

View file

@ -22,12 +22,12 @@ function addFileResource($type, $path) {
} }
global $resources; global $resources;
if (!is_array($resources[$type])) { if (!isset($resources[$type])) {
$resources[$type] = array(); $resources[$type] = array();
} }
$contents = @file_get_contents($path); $contents = @file_get_contents($path);
if ($contents === FALSE) { if ($contents === false) {
l::notice("Resource type '$type': unable to read '$path'"); l::notice("Resource type '$type': unable to read '$path'");
return false; return false;
} }
@ -42,7 +42,7 @@ function addFileResource($type, $path) {
*/ */
function addRawResource($type, $text) { function addRawResource($type, $text) {
global $resources; global $resources;
if (!is_array($resources[$type])) { if (!isset($resources[$type])) {
$resources[$type] = array(); $resources[$type] = array();
} }
@ -57,11 +57,14 @@ function addRawResource($type, $text) {
*/ */
function generateResourceCache($type, $md5) { function generateResourceCache($type, $md5) {
if (!is_dir(config::$main['cachedir']) && !@mkdir(config::$main['cachedir'])) { if (!is_dir(config::$main['cachedir']) && !@mkdir(config::$main['cachedir'])) {
l::warn("Could not create cache dir " . config::$main['cachedir']);
return false; return false;
} }
$f = @fopen(config::$main['cachedir'] . "/$md5.$type", "w"); $path = config::$main['cachedir'] . "/$md5.$type";
$f = @fopen($path, "w");
if (!$f) { if (!$f) {
l::warn("Could not create file " . config::$main['cachedir']);
return false; return false;
} }
@ -82,7 +85,7 @@ function generateResourceCache($type, $md5) {
*/ */
function storeResource($type, $deleteOld = 0, $write = true) { function storeResource($type, $deleteOld = 0, $write = true) {
global $resources; global $resources;
if (!is_array($resources[$type])) { if (!isset($resources[$type])) {
// l::notice("No resource of type $type"); // l::notice("No resource of type $type");
return null; return null;
} }
@ -103,7 +106,12 @@ function storeResource($type, $deleteOld = 0, $write = true) {
$q = dbQuery("SELECT id FROM web_cache WHERE rtype='$type' AND md5='$md5' FOR SHARE"); $q = dbQuery("SELECT id FROM web_cache WHERE rtype='$type' AND md5='$md5' FOR SHARE");
if ($q && dbCount($q)) { if ($q && dbCount($q)) {
list($id) = dbFetchArray($q); list($id) = dbFetchArray($q);
return $id; $path = config::$main['cachedir'] . "/$md5.$type";
if (!$write || file_exists($path)) {
return $id;
}
l::debug("Resource $id no longer exists on disk ($path), re-generating");
dbQuery("DELETE FROM web_cache WHERE id = $id");
} }
// Generate the output file // Generate the output file
@ -134,14 +142,17 @@ function displayResource($id, $rtype) {
return false; return false;
} }
$path = config::$main['cachedir'] . "/$md5.$rtype";
if (readfile($path) === false) {
dbQuery("DELETE FROM web_cache WHERE id=$id");
l::warn("File not found for resource '$id': $path");
endRequest(false);
return false;
}
dbQuery("UPDATE web_cache SET last_used=" . time() . " WHERE id=$id"); dbQuery("UPDATE web_cache SET last_used=" . time() . " WHERE id=$id");
endRequest(false); endRequest(false);
$path = config::$main['cachedir'] . "/$md5.$rtype";
if (readfile($path) === FALSE) {
l::warn("File not found for resource '$id': $path");
return false;
}
exit(0); exit(0);
} }