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>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
48 #include "got_commit_graph.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
52 #include "got_compat.h"
58 static const struct querystring_keys querystring_keys[] = {
63 { "headref", HEADREF },
64 { "index_page", INDEX_PAGE },
69 static const struct action_keys action_keys[] = {
72 { "blobraw", BLOBRAW },
74 { "commits", COMMITS },
78 { "summary", SUMMARY },
85 static const struct got_error *gotweb_init_querystring(struct querystring **);
86 static const struct got_error *gotweb_parse_querystring(struct querystring **,
88 static const struct got_error *gotweb_assign_querystring(struct querystring **,
90 static const struct got_error *gotweb_render_index(struct request *);
91 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
93 static const struct got_error *gotweb_load_got_path(struct request *c,
95 static const struct got_error *gotweb_get_repo_description(char **,
96 struct server *, const char *, int);
97 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
100 static void gotweb_free_querystring(struct querystring *);
101 static void gotweb_free_repo_dir(struct repo_dir *);
103 struct server *gotweb_get_server(uint8_t *, uint8_t *);
106 gotweb_reply(struct request *c, int status, const char *ctype,
107 struct gotweb_url *location)
111 if (status != 200 && fcgi_printf(c, "Status: %d\r\n", status) == -1)
115 if (fcgi_puts(c->tp, "Location: ") == -1 ||
116 gotweb_render_url(c, location) == -1 ||
117 fcgi_puts(c->tp, "\r\n") == -1)
121 csp = "Content-Security-Policy: default-src 'self'; "
122 "script-src 'none'; object-src 'none';\r\n";
123 if (fcgi_puts(c->tp, csp) == -1)
126 if (ctype && fcgi_printf(c, "Content-Type: %s\r\n", ctype) == -1)
129 return fcgi_puts(c->tp, "\r\n");
133 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
138 r = fcgi_printf(c, "Content-Disposition: attachment; "
139 "filename=%s%s\r\n", file, suffix ? suffix : "");
142 return gotweb_reply(c, 200, ctype, NULL);
146 gotweb_process_request(struct request *c)
148 const struct got_error *error = NULL, *error2 = NULL;
149 struct got_blob_object *blob = NULL;
150 struct server *srv = NULL;
151 struct querystring *qs = NULL;
152 struct repo_dir *repo_dir = NULL;
153 struct got_reflist_head refs;
155 uint8_t err[] = "gotwebd experienced an error: ";
156 int r, html = 0, fd = -1;
160 /* init the transport */
161 error = gotweb_init_transport(&c->t);
163 log_warnx("%s: %s", __func__, error->msg);
166 /* don't process any further if client disconnected */
167 if (c->sock->client_status == CLIENT_DISCONNECT)
169 /* get the gotwebd server */
170 srv = gotweb_get_server(c->server_name, c->http_host);
172 log_warnx("%s: error server is NULL", __func__);
176 /* parse our querystring */
177 error = gotweb_init_querystring(&qs);
179 log_warnx("%s: %s", __func__, error->msg);
183 error = gotweb_parse_querystring(&qs, c->querystring);
185 log_warnx("%s: %s", __func__, error->msg);
190 * certain actions require a commit id in the querystring. this stops
191 * bad actors from exploiting this by manually manipulating the
195 if (qs->action == BLAME || qs->action == BLOB ||
196 qs->action == BLOBRAW || qs->action == DIFF) {
197 if (qs->commit == NULL) {
198 error2 = got_error(GOT_ERR_QUERYSTRING);
203 if (qs->action != INDEX) {
204 error = gotweb_init_repo_dir(&repo_dir, qs->path);
207 error = gotweb_load_got_path(c, repo_dir);
208 c->t->repo_dir = repo_dir;
209 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
213 if (qs->action == BLOBRAW) {
218 error = got_get_repo_commits(c, 1);
222 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
227 r = gotweb_reply_file(c, "application/octet-stream",
230 r = gotweb_reply(c, 200, "text/plain", NULL);
235 error = got_object_blob_read_block(&len, blob);
240 buf = got_object_blob_get_read_buf(blob);
241 if (fcgi_gen_binary_response(c, buf, len) == -1)
248 if (qs->action == BLOB) {
250 struct gotweb_url url = {
255 .commit = qs->commit,
256 .folder = qs->folder,
260 error = got_get_repo_commits(c, 1);
264 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
268 gotweb_reply(c, 302, NULL, &url);
273 if (qs->action == RSS) {
274 const char *ctype = "application/rss+xml;charset=utf-8";
276 if (gotweb_reply_file(c, ctype, repo_dir->name, ".rss") == -1)
279 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
281 log_warnx("%s: %s", __func__, error->msg);
284 if (gotweb_render_rss(c->tp) == -1)
290 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
294 if (gotweb_render_header(c->tp) == -1)
304 error = got_get_repo_commits(c, 1);
306 log_warnx("%s: %s", __func__, error->msg);
309 if (gotweb_render_blame(c->tp) == -1)
313 if (gotweb_render_blob(c->tp, blob) == -1)
317 if (gotweb_render_briefs(c->tp) == -1)
321 error = got_get_repo_commits(c, srv->max_commits_display);
323 log_warnx("%s: %s", __func__, error->msg);
326 if (gotweb_render_commits(c->tp) == -1)
330 error = got_get_repo_commits(c, 1);
332 log_warnx("%s: %s", __func__, error->msg);
335 error = got_open_diff_for_output(&fp, &fd, c);
337 log_warnx("%s: %s", __func__, error->msg);
340 if (gotweb_render_diff(c->tp, fp) == -1)
344 error = gotweb_render_index(c);
346 log_warnx("%s: %s", __func__, error->msg);
351 error = got_ref_list(&refs, c->t->repo, "refs/heads",
352 got_ref_cmp_by_name, NULL);
354 log_warnx("%s: got_ref_list: %s", __func__,
359 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
361 log_warnx("%s: got_get_repo_tags: %s", __func__,
365 qs->action = SUMMARY;
366 if (gotweb_render_summary(c->tp, &refs) == -1)
370 error = got_get_repo_tags(c, 1);
372 log_warnx("%s: %s", __func__, error->msg);
375 if (c->t->tag_count == 0) {
376 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
380 if (gotweb_render_tag(c->tp) == -1)
384 error = got_get_repo_tags(c, srv->max_commits_display);
386 log_warnx("%s: %s", __func__, error->msg);
389 if (gotweb_render_tags(c->tp) == -1)
393 error = got_get_repo_commits(c, 1);
395 log_warnx("%s: %s", __func__, error->msg);
398 if (gotweb_render_tree(c->tp) == -1)
403 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
404 "Erorr: Bad Querystring");
412 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
414 if (fcgi_printf(c, "\n%s", err) == -1)
417 if (fcgi_printf(c, "%s", error->msg) == -1)
420 if (fcgi_printf(c, "see daemon logs for details") == -1)
423 if (html && fcgi_printf(c, "</div>\n") == -1)
427 got_object_blob_close(blob);
429 error = got_gotweb_flushfile(fp, fd);
431 log_warnx("%s: got_gotweb_flushfile failure: %s",
432 __func__, error->msg);
437 if (html && srv != NULL)
438 gotweb_render_footer(c->tp);
440 got_ref_list_free(&refs);
444 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
446 struct server *srv = NULL;
448 /* check against the server name first */
449 if (strlen(server_name) > 0)
450 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
451 if (strcmp(srv->name, server_name) == 0)
454 /* check against subdomain second */
455 if (strlen(subdomain) > 0)
456 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
457 if (strcmp(srv->name, subdomain) == 0)
460 /* if those fail, send first server */
461 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
468 const struct got_error *
469 gotweb_init_transport(struct transport **t)
471 const struct got_error *error = NULL;
473 *t = calloc(1, sizeof(**t));
475 return got_error_from_errno2("%s: calloc", __func__);
477 TAILQ_INIT(&(*t)->repo_commits);
478 TAILQ_INIT(&(*t)->repo_tags);
481 (*t)->repo_dir = NULL;
483 (*t)->next_id = NULL;
484 (*t)->prev_id = NULL;
491 static const struct got_error *
492 gotweb_init_querystring(struct querystring **qs)
494 const struct got_error *error = NULL;
496 *qs = calloc(1, sizeof(**qs));
498 return got_error_from_errno2("%s: calloc", __func__);
500 (*qs)->headref = strdup("HEAD");
501 if ((*qs)->headref == NULL) {
504 return got_error_from_errno2("%s: strdup", __func__);
507 (*qs)->action = INDEX;
508 (*qs)->commit = NULL;
510 (*qs)->folder = NULL;
511 (*qs)->index_page = 0;
517 static const struct got_error *
518 gotweb_parse_querystring(struct querystring **qs, char *qst)
520 const struct got_error *error = NULL;
521 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
522 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
529 return got_error_from_errno2("%s: strdup", __func__);
534 while (tok1_pair != NULL) {
535 strsep(&tok1_end, "&");
537 tok2 = strdup(tok1_pair);
540 return got_error_from_errno2("%s: strdup", __func__);
546 while (tok2_pair != NULL) {
547 strsep(&tok2_end, "=");
549 error = gotweb_assign_querystring(qs, tok2_pair,
554 tok2_pair = tok2_end;
557 tok1_pair = tok1_end;
568 * Adapted from usr.sbin/httpd/httpd.c url_decode.
570 static const struct got_error *
571 gotweb_urldecode(char *url)
583 /* Encoding character is followed by two hex chars */
584 if (!isxdigit((unsigned char)p[1]) ||
585 !isxdigit((unsigned char)p[2]) ||
586 (p[1] == '0' && p[2] == '0'))
587 return got_error(GOT_ERR_BAD_QUERYSTRING);
593 * We don't have to validate "hex" because it is
594 * guaranteed to include two hex chars followed by nul.
596 x = strtoul(hex, NULL, 16);
612 static const struct got_error *
613 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
615 const struct got_error *error = NULL;
619 error = gotweb_urldecode(value);
623 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
624 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
627 switch (querystring_keys[el_cnt].element) {
629 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
630 if (strcmp(value, action_keys[a_cnt].name) != 0)
632 else if (strcmp(value,
633 action_keys[a_cnt].name) == 0){
635 action_keys[a_cnt].action;
643 (*qs)->commit = strdup(value);
644 if ((*qs)->commit == NULL) {
645 error = got_error_from_errno2("%s: strdup",
651 (*qs)->file = strdup(value);
652 if ((*qs)->file == NULL) {
653 error = got_error_from_errno2("%s: strdup",
659 (*qs)->folder = strdup(value);
660 if ((*qs)->folder == NULL) {
661 error = got_error_from_errno2("%s: strdup",
667 free((*qs)->headref);
668 (*qs)->headref = strdup(value);
669 if ((*qs)->headref == NULL) {
670 error = got_error_from_errno2("%s: strdup",
676 if (strlen(value) == 0)
678 (*qs)->index_page = strtonum(value, INT64_MIN,
681 error = got_error_from_errno3("%s: strtonum %s",
685 if ((*qs)->index_page < 0)
686 (*qs)->index_page = 0;
689 (*qs)->path = strdup(value);
690 if ((*qs)->path == NULL) {
691 error = got_error_from_errno2("%s: strdup",
697 if (strlen(value) == 0)
699 (*qs)->page = strtonum(value, INT64_MIN,
702 error = got_error_from_errno3("%s: strtonum %s",
718 gotweb_free_repo_tag(struct repo_tag *rt)
723 free(rt->tag_commit);
724 free(rt->commit_msg);
731 gotweb_free_repo_commit(struct repo_commit *rc)
741 free(rc->commit_msg);
747 gotweb_free_querystring(struct querystring *qs)
760 gotweb_free_repo_dir(struct repo_dir *repo_dir)
762 if (repo_dir != NULL) {
763 free(repo_dir->name);
764 free(repo_dir->owner);
765 free(repo_dir->description);
767 free(repo_dir->path);
773 gotweb_free_transport(struct transport *t)
775 struct repo_commit *rc = NULL, *trc = NULL;
776 struct repo_tag *rt = NULL, *trt = NULL;
778 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
779 TAILQ_REMOVE(&t->repo_commits, rc, entry);
780 gotweb_free_repo_commit(rc);
782 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
783 TAILQ_REMOVE(&t->repo_tags, rt, entry);
784 gotweb_free_repo_tag(rt);
786 gotweb_free_repo_dir(t->repo_dir);
787 gotweb_free_querystring(t->qs);
795 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
796 struct gotweb_url *next, int *have_next)
798 struct transport *t = c->t;
799 struct querystring *qs = t->qs;
800 struct server *srv = c->srv;
802 *have_prev = *have_next = 0;
806 if (qs->index_page > 0) {
808 *prev = (struct gotweb_url){
810 .index_page = qs->index_page - 1,
814 if (t->next_disp == srv->max_repos_display &&
815 t->repos_total != (qs->index_page + 1) *
816 srv->max_repos_display) {
818 *next = (struct gotweb_url){
820 .index_page = qs->index_page + 1,
826 if (t->prev_id && qs->commit != NULL &&
827 strcmp(qs->commit, t->prev_id) != 0) {
829 *prev = (struct gotweb_url){
832 .page = qs->page - 1,
834 .commit = t->prev_id,
835 .headref = qs->headref,
840 *next = (struct gotweb_url){
843 .page = qs->page + 1,
845 .commit = t->next_id,
846 .headref = qs->headref,
853 static const struct got_error *
854 gotweb_render_index(struct request *c)
856 const struct got_error *error = NULL;
857 struct server *srv = c->srv;
858 struct transport *t = c->t;
859 struct querystring *qs = t->qs;
860 struct repo_dir *repo_dir = NULL;
861 struct dirent **sd_dent = NULL;
862 unsigned int d_cnt, d_i, d_disp = 0;
863 unsigned int d_skipped = 0;
866 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
869 error = got_error_from_errno2("scandir", srv->repos_path);
873 if (gotweb_render_repo_table_hdr(c->tp) == -1)
876 for (d_i = 0; d_i < d_cnt; d_i++) {
877 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
880 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
881 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
886 error = got_path_dirent_type(&type, srv->repos_path,
890 if (type != DT_DIR) {
895 if (qs->index_page > 0 && (qs->index_page *
896 srv->max_repos_display) > t->prev_disp) {
901 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
905 error = gotweb_load_got_path(c, repo_dir);
906 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
908 gotweb_free_repo_dir(repo_dir);
913 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
919 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
922 gotweb_free_repo_dir(repo_dir);
925 if (d_disp == srv->max_repos_display)
928 t->repos_total = d_cnt - d_skipped;
930 if (srv->max_repos_display == 0)
932 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
934 if (t->repos_total <= srv->max_repos ||
935 t->repos_total <= srv->max_repos_display)
938 if (gotweb_render_navs(c->tp) == -1)
942 for (d_i = 0; d_i < d_cnt; d_i++)
950 should_urlencode(int c)
952 if (c <= ' ' || c >= 127)
976 /* needed because the URLs are embedded into the HTML */
985 gotweb_urlencode(const char *str)
993 for (s = str; *s; ++s) {
995 if (should_urlencode(*s))
999 escaped = calloc(1, len + 1);
1000 if (escaped == NULL)
1004 for (s = str; *s; ++s) {
1005 if (should_urlencode(*s)) {
1006 a = (*s & 0xF0) >> 4;
1010 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1011 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1020 gotweb_action_name(int action)
1055 gotweb_render_url(struct request *c, struct gotweb_url *url)
1057 const char *sep = "?", *action;
1061 action = gotweb_action_name(url->action);
1062 if (action != NULL) {
1063 if (fcgi_printf(c, "?action=%s", action) == -1)
1069 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1075 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1081 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1087 tmp = gotweb_urlencode(url->file);
1090 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1098 tmp = gotweb_urlencode(url->folder);
1101 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1109 tmp = gotweb_urlencode(url->headref);
1112 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1119 if (url->index_page != -1) {
1120 if (fcgi_printf(c, "%sindex_page=%d", sep,
1121 url->index_page) == -1)
1127 tmp = gotweb_urlencode(url->path);
1130 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1137 if (url->page != -1) {
1138 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1147 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1149 struct template *tp = c->tp;
1150 const char *proto = c->https ? "https" : "http";
1152 if (fcgi_puts(tp, proto) == -1 ||
1153 fcgi_puts(tp, "://") == -1 ||
1154 tp_htmlescape(tp, c->server_name) == -1 ||
1155 tp_htmlescape(tp, c->document_uri) == -1)
1158 return gotweb_render_url(c, url);
1161 static struct got_repository *
1162 find_cached_repo(struct server *srv, const char *path)
1166 for (i = 0; i < srv->ncached_repos; i++) {
1167 if (strcmp(srv->cached_repos[i].path, path) == 0)
1168 return srv->cached_repos[i].repo;
1174 static const struct got_error *
1175 cache_repo(struct got_repository **new, struct server *srv,
1176 struct repo_dir *repo_dir, struct socket *sock)
1178 const struct got_error *error = NULL;
1179 struct got_repository *repo;
1180 struct cached_repo *cr;
1183 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1184 cr = &srv->cached_repos[srv->ncached_repos - 1];
1185 error = got_repo_close(cr->repo);
1186 memset(cr, 0, sizeof(*cr));
1187 srv->ncached_repos--;
1190 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1191 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1192 cr = &srv->cached_repos[0];
1195 cr = &srv->cached_repos[srv->ncached_repos];
1198 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1201 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1202 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1207 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1208 >= sizeof(cr->path)) {
1210 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1211 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1213 return got_error(GOT_ERR_NO_SPACE);
1217 srv->ncached_repos++;
1222 static const struct got_error *
1223 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1225 const struct got_error *error = NULL;
1226 struct socket *sock = c->sock;
1227 struct server *srv = c->srv;
1228 struct transport *t = c->t;
1229 struct got_repository *repo = NULL;
1233 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1234 GOTWEB_GIT_DIR) == -1)
1235 return got_error_from_errno("asprintf");
1237 dt = opendir(dir_test);
1241 repo_dir->path = dir_test;
1246 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1247 repo_dir->name) == -1)
1248 return got_error_from_errno("asprintf");
1250 dt = opendir(dir_test);
1252 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1255 repo_dir->path = dir_test;
1260 if (srv->respect_exportok &&
1261 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1262 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1266 repo = find_cached_repo(srv, repo_dir->path);
1268 error = cache_repo(&repo, srv, repo_dir, sock);
1273 error = gotweb_get_repo_description(&repo_dir->description, srv,
1274 repo_dir->path, dirfd(dt));
1277 error = got_get_repo_owner(&repo_dir->owner, c);
1280 error = got_get_repo_age(&repo_dir->age, c, NULL);
1283 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1287 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1288 error = got_error_from_errno("closedir");
1292 static const struct got_error *
1293 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1295 const struct got_error *error;
1297 *repo_dir = calloc(1, sizeof(**repo_dir));
1298 if (*repo_dir == NULL)
1299 return got_error_from_errno("calloc");
1301 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1302 error = got_error_from_errno("asprintf");
1307 (*repo_dir)->owner = NULL;
1308 (*repo_dir)->description = NULL;
1309 (*repo_dir)->url = NULL;
1310 (*repo_dir)->path = NULL;
1315 static const struct got_error *
1316 gotweb_get_repo_description(char **description, struct server *srv,
1317 const char *dirpath, int dir)
1319 const struct got_error *error = NULL;
1324 *description = NULL;
1325 if (srv->show_repo_description == 0)
1328 fd = openat(dir, "description", O_RDONLY);
1330 if (errno != ENOENT && errno != EACCES) {
1331 error = got_error_from_errno_fmt("openat %s/%s",
1332 dirpath, "description");
1337 if (fstat(fd, &sb) == -1) {
1338 error = got_error_from_errno_fmt("fstat %s/%s",
1339 dirpath, "description");
1344 if (len > GOTWEBD_MAXDESCRSZ - 1)
1345 len = GOTWEBD_MAXDESCRSZ - 1;
1347 *description = calloc(len + 1, sizeof(**description));
1348 if (*description == NULL) {
1349 error = got_error_from_errno("calloc");
1353 if (read(fd, *description, len) == -1)
1354 error = got_error_from_errno("read");
1356 if (fd != -1 && close(fd) == -1 && error == NULL)
1357 error = got_error_from_errno("close");
1361 static const struct got_error *
1362 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1365 const struct got_error *error = NULL;
1371 if (srv->show_repo_cloneurl == 0)
1374 fd = openat(dir, "cloneurl", O_RDONLY);
1376 if (errno != ENOENT && errno != EACCES) {
1377 error = got_error_from_errno_fmt("openat %s/%s",
1378 dirpath, "cloneurl");
1383 if (fstat(fd, &sb) == -1) {
1384 error = got_error_from_errno_fmt("fstat %s/%s",
1385 dirpath, "cloneurl");
1390 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1391 len = GOTWEBD_MAXCLONEURLSZ - 1;
1393 *url = calloc(len + 1, sizeof(**url));
1395 error = got_error_from_errno("calloc");
1399 if (read(fd, *url, len) == -1)
1400 error = got_error_from_errno("read");
1402 if (fd != -1 && close(fd) == -1 && error == NULL)
1403 error = got_error_from_errno("close");
1408 gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1410 struct request *c = tp->tp_arg;
1412 long long diff_time;
1413 const char *years = "years ago", *months = "months ago";
1414 const char *weeks = "weeks ago", *days = "days ago";
1415 const char *hours = "hours ago", *minutes = "minutes ago";
1416 const char *seconds = "seconds ago", *now = "right now";
1423 diff_time = time(NULL) - committer_time;
1424 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1425 if (fcgi_printf(c, "%lld %s",
1426 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1428 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1429 if (fcgi_printf(c, "%lld %s",
1430 (diff_time / 60 / 60 / 24 / (365 / 12)),
1433 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1434 if (fcgi_printf(c, "%lld %s",
1435 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1437 } else if (diff_time > 60 * 60 * 24 * 2) {
1438 if (fcgi_printf(c, "%lld %s",
1439 (diff_time / 60 / 60 / 24), days) == -1)
1441 } else if (diff_time > 60 * 60 * 2) {
1442 if (fcgi_printf(c, "%lld %s",
1443 (diff_time / 60 / 60), hours) == -1)
1445 } else if (diff_time > 60 * 2) {
1446 if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1449 } else if (diff_time > 2) {
1450 if (fcgi_printf(c, "%lld %s", diff_time,
1454 if (fcgi_puts(tp, now) == -1)
1459 if (gmtime_r(&committer_time, &tm) == NULL)
1462 s = asctime_r(&tm, datebuf);
1466 if (fcgi_puts(tp, datebuf) == -1 ||
1467 fcgi_puts(tp, " UTC") == -1)
1471 if (gmtime_r(&committer_time, &tm) == NULL)
1474 r = strftime(datebuf, sizeof(datebuf),
1475 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1479 if (fcgi_puts(tp, datebuf) == -1)