From cab58101cf8f8d31fadcfe21f5bcdd64c27faecc Mon Sep 17 00:00:00 2001 From: mlugg Date: Sun, 29 Jun 2025 16:37:03 +0100 Subject: [PATCH] post: simplify and rename `totalSize` ...and add some more logging. Related to #45. --- post.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/post.js b/post.js index 563a17b..572809a 100644 --- a/post.js +++ b/post.js @@ -19,7 +19,7 @@ async function main() { if (accessible) { core.info('Checking cache size'); - const size = await totalSize(cache_path); + const size = await dirSize(cache_path); const size_limit = core.getInput('cache-size-limit') * 1024 * 1024; // MiB -> bytes if (size_limit !== 0 && size > size_limit) { core.info(`Cache directory reached ${size} bytes, exceeding limit of ${size_limit} bytes; clearing cache`); @@ -35,6 +35,8 @@ async function main() { const name = prefix + github.context.runId; core.info('Saving Zig cache'); await cache.saveCache([cache_path], name); + } else { + core.info('Zig cache directory is inaccessible; nothing to save'); } } } catch (err) { @@ -42,18 +44,18 @@ async function main() { } } -async function totalSize(p) { +async function dirSize(dir_path) { try { - const stat = await fs.stat(p); - if (stat.isFile()) return stat.size; - if (stat.isDirectory()) { - let total = 0; - for (const entry of await fs.readdir(p)) { - total += await totalSize(path.join(p, entry)); + let total = 0; + for (const ent of await fs.readdir(dir_path, { withFileTypes: true, recursive: true })) { + if (ent.isFile()) { + try { + const stat = await fs.stat(path.join(ent.parentPath, ent.name)); + total += stat.size; + } catch {} } - return total; } - return 0; + return total; } catch { return 0; }