Diff options
authorBryce Kwon <bryce@brycekwon.com>
committerBryce Kwon <bryce@brycekwon.com>
commit
parent
tree
download
Keep the cache's trailing slash helper in cache.c
Diffstat (limited to 'source/cache.c')
-rw-r--r--source/cache.c15
1 file changed, 12 insertions, 3 deletions
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 <sys/file.h>
#ifdef HAVE_LINUX_SENDFILE
#include <sys/sendfile.h>
@@ -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] = "-";