commit df2d3cd2545e0a1579ce83ae137e52135755ed1f from: Omar Polo date: Sat Mar 11 09:53:58 2023 UTC gotwebd: provide gotweb_render_page() entrypoint for all pages simplify gotweb_process_request more, handling all the pages inside the big switch. There's only one entrypoint for rendering the templates gotweb_render_page() that takes the page' content as argument. The only real difference is that gotweb_render_index() now skips directory entries which fails to handle. ok tracey@ commit - d48a8086b67edd7493bbf2476bf5f75eaf47ba2c commit + df2d3cd2545e0a1579ce83ae137e52135755ed1f blob - ec88e1ef5acdb77906f2588283329e2a1f4eda21 blob + d6d66367bedde97c816faa38db5c2fc63e5cb9bb --- gotwebd/config.c +++ gotwebd/config.c @@ -37,6 +37,7 @@ #include #include "got_opentemp.h" +#include "got_reference.h" #include "proc.h" #include "gotwebd.h" blob - ba1803b45941d2c4b83b31f232b8e7edfb92df63 blob + 7ef1dc685a64c90fa69ff126536248ae78ed21dc --- gotwebd/fcgi.c +++ gotwebd/fcgi.c @@ -33,6 +33,7 @@ #include #include "got_error.h" +#include "got_reference.h" #include "proc.h" #include "gotwebd.h" blob - 52aa037e4a8d734d17311aabbd7586d7e23d8fcd blob + ca03025fe2d5b211ca540d4ee3b9410d8c0ea611 --- gotwebd/gotweb.c +++ gotwebd/gotweb.c @@ -85,7 +85,7 @@ static const struct got_error *gotweb_parse_querystrin char *); static const struct got_error *gotweb_assign_querystring(struct querystring **, char *, char *); -static const struct got_error *gotweb_render_index(struct request *); +static int gotweb_render_index(struct template *); static const struct got_error *gotweb_init_repo_dir(struct repo_dir **, const char *); static const struct got_error *gotweb_load_got_path(struct request *c, @@ -144,17 +144,12 @@ void gotweb_process_request(struct request *c) { const struct got_error *error = NULL, *error2 = NULL; - struct got_blob_object *blob = NULL; struct server *srv = NULL; struct querystring *qs = NULL; struct repo_dir *repo_dir = NULL; - struct got_reflist_head refs; - FILE *fp = NULL; uint8_t err[] = "gotwebd experienced an error: "; - int r, html = 0, fd = -1; + int r, html = 0; - TAILQ_INIT(&refs); - /* init the transport */ error = gotweb_init_transport(&c->t); if (error) { @@ -217,7 +212,8 @@ gotweb_process_request(struct request *c) if (error) goto done; - error2 = got_open_blob_for_output(&blob, &fd, &binary, c); + error2 = got_open_blob_for_output(&c->t->blob, &c->t->fd, + &binary, c); if (error2) goto render; @@ -230,12 +226,12 @@ gotweb_process_request(struct request *c) goto done; for (;;) { - error = got_object_blob_read_block(&len, blob); + error = got_object_blob_read_block(&len, c->t->blob); if (error) goto done; if (len == 0) break; - buf = got_object_blob_get_read_buf(blob); + buf = got_object_blob_get_read_buf(c->t->blob); if (fcgi_gen_binary_response(c, buf, len) == -1) goto done; } @@ -259,7 +255,8 @@ gotweb_process_request(struct request *c) if (error) goto done; - error2 = got_open_blob_for_output(&blob, &fd, &binary, c); + error2 = got_open_blob_for_output(&c->t->blob, &c->t->fd, + &binary, c); if (error2) goto render; if (binary) { @@ -288,9 +285,6 @@ render: if (gotweb_reply(c, 200, "text/html", NULL) == -1) goto done; html = 1; - - if (gotweb_render_header(c->tp) == -1) - goto err; if (error2) { error = error2; @@ -304,15 +298,15 @@ render: log_warnx("%s: %s", __func__, error->msg); goto err; } - if (gotweb_render_blame(c->tp) == -1) + if (gotweb_render_page(c->tp, gotweb_render_blame) == -1) goto done; break; case BLOB: - if (gotweb_render_blob(c->tp, blob) == -1) + if (gotweb_render_page(c->tp, gotweb_render_blob) == -1) goto err; break; case BRIEFS: - if (gotweb_render_briefs(c->tp) == -1) + if (gotweb_render_page(c->tp, gotweb_render_briefs) == -1) goto err; break; case COMMITS: @@ -321,7 +315,7 @@ render: log_warnx("%s: %s", __func__, error->msg); goto err; } - if (gotweb_render_commits(c->tp) == -1) + if (gotweb_render_page(c->tp, gotweb_render_commits) == -1) goto err; break; case DIFF: @@ -330,23 +324,28 @@ render: log_warnx("%s: %s", __func__, error->msg); goto err; } - error = got_open_diff_for_output(&fp, &fd, c); + error = got_open_diff_for_output(&c->t->fp, &c->t->fd, c); if (error) { log_warnx("%s: %s", __func__, error->msg); goto err; } - if (gotweb_render_diff(c->tp, fp) == -1) + if (gotweb_render_page(c->tp, gotweb_render_diff) == -1) goto err; break; case INDEX: - error = gotweb_render_index(c); - if (error) { - log_warnx("%s: %s", __func__, error->msg); + c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL, + alphasort); + if (c->t->nrepos == -1) { + c->t->repos = NULL; + error = got_error_from_errno2("scandir", + srv->repos_path); goto err; } + if (gotweb_render_page(c->tp, gotweb_render_index) == -1) + goto err; break; case SUMMARY: - error = got_ref_list(&refs, c->t->repo, "refs/heads", + error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads", got_ref_cmp_by_name, NULL); if (error) { log_warnx("%s: got_ref_list: %s", __func__, @@ -361,7 +360,7 @@ render: goto err; } qs->action = SUMMARY; - if (gotweb_render_summary(c->tp, &refs) == -1) + if (gotweb_render_page(c->tp, gotweb_render_summary) == -1) goto done; break; case TAG: @@ -375,7 +374,7 @@ render: "bad commit id"); goto err; } - if (gotweb_render_tag(c->tp) == -1) + if (gotweb_render_page(c->tp, gotweb_render_tag) == -1) goto done; break; case TAGS: @@ -384,7 +383,7 @@ render: log_warnx("%s: %s", __func__, error->msg); goto err; } - if (gotweb_render_tags(c->tp) == -1) + if (gotweb_render_page(c->tp, gotweb_render_tags) == -1) goto done; break; case TREE: @@ -393,7 +392,7 @@ render: log_warnx("%s: %s", __func__, error->msg); goto err; } - if (gotweb_render_tree(c->tp) == -1) + if (gotweb_render_page(c->tp, gotweb_render_tree) == -1) goto err; break; case ERR: @@ -421,21 +420,7 @@ err: if (html && fcgi_printf(c, "\n") == -1) return; done: - if (blob) - got_object_blob_close(blob); - if (fp) { - error = got_gotweb_flushfile(fp, fd); - if (error) - log_warnx("%s: got_gotweb_flushfile failure: %s", - __func__, error->msg); - fd = -1; - } - if (fd != -1) - close(fd); - if (html && srv != NULL) - gotweb_render_footer(c->tp); - - got_ref_list_free(&refs); + return; } struct server * @@ -474,6 +459,7 @@ gotweb_init_transport(struct transport **t) TAILQ_INIT(&(*t)->repo_commits); TAILQ_INIT(&(*t)->repo_tags); + TAILQ_INIT(&(*t)->refs); (*t)->repo = NULL; (*t)->repo_dir = NULL; @@ -482,6 +468,8 @@ gotweb_init_transport(struct transport **t) (*t)->prev_id = NULL; (*t)->next_disp = 0; (*t)->prev_disp = 0; + + (*t)->fd = -1; return error; } @@ -770,9 +758,12 @@ gotweb_free_repo_dir(struct repo_dir *repo_dir) void gotweb_free_transport(struct transport *t) { + const struct got_error *err; struct repo_commit *rc = NULL, *trc = NULL; struct repo_tag *rt = NULL, *trt = NULL; + int i; + got_ref_list_free(&t->refs); TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) { TAILQ_REMOVE(&t->repo_commits, rc, entry); gotweb_free_repo_commit(rc); @@ -786,6 +777,22 @@ gotweb_free_transport(struct transport *t) free(t->more_id); free(t->next_id); free(t->prev_id); + if (t->blob) + got_object_blob_close(t->blob); + if (t->fp) { + err = got_gotweb_flushfile(t->fp, t->fd); + if (err) + log_warnx("%s: got_gotweb_flushfile failure: %s", + __func__, err->msg); + t->fd = -1; + } + if (t->fd != -1) + close(t->fd); + if (t->repos) { + for (i = 0; i < t->nrepos; ++i) + free(t->repos[i]); + free(t->repos); + } free(t); } @@ -848,30 +855,24 @@ gotweb_get_navs(struct request *c, struct gotweb_url * } } -static const struct got_error * -gotweb_render_index(struct request *c) +static int +gotweb_render_index(struct template *tp) { const struct got_error *error = NULL; + struct request *c = tp->tp_arg; struct server *srv = c->srv; struct transport *t = c->t; struct querystring *qs = t->qs; struct repo_dir *repo_dir = NULL; - struct dirent **sd_dent = NULL; - unsigned int d_cnt, d_i, d_disp = 0; + struct dirent **sd_dent = t->repos; + unsigned int d_i, d_disp = 0; unsigned int d_skipped = 0; - int type; + int type, r; - d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort); - if (d_cnt == -1) { - sd_dent = NULL; - error = got_error_from_errno2("scandir", srv->repos_path); - goto done; - } - if (gotweb_render_repo_table_hdr(c->tp) == -1) - goto done; + return -1; - for (d_i = 0; d_i < d_cnt; d_i++) { + for (d_i = 0; d_i < t->nrepos; d_i++) { if (srv->max_repos > 0 && t->prev_disp == srv->max_repos) break; @@ -884,7 +885,7 @@ gotweb_render_index(struct request *c) error = got_path_dirent_type(&type, srv->repos_path, sd_dent[d_i]); if (error) - goto done; + continue; if (type != DT_DIR) { d_skipped++; continue; @@ -898,50 +899,45 @@ gotweb_render_index(struct request *c) error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name); if (error) - goto done; + continue; error = gotweb_load_got_path(c, repo_dir); - if (error && error->code == GOT_ERR_NOT_GIT_REPO) { - error = NULL; + if (error && error->code == GOT_ERR_LONELY_PACKIDX) { + if (error->code != GOT_ERR_NOT_GIT_REPO) + log_warnx("%s: %s: %s", __func__, + sd_dent[d_i]->d_name, error->msg); gotweb_free_repo_dir(repo_dir); repo_dir = NULL; d_skipped++; continue; } - if (error && error->code != GOT_ERR_LONELY_PACKIDX) - goto done; d_disp++; t->prev_disp++; - if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1) - goto done; - + r = gotweb_render_repo_fragment(c->tp, repo_dir); gotweb_free_repo_dir(repo_dir); - repo_dir = NULL; + if (r == -1) + return -1; + t->next_disp++; if (d_disp == srv->max_repos_display) break; } - t->repos_total = d_cnt - d_skipped; + t->repos_total = t->nrepos - d_skipped; if (srv->max_repos_display == 0) - goto done; + return 0; if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display) - goto done; + return 0; if (t->repos_total <= srv->max_repos || t->repos_total <= srv->max_repos_display) - goto done; + return 0; if (gotweb_render_navs(c->tp) == -1) - goto done; -done: - if (sd_dent) { - for (d_i = 0; d_i < d_cnt; d_i++) - free(sd_dent[d_i]); - free(sd_dent); - } - return error; + return -1; + + return 0; } static inline int blob - cc0a4954a61a219297305919f2441a682fcb9a78 blob + 3633c55f9eed4080f24d13cadb5b1fb99b89cf9c --- gotwebd/gotwebd.c +++ gotwebd/gotwebd.c @@ -41,6 +41,7 @@ #include #include "got_opentemp.h" +#include "got_reference.h" #include "proc.h" #include "gotwebd.h" blob - 98e9f879c0f9fc272de90ddc649d0c9aa2d88875 blob + 56c7eb962abf8757106cc2b4836d502c6a6655e9 --- gotwebd/gotwebd.h +++ gotwebd/gotwebd.h @@ -186,6 +186,7 @@ struct got_repository; struct transport { TAILQ_HEAD(repo_commits_head, repo_commit) repo_commits; TAILQ_HEAD(repo_tags_head, repo_tag) repo_tags; + struct got_reflist_head refs; struct got_repository *repo; struct repo_dir *repo_dir; struct querystring *qs; @@ -196,6 +197,11 @@ struct transport { unsigned int next_disp; unsigned int prev_disp; unsigned int tag_count; + struct got_blob_object *blob; + int fd; + FILE *fp; + struct dirent **repos; + int nrepos; }; enum socket_priv_fds { @@ -463,20 +469,19 @@ void gotweb_process_request(struct request *); void gotweb_free_transport(struct transport *); /* pages.tmpl */ -int gotweb_render_header(struct template *); -int gotweb_render_footer(struct template *); +int gotweb_render_page(struct template *, int (*)(struct template *)); int gotweb_render_repo_table_hdr(struct template *); int gotweb_render_repo_fragment(struct template *, struct repo_dir *); int gotweb_render_briefs(struct template *); int gotweb_render_navs(struct template *); int gotweb_render_commits(struct template *); -int gotweb_render_blob(struct template *, struct got_blob_object *); +int gotweb_render_blob(struct template *); int gotweb_render_tree(struct template *); int gotweb_render_tags(struct template *); int gotweb_render_tag(struct template *); -int gotweb_render_diff(struct template *, FILE *); +int gotweb_render_diff(struct template *); int gotweb_render_branches(struct template *, struct got_reflist_head *); -int gotweb_render_summary(struct template *, struct got_reflist_head *); +int gotweb_render_summary(struct template *); int gotweb_render_blame(struct template *); int gotweb_render_rss(struct template *); blob - bea0461c4b1286b162ce1d279fb93dd847ff4ab5 blob + 6517aad5f520ea885ec366bd60717093b4ce85ae --- gotwebd/pages.tmpl +++ gotwebd/pages.tmpl @@ -54,7 +54,8 @@ static inline int rss_author(struct template *, char * !} -{{ define gotweb_render_header(struct template *tp) }} +{{ define gotweb_render_page(struct template *tp, + int (*body)(struct template *)) }} {! struct request *c = tp->tp_arg; struct server *srv = c->srv; @@ -110,13 +111,7 @@ static inline int rss_author(struct template *, char *
-{{ end }} - -{{ define gotweb_render_footer(struct template *tp) }} -{! - struct request *c = tp->tp_arg; - struct server *srv = c->srv; -!} + {{ render body(tp) }}
{{ if srv->show_site_owner }} @@ -433,11 +428,11 @@ static inline int rss_author(struct template *, char *
{{ end }} -{{ define gotweb_render_blob(struct template *tp, - struct got_blob_object *blob) }} +{{ define gotweb_render_blob(struct template *tp) }} {! struct request *c = tp->tp_arg; struct transport *t = c->t; + struct got_blob_object *blob = t->blob; struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits); !}
@@ -711,10 +706,11 @@ static inline int rss_author(struct template *, char *
{{ end }} -{{ define gotweb_render_diff(struct template *tp, FILE *fp) }} +{{ define gotweb_render_diff(struct template *tp) }} {! struct request *c = tp->tp_arg; struct transport *t = c->t; + FILE *fp = t->fp; struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits); char *line = NULL; size_t linesize = 0; @@ -851,12 +847,12 @@ static inline int rss_author(struct template *, char *
{{ end }} -{{ define gotweb_render_summary(struct template *tp, - struct got_reflist_head *refs) }} +{{ define gotweb_render_summary(struct template *tp) }} {! struct request *c = tp->tp_arg; struct server *srv = c->srv; struct transport *t = c->t; + struct got_reflist_head *refs = &t->refs; !}
{{ if srv->show_repo_description }} blob - cc7ccf7ef44f2f9a5f354959abcc57ee681162bf blob + d5a7dbbee8448313fbb103ee1f02db4a45a1bb10 --- gotwebd/parse.y +++ gotwebd/parse.y @@ -46,10 +46,12 @@ #include #include #include + +#include "got_sockaddr.h" +#include "got_reference.h" #include "proc.h" #include "gotwebd.h" -#include "got_sockaddr.h" TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files); static struct file { blob - ecb88027e072a125a309c08b985733a77ab2a26d blob + 2e65dd1a33285ce42d25e3660426529b8adb5416 --- gotwebd/sockets.c +++ gotwebd/sockets.c @@ -51,6 +51,7 @@ #include "got_error.h" #include "got_opentemp.h" +#include "got_reference.h" #include "got_repository.h" #include "proc.h"