2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <netinet/in.h>
23 #include <sys/queue.h>
25 #include <sys/types.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
51 #include "got_compat.h"
57 static const struct querystring_keys querystring_keys[] = {
62 { "headref", HEADREF },
63 { "index_page", INDEX_PAGE },
68 static const struct action_keys action_keys[] = {
71 { "blobraw", BLOBRAW },
73 { "commits", COMMITS },
77 { "summary", SUMMARY },
84 static const struct got_error *gotweb_init_querystring(struct querystring **);
85 static const struct got_error *gotweb_parse_querystring(struct querystring **,
87 static const struct got_error *gotweb_assign_querystring(struct querystring **,
89 static const struct got_error *gotweb_render_index(struct request *);
90 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
92 static const struct got_error *gotweb_load_got_path(struct request *c,
94 static const struct got_error *gotweb_get_repo_description(char **,
95 struct server *, const char *, int);
96 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
99 static void gotweb_free_querystring(struct querystring *);
100 static void gotweb_free_repo_dir(struct repo_dir *);
102 struct server *gotweb_get_server(uint8_t *, uint8_t *);
105 gotweb_reply(struct request *c, int status, const char *ctype,
106 struct gotweb_url *location)
110 if (status != 200 && fcgi_printf(c, "Status: %d\r\n", status) == -1)
114 if (fcgi_puts(c->tp, "Location: ") == -1 ||
115 gotweb_render_url(c, location) == -1 ||
116 fcgi_puts(c->tp, "\r\n") == -1)
120 csp = "Content-Security-Policy: default-src 'self'; "
121 "script-src 'none'; object-src 'none';\r\n";
122 if (fcgi_puts(c->tp, csp) == -1)
125 if (ctype && fcgi_printf(c, "Content-Type: %s\r\n", ctype) == -1)
128 return fcgi_puts(c->tp, "\r\n");
132 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
137 r = fcgi_printf(c, "Content-Disposition: attachment; "
138 "filename=%s%s\r\n", file, suffix ? suffix : "");
141 return gotweb_reply(c, 200, ctype, NULL);
145 gotweb_process_request(struct request *c)
147 const struct got_error *error = NULL, *error2 = NULL;
148 struct got_blob_object *blob = NULL;
149 struct server *srv = NULL;
150 struct querystring *qs = NULL;
151 struct repo_dir *repo_dir = NULL;
152 struct got_reflist_head refs;
154 uint8_t err[] = "gotwebd experienced an error: ";
155 int r, html = 0, fd = -1;
159 /* init the transport */
160 error = gotweb_init_transport(&c->t);
162 log_warnx("%s: %s", __func__, error->msg);
165 /* don't process any further if client disconnected */
166 if (c->sock->client_status == CLIENT_DISCONNECT)
168 /* get the gotwebd server */
169 srv = gotweb_get_server(c->server_name, c->http_host);
171 log_warnx("%s: error server is NULL", __func__);
175 /* parse our querystring */
176 error = gotweb_init_querystring(&qs);
178 log_warnx("%s: %s", __func__, error->msg);
182 error = gotweb_parse_querystring(&qs, c->querystring);
184 log_warnx("%s: %s", __func__, error->msg);
189 * certain actions require a commit id in the querystring. this stops
190 * bad actors from exploiting this by manually manipulating the
194 if (qs->action == BLAME || qs->action == BLOB ||
195 qs->action == BLOBRAW || qs->action == DIFF) {
196 if (qs->commit == NULL) {
197 error2 = got_error(GOT_ERR_QUERYSTRING);
202 if (qs->action != INDEX) {
203 error = gotweb_init_repo_dir(&repo_dir, qs->path);
206 error = gotweb_load_got_path(c, repo_dir);
207 c->t->repo_dir = repo_dir;
208 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
212 if (qs->action == BLOBRAW) {
217 error = got_get_repo_commits(c, 1);
221 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
226 r = gotweb_reply_file(c, "application/octet-stream",
229 r = gotweb_reply(c, 200, "text/plain", NULL);
234 error = got_object_blob_read_block(&len, blob);
239 buf = got_object_blob_get_read_buf(blob);
240 if (fcgi_gen_binary_response(c, buf, len) == -1)
247 if (qs->action == BLOB) {
249 struct gotweb_url url = {
254 .commit = qs->commit,
255 .folder = qs->folder,
259 error = got_get_repo_commits(c, 1);
263 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
267 gotweb_reply(c, 302, NULL, &url);
272 if (qs->action == RSS) {
273 const char *ctype = "application/rss+xml;charset=utf-8";
275 if (gotweb_reply_file(c, ctype, repo_dir->name, ".rss") == -1)
278 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
280 log_warnx("%s: %s", __func__, error->msg);
283 if (gotweb_render_rss(c->tp) == -1)
289 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
293 if (gotweb_render_header(c->tp) == -1)
303 error = got_get_repo_commits(c, 1);
305 log_warnx("%s: %s", __func__, error->msg);
308 if (gotweb_render_blame(c->tp) == -1)
312 if (gotweb_render_blob(c->tp, blob) == -1)
316 if (gotweb_render_briefs(c->tp) == -1)
320 error = got_get_repo_commits(c, srv->max_commits_display);
322 log_warnx("%s: %s", __func__, error->msg);
325 if (gotweb_render_commits(c->tp) == -1)
329 error = got_get_repo_commits(c, 1);
331 log_warnx("%s: %s", __func__, error->msg);
334 error = got_open_diff_for_output(&fp, &fd, c);
336 log_warnx("%s: %s", __func__, error->msg);
339 if (gotweb_render_diff(c->tp, fp) == -1)
343 error = gotweb_render_index(c);
345 log_warnx("%s: %s", __func__, error->msg);
350 error = got_ref_list(&refs, c->t->repo, "refs/heads",
351 got_ref_cmp_by_name, NULL);
353 log_warnx("%s: got_ref_list: %s", __func__,
358 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
360 log_warnx("%s: got_get_repo_tags: %s", __func__,
364 qs->action = SUMMARY;
365 if (gotweb_render_summary(c->tp, &refs) == -1)
369 error = got_get_repo_tags(c, 1);
371 log_warnx("%s: %s", __func__, error->msg);
374 if (c->t->tag_count == 0) {
375 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
379 if (gotweb_render_tag(c->tp) == -1)
383 error = got_get_repo_tags(c, srv->max_commits_display);
385 log_warnx("%s: %s", __func__, error->msg);
388 if (gotweb_render_tags(c->tp) == -1)
392 error = got_get_repo_commits(c, 1);
394 log_warnx("%s: %s", __func__, error->msg);
397 if (gotweb_render_tree(c->tp) == -1)
402 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
403 "Erorr: Bad Querystring");
411 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
413 if (fcgi_printf(c, "\n%s", err) == -1)
416 if (fcgi_printf(c, "%s", error->msg) == -1)
419 if (fcgi_printf(c, "see daemon logs for details") == -1)
422 if (html && fcgi_printf(c, "</div>\n") == -1)
426 got_object_blob_close(blob);
428 error = got_gotweb_flushfile(fp, fd);
430 log_warnx("%s: got_gotweb_flushfile failure: %s",
431 __func__, error->msg);
436 if (html && srv != NULL)
437 gotweb_render_footer(c->tp);
439 got_ref_list_free(&refs);
443 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
445 struct server *srv = NULL;
447 /* check against the server name first */
448 if (strlen(server_name) > 0)
449 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
450 if (strcmp(srv->name, server_name) == 0)
453 /* check against subdomain second */
454 if (strlen(subdomain) > 0)
455 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
456 if (strcmp(srv->name, subdomain) == 0)
459 /* if those fail, send first server */
460 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
467 const struct got_error *
468 gotweb_init_transport(struct transport **t)
470 const struct got_error *error = NULL;
472 *t = calloc(1, sizeof(**t));
474 return got_error_from_errno2("%s: calloc", __func__);
476 TAILQ_INIT(&(*t)->repo_commits);
477 TAILQ_INIT(&(*t)->repo_tags);
480 (*t)->repo_dir = NULL;
482 (*t)->next_id = NULL;
483 (*t)->prev_id = NULL;
490 static const struct got_error *
491 gotweb_init_querystring(struct querystring **qs)
493 const struct got_error *error = NULL;
495 *qs = calloc(1, sizeof(**qs));
497 return got_error_from_errno2("%s: calloc", __func__);
499 (*qs)->headref = strdup("HEAD");
500 if ((*qs)->headref == NULL) {
503 return got_error_from_errno2("%s: strdup", __func__);
506 (*qs)->action = INDEX;
507 (*qs)->commit = NULL;
509 (*qs)->folder = NULL;
510 (*qs)->index_page = 0;
516 static const struct got_error *
517 gotweb_parse_querystring(struct querystring **qs, char *qst)
519 const struct got_error *error = NULL;
520 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
521 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
528 return got_error_from_errno2("%s: strdup", __func__);
533 while (tok1_pair != NULL) {
534 strsep(&tok1_end, "&");
536 tok2 = strdup(tok1_pair);
539 return got_error_from_errno2("%s: strdup", __func__);
545 while (tok2_pair != NULL) {
546 strsep(&tok2_end, "=");
548 error = gotweb_assign_querystring(qs, tok2_pair,
553 tok2_pair = tok2_end;
556 tok1_pair = tok1_end;
567 * Adapted from usr.sbin/httpd/httpd.c url_decode.
569 static const struct got_error *
570 gotweb_urldecode(char *url)
582 /* Encoding character is followed by two hex chars */
583 if (!isxdigit((unsigned char)p[1]) ||
584 !isxdigit((unsigned char)p[2]) ||
585 (p[1] == '0' && p[2] == '0'))
586 return got_error(GOT_ERR_BAD_QUERYSTRING);
592 * We don't have to validate "hex" because it is
593 * guaranteed to include two hex chars followed by nul.
595 x = strtoul(hex, NULL, 16);
611 static const struct got_error *
612 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
614 const struct got_error *error = NULL;
618 error = gotweb_urldecode(value);
622 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
623 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
626 switch (querystring_keys[el_cnt].element) {
628 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
629 if (strcmp(value, action_keys[a_cnt].name) != 0)
631 else if (strcmp(value,
632 action_keys[a_cnt].name) == 0){
634 action_keys[a_cnt].action;
642 (*qs)->commit = strdup(value);
643 if ((*qs)->commit == NULL) {
644 error = got_error_from_errno2("%s: strdup",
650 (*qs)->file = strdup(value);
651 if ((*qs)->file == NULL) {
652 error = got_error_from_errno2("%s: strdup",
658 (*qs)->folder = strdup(value);
659 if ((*qs)->folder == NULL) {
660 error = got_error_from_errno2("%s: strdup",
666 free((*qs)->headref);
667 (*qs)->headref = strdup(value);
668 if ((*qs)->headref == NULL) {
669 error = got_error_from_errno2("%s: strdup",
675 if (strlen(value) == 0)
677 (*qs)->index_page = strtonum(value, INT64_MIN,
680 error = got_error_from_errno3("%s: strtonum %s",
684 if ((*qs)->index_page < 0)
685 (*qs)->index_page = 0;
688 (*qs)->path = strdup(value);
689 if ((*qs)->path == NULL) {
690 error = got_error_from_errno2("%s: strdup",
696 if (strlen(value) == 0)
698 (*qs)->page = strtonum(value, INT64_MIN,
701 error = got_error_from_errno3("%s: strtonum %s",
717 gotweb_free_repo_tag(struct repo_tag *rt)
722 free(rt->tag_commit);
723 free(rt->commit_msg);
730 gotweb_free_repo_commit(struct repo_commit *rc)
740 free(rc->commit_msg);
746 gotweb_free_querystring(struct querystring *qs)
759 gotweb_free_repo_dir(struct repo_dir *repo_dir)
761 if (repo_dir != NULL) {
762 free(repo_dir->name);
763 free(repo_dir->owner);
764 free(repo_dir->description);
766 free(repo_dir->path);
772 gotweb_free_transport(struct transport *t)
774 struct repo_commit *rc = NULL, *trc = NULL;
775 struct repo_tag *rt = NULL, *trt = NULL;
777 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
778 TAILQ_REMOVE(&t->repo_commits, rc, entry);
779 gotweb_free_repo_commit(rc);
781 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
782 TAILQ_REMOVE(&t->repo_tags, rt, entry);
783 gotweb_free_repo_tag(rt);
785 gotweb_free_repo_dir(t->repo_dir);
786 gotweb_free_querystring(t->qs);
794 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
795 struct gotweb_url *next, int *have_next)
797 struct transport *t = c->t;
798 struct querystring *qs = t->qs;
799 struct server *srv = c->srv;
801 *have_prev = *have_next = 0;
805 if (qs->index_page > 0) {
807 *prev = (struct gotweb_url){
809 .index_page = qs->index_page - 1,
813 if (t->next_disp == srv->max_repos_display &&
814 t->repos_total != (qs->index_page + 1) *
815 srv->max_repos_display) {
817 *next = (struct gotweb_url){
819 .index_page = qs->index_page + 1,
825 if (t->prev_id && qs->commit != NULL &&
826 strcmp(qs->commit, t->prev_id) != 0) {
828 *prev = (struct gotweb_url){
831 .page = qs->page - 1,
833 .commit = t->prev_id,
834 .headref = qs->headref,
839 *next = (struct gotweb_url){
842 .page = qs->page + 1,
844 .commit = t->next_id,
845 .headref = qs->headref,
852 static const struct got_error *
853 gotweb_render_index(struct request *c)
855 const struct got_error *error = NULL;
856 struct server *srv = c->srv;
857 struct transport *t = c->t;
858 struct querystring *qs = t->qs;
859 struct repo_dir *repo_dir = NULL;
860 struct dirent **sd_dent = NULL;
861 unsigned int d_cnt, d_i, d_disp = 0;
862 unsigned int d_skipped = 0;
865 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
868 error = got_error_from_errno2("scandir", srv->repos_path);
872 if (gotweb_render_repo_table_hdr(c->tp) == -1)
875 for (d_i = 0; d_i < d_cnt; d_i++) {
876 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
879 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
880 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
885 error = got_path_dirent_type(&type, srv->repos_path,
889 if (type != DT_DIR) {
894 if (qs->index_page > 0 && (qs->index_page *
895 srv->max_repos_display) > t->prev_disp) {
900 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
904 error = gotweb_load_got_path(c, repo_dir);
905 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
907 gotweb_free_repo_dir(repo_dir);
912 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
918 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
921 gotweb_free_repo_dir(repo_dir);
924 if (d_disp == srv->max_repos_display)
927 t->repos_total = d_cnt - d_skipped;
929 if (srv->max_repos_display == 0)
931 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
933 if (t->repos_total <= srv->max_repos ||
934 t->repos_total <= srv->max_repos_display)
937 if (gotweb_render_navs(c->tp) == -1)
941 for (d_i = 0; d_i < d_cnt; d_i++)
949 should_urlencode(int c)
951 if (c <= ' ' || c >= 127)
975 /* needed because the URLs are embedded into the HTML */
984 gotweb_urlencode(const char *str)
992 for (s = str; *s; ++s) {
994 if (should_urlencode(*s))
998 escaped = calloc(1, len + 1);
1003 for (s = str; *s; ++s) {
1004 if (should_urlencode(*s)) {
1005 a = (*s & 0xF0) >> 4;
1009 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1010 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1019 gotweb_action_name(int action)
1054 gotweb_render_url(struct request *c, struct gotweb_url *url)
1056 const char *sep = "?", *action;
1060 action = gotweb_action_name(url->action);
1061 if (action != NULL) {
1062 if (fcgi_printf(c, "?action=%s", action) == -1)
1068 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1074 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1080 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1086 tmp = gotweb_urlencode(url->file);
1089 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1097 tmp = gotweb_urlencode(url->folder);
1100 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1108 tmp = gotweb_urlencode(url->headref);
1111 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1118 if (url->index_page != -1) {
1119 if (fcgi_printf(c, "%sindex_page=%d", sep,
1120 url->index_page) == -1)
1126 tmp = gotweb_urlencode(url->path);
1129 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1136 if (url->page != -1) {
1137 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1146 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1148 struct template *tp = c->tp;
1149 const char *proto = c->https ? "https" : "http";
1151 if (fcgi_puts(tp, proto) == -1 ||
1152 fcgi_puts(tp, "://") == -1 ||
1153 tp_htmlescape(tp, c->server_name) == -1 ||
1154 tp_htmlescape(tp, c->document_uri) == -1)
1157 return gotweb_render_url(c, url);
1160 static struct got_repository *
1161 find_cached_repo(struct server *srv, const char *path)
1165 for (i = 0; i < srv->ncached_repos; i++) {
1166 if (strcmp(srv->cached_repos[i].path, path) == 0)
1167 return srv->cached_repos[i].repo;
1173 static const struct got_error *
1174 cache_repo(struct got_repository **new, struct server *srv,
1175 struct repo_dir *repo_dir, struct socket *sock)
1177 const struct got_error *error = NULL;
1178 struct got_repository *repo;
1179 struct cached_repo *cr;
1182 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1183 cr = &srv->cached_repos[srv->ncached_repos - 1];
1184 error = got_repo_close(cr->repo);
1185 memset(cr, 0, sizeof(*cr));
1186 srv->ncached_repos--;
1189 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1190 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1191 cr = &srv->cached_repos[0];
1194 cr = &srv->cached_repos[srv->ncached_repos];
1197 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1200 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1201 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1206 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1207 >= sizeof(cr->path)) {
1209 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1210 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1212 return got_error(GOT_ERR_NO_SPACE);
1216 srv->ncached_repos++;
1221 static const struct got_error *
1222 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1224 const struct got_error *error = NULL;
1225 struct socket *sock = c->sock;
1226 struct server *srv = c->srv;
1227 struct transport *t = c->t;
1228 struct got_repository *repo = NULL;
1232 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1233 GOTWEB_GIT_DIR) == -1)
1234 return got_error_from_errno("asprintf");
1236 dt = opendir(dir_test);
1240 repo_dir->path = dir_test;
1245 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1246 repo_dir->name) == -1)
1247 return got_error_from_errno("asprintf");
1249 dt = opendir(dir_test);
1251 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1254 repo_dir->path = dir_test;
1259 if (srv->respect_exportok &&
1260 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1261 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1265 repo = find_cached_repo(srv, repo_dir->path);
1267 error = cache_repo(&repo, srv, repo_dir, sock);
1272 error = gotweb_get_repo_description(&repo_dir->description, srv,
1273 repo_dir->path, dirfd(dt));
1276 error = got_get_repo_owner(&repo_dir->owner, c);
1279 error = got_get_repo_age(&repo_dir->age, c, NULL);
1282 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1286 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1287 error = got_error_from_errno("closedir");
1291 static const struct got_error *
1292 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1294 const struct got_error *error;
1296 *repo_dir = calloc(1, sizeof(**repo_dir));
1297 if (*repo_dir == NULL)
1298 return got_error_from_errno("calloc");
1300 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1301 error = got_error_from_errno("asprintf");
1306 (*repo_dir)->owner = NULL;
1307 (*repo_dir)->description = NULL;
1308 (*repo_dir)->url = NULL;
1309 (*repo_dir)->path = NULL;
1314 static const struct got_error *
1315 gotweb_get_repo_description(char **description, struct server *srv,
1316 const char *dirpath, int dir)
1318 const struct got_error *error = NULL;
1323 *description = NULL;
1324 if (srv->show_repo_description == 0)
1327 fd = openat(dir, "description", O_RDONLY);
1329 if (errno != ENOENT && errno != EACCES) {
1330 error = got_error_from_errno_fmt("openat %s/%s",
1331 dirpath, "description");
1336 if (fstat(fd, &sb) == -1) {
1337 error = got_error_from_errno_fmt("fstat %s/%s",
1338 dirpath, "description");
1343 if (len > GOTWEBD_MAXDESCRSZ - 1)
1344 len = GOTWEBD_MAXDESCRSZ - 1;
1346 *description = calloc(len + 1, sizeof(**description));
1347 if (*description == NULL) {
1348 error = got_error_from_errno("calloc");
1352 if (read(fd, *description, len) == -1)
1353 error = got_error_from_errno("read");
1355 if (fd != -1 && close(fd) == -1 && error == NULL)
1356 error = got_error_from_errno("close");
1360 static const struct got_error *
1361 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1364 const struct got_error *error = NULL;
1370 if (srv->show_repo_cloneurl == 0)
1373 fd = openat(dir, "cloneurl", O_RDONLY);
1375 if (errno != ENOENT && errno != EACCES) {
1376 error = got_error_from_errno_fmt("openat %s/%s",
1377 dirpath, "cloneurl");
1382 if (fstat(fd, &sb) == -1) {
1383 error = got_error_from_errno_fmt("fstat %s/%s",
1384 dirpath, "cloneurl");
1389 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1390 len = GOTWEBD_MAXCLONEURLSZ - 1;
1392 *url = calloc(len + 1, sizeof(**url));
1394 error = got_error_from_errno("calloc");
1398 if (read(fd, *url, len) == -1)
1399 error = got_error_from_errno("read");
1401 if (fd != -1 && close(fd) == -1 && error == NULL)
1402 error = got_error_from_errno("close");
1407 gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1409 struct request *c = tp->tp_arg;
1411 long long diff_time;
1412 const char *years = "years ago", *months = "months ago";
1413 const char *weeks = "weeks ago", *days = "days ago";
1414 const char *hours = "hours ago", *minutes = "minutes ago";
1415 const char *seconds = "seconds ago", *now = "right now";
1422 diff_time = time(NULL) - committer_time;
1423 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1424 if (fcgi_printf(c, "%lld %s",
1425 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1427 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1428 if (fcgi_printf(c, "%lld %s",
1429 (diff_time / 60 / 60 / 24 / (365 / 12)),
1432 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1433 if (fcgi_printf(c, "%lld %s",
1434 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1436 } else if (diff_time > 60 * 60 * 24 * 2) {
1437 if (fcgi_printf(c, "%lld %s",
1438 (diff_time / 60 / 60 / 24), days) == -1)
1440 } else if (diff_time > 60 * 60 * 2) {
1441 if (fcgi_printf(c, "%lld %s",
1442 (diff_time / 60 / 60), hours) == -1)
1444 } else if (diff_time > 60 * 2) {
1445 if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1448 } else if (diff_time > 2) {
1449 if (fcgi_printf(c, "%lld %s", diff_time,
1453 if (fcgi_puts(tp, now) == -1)
1458 if (gmtime_r(&committer_time, &tm) == NULL)
1461 s = asctime_r(&tm, datebuf);
1465 if (fcgi_puts(tp, datebuf) == -1 ||
1466 fcgi_puts(tp, " UTC") == -1)
1470 if (gmtime_r(&committer_time, &tm) == NULL)
1473 r = strftime(datebuf, sizeof(datebuf),
1474 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1478 if (fcgi_puts(tp, datebuf) == -1)