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"
56 static const struct querystring_keys querystring_keys[] = {
61 { "headref", HEADREF },
62 { "index_page", INDEX_PAGE },
67 static const struct action_keys action_keys[] = {
70 { "blobraw", BLOBRAW },
72 { "commits", COMMITS },
76 { "summary", SUMMARY },
83 static const struct got_error *gotweb_init_querystring(struct querystring **);
84 static const struct got_error *gotweb_parse_querystring(struct querystring **,
86 static const struct got_error *gotweb_assign_querystring(struct querystring **,
88 static const struct got_error *gotweb_render_index(struct request *);
89 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
91 static const struct got_error *gotweb_load_got_path(struct request *c,
93 static const struct got_error *gotweb_get_repo_description(char **,
94 struct server *, const char *, int);
95 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
98 static void gotweb_free_querystring(struct querystring *);
99 static void gotweb_free_repo_dir(struct repo_dir *);
101 struct server *gotweb_get_server(uint8_t *, uint8_t *);
104 gotweb_reply(struct request *c, int status, const char *ctype,
105 struct gotweb_url *location)
109 if (status != 200 && fcgi_printf(c, "Status: %d\r\n", status) == -1)
113 if (fcgi_puts(c->tp, "Location: ") == -1 ||
114 gotweb_render_url(c, location) == -1 ||
115 fcgi_puts(c->tp, "\r\n") == -1)
119 csp = "Content-Security-Policy: default-src 'self'; "
120 "script-src 'none'; object-src 'none';\r\n";
121 if (fcgi_puts(c->tp, csp) == -1)
124 if (ctype && fcgi_printf(c, "Content-Type: %s\r\n", ctype) == -1)
127 return fcgi_puts(c->tp, "\r\n");
131 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
136 r = fcgi_printf(c, "Content-Disposition: attachment; "
137 "filename=%s%s\r\n", file, suffix ? suffix : "");
140 return gotweb_reply(c, 200, ctype, NULL);
144 gotweb_process_request(struct request *c)
146 const struct got_error *error = NULL, *error2 = NULL;
147 struct got_blob_object *blob = NULL;
148 struct server *srv = NULL;
149 struct querystring *qs = NULL;
150 struct repo_dir *repo_dir = NULL;
151 struct got_reflist_head refs;
153 uint8_t err[] = "gotwebd experienced an error: ";
154 int r, html = 0, fd = -1;
158 /* init the transport */
159 error = gotweb_init_transport(&c->t);
161 log_warnx("%s: %s", __func__, error->msg);
164 /* don't process any further if client disconnected */
165 if (c->sock->client_status == CLIENT_DISCONNECT)
167 /* get the gotwebd server */
168 srv = gotweb_get_server(c->server_name, c->http_host);
170 log_warnx("%s: error server is NULL", __func__);
174 /* parse our querystring */
175 error = gotweb_init_querystring(&qs);
177 log_warnx("%s: %s", __func__, error->msg);
181 error = gotweb_parse_querystring(&qs, c->querystring);
183 log_warnx("%s: %s", __func__, error->msg);
188 * certain actions require a commit id in the querystring. this stops
189 * bad actors from exploiting this by manually manipulating the
193 if (qs->action == BLAME || qs->action == BLOB ||
194 qs->action == BLOBRAW || qs->action == DIFF) {
195 if (qs->commit == NULL) {
196 error2 = got_error(GOT_ERR_QUERYSTRING);
201 if (qs->action != INDEX) {
202 error = gotweb_init_repo_dir(&repo_dir, qs->path);
205 error = gotweb_load_got_path(c, repo_dir);
206 c->t->repo_dir = repo_dir;
207 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
211 if (qs->action == BLOBRAW) {
216 error = got_get_repo_commits(c, 1);
220 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
225 r = gotweb_reply_file(c, "application/octet-stream",
228 r = gotweb_reply(c, 200, "text/plain", NULL);
233 error = got_object_blob_read_block(&len, blob);
238 buf = got_object_blob_get_read_buf(blob);
239 if (fcgi_gen_binary_response(c, buf, len) == -1)
246 if (qs->action == BLOB) {
248 struct gotweb_url url = {
253 .commit = qs->commit,
254 .folder = qs->folder,
258 error = got_get_repo_commits(c, 1);
262 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
266 gotweb_reply(c, 302, NULL, &url);
271 if (qs->action == RSS) {
272 const char *ctype = "application/rss+xml;charset=utf-8";
274 if (gotweb_reply_file(c, ctype, repo_dir->name, ".rss") == -1)
277 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
279 log_warnx("%s: %s", __func__, error->msg);
282 if (gotweb_render_rss(c->tp) == -1)
288 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
292 if (gotweb_render_header(c->tp) == -1)
302 error = got_get_repo_commits(c, 1);
304 log_warnx("%s: %s", __func__, error->msg);
307 if (gotweb_render_blame(c->tp) == -1)
311 if (gotweb_render_blob(c->tp, blob) == -1)
315 if (gotweb_render_briefs(c->tp) == -1)
319 error = got_get_repo_commits(c, srv->max_commits_display);
321 log_warnx("%s: %s", __func__, error->msg);
324 if (gotweb_render_commits(c->tp) == -1)
328 error = got_get_repo_commits(c, 1);
330 log_warnx("%s: %s", __func__, error->msg);
333 error = got_open_diff_for_output(&fp, &fd, c);
335 log_warnx("%s: %s", __func__, error->msg);
338 if (gotweb_render_diff(c->tp, fp) == -1)
342 error = gotweb_render_index(c);
344 log_warnx("%s: %s", __func__, error->msg);
349 error = got_ref_list(&refs, c->t->repo, "refs/heads",
350 got_ref_cmp_by_name, NULL);
352 log_warnx("%s: got_ref_list: %s", __func__,
357 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
359 log_warnx("%s: got_get_repo_tags: %s", __func__,
363 qs->action = SUMMARY;
364 if (gotweb_render_summary(c->tp, &refs) == -1)
368 error = got_get_repo_tags(c, 1);
370 log_warnx("%s: %s", __func__, error->msg);
373 if (c->t->tag_count == 0) {
374 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
378 if (gotweb_render_tag(c->tp) == -1)
382 error = got_get_repo_tags(c, srv->max_commits_display);
384 log_warnx("%s: %s", __func__, error->msg);
387 if (gotweb_render_tags(c->tp) == -1)
391 error = got_get_repo_commits(c, 1);
393 log_warnx("%s: %s", __func__, error->msg);
396 if (gotweb_render_tree(c->tp) == -1)
401 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
402 "Erorr: Bad Querystring");
410 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
412 if (fcgi_printf(c, "\n%s", err) == -1)
415 if (fcgi_printf(c, "%s", error->msg) == -1)
418 if (fcgi_printf(c, "see daemon logs for details") == -1)
421 if (html && fcgi_printf(c, "</div>\n") == -1)
425 got_object_blob_close(blob);
427 error = got_gotweb_flushfile(fp, fd);
429 log_warnx("%s: got_gotweb_flushfile failure: %s",
430 __func__, error->msg);
435 if (html && srv != NULL)
436 gotweb_render_footer(c->tp);
438 got_ref_list_free(&refs);
442 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
444 struct server *srv = NULL;
446 /* check against the server name first */
447 if (strlen(server_name) > 0)
448 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
449 if (strcmp(srv->name, server_name) == 0)
452 /* check against subdomain second */
453 if (strlen(subdomain) > 0)
454 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
455 if (strcmp(srv->name, subdomain) == 0)
458 /* if those fail, send first server */
459 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
466 const struct got_error *
467 gotweb_init_transport(struct transport **t)
469 const struct got_error *error = NULL;
471 *t = calloc(1, sizeof(**t));
473 return got_error_from_errno2("%s: calloc", __func__);
475 TAILQ_INIT(&(*t)->repo_commits);
476 TAILQ_INIT(&(*t)->repo_tags);
479 (*t)->repo_dir = NULL;
481 (*t)->next_id = NULL;
482 (*t)->prev_id = NULL;
489 static const struct got_error *
490 gotweb_init_querystring(struct querystring **qs)
492 const struct got_error *error = NULL;
494 *qs = calloc(1, sizeof(**qs));
496 return got_error_from_errno2("%s: calloc", __func__);
498 (*qs)->headref = strdup("HEAD");
499 if ((*qs)->headref == NULL) {
502 return got_error_from_errno2("%s: strdup", __func__);
505 (*qs)->action = INDEX;
506 (*qs)->commit = NULL;
508 (*qs)->folder = NULL;
509 (*qs)->index_page = 0;
515 static const struct got_error *
516 gotweb_parse_querystring(struct querystring **qs, char *qst)
518 const struct got_error *error = NULL;
519 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
520 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
527 return got_error_from_errno2("%s: strdup", __func__);
532 while (tok1_pair != NULL) {
533 strsep(&tok1_end, "&");
535 tok2 = strdup(tok1_pair);
538 return got_error_from_errno2("%s: strdup", __func__);
544 while (tok2_pair != NULL) {
545 strsep(&tok2_end, "=");
547 error = gotweb_assign_querystring(qs, tok2_pair,
552 tok2_pair = tok2_end;
555 tok1_pair = tok1_end;
566 * Adapted from usr.sbin/httpd/httpd.c url_decode.
568 static const struct got_error *
569 gotweb_urldecode(char *url)
581 /* Encoding character is followed by two hex chars */
582 if (!isxdigit((unsigned char)p[1]) ||
583 !isxdigit((unsigned char)p[2]) ||
584 (p[1] == '0' && p[2] == '0'))
585 return got_error(GOT_ERR_BAD_QUERYSTRING);
591 * We don't have to validate "hex" because it is
592 * guaranteed to include two hex chars followed by nul.
594 x = strtoul(hex, NULL, 16);
610 static const struct got_error *
611 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
613 const struct got_error *error = NULL;
617 error = gotweb_urldecode(value);
621 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
622 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
625 switch (querystring_keys[el_cnt].element) {
627 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
628 if (strcmp(value, action_keys[a_cnt].name) != 0)
630 else if (strcmp(value,
631 action_keys[a_cnt].name) == 0){
633 action_keys[a_cnt].action;
641 (*qs)->commit = strdup(value);
642 if ((*qs)->commit == NULL) {
643 error = got_error_from_errno2("%s: strdup",
649 (*qs)->file = strdup(value);
650 if ((*qs)->file == NULL) {
651 error = got_error_from_errno2("%s: strdup",
657 (*qs)->folder = strdup(value);
658 if ((*qs)->folder == NULL) {
659 error = got_error_from_errno2("%s: strdup",
665 free((*qs)->headref);
666 (*qs)->headref = strdup(value);
667 if ((*qs)->headref == NULL) {
668 error = got_error_from_errno2("%s: strdup",
674 if (strlen(value) == 0)
676 (*qs)->index_page = strtonum(value, INT64_MIN,
679 error = got_error_from_errno3("%s: strtonum %s",
683 if ((*qs)->index_page < 0)
684 (*qs)->index_page = 0;
687 (*qs)->path = strdup(value);
688 if ((*qs)->path == NULL) {
689 error = got_error_from_errno2("%s: strdup",
695 if (strlen(value) == 0)
697 (*qs)->page = strtonum(value, INT64_MIN,
700 error = got_error_from_errno3("%s: strtonum %s",
716 gotweb_free_repo_tag(struct repo_tag *rt)
721 free(rt->tag_commit);
722 free(rt->commit_msg);
729 gotweb_free_repo_commit(struct repo_commit *rc)
739 free(rc->commit_msg);
745 gotweb_free_querystring(struct querystring *qs)
758 gotweb_free_repo_dir(struct repo_dir *repo_dir)
760 if (repo_dir != NULL) {
761 free(repo_dir->name);
762 free(repo_dir->owner);
763 free(repo_dir->description);
765 free(repo_dir->path);
771 gotweb_free_transport(struct transport *t)
773 struct repo_commit *rc = NULL, *trc = NULL;
774 struct repo_tag *rt = NULL, *trt = NULL;
776 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
777 TAILQ_REMOVE(&t->repo_commits, rc, entry);
778 gotweb_free_repo_commit(rc);
780 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
781 TAILQ_REMOVE(&t->repo_tags, rt, entry);
782 gotweb_free_repo_tag(rt);
784 gotweb_free_repo_dir(t->repo_dir);
785 gotweb_free_querystring(t->qs);
793 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
794 struct gotweb_url *next, int *have_next)
796 struct transport *t = c->t;
797 struct querystring *qs = t->qs;
798 struct server *srv = c->srv;
800 *have_prev = *have_next = 0;
804 if (qs->index_page > 0) {
806 *prev = (struct gotweb_url){
808 .index_page = qs->index_page - 1,
812 if (t->next_disp == srv->max_repos_display &&
813 t->repos_total != (qs->index_page + 1) *
814 srv->max_repos_display) {
816 *next = (struct gotweb_url){
818 .index_page = qs->index_page + 1,
824 if (t->prev_id && qs->commit != NULL &&
825 strcmp(qs->commit, t->prev_id) != 0) {
827 *prev = (struct gotweb_url){
830 .page = qs->page - 1,
832 .commit = t->prev_id,
833 .headref = qs->headref,
838 *next = (struct gotweb_url){
841 .page = qs->page + 1,
843 .commit = t->next_id,
844 .headref = qs->headref,
851 static const struct got_error *
852 gotweb_render_index(struct request *c)
854 const struct got_error *error = NULL;
855 struct server *srv = c->srv;
856 struct transport *t = c->t;
857 struct querystring *qs = t->qs;
858 struct repo_dir *repo_dir = NULL;
859 struct dirent **sd_dent = NULL;
860 unsigned int d_cnt, d_i, d_disp = 0;
861 unsigned int d_skipped = 0;
864 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
867 error = got_error_from_errno2("scandir", srv->repos_path);
871 if (gotweb_render_repo_table_hdr(c->tp) == -1)
874 for (d_i = 0; d_i < d_cnt; d_i++) {
875 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
878 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
879 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
884 error = got_path_dirent_type(&type, srv->repos_path,
888 if (type != DT_DIR) {
893 if (qs->index_page > 0 && (qs->index_page *
894 srv->max_repos_display) > t->prev_disp) {
899 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
903 error = gotweb_load_got_path(c, repo_dir);
904 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
906 gotweb_free_repo_dir(repo_dir);
911 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
917 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
920 gotweb_free_repo_dir(repo_dir);
923 if (d_disp == srv->max_repos_display)
926 t->repos_total = d_cnt - d_skipped;
928 if (srv->max_repos_display == 0)
930 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
932 if (t->repos_total <= srv->max_repos ||
933 t->repos_total <= srv->max_repos_display)
936 if (gotweb_render_navs(c->tp) == -1)
940 for (d_i = 0; d_i < d_cnt; d_i++)
948 should_urlencode(int c)
950 if (c <= ' ' || c >= 127)
974 /* needed because the URLs are embedded into the HTML */
983 gotweb_urlencode(const char *str)
991 for (s = str; *s; ++s) {
993 if (should_urlencode(*s))
997 escaped = calloc(1, len + 1);
1002 for (s = str; *s; ++s) {
1003 if (should_urlencode(*s)) {
1004 a = (*s & 0xF0) >> 4;
1008 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1009 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1018 gotweb_action_name(int action)
1053 gotweb_render_url(struct request *c, struct gotweb_url *url)
1055 const char *sep = "?", *action;
1059 action = gotweb_action_name(url->action);
1060 if (action != NULL) {
1061 if (fcgi_printf(c, "?action=%s", action) == -1)
1067 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1073 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1079 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1085 tmp = gotweb_urlencode(url->file);
1088 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1096 tmp = gotweb_urlencode(url->folder);
1099 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1107 tmp = gotweb_urlencode(url->headref);
1110 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1117 if (url->index_page != -1) {
1118 if (fcgi_printf(c, "%sindex_page=%d", sep,
1119 url->index_page) == -1)
1125 tmp = gotweb_urlencode(url->path);
1128 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1135 if (url->page != -1) {
1136 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1145 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1147 struct template *tp = c->tp;
1148 const char *proto = c->https ? "https" : "http";
1150 if (fcgi_puts(tp, proto) == -1 ||
1151 fcgi_puts(tp, "://") == -1 ||
1152 tp_htmlescape(tp, c->server_name) == -1 ||
1153 tp_htmlescape(tp, c->document_uri) == -1)
1156 return gotweb_render_url(c, url);
1159 static struct got_repository *
1160 find_cached_repo(struct server *srv, const char *path)
1164 for (i = 0; i < srv->ncached_repos; i++) {
1165 if (strcmp(srv->cached_repos[i].path, path) == 0)
1166 return srv->cached_repos[i].repo;
1172 static const struct got_error *
1173 cache_repo(struct got_repository **new, struct server *srv,
1174 struct repo_dir *repo_dir, struct socket *sock)
1176 const struct got_error *error = NULL;
1177 struct got_repository *repo;
1178 struct cached_repo *cr;
1181 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1182 cr = &srv->cached_repos[srv->ncached_repos - 1];
1183 error = got_repo_close(cr->repo);
1184 memset(cr, 0, sizeof(*cr));
1185 srv->ncached_repos--;
1188 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1189 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1190 cr = &srv->cached_repos[0];
1193 cr = &srv->cached_repos[srv->ncached_repos];
1196 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1199 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1200 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1205 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1206 >= sizeof(cr->path)) {
1208 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1209 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1211 return got_error(GOT_ERR_NO_SPACE);
1215 srv->ncached_repos++;
1220 static const struct got_error *
1221 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1223 const struct got_error *error = NULL;
1224 struct socket *sock = c->sock;
1225 struct server *srv = c->srv;
1226 struct transport *t = c->t;
1227 struct got_repository *repo = NULL;
1231 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1232 GOTWEB_GIT_DIR) == -1)
1233 return got_error_from_errno("asprintf");
1235 dt = opendir(dir_test);
1239 repo_dir->path = dir_test;
1244 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1245 repo_dir->name) == -1)
1246 return got_error_from_errno("asprintf");
1248 dt = opendir(dir_test);
1250 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1253 repo_dir->path = dir_test;
1258 if (srv->respect_exportok &&
1259 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1260 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1264 repo = find_cached_repo(srv, repo_dir->path);
1266 error = cache_repo(&repo, srv, repo_dir, sock);
1271 error = gotweb_get_repo_description(&repo_dir->description, srv,
1272 repo_dir->path, dirfd(dt));
1275 error = got_get_repo_owner(&repo_dir->owner, c);
1278 error = got_get_repo_age(&repo_dir->age, c, NULL);
1281 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1285 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1286 error = got_error_from_errno("closedir");
1290 static const struct got_error *
1291 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1293 const struct got_error *error;
1295 *repo_dir = calloc(1, sizeof(**repo_dir));
1296 if (*repo_dir == NULL)
1297 return got_error_from_errno("calloc");
1299 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1300 error = got_error_from_errno("asprintf");
1305 (*repo_dir)->owner = NULL;
1306 (*repo_dir)->description = NULL;
1307 (*repo_dir)->url = NULL;
1308 (*repo_dir)->path = NULL;
1313 static const struct got_error *
1314 gotweb_get_repo_description(char **description, struct server *srv,
1315 const char *dirpath, int dir)
1317 const struct got_error *error = NULL;
1322 *description = NULL;
1323 if (srv->show_repo_description == 0)
1326 fd = openat(dir, "description", O_RDONLY);
1328 if (errno != ENOENT && errno != EACCES) {
1329 error = got_error_from_errno_fmt("openat %s/%s",
1330 dirpath, "description");
1335 if (fstat(fd, &sb) == -1) {
1336 error = got_error_from_errno_fmt("fstat %s/%s",
1337 dirpath, "description");
1342 if (len > GOTWEBD_MAXDESCRSZ - 1)
1343 len = GOTWEBD_MAXDESCRSZ - 1;
1345 *description = calloc(len + 1, sizeof(**description));
1346 if (*description == NULL) {
1347 error = got_error_from_errno("calloc");
1351 if (read(fd, *description, len) == -1)
1352 error = got_error_from_errno("read");
1354 if (fd != -1 && close(fd) == -1 && error == NULL)
1355 error = got_error_from_errno("close");
1359 static const struct got_error *
1360 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1363 const struct got_error *error = NULL;
1369 if (srv->show_repo_cloneurl == 0)
1372 fd = openat(dir, "cloneurl", O_RDONLY);
1374 if (errno != ENOENT && errno != EACCES) {
1375 error = got_error_from_errno_fmt("openat %s/%s",
1376 dirpath, "cloneurl");
1381 if (fstat(fd, &sb) == -1) {
1382 error = got_error_from_errno_fmt("fstat %s/%s",
1383 dirpath, "cloneurl");
1388 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1389 len = GOTWEBD_MAXCLONEURLSZ - 1;
1391 *url = calloc(len + 1, sizeof(**url));
1393 error = got_error_from_errno("calloc");
1397 if (read(fd, *url, len) == -1)
1398 error = got_error_from_errno("read");
1400 if (fd != -1 && close(fd) == -1 && error == NULL)
1401 error = got_error_from_errno("close");
1406 gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1408 struct request *c = tp->tp_arg;
1410 long long diff_time;
1411 const char *years = "years ago", *months = "months ago";
1412 const char *weeks = "weeks ago", *days = "days ago";
1413 const char *hours = "hours ago", *minutes = "minutes ago";
1414 const char *seconds = "seconds ago", *now = "right now";
1421 diff_time = time(NULL) - committer_time;
1422 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1423 if (fcgi_printf(c, "%lld %s",
1424 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1426 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1427 if (fcgi_printf(c, "%lld %s",
1428 (diff_time / 60 / 60 / 24 / (365 / 12)),
1431 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1432 if (fcgi_printf(c, "%lld %s",
1433 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1435 } else if (diff_time > 60 * 60 * 24 * 2) {
1436 if (fcgi_printf(c, "%lld %s",
1437 (diff_time / 60 / 60 / 24), days) == -1)
1439 } else if (diff_time > 60 * 60 * 2) {
1440 if (fcgi_printf(c, "%lld %s",
1441 (diff_time / 60 / 60), hours) == -1)
1443 } else if (diff_time > 60 * 2) {
1444 if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1447 } else if (diff_time > 2) {
1448 if (fcgi_printf(c, "%lld %s", diff_time,
1452 if (fcgi_puts(tp, now) == -1)
1457 if (gmtime_r(&committer_time, &tm) == NULL)
1460 s = asctime_r(&tm, datebuf);
1464 if (fcgi_puts(tp, datebuf) == -1 ||
1465 fcgi_puts(tp, " UTC") == -1)
1469 if (gmtime_r(&committer_time, &tm) == NULL)
1472 r = strftime(datebuf, sizeof(datebuf),
1473 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1477 if (fcgi_puts(tp, datebuf) == -1)