From fe02111888dc75b8a789e92bb5f674f2e1426a00 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Tue, 13 Oct 2020 13:57:37 -0400 Subject: [PATCH] [multiple] stat_cache_path_stat() for struct st stat_cache_path_stat() for cached (struct st *) --- src/mod_cgi.c | 13 ++++--------- src/mod_expire.c | 6 +++--- src/mod_indexfile.c | 3 +-- src/mod_rewrite.c | 4 ++-- src/response.c | 18 +++++++++--------- src/stat_cache.c | 5 +++++ src/stat_cache.h | 3 +++ 7 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/mod_cgi.c b/src/mod_cgi.c index 7659281d..3d67b8b8 100644 --- a/src/mod_cgi.c +++ b/src/mod_cgi.c @@ -747,12 +747,6 @@ static int cgi_write_request(handler_ctx *hctx, int fd) { return 0; } -static const struct stat * cgi_stat(buffer *path) { - /* CGI might be executable even if it is not readable */ - const stat_cache_entry * const sce = stat_cache_get_entry(path); - return sce ? &sce->st : NULL; -} - static int cgi_create_env(request_st * const r, plugin_data * const p, handler_ctx * const hctx, buffer * const cgi_handler) { char *args[3]; int to_cgi_fds[2]; @@ -761,7 +755,7 @@ static int cgi_create_env(request_st * const r, plugin_data * const p, handler_c UNUSED(p); if (!buffer_string_is_empty(cgi_handler)) { - if (NULL == cgi_stat(cgi_handler)) { + if (NULL == stat_cache_path_stat(cgi_handler)) { log_perror(r->conf.errh, __FILE__, __LINE__, "stat for cgi-handler %s", cgi_handler->ptr); return -1; @@ -887,7 +881,7 @@ static int cgi_create_env(request_st * const r, plugin_data * const p, handler_c URIHANDLER_FUNC(cgi_is_handled) { plugin_data *p = p_d; - const struct stat *st; + const stat_cache_st *st; data_string *ds; if (NULL != r->handler_module) return HANDLER_GO_ON; @@ -899,9 +893,10 @@ URIHANDLER_FUNC(cgi_is_handled) { ds = (data_string *)array_match_key_suffix(p->conf.cgi, &r->physical.path); if (NULL == ds) return HANDLER_GO_ON; - st = cgi_stat(&r->physical.path); + st = stat_cache_path_stat(&r->physical.path); if (NULL == st) return HANDLER_GO_ON; + /* (aside: CGI might be executable even if it is not readable) */ if (!S_ISREG(st->st_mode)) return HANDLER_GO_ON; if (p->conf.execute_x_only == 1 && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) return HANDLER_GO_ON; diff --git a/src/mod_expire.c b/src/mod_expire.c index 4ada2a85..cef62d9c 100644 --- a/src/mod_expire.c +++ b/src/mod_expire.c @@ -304,10 +304,10 @@ REQUEST_FUNC(mod_expire_handler) { expires += cur_ts; } else { /* modification */ - stat_cache_entry *sce = stat_cache_get_entry(&r->physical.path); + const stat_cache_st * const st = stat_cache_path_stat(&r->physical.path); /* can't set modification-based expire if mtime is not available */ - if (NULL == sce) return HANDLER_GO_ON; - expires += sce->st.st_mtime; + if (NULL == st) return HANDLER_GO_ON; + expires += st->st_mtime; } /* expires should be at least cur_ts */ diff --git a/src/mod_indexfile.c b/src/mod_indexfile.c index 9ae72cb7..e258b6ad 100644 --- a/src/mod_indexfile.c +++ b/src/mod_indexfile.c @@ -113,8 +113,7 @@ URIHANDLER_FUNC(mod_indexfile_subrequest) { } buffer_append_string_buffer(b, &ds->value); - stat_cache_entry * const sce = stat_cache_get_entry(b); - if (NULL == sce) { + if (NULL == stat_cache_path_stat(b)) { if (errno == EACCES) { r->http_status = 403; buffer_reset(&r->physical.path); diff --git a/src/mod_rewrite.c b/src/mod_rewrite.c index a255bd06..4627e5ed 100644 --- a/src/mod_rewrite.c +++ b/src/mod_rewrite.c @@ -323,8 +323,8 @@ URIHANDLER_FUNC(mod_rewrite_physical) { if (!p->conf.rewrite_NF || !p->conf.rewrite_NF->used) return HANDLER_GO_ON; /* skip if physical.path is a regular file */ - stat_cache_entry *sce = stat_cache_get_entry(&r->physical.path); - if (sce && S_ISREG(sce->st.st_mode)) return HANDLER_GO_ON; + const stat_cache_st * const st = stat_cache_path_stat(&r->physical.path); + if (st && S_ISREG(st->st_mode)) return HANDLER_GO_ON; return process_rewrite_rules(r, p, p->conf.rewrite_NF); } diff --git a/src/response.c b/src/response.c index 2b1dc8c9..26ef9ac9 100644 --- a/src/response.c +++ b/src/response.c @@ -174,9 +174,9 @@ http_response_write_header (request_st * const r) static handler_t http_response_physical_path_check(request_st * const r) { - stat_cache_entry *sce = stat_cache_get_entry(&r->physical.path); + const stat_cache_st *st = stat_cache_path_stat(&r->physical.path); - if (sce) { + if (st) { /* file exists */ } else { char *pathinfo = NULL; @@ -246,16 +246,16 @@ static handler_t http_response_physical_path_check(request_st * const r) { buffer * const tb = r->tmp_buf; for (char *pprev = pathinfo; pathinfo; pprev = pathinfo, pathinfo = strchr(pathinfo+1, '/')) { buffer_copy_string_len(tb, r->physical.path.ptr, pathinfo - r->physical.path.ptr); - stat_cache_entry *nsce = stat_cache_get_entry(tb); - if (NULL == nsce) { + const stat_cache_st * const nst = stat_cache_path_stat(tb); + if (NULL == nst) { pathinfo = pathinfo != pprev ? pprev : NULL; break; } - sce = nsce; - if (!S_ISDIR(sce->st.st_mode)) break; + st = nst; + if (!S_ISDIR(st->st_mode)) break; } - if (NULL == pathinfo || !S_ISREG(sce->st.st_mode)) { + if (NULL == pathinfo || !S_ISREG(st->st_mode)) { /* no it really doesn't exists */ r->http_status = 404; @@ -309,10 +309,10 @@ static handler_t http_response_physical_path_check(request_st * const r) { return HANDLER_FINISHED; } - if (S_ISREG(sce->st.st_mode)) /*(common case)*/ + if (S_ISREG(st->st_mode)) /*(common case)*/ return HANDLER_GO_ON; - if (S_ISDIR(sce->st.st_mode)) { + if (S_ISDIR(st->st_mode)) { if (r->uri.path.ptr[buffer_string_length(&r->uri.path) - 1] != '/') { /* redirect to .../ */ diff --git a/src/stat_cache.c b/src/stat_cache.c index b797453e..d30a80f1 100644 --- a/src/stat_cache.c +++ b/src/stat_cache.c @@ -1090,6 +1090,11 @@ stat_cache_entry * stat_cache_get_entry_open(const buffer * const name, const in return sce; /* (note: sce->fd might still be -1 if open() failed) */ } +const stat_cache_st * stat_cache_path_stat (const buffer * const name) { + const stat_cache_entry * const sce = stat_cache_get_entry(name); + return sce ? &sce->st : NULL; +} + int stat_cache_path_isdir(const buffer *name) { const stat_cache_entry * const sce = stat_cache_get_entry(name); return (sce && (S_ISDIR(sce->st.st_mode) ? 1 : (errno = ENOTDIR, 0))); diff --git a/src/stat_cache.h b/src/stat_cache.h index ad593730..6f4bd1bd 100644 --- a/src/stat_cache.h +++ b/src/stat_cache.h @@ -10,6 +10,8 @@ #include #include +typedef struct stat stat_cache_st; + typedef struct { buffer name; time_t stat_ts; @@ -52,6 +54,7 @@ void stat_cache_delete_dir(const char *name, uint32_t len); void stat_cache_invalidate_entry(const char *name, uint32_t len); stat_cache_entry * stat_cache_get_entry(const buffer *name); stat_cache_entry * stat_cache_get_entry_open(const buffer *name, int symlinks); +const stat_cache_st * stat_cache_path_stat(const buffer *name); int stat_cache_path_isdir(const buffer *name); __attribute_cold__