From 0daa60a6cfdb1c147876167176195fefa07f8d63 Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 31 Oct 2026 13:24:55 -0700 Subject: Close the cache's descriptors through one helper --- source/cache.c | 48 +++++++++++++++--------------------------------- 1 file changed, 15 insertions(+), 33 deletions(-) (limited to 'source') diff --git a/source/cache.c b/source/cache.c index 7b4e0dd..5bedb87 100644 --- a/source/cache.c +++ b/source/cache.c @@ -97,17 +97,12 @@ static int key_fits_slot(const char *key) return strlen(key) + 1 <= CACHE_BUFSIZE; } -static int close_slot(struct cache_slot *slot) +static void close_fd(int *fd) { - int err = 0; - - if (slot->cache_fd > 0) { - if (close(slot->cache_fd)) - err = errno; - else - slot->cache_fd = -1; + if (*fd > 0) { + close(*fd); + *fd = -1; } - return err; } static int print_slot(struct cache_slot *slot) @@ -197,19 +192,6 @@ static int is_modified(struct cache_slot *slot) ); } -static int close_lock(struct cache_slot *slot) -{ - int err = 0; - - if (slot->lock_fd > 0) { - if (close(slot->lock_fd)) - err = errno; - else - slot->lock_fd = -1; - } - return err; -} - /* * The lock file becomes the slot once it is renamed, so it has to open with * the key the same way a slot does. The lock is taken without blocking, @@ -390,13 +372,13 @@ static void refresh_slot(struct cache_slot *slot) // lock_slot, the copy already open is served rather than the newer. if (is_modified(slot) || fill_slot(slot)) { unlock_slot(slot, 0); - close_lock(slot); + close_fd(&slot->lock_fd); } else if (slot->abandoned) { // The abandoned fill answered the visitor itself and removed // the lock file, so only the descriptor is left to clean up. - close_lock(slot); + close_fd(&slot->lock_fd); } else { - close_slot(slot); + close_fd(&slot->cache_fd); if (slot->oversized) { // The expired copy goes too, or every later request // would refill it and drop the result again. @@ -421,11 +403,11 @@ static int process_slot(struct cache_slot *slot) // visitor, and serving the stale copy still open would append // a second page to that answer. if (slot->abandoned) { - close_slot(slot); + close_fd(&slot->cache_fd); return 0; } err = serve_slot(slot); - close_slot(slot); + close_fd(&slot->cache_fd); return err; } @@ -439,7 +421,7 @@ static int process_slot(struct cache_slot *slot) // If any part of creating a slot fails the page is still rendered // straight to the client and the caller is told the request succeeded, // because it did. - close_slot(slot); + close_fd(&slot->cache_fd); if ((err = lock_slot(slot)) != 0) { fprintf(stderr, "[cgit] Unable to lock slot %s: %s (%d)\n", slot->lock_path, strerror(err), err); slot->fn(); @@ -449,7 +431,7 @@ static int process_slot(struct cache_slot *slot) if ((err = fill_slot(slot)) != 0) { fprintf(stderr, "[cgit] Unable to fill slot %s: %s (%d)\n", slot->lock_path, strerror(err), err); unlock_slot(slot, 0); - close_lock(slot); + close_fd(&slot->lock_fd); // Rendering again is only right when nothing was delivered, // and an abandoned fill has already sent the error page. if (!slot->abandoned) @@ -458,7 +440,7 @@ static int process_slot(struct cache_slot *slot) } if (slot->abandoned) { - close_lock(slot); + close_fd(&slot->lock_fd); return 0; } @@ -471,7 +453,7 @@ static int process_slot(struct cache_slot *slot) else publish_slot(slot); err = serve_slot(slot); - close_slot(slot); + close_fd(&slot->cache_fd); return err; } @@ -582,7 +564,7 @@ int cache_ls(const char *path) slot.path = slot_path.buf; if ((err = open_slot(&slot)) != 0) { fprintf(stderr, "[cgit] Unable to open %s: %s (%d)\n", slot_path.buf, strerror(err), err); - close_slot(&slot); + close_fd(&slot.cache_fd); continue; } // A truncated or corrupt slot may hold no NUL, so the print is @@ -594,7 +576,7 @@ int cache_ls(const char *path) format_time("%Y-%m-%d %H:%M:%S", slot.st.st_mtime), (uintmax_t)slot.st.st_size, keylen, slot.buf ); - close_slot(&slot); + close_fd(&slot.cache_fd); } closedir(dir); strbuf_release(&slot_path); -- cgit