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 int gotweb_render_index(struct template *);
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(const char *);
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;
147 struct server *srv = NULL;
148 struct querystring *qs = NULL;
149 struct repo_dir *repo_dir = NULL;
150 const char *rss_ctype = "application/rss+xml;charset=utf-8";
155 /* init the transport */
156 error = gotweb_init_transport(&c->t);
158 log_warnx("%s: %s", __func__, error->msg);
161 /* don't process any further if client disconnected */
162 if (c->sock->client_status == CLIENT_DISCONNECT)
164 /* get the gotwebd server */
165 srv = gotweb_get_server(c->server_name);
167 log_warnx("%s: error server is NULL", __func__);
171 /* parse our querystring */
172 error = gotweb_init_querystring(&qs);
174 log_warnx("%s: %s", __func__, error->msg);
178 error = gotweb_parse_querystring(&qs, c->querystring);
180 log_warnx("%s: %s", __func__, error->msg);
185 * certain actions require a commit id in the querystring. this stops
186 * bad actors from exploiting this by manually manipulating the
190 if (qs->action == BLAME || qs->action == BLOB ||
191 qs->action == BLOBRAW || qs->action == DIFF) {
192 if (qs->commit == NULL) {
193 error = got_error(GOT_ERR_BAD_QUERYSTRING);
198 if (qs->action != INDEX) {
199 error = gotweb_init_repo_dir(&repo_dir, qs->path);
202 error = gotweb_load_got_path(c, repo_dir);
203 c->t->repo_dir = repo_dir;
204 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
208 if (qs->action == BLOBRAW || qs->action == BLOB) {
209 error = got_get_repo_commits(c, 1);
213 error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
221 error = got_get_repo_commits(c, 1);
223 log_warnx("%s: %s", __func__, error->msg);
226 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
228 gotweb_render_page(c->tp, gotweb_render_blame);
232 struct gotweb_url url = {
237 .commit = qs->commit,
238 .folder = qs->folder,
242 gotweb_reply(c, 302, NULL, &url);
246 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
248 gotweb_render_page(c->tp, gotweb_render_blob);
252 r = gotweb_reply_file(c, "application/octet-stream",
255 r = gotweb_reply(c, 200, "text/plain", NULL);
260 error = got_object_blob_read_block(&len, c->t->blob);
265 buf = got_object_blob_get_read_buf(c->t->blob);
266 if (fcgi_gen_binary_response(c, buf, len) == -1)
271 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
273 gotweb_render_page(c->tp, gotweb_render_briefs);
276 error = got_get_repo_commits(c, srv->max_commits_display);
278 log_warnx("%s: %s", __func__, error->msg);
281 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
283 gotweb_render_page(c->tp, gotweb_render_commits);
286 error = got_get_repo_commits(c, 1);
288 log_warnx("%s: %s", __func__, error->msg);
291 error = got_open_diff_for_output(&c->t->fp, c);
293 log_warnx("%s: %s", __func__, error->msg);
296 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
298 gotweb_render_page(c->tp, gotweb_render_diff);
301 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
303 if (c->t->nrepos == -1) {
305 error = got_error_from_errno2("scandir",
309 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
311 gotweb_render_page(c->tp, gotweb_render_index);
314 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
317 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
320 gotweb_render_rss(c->tp);
323 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
324 got_ref_cmp_by_name, NULL);
326 log_warnx("%s: got_ref_list: %s", __func__,
331 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
333 log_warnx("%s: got_get_repo_tags: %s", __func__,
337 qs->action = SUMMARY;
338 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
340 gotweb_render_page(c->tp, gotweb_render_summary);
343 error = got_get_repo_tags(c, 1);
345 log_warnx("%s: %s", __func__, error->msg);
348 if (c->t->tag_count == 0) {
349 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
353 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
355 gotweb_render_page(c->tp, gotweb_render_tag);
358 error = got_get_repo_tags(c, srv->max_commits_display);
360 log_warnx("%s: %s", __func__, error->msg);
363 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
365 gotweb_render_page(c->tp, gotweb_render_tags);
368 error = got_get_repo_commits(c, 1);
370 log_warnx("%s: %s", __func__, error->msg);
373 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
375 gotweb_render_page(c->tp, gotweb_render_tree);
379 error = got_error(GOT_ERR_BAD_QUERYSTRING);
384 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
386 gotweb_render_page(c->tp, gotweb_render_error);
390 gotweb_get_server(const char *server_name)
394 /* check against the server name first */
395 if (*server_name != '\0')
396 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
397 if (strcmp(srv->name, server_name) == 0)
400 /* otherwise, use the first server */
401 return TAILQ_FIRST(&gotwebd_env->servers);
404 const struct got_error *
405 gotweb_init_transport(struct transport **t)
407 const struct got_error *error = NULL;
409 *t = calloc(1, sizeof(**t));
411 return got_error_from_errno2(__func__, "calloc");
413 TAILQ_INIT(&(*t)->repo_commits);
414 TAILQ_INIT(&(*t)->repo_tags);
415 TAILQ_INIT(&(*t)->refs);
422 static const struct got_error *
423 gotweb_init_querystring(struct querystring **qs)
425 const struct got_error *error = NULL;
427 *qs = calloc(1, sizeof(**qs));
429 return got_error_from_errno2(__func__, "calloc");
431 (*qs)->headref = strdup("HEAD");
432 if ((*qs)->headref == NULL) {
435 return got_error_from_errno2(__func__, "strdup");
438 (*qs)->action = INDEX;
439 (*qs)->commit = NULL;
441 (*qs)->folder = NULL;
442 (*qs)->index_page = 0;
448 static const struct got_error *
449 gotweb_parse_querystring(struct querystring **qs, char *qst)
451 const struct got_error *error = NULL;
452 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
453 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
460 return got_error_from_errno2(__func__, "strdup");
465 while (tok1_pair != NULL) {
466 strsep(&tok1_end, "&");
468 tok2 = strdup(tok1_pair);
471 return got_error_from_errno2(__func__, "strdup");
477 while (tok2_pair != NULL) {
478 strsep(&tok2_end, "=");
480 error = gotweb_assign_querystring(qs, tok2_pair,
485 tok2_pair = tok2_end;
488 tok1_pair = tok1_end;
499 * Adapted from usr.sbin/httpd/httpd.c url_decode.
501 static const struct got_error *
502 gotweb_urldecode(char *url)
514 /* Encoding character is followed by two hex chars */
515 if (!isxdigit((unsigned char)p[1]) ||
516 !isxdigit((unsigned char)p[2]) ||
517 (p[1] == '0' && p[2] == '0'))
518 return got_error(GOT_ERR_BAD_QUERYSTRING);
524 * We don't have to validate "hex" because it is
525 * guaranteed to include two hex chars followed by nul.
527 x = strtoul(hex, NULL, 16);
543 static const struct got_error *
544 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
546 const struct got_error *error = NULL;
550 error = gotweb_urldecode(value);
554 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
555 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
558 switch (querystring_keys[el_cnt].element) {
560 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
561 if (strcmp(value, action_keys[a_cnt].name) != 0)
563 else if (strcmp(value,
564 action_keys[a_cnt].name) == 0){
566 action_keys[a_cnt].action;
574 (*qs)->commit = strdup(value);
575 if ((*qs)->commit == NULL) {
576 error = got_error_from_errno2(__func__,
582 (*qs)->file = strdup(value);
583 if ((*qs)->file == NULL) {
584 error = got_error_from_errno2(__func__,
590 (*qs)->folder = strdup(value);
591 if ((*qs)->folder == NULL) {
592 error = got_error_from_errno2(__func__,
598 free((*qs)->headref);
599 (*qs)->headref = strdup(value);
600 if ((*qs)->headref == NULL) {
601 error = got_error_from_errno2(__func__,
609 (*qs)->index_page = strtonum(value, INT64_MIN,
612 error = got_error_from_errno3(__func__,
616 if ((*qs)->index_page < 0)
617 (*qs)->index_page = 0;
620 (*qs)->path = strdup(value);
621 if ((*qs)->path == NULL) {
622 error = got_error_from_errno2(__func__,
630 (*qs)->page = strtonum(value, INT64_MIN,
633 error = got_error_from_errno3(__func__,
649 gotweb_free_repo_tag(struct repo_tag *rt)
654 free(rt->tag_commit);
655 free(rt->commit_msg);
662 gotweb_free_repo_commit(struct repo_commit *rc)
672 free(rc->commit_msg);
678 gotweb_free_querystring(struct querystring *qs)
691 gotweb_free_repo_dir(struct repo_dir *repo_dir)
693 if (repo_dir != NULL) {
694 free(repo_dir->name);
695 free(repo_dir->owner);
696 free(repo_dir->description);
698 free(repo_dir->path);
704 gotweb_free_transport(struct transport *t)
706 const struct got_error *err;
707 struct repo_commit *rc = NULL, *trc = NULL;
708 struct repo_tag *rt = NULL, *trt = NULL;
711 got_ref_list_free(&t->refs);
712 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
713 TAILQ_REMOVE(&t->repo_commits, rc, entry);
714 gotweb_free_repo_commit(rc);
716 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
717 TAILQ_REMOVE(&t->repo_tags, rt, entry);
718 gotweb_free_repo_tag(rt);
720 gotweb_free_repo_dir(t->repo_dir);
721 gotweb_free_querystring(t->qs);
726 got_object_blob_close(t->blob);
728 err = got_gotweb_closefile(t->fp);
730 log_warnx("%s: got_gotweb_closefile failure: %s",
733 if (t->fd != -1 && close(t->fd) == -1)
734 log_warn("%s: close", __func__);
736 for (i = 0; i < t->nrepos; ++i)
744 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
745 struct gotweb_url *next, int *have_next)
747 struct transport *t = c->t;
748 struct querystring *qs = t->qs;
749 struct server *srv = c->srv;
751 *have_prev = *have_next = 0;
755 if (qs->index_page > 0) {
757 *prev = (struct gotweb_url){
759 .index_page = qs->index_page - 1,
763 if (t->next_disp == srv->max_repos_display &&
764 t->repos_total != (qs->index_page + 1) *
765 srv->max_repos_display) {
767 *next = (struct gotweb_url){
769 .index_page = qs->index_page + 1,
775 if (t->prev_id && qs->commit != NULL &&
776 strcmp(qs->commit, t->prev_id) != 0) {
778 *prev = (struct gotweb_url){
781 .page = qs->page - 1,
783 .commit = t->prev_id,
784 .headref = qs->headref,
789 *next = (struct gotweb_url){
792 .page = qs->page + 1,
794 .commit = t->next_id,
795 .headref = qs->headref,
803 gotweb_render_index(struct template *tp)
805 const struct got_error *error = NULL;
806 struct request *c = tp->tp_arg;
807 struct server *srv = c->srv;
808 struct transport *t = c->t;
809 struct querystring *qs = t->qs;
810 struct repo_dir *repo_dir = NULL;
811 struct dirent **sd_dent = t->repos;
812 unsigned int d_i, d_disp = 0;
813 unsigned int d_skipped = 0;
816 if (gotweb_render_repo_table_hdr(c->tp) == -1)
819 for (d_i = 0; d_i < t->nrepos; d_i++) {
820 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
823 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
824 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
829 error = got_path_dirent_type(&type, srv->repos_path,
833 if (type != DT_DIR) {
838 if (qs->index_page > 0 && (qs->index_page *
839 srv->max_repos_display) > t->prev_disp) {
844 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
848 error = gotweb_load_got_path(c, repo_dir);
849 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
850 if (error->code != GOT_ERR_NOT_GIT_REPO)
851 log_warnx("%s: %s: %s", __func__,
852 sd_dent[d_i]->d_name, error->msg);
853 gotweb_free_repo_dir(repo_dir);
862 r = gotweb_render_repo_fragment(c->tp, repo_dir);
863 gotweb_free_repo_dir(repo_dir);
868 if (d_disp == srv->max_repos_display)
871 t->repos_total = t->nrepos - d_skipped;
873 if (srv->max_repos_display == 0)
875 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
877 if (t->repos_total <= srv->max_repos ||
878 t->repos_total <= srv->max_repos_display)
881 if (gotweb_render_navs(c->tp) == -1)
888 should_urlencode(int c)
890 if (c <= ' ' || c >= 127)
914 /* needed because the URLs are embedded into the HTML */
923 gotweb_urlencode(const char *str)
931 for (s = str; *s; ++s) {
933 if (should_urlencode(*s))
937 escaped = calloc(1, len + 1);
942 for (s = str; *s; ++s) {
943 if (should_urlencode(*s)) {
944 a = (*s & 0xF0) >> 4;
948 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
949 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
958 gotweb_action_name(int action)
993 gotweb_render_url(struct request *c, struct gotweb_url *url)
995 const char *sep = "?", *action;
999 action = gotweb_action_name(url->action);
1000 if (action != NULL) {
1001 if (fcgi_printf(c, "?action=%s", action) == -1)
1007 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1013 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1019 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1025 tmp = gotweb_urlencode(url->file);
1028 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1036 tmp = gotweb_urlencode(url->folder);
1039 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1047 tmp = gotweb_urlencode(url->headref);
1050 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1057 if (url->index_page != -1) {
1058 if (fcgi_printf(c, "%sindex_page=%d", sep,
1059 url->index_page) == -1)
1065 tmp = gotweb_urlencode(url->path);
1068 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1075 if (url->page != -1) {
1076 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1085 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1087 struct template *tp = c->tp;
1088 const char *proto = c->https ? "https" : "http";
1090 if (fcgi_puts(tp, proto) == -1 ||
1091 fcgi_puts(tp, "://") == -1 ||
1092 tp_htmlescape(tp, c->server_name) == -1 ||
1093 tp_htmlescape(tp, c->document_uri) == -1)
1096 return gotweb_render_url(c, url);
1099 static struct got_repository *
1100 find_cached_repo(struct server *srv, const char *path)
1104 for (i = 0; i < srv->ncached_repos; i++) {
1105 if (strcmp(srv->cached_repos[i].path, path) == 0)
1106 return srv->cached_repos[i].repo;
1112 static const struct got_error *
1113 cache_repo(struct got_repository **new, struct server *srv,
1114 struct repo_dir *repo_dir, struct socket *sock)
1116 const struct got_error *error = NULL;
1117 struct got_repository *repo;
1118 struct cached_repo *cr;
1121 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1122 cr = &srv->cached_repos[srv->ncached_repos - 1];
1123 error = got_repo_close(cr->repo);
1124 memset(cr, 0, sizeof(*cr));
1125 srv->ncached_repos--;
1128 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1129 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1130 cr = &srv->cached_repos[0];
1133 cr = &srv->cached_repos[srv->ncached_repos];
1136 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1139 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1140 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1145 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1146 >= sizeof(cr->path)) {
1148 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1149 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1151 return got_error(GOT_ERR_NO_SPACE);
1155 srv->ncached_repos++;
1160 static const struct got_error *
1161 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1163 const struct got_error *error = NULL;
1164 struct socket *sock = c->sock;
1165 struct server *srv = c->srv;
1166 struct transport *t = c->t;
1167 struct got_repository *repo = NULL;
1171 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1172 GOTWEB_GIT_DIR) == -1)
1173 return got_error_from_errno("asprintf");
1175 dt = opendir(dir_test);
1179 repo_dir->path = dir_test;
1184 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1185 repo_dir->name) == -1)
1186 return got_error_from_errno("asprintf");
1188 dt = opendir(dir_test);
1190 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1193 repo_dir->path = dir_test;
1198 if (srv->respect_exportok &&
1199 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1200 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1204 repo = find_cached_repo(srv, repo_dir->path);
1206 error = cache_repo(&repo, srv, repo_dir, sock);
1211 error = gotweb_get_repo_description(&repo_dir->description, srv,
1212 repo_dir->path, dirfd(dt));
1215 error = got_get_repo_owner(&repo_dir->owner, c);
1218 error = got_get_repo_age(&repo_dir->age, c, NULL);
1221 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1225 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1226 error = got_error_from_errno("closedir");
1230 static const struct got_error *
1231 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1233 const struct got_error *error;
1235 *repo_dir = calloc(1, sizeof(**repo_dir));
1236 if (*repo_dir == NULL)
1237 return got_error_from_errno("calloc");
1239 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1240 error = got_error_from_errno("asprintf");
1245 (*repo_dir)->owner = NULL;
1246 (*repo_dir)->description = NULL;
1247 (*repo_dir)->url = NULL;
1248 (*repo_dir)->path = NULL;
1253 static const struct got_error *
1254 gotweb_get_repo_description(char **description, struct server *srv,
1255 const char *dirpath, int dir)
1257 const struct got_error *error = NULL;
1262 *description = NULL;
1263 if (srv->show_repo_description == 0)
1266 fd = openat(dir, "description", O_RDONLY);
1268 if (errno != ENOENT && errno != EACCES) {
1269 error = got_error_from_errno_fmt("openat %s/%s",
1270 dirpath, "description");
1275 if (fstat(fd, &sb) == -1) {
1276 error = got_error_from_errno_fmt("fstat %s/%s",
1277 dirpath, "description");
1282 if (len > GOTWEBD_MAXDESCRSZ - 1)
1283 len = GOTWEBD_MAXDESCRSZ - 1;
1285 *description = calloc(len + 1, sizeof(**description));
1286 if (*description == NULL) {
1287 error = got_error_from_errno("calloc");
1291 if (read(fd, *description, len) == -1)
1292 error = got_error_from_errno("read");
1294 if (fd != -1 && close(fd) == -1 && error == NULL)
1295 error = got_error_from_errno("close");
1299 static const struct got_error *
1300 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1303 const struct got_error *error = NULL;
1309 if (srv->show_repo_cloneurl == 0)
1312 fd = openat(dir, "cloneurl", O_RDONLY);
1314 if (errno != ENOENT && errno != EACCES) {
1315 error = got_error_from_errno_fmt("openat %s/%s",
1316 dirpath, "cloneurl");
1321 if (fstat(fd, &sb) == -1) {
1322 error = got_error_from_errno_fmt("fstat %s/%s",
1323 dirpath, "cloneurl");
1328 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1329 len = GOTWEBD_MAXCLONEURLSZ - 1;
1331 *url = calloc(len + 1, sizeof(**url));
1333 error = got_error_from_errno("calloc");
1337 if (read(fd, *url, len) == -1)
1338 error = got_error_from_errno("read");
1340 if (fd != -1 && close(fd) == -1 && error == NULL)
1341 error = got_error_from_errno("close");
1346 gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1348 struct request *c = tp->tp_arg;
1350 long long diff_time;
1351 const char *years = "years ago", *months = "months ago";
1352 const char *weeks = "weeks ago", *days = "days ago";
1353 const char *hours = "hours ago", *minutes = "minutes ago";
1354 const char *seconds = "seconds ago", *now = "right now";
1361 diff_time = time(NULL) - committer_time;
1362 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1363 if (fcgi_printf(c, "%lld %s",
1364 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1366 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1367 if (fcgi_printf(c, "%lld %s",
1368 (diff_time / 60 / 60 / 24 / (365 / 12)),
1371 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1372 if (fcgi_printf(c, "%lld %s",
1373 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1375 } else if (diff_time > 60 * 60 * 24 * 2) {
1376 if (fcgi_printf(c, "%lld %s",
1377 (diff_time / 60 / 60 / 24), days) == -1)
1379 } else if (diff_time > 60 * 60 * 2) {
1380 if (fcgi_printf(c, "%lld %s",
1381 (diff_time / 60 / 60), hours) == -1)
1383 } else if (diff_time > 60 * 2) {
1384 if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1387 } else if (diff_time > 2) {
1388 if (fcgi_printf(c, "%lld %s", diff_time,
1392 if (fcgi_puts(tp, now) == -1)
1397 if (gmtime_r(&committer_time, &tm) == NULL)
1400 s = asctime_r(&tm, datebuf);
1404 if (fcgi_puts(tp, datebuf) == -1 ||
1405 fcgi_puts(tp, " UTC") == -1)
1409 if (gmtime_r(&committer_time, &tm) == NULL)
1412 r = strftime(datebuf, sizeof(datebuf),
1413 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1417 if (fcgi_puts(tp, datebuf) == -1)