From f2c7224424392515867ee77932f8dfd7ea8ceffa Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 31 Oct 2026 14:31:15 -0700 Subject: Keep the cache's trailing slash helper in cache.c --- source/cache.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'source/cache.c') diff --git a/source/cache.c b/source/cache.c index 004a84d..6317a12 100644 --- a/source/cache.c +++ b/source/cache.c @@ -12,7 +12,6 @@ #include "cache.h" #include "cgit.h" #include "html.h" -#include "shared.h" #include #ifdef HAVE_LINUX_SENDFILE #include @@ -493,6 +492,16 @@ unsigned long cache_hash_str(const char *str) return h; } +/* + * Unlike git's strbuf_complete, an empty path gains the slash too, so an empty + * cache-root keeps its slots under / rather than in the working directory. + */ +static void end_with_slash(struct strbuf *path) +{ + if (!path->len || path->buf[path->len - 1] != '/') + strbuf_addch(path, '/'); +} + int cache_process(int size, const char *path, const char *key, int ttl, size_t max_bytes, cache_fill_fn fn) { @@ -515,7 +524,7 @@ int cache_process(int size, const char *path, const char *key, int ttl, size_t m } hash = cache_hash_str(key) % size; strbuf_addstr(&slot_path, path); - strbuf_ensure_end(&slot_path, '/'); + end_with_slash(&slot_path); for (i = 0; i < SLOT_NAME_LEN; i++) { strbuf_addf(&slot_path, "%x", (unsigned char)(hash & 0xf)); hash >>= 4; @@ -559,7 +568,7 @@ void cache_ls(const char *path) return; } strbuf_addstr(&slot_path, path); - strbuf_ensure_end(&slot_path, '/'); + end_with_slash(&slot_path); prefixlen = slot_path.len; while ((ent = readdir(dir))) { char mtime[64] = "-"; -- cgit