From 077f97d5fc028be5c2ec86afcc9ce1e91583e895 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20BENO=C3=8ET?= Date: Tue, 31 Dec 2024 13:39:53 +0100 Subject: [PATCH] fix: reset cached resources when the file is missing --- scripts/lib/resource.inc | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/scripts/lib/resource.inc b/scripts/lib/resource.inc index 93884c9..ac9cb39 100644 --- a/scripts/lib/resource.inc +++ b/scripts/lib/resource.inc @@ -22,12 +22,12 @@ function addFileResource($type, $path) { } global $resources; - if (!is_array($resources[$type])) { + if (!isset($resources[$type])) { $resources[$type] = array(); } $contents = @file_get_contents($path); - if ($contents === FALSE) { + if ($contents === false) { l::notice("Resource type '$type': unable to read '$path'"); return false; } @@ -42,7 +42,7 @@ function addFileResource($type, $path) { */ function addRawResource($type, $text) { global $resources; - if (!is_array($resources[$type])) { + if (!isset($resources[$type])) { $resources[$type] = array(); } @@ -57,11 +57,14 @@ function addRawResource($type, $text) { */ function generateResourceCache($type, $md5) { if (!is_dir(config::$main['cachedir']) && !@mkdir(config::$main['cachedir'])) { + l::warn("Could not create cache dir " . config::$main['cachedir']); return false; } - $f = @fopen(config::$main['cachedir'] . "/$md5.$type", "w"); + $path = config::$main['cachedir'] . "/$md5.$type"; + $f = @fopen($path, "w"); if (!$f) { + l::warn("Could not create file " . config::$main['cachedir']); return false; } @@ -82,7 +85,7 @@ function generateResourceCache($type, $md5) { */ function storeResource($type, $deleteOld = 0, $write = true) { global $resources; - if (!is_array($resources[$type])) { + if (!isset($resources[$type])) { // l::notice("No resource of type $type"); 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"); if ($q && dbCount($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 @@ -134,14 +142,17 @@ function displayResource($id, $rtype) { 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"); 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); }