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.
20 #include "got_compat.h"
23 #include <netinet/in.h>
24 #include <sys/queue.h>
26 #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"
55 static const struct querystring_keys querystring_keys[] = {
60 { "headref", HEADREF },
61 { "index_page", INDEX_PAGE },
66 static const struct action_keys action_keys[] = {
69 { "blobraw", BLOBRAW },
71 { "commits", COMMITS },
75 { "summary", SUMMARY },
82 static const struct got_error *gotweb_init_querystring(struct querystring **);
83 static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 static int gotweb_render_index(struct template *);
88 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 static const struct got_error *gotweb_load_got_path(struct request *c,
92 static const struct got_error *gotweb_get_repo_description(char **,
93 struct server *, const char *, int);
94 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
97 static void gotweb_free_querystring(struct querystring *);
98 static void gotweb_free_repo_dir(struct repo_dir *);
100 struct server *gotweb_get_server(const char *);
103 gotweb_reply(struct request *c, int status, const char *ctype,
104 struct gotweb_url *location)
108 if (status != 200 && fcgi_printf(c, "Status: %d\r\n", status) == -1)
112 if (fcgi_puts(c->tp, "Location: ") == -1 ||
113 gotweb_render_url(c, location) == -1 ||
114 fcgi_puts(c->tp, "\r\n") == -1)
118 csp = "Content-Security-Policy: default-src 'self'; "
119 "script-src 'none'; object-src 'none';\r\n";
120 if (fcgi_puts(c->tp, csp) == -1)
123 if (ctype && fcgi_printf(c, "Content-Type: %s\r\n", ctype) == -1)
126 return fcgi_puts(c->tp, "\r\n");
130 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
135 r = fcgi_printf(c, "Content-Disposition: attachment; "
136 "filename=%s%s\r\n", file, suffix ? suffix : "");
139 return gotweb_reply(c, 200, ctype, NULL);
143 gotweb_process_request(struct request *c)
145 const struct got_error *error = NULL;
146 struct server *srv = NULL;
147 struct querystring *qs = NULL;
148 struct repo_dir *repo_dir = NULL;
149 const char *rss_ctype = "application/rss+xml;charset=utf-8";
154 /* init the transport */
155 error = gotweb_init_transport(&c->t);
157 log_warnx("%s: %s", __func__, error->msg);
160 /* don't process any further if client disconnected */
161 if (c->sock->client_status == CLIENT_DISCONNECT)
163 /* get the gotwebd server */
164 srv = gotweb_get_server(c->server_name);
166 log_warnx("%s: error server is NULL", __func__);
170 /* parse our querystring */
171 error = gotweb_init_querystring(&qs);
173 log_warnx("%s: %s", __func__, error->msg);
177 error = gotweb_parse_querystring(&qs, c->querystring);
179 log_warnx("%s: %s", __func__, error->msg);
184 * certain actions require a commit id in the querystring. this stops
185 * bad actors from exploiting this by manually manipulating the
189 if (qs->action == BLAME || qs->action == BLOB ||
190 qs->action == BLOBRAW || qs->action == DIFF) {
191 if (qs->commit == NULL) {
192 error = got_error(GOT_ERR_BAD_QUERYSTRING);
197 if (qs->action != INDEX) {
198 error = gotweb_init_repo_dir(&repo_dir, qs->path);
201 error = gotweb_load_got_path(c, repo_dir);
202 c->t->repo_dir = repo_dir;
203 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
207 if (qs->action == BLOBRAW || qs->action == BLOB) {
208 error = got_get_repo_commits(c, 1);
212 error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
220 error = got_get_repo_commits(c, 1);
222 log_warnx("%s: %s", __func__, error->msg);
225 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
227 gotweb_render_page(c->tp, gotweb_render_blame);
231 struct gotweb_url url = {
236 .commit = qs->commit,
237 .folder = qs->folder,
241 gotweb_reply(c, 302, NULL, &url);
245 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
247 gotweb_render_page(c->tp, gotweb_render_blob);
251 r = gotweb_reply_file(c, "application/octet-stream",
254 r = gotweb_reply(c, 200, "text/plain", NULL);
259 error = got_object_blob_read_block(&len, c->t->blob);
264 buf = got_object_blob_get_read_buf(c->t->blob);
265 if (fcgi_gen_binary_response(c, buf, len) == -1)
270 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
272 gotweb_render_page(c->tp, gotweb_render_briefs);
275 error = got_get_repo_commits(c, srv->max_commits_display);
277 log_warnx("%s: %s", __func__, error->msg);
280 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
282 gotweb_render_page(c->tp, gotweb_render_commits);
285 error = got_get_repo_commits(c, 1);
287 log_warnx("%s: %s", __func__, error->msg);
290 error = got_open_diff_for_output(&c->t->fp, c);
292 log_warnx("%s: %s", __func__, error->msg);
295 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
297 gotweb_render_page(c->tp, gotweb_render_diff);
300 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
302 if (c->t->nrepos == -1) {
304 error = got_error_from_errno2("scandir",
308 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
310 gotweb_render_page(c->tp, gotweb_render_index);
313 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
316 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
319 gotweb_render_rss(c->tp);
322 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
323 got_ref_cmp_by_name, NULL);
325 log_warnx("%s: got_ref_list: %s", __func__,
330 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
332 log_warnx("%s: got_get_repo_tags: %s", __func__,
336 qs->action = SUMMARY;
337 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
339 gotweb_render_page(c->tp, gotweb_render_summary);
342 error = got_get_repo_tags(c, 1);
344 log_warnx("%s: %s", __func__, error->msg);
347 if (c->t->tag_count == 0) {
348 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
352 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
354 gotweb_render_page(c->tp, gotweb_render_tag);
357 error = got_get_repo_tags(c, srv->max_commits_display);
359 log_warnx("%s: %s", __func__, error->msg);
362 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
364 gotweb_render_page(c->tp, gotweb_render_tags);
367 error = got_get_repo_commits(c, 1);
369 log_warnx("%s: %s", __func__, error->msg);
372 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
374 gotweb_render_page(c->tp, gotweb_render_tree);
378 error = got_error(GOT_ERR_BAD_QUERYSTRING);
383 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
385 gotweb_render_page(c->tp, gotweb_render_error);
389 gotweb_get_server(const char *server_name)
393 /* check against the server name first */
394 if (*server_name != '\0')
395 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
396 if (strcmp(srv->name, server_name) == 0)
399 /* otherwise, use the first server */
400 return TAILQ_FIRST(&gotwebd_env->servers);
403 const struct got_error *
404 gotweb_init_transport(struct transport **t)
406 const struct got_error *error = NULL;
408 *t = calloc(1, sizeof(**t));
410 return got_error_from_errno2(__func__, "calloc");
412 TAILQ_INIT(&(*t)->repo_commits);
413 TAILQ_INIT(&(*t)->repo_tags);
414 TAILQ_INIT(&(*t)->refs);
421 static const struct got_error *
422 gotweb_init_querystring(struct querystring **qs)
424 const struct got_error *error = NULL;
426 *qs = calloc(1, sizeof(**qs));
428 return got_error_from_errno2(__func__, "calloc");
430 (*qs)->headref = strdup("HEAD");
431 if ((*qs)->headref == NULL) {
434 return got_error_from_errno2(__func__, "strdup");
437 (*qs)->action = INDEX;
438 (*qs)->commit = NULL;
440 (*qs)->folder = NULL;
441 (*qs)->index_page = 0;
447 static const struct got_error *
448 gotweb_parse_querystring(struct querystring **qs, char *qst)
450 const struct got_error *error = NULL;
451 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
452 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
459 return got_error_from_errno2(__func__, "strdup");
464 while (tok1_pair != NULL) {
465 strsep(&tok1_end, "&");
467 tok2 = strdup(tok1_pair);
470 return got_error_from_errno2(__func__, "strdup");
476 while (tok2_pair != NULL) {
477 strsep(&tok2_end, "=");
479 error = gotweb_assign_querystring(qs, tok2_pair,
484 tok2_pair = tok2_end;
487 tok1_pair = tok1_end;
498 * Adapted from usr.sbin/httpd/httpd.c url_decode.
500 static const struct got_error *
501 gotweb_urldecode(char *url)
513 /* Encoding character is followed by two hex chars */
514 if (!isxdigit((unsigned char)p[1]) ||
515 !isxdigit((unsigned char)p[2]) ||
516 (p[1] == '0' && p[2] == '0'))
517 return got_error(GOT_ERR_BAD_QUERYSTRING);
523 * We don't have to validate "hex" because it is
524 * guaranteed to include two hex chars followed by nul.
526 x = strtoul(hex, NULL, 16);
542 static const struct got_error *
543 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
545 const struct got_error *error = NULL;
549 error = gotweb_urldecode(value);
553 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
554 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
557 switch (querystring_keys[el_cnt].element) {
559 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
560 if (strcmp(value, action_keys[a_cnt].name) != 0)
562 else if (strcmp(value,
563 action_keys[a_cnt].name) == 0){
565 action_keys[a_cnt].action;
573 (*qs)->commit = strdup(value);
574 if ((*qs)->commit == NULL) {
575 error = got_error_from_errno2(__func__,
581 (*qs)->file = strdup(value);
582 if ((*qs)->file == NULL) {
583 error = got_error_from_errno2(__func__,
589 (*qs)->folder = strdup(value);
590 if ((*qs)->folder == NULL) {
591 error = got_error_from_errno2(__func__,
597 free((*qs)->headref);
598 (*qs)->headref = strdup(value);
599 if ((*qs)->headref == NULL) {
600 error = got_error_from_errno2(__func__,
608 (*qs)->index_page = strtonum(value, INT64_MIN,
611 error = got_error_from_errno3(__func__,
615 if ((*qs)->index_page < 0)
616 (*qs)->index_page = 0;
619 (*qs)->path = strdup(value);
620 if ((*qs)->path == NULL) {
621 error = got_error_from_errno2(__func__,
629 (*qs)->page = strtonum(value, INT64_MIN,
632 error = got_error_from_errno3(__func__,
648 gotweb_free_repo_tag(struct repo_tag *rt)
653 free(rt->tag_commit);
654 free(rt->commit_msg);
661 gotweb_free_repo_commit(struct repo_commit *rc)
671 free(rc->commit_msg);
677 gotweb_free_querystring(struct querystring *qs)
690 gotweb_free_repo_dir(struct repo_dir *repo_dir)
692 if (repo_dir != NULL) {
693 free(repo_dir->name);
694 free(repo_dir->owner);
695 free(repo_dir->description);
697 free(repo_dir->path);
703 gotweb_free_transport(struct transport *t)
705 const struct got_error *err;
706 struct repo_commit *rc = NULL, *trc = NULL;
707 struct repo_tag *rt = NULL, *trt = NULL;
710 got_ref_list_free(&t->refs);
711 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
712 TAILQ_REMOVE(&t->repo_commits, rc, entry);
713 gotweb_free_repo_commit(rc);
715 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
716 TAILQ_REMOVE(&t->repo_tags, rt, entry);
717 gotweb_free_repo_tag(rt);
719 gotweb_free_repo_dir(t->repo_dir);
720 gotweb_free_querystring(t->qs);
725 got_object_blob_close(t->blob);
727 err = got_gotweb_closefile(t->fp);
729 log_warnx("%s: got_gotweb_closefile failure: %s",
732 if (t->fd != -1 && close(t->fd) == -1)
733 log_warn("%s: close", __func__);
735 for (i = 0; i < t->nrepos; ++i)
743 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
744 struct gotweb_url *next, int *have_next)
746 struct transport *t = c->t;
747 struct querystring *qs = t->qs;
748 struct server *srv = c->srv;
750 *have_prev = *have_next = 0;
754 if (qs->index_page > 0) {
756 *prev = (struct gotweb_url){
758 .index_page = qs->index_page - 1,
762 if (t->next_disp == srv->max_repos_display &&
763 t->repos_total != (qs->index_page + 1) *
764 srv->max_repos_display) {
766 *next = (struct gotweb_url){
768 .index_page = qs->index_page + 1,
774 if (t->prev_id && qs->commit != NULL &&
775 strcmp(qs->commit, t->prev_id) != 0) {
777 *prev = (struct gotweb_url){
780 .page = qs->page - 1,
782 .commit = t->prev_id,
783 .headref = qs->headref,
788 *next = (struct gotweb_url){
791 .page = qs->page + 1,
793 .commit = t->next_id,
794 .headref = qs->headref,
802 gotweb_render_index(struct template *tp)
804 const struct got_error *error = NULL;
805 struct request *c = tp->tp_arg;
806 struct server *srv = c->srv;
807 struct transport *t = c->t;
808 struct querystring *qs = t->qs;
809 struct repo_dir *repo_dir = NULL;
810 struct dirent **sd_dent = t->repos;
811 unsigned int d_i, d_disp = 0;
812 unsigned int d_skipped = 0;
815 if (gotweb_render_repo_table_hdr(c->tp) == -1)
818 for (d_i = 0; d_i < t->nrepos; d_i++) {
819 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
822 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
823 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
828 error = got_path_dirent_type(&type, srv->repos_path,
832 if (type != DT_DIR) {
837 if (qs->index_page > 0 && (qs->index_page *
838 srv->max_repos_display) > t->prev_disp) {
843 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
847 error = gotweb_load_got_path(c, repo_dir);
848 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
849 if (error->code != GOT_ERR_NOT_GIT_REPO)
850 log_warnx("%s: %s: %s", __func__,
851 sd_dent[d_i]->d_name, error->msg);
852 gotweb_free_repo_dir(repo_dir);
861 r = gotweb_render_repo_fragment(c->tp, repo_dir);
862 gotweb_free_repo_dir(repo_dir);
867 if (d_disp == srv->max_repos_display)
870 t->repos_total = t->nrepos - d_skipped;
872 if (srv->max_repos_display == 0)
874 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
876 if (t->repos_total <= srv->max_repos ||
877 t->repos_total <= srv->max_repos_display)
880 if (gotweb_render_navs(c->tp) == -1)
887 should_urlencode(int c)
889 if (c <= ' ' || c >= 127)
913 /* needed because the URLs are embedded into the HTML */
922 gotweb_urlencode(const char *str)
930 for (s = str; *s; ++s) {
932 if (should_urlencode(*s))
936 escaped = calloc(1, len + 1);
941 for (s = str; *s; ++s) {
942 if (should_urlencode(*s)) {
943 a = (*s & 0xF0) >> 4;
947 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
948 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
957 gotweb_action_name(int action)
992 gotweb_render_url(struct request *c, struct gotweb_url *url)
994 const char *sep = "?", *action;
998 action = gotweb_action_name(url->action);
999 if (action != NULL) {
1000 if (fcgi_printf(c, "?action=%s", action) == -1)
1006 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1012 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1018 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1024 tmp = gotweb_urlencode(url->file);
1027 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1035 tmp = gotweb_urlencode(url->folder);
1038 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1046 tmp = gotweb_urlencode(url->headref);
1049 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1056 if (url->index_page != -1) {
1057 if (fcgi_printf(c, "%sindex_page=%d", sep,
1058 url->index_page) == -1)
1064 tmp = gotweb_urlencode(url->path);
1067 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1074 if (url->page != -1) {
1075 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1084 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1086 struct template *tp = c->tp;
1087 const char *proto = c->https ? "https" : "http";
1089 if (fcgi_puts(tp, proto) == -1 ||
1090 fcgi_puts(tp, "://") == -1 ||
1091 tp_htmlescape(tp, c->server_name) == -1 ||
1092 tp_htmlescape(tp, c->document_uri) == -1)
1095 return gotweb_render_url(c, url);
1098 static struct got_repository *
1099 find_cached_repo(struct server *srv, const char *path)
1103 for (i = 0; i < srv->ncached_repos; i++) {
1104 if (strcmp(srv->cached_repos[i].path, path) == 0)
1105 return srv->cached_repos[i].repo;
1111 static const struct got_error *
1112 cache_repo(struct got_repository **new, struct server *srv,
1113 struct repo_dir *repo_dir, struct socket *sock)
1115 const struct got_error *error = NULL;
1116 struct got_repository *repo;
1117 struct cached_repo *cr;
1120 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1121 cr = &srv->cached_repos[srv->ncached_repos - 1];
1122 error = got_repo_close(cr->repo);
1123 memset(cr, 0, sizeof(*cr));
1124 srv->ncached_repos--;
1127 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1128 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1129 cr = &srv->cached_repos[0];
1132 cr = &srv->cached_repos[srv->ncached_repos];
1135 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1138 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1139 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1144 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1145 >= sizeof(cr->path)) {
1147 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1148 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1150 return got_error(GOT_ERR_NO_SPACE);
1154 srv->ncached_repos++;
1159 static const struct got_error *
1160 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1162 const struct got_error *error = NULL;
1163 struct socket *sock = c->sock;
1164 struct server *srv = c->srv;
1165 struct transport *t = c->t;
1166 struct got_repository *repo = NULL;
1170 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1171 GOTWEB_GIT_DIR) == -1)
1172 return got_error_from_errno("asprintf");
1174 dt = opendir(dir_test);
1178 repo_dir->path = dir_test;
1183 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1184 repo_dir->name) == -1)
1185 return got_error_from_errno("asprintf");
1187 dt = opendir(dir_test);
1189 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1192 repo_dir->path = dir_test;
1197 if (srv->respect_exportok &&
1198 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1199 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1203 repo = find_cached_repo(srv, repo_dir->path);
1205 error = cache_repo(&repo, srv, repo_dir, sock);
1210 error = gotweb_get_repo_description(&repo_dir->description, srv,
1211 repo_dir->path, dirfd(dt));
1214 error = got_get_repo_owner(&repo_dir->owner, c);
1217 error = got_get_repo_age(&repo_dir->age, c, NULL);
1220 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1224 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1225 error = got_error_from_errno("closedir");
1229 static const struct got_error *
1230 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1232 const struct got_error *error;
1234 *repo_dir = calloc(1, sizeof(**repo_dir));
1235 if (*repo_dir == NULL)
1236 return got_error_from_errno("calloc");
1238 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1239 error = got_error_from_errno("asprintf");
1244 (*repo_dir)->owner = NULL;
1245 (*repo_dir)->description = NULL;
1246 (*repo_dir)->url = NULL;
1247 (*repo_dir)->path = NULL;
1252 static const struct got_error *
1253 gotweb_get_repo_description(char **description, struct server *srv,
1254 const char *dirpath, int dir)
1256 const struct got_error *error = NULL;
1261 *description = NULL;
1262 if (srv->show_repo_description == 0)
1265 fd = openat(dir, "description", O_RDONLY);
1267 if (errno != ENOENT && errno != EACCES) {
1268 error = got_error_from_errno_fmt("openat %s/%s",
1269 dirpath, "description");
1274 if (fstat(fd, &sb) == -1) {
1275 error = got_error_from_errno_fmt("fstat %s/%s",
1276 dirpath, "description");
1281 if (len > GOTWEBD_MAXDESCRSZ - 1)
1282 len = GOTWEBD_MAXDESCRSZ - 1;
1284 *description = calloc(len + 1, sizeof(**description));
1285 if (*description == NULL) {
1286 error = got_error_from_errno("calloc");
1290 if (read(fd, *description, len) == -1)
1291 error = got_error_from_errno("read");
1293 if (fd != -1 && close(fd) == -1 && error == NULL)
1294 error = got_error_from_errno("close");
1298 static const struct got_error *
1299 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1302 const struct got_error *error = NULL;
1308 if (srv->show_repo_cloneurl == 0)
1311 fd = openat(dir, "cloneurl", O_RDONLY);
1313 if (errno != ENOENT && errno != EACCES) {
1314 error = got_error_from_errno_fmt("openat %s/%s",
1315 dirpath, "cloneurl");
1320 if (fstat(fd, &sb) == -1) {
1321 error = got_error_from_errno_fmt("fstat %s/%s",
1322 dirpath, "cloneurl");
1327 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1328 len = GOTWEBD_MAXCLONEURLSZ - 1;
1330 *url = calloc(len + 1, sizeof(**url));
1332 error = got_error_from_errno("calloc");
1336 if (read(fd, *url, len) == -1)
1337 error = got_error_from_errno("read");
1339 if (fd != -1 && close(fd) == -1 && error == NULL)
1340 error = got_error_from_errno("close");
1345 gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1347 struct request *c = tp->tp_arg;
1349 long long diff_time;
1350 const char *years = "years ago", *months = "months ago";
1351 const char *weeks = "weeks ago", *days = "days ago";
1352 const char *hours = "hours ago", *minutes = "minutes ago";
1353 const char *seconds = "seconds ago", *now = "right now";
1360 diff_time = time(NULL) - committer_time;
1361 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1362 if (fcgi_printf(c, "%lld %s",
1363 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1365 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1366 if (fcgi_printf(c, "%lld %s",
1367 (diff_time / 60 / 60 / 24 / (365 / 12)),
1370 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1371 if (fcgi_printf(c, "%lld %s",
1372 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1374 } else if (diff_time > 60 * 60 * 24 * 2) {
1375 if (fcgi_printf(c, "%lld %s",
1376 (diff_time / 60 / 60 / 24), days) == -1)
1378 } else if (diff_time > 60 * 60 * 2) {
1379 if (fcgi_printf(c, "%lld %s",
1380 (diff_time / 60 / 60), hours) == -1)
1382 } else if (diff_time > 60 * 2) {
1383 if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1386 } else if (diff_time > 2) {
1387 if (fcgi_printf(c, "%lld %s", diff_time,
1391 if (fcgi_puts(tp, now) == -1)
1396 if (gmtime_r(&committer_time, &tm) == NULL)
1399 s = asctime_r(&tm, datebuf);
1403 if (fcgi_puts(tp, datebuf) == -1 ||
1404 fcgi_puts(tp, " UTC") == -1)
1408 if (gmtime_r(&committer_time, &tm) == NULL)
1411 r = strftime(datebuf, sizeof(datebuf),
1412 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1416 if (fcgi_puts(tp, datebuf) == -1)