2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <netinet/in.h>
23 #include <sys/queue.h>
25 #include <sys/types.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
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 const struct got_error *gotweb_render_index(struct request *);
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(uint8_t *, uint8_t *);
103 gotweb_process_request(struct request *c)
105 const struct got_error *error = NULL, *error2 = NULL;
106 struct got_blob_object *blob = NULL;
107 struct server *srv = NULL;
108 struct querystring *qs = NULL;
109 struct repo_dir *repo_dir = NULL;
110 struct got_reflist_head refs;
112 uint8_t err[] = "gotwebd experienced an error: ";
113 int r, html = 0, fd = -1;
117 /* init the transport */
118 error = gotweb_init_transport(&c->t);
120 log_warnx("%s: %s", __func__, error->msg);
123 /* don't process any further if client disconnected */
124 if (c->sock->client_status == CLIENT_DISCONNECT)
126 /* get the gotwebd server */
127 srv = gotweb_get_server(c->server_name, c->http_host);
129 log_warnx("%s: error server is NULL", __func__);
133 /* parse our querystring */
134 error = gotweb_init_querystring(&qs);
136 log_warnx("%s: %s", __func__, error->msg);
140 error = gotweb_parse_querystring(&qs, c->querystring);
142 log_warnx("%s: %s", __func__, error->msg);
147 * certain actions require a commit id in the querystring. this stops
148 * bad actors from exploiting this by manually manipulating the
152 if (qs->action == BLAME || qs->action == BLOB ||
153 qs->action == BLOBRAW || qs->action == DIFF) {
154 if (qs->commit == NULL) {
155 error2 = got_error(GOT_ERR_QUERYSTRING);
160 if (qs->action != INDEX) {
161 error = gotweb_init_repo_dir(&repo_dir, qs->path);
164 error = gotweb_load_got_path(c, repo_dir);
165 c->t->repo_dir = repo_dir;
166 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
170 if (qs->action == BLOBRAW) {
175 error = got_get_repo_commits(c, 1);
179 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
184 error = gotweb_render_content_type_file(c,
185 "application/octet-stream", qs->file, NULL);
187 error = gotweb_render_content_type(c, "text/plain");
190 log_warnx("%s: %s", __func__, error->msg);
195 error = got_object_blob_read_block(&len, blob);
200 buf = got_object_blob_get_read_buf(blob);
201 if (fcgi_gen_binary_response(c, buf, len) == -1)
208 if (qs->action == BLOB) {
210 struct gotweb_url url = {
215 .commit = qs->commit,
216 .folder = qs->folder,
220 error = got_get_repo_commits(c, 1);
224 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
228 fcgi_puts(c->tp, "Status: 302\r\n");
229 fcgi_puts(c->tp, "Location: ");
230 gotweb_render_url(c, &url);
231 fcgi_puts(c->tp, "\r\n\r\n");
236 if (qs->action == RSS) {
237 error = gotweb_render_content_type_file(c,
238 "application/rss+xml;charset=utf-8",
239 repo_dir->name, ".rss");
241 log_warnx("%s: %s", __func__, error->msg);
245 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
247 log_warnx("%s: %s", __func__, error->msg);
250 if (gotweb_render_rss(c->tp) == -1)
256 error = gotweb_render_content_type(c, "text/html");
258 log_warnx("%s: %s", __func__, error->msg);
263 if (gotweb_render_header(c->tp) == -1)
273 error = got_get_repo_commits(c, 1);
275 log_warnx("%s: %s", __func__, error->msg);
278 if (gotweb_render_blame(c->tp) == -1)
282 if (gotweb_render_blob(c->tp, blob) == -1)
286 if (gotweb_render_briefs(c->tp) == -1)
290 error = got_get_repo_commits(c, srv->max_commits_display);
292 log_warnx("%s: %s", __func__, error->msg);
295 if (gotweb_render_commits(c->tp) == -1)
299 error = got_get_repo_commits(c, 1);
301 log_warnx("%s: %s", __func__, error->msg);
304 error = got_open_diff_for_output(&fp, &fd, c);
306 log_warnx("%s: %s", __func__, error->msg);
309 if (gotweb_render_diff(c->tp, fp) == -1)
313 error = gotweb_render_index(c);
315 log_warnx("%s: %s", __func__, error->msg);
320 error = got_ref_list(&refs, c->t->repo, "refs/heads",
321 got_ref_cmp_by_name, NULL);
323 log_warnx("%s: got_ref_list: %s", __func__,
328 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
330 log_warnx("%s: got_get_repo_tags: %s", __func__,
334 qs->action = SUMMARY;
335 if (gotweb_render_summary(c->tp, &refs) == -1)
339 error = got_get_repo_tags(c, 1);
341 log_warnx("%s: %s", __func__, error->msg);
344 if (c->t->tag_count == 0) {
345 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
349 if (gotweb_render_tag(c->tp) == -1)
353 error = got_get_repo_tags(c, srv->max_commits_display);
355 log_warnx("%s: %s", __func__, error->msg);
358 if (gotweb_render_tags(c->tp) == -1)
362 error = got_get_repo_commits(c, 1);
364 log_warnx("%s: %s", __func__, error->msg);
367 if (gotweb_render_tree(c->tp) == -1)
372 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
373 "Erorr: Bad Querystring");
381 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
383 if (fcgi_printf(c, "\n%s", err) == -1)
386 if (fcgi_printf(c, "%s", error->msg) == -1)
389 if (fcgi_printf(c, "see daemon logs for details") == -1)
392 if (html && fcgi_printf(c, "</div>\n") == -1)
396 got_object_blob_close(blob);
398 error = got_gotweb_flushfile(fp, fd);
400 log_warnx("%s: got_gotweb_flushfile failure: %s",
401 __func__, error->msg);
406 if (html && srv != NULL)
407 gotweb_render_footer(c->tp);
409 got_ref_list_free(&refs);
413 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
415 struct server *srv = NULL;
417 /* check against the server name first */
418 if (strlen(server_name) > 0)
419 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
420 if (strcmp(srv->name, server_name) == 0)
423 /* check against subdomain second */
424 if (strlen(subdomain) > 0)
425 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
426 if (strcmp(srv->name, subdomain) == 0)
429 /* if those fail, send first server */
430 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
437 const struct got_error *
438 gotweb_init_transport(struct transport **t)
440 const struct got_error *error = NULL;
442 *t = calloc(1, sizeof(**t));
444 return got_error_from_errno2("%s: calloc", __func__);
446 TAILQ_INIT(&(*t)->repo_commits);
447 TAILQ_INIT(&(*t)->repo_tags);
450 (*t)->repo_dir = NULL;
452 (*t)->next_id = NULL;
453 (*t)->prev_id = NULL;
460 static const struct got_error *
461 gotweb_init_querystring(struct querystring **qs)
463 const struct got_error *error = NULL;
465 *qs = calloc(1, sizeof(**qs));
467 return got_error_from_errno2("%s: calloc", __func__);
469 (*qs)->headref = strdup("HEAD");
470 if ((*qs)->headref == NULL) {
473 return got_error_from_errno2("%s: strdup", __func__);
476 (*qs)->action = INDEX;
477 (*qs)->commit = NULL;
479 (*qs)->folder = NULL;
480 (*qs)->index_page = 0;
486 static const struct got_error *
487 gotweb_parse_querystring(struct querystring **qs, char *qst)
489 const struct got_error *error = NULL;
490 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
491 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
498 return got_error_from_errno2("%s: strdup", __func__);
503 while (tok1_pair != NULL) {
504 strsep(&tok1_end, "&");
506 tok2 = strdup(tok1_pair);
509 return got_error_from_errno2("%s: strdup", __func__);
515 while (tok2_pair != NULL) {
516 strsep(&tok2_end, "=");
518 error = gotweb_assign_querystring(qs, tok2_pair,
523 tok2_pair = tok2_end;
526 tok1_pair = tok1_end;
537 * Adapted from usr.sbin/httpd/httpd.c url_decode.
539 static const struct got_error *
540 gotweb_urldecode(char *url)
552 /* Encoding character is followed by two hex chars */
553 if (!isxdigit((unsigned char)p[1]) ||
554 !isxdigit((unsigned char)p[2]) ||
555 (p[1] == '0' && p[2] == '0'))
556 return got_error(GOT_ERR_BAD_QUERYSTRING);
562 * We don't have to validate "hex" because it is
563 * guaranteed to include two hex chars followed by nul.
565 x = strtoul(hex, NULL, 16);
581 static const struct got_error *
582 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
584 const struct got_error *error = NULL;
588 error = gotweb_urldecode(value);
592 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
593 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
596 switch (querystring_keys[el_cnt].element) {
598 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
599 if (strcmp(value, action_keys[a_cnt].name) != 0)
601 else if (strcmp(value,
602 action_keys[a_cnt].name) == 0){
604 action_keys[a_cnt].action;
612 (*qs)->commit = strdup(value);
613 if ((*qs)->commit == NULL) {
614 error = got_error_from_errno2("%s: strdup",
620 (*qs)->file = strdup(value);
621 if ((*qs)->file == NULL) {
622 error = got_error_from_errno2("%s: strdup",
628 (*qs)->folder = strdup(value);
629 if ((*qs)->folder == NULL) {
630 error = got_error_from_errno2("%s: strdup",
636 free((*qs)->headref);
637 (*qs)->headref = strdup(value);
638 if ((*qs)->headref == NULL) {
639 error = got_error_from_errno2("%s: strdup",
645 if (strlen(value) == 0)
647 (*qs)->index_page = strtonum(value, INT64_MIN,
650 error = got_error_from_errno3("%s: strtonum %s",
654 if ((*qs)->index_page < 0)
655 (*qs)->index_page = 0;
658 (*qs)->path = strdup(value);
659 if ((*qs)->path == NULL) {
660 error = got_error_from_errno2("%s: strdup",
666 if (strlen(value) == 0)
668 (*qs)->page = strtonum(value, INT64_MIN,
671 error = got_error_from_errno3("%s: strtonum %s",
687 gotweb_free_repo_tag(struct repo_tag *rt)
692 free(rt->tag_commit);
693 free(rt->commit_msg);
700 gotweb_free_repo_commit(struct repo_commit *rc)
710 free(rc->commit_msg);
716 gotweb_free_querystring(struct querystring *qs)
729 gotweb_free_repo_dir(struct repo_dir *repo_dir)
731 if (repo_dir != NULL) {
732 free(repo_dir->name);
733 free(repo_dir->owner);
734 free(repo_dir->description);
737 free(repo_dir->path);
743 gotweb_free_transport(struct transport *t)
745 struct repo_commit *rc = NULL, *trc = NULL;
746 struct repo_tag *rt = NULL, *trt = NULL;
748 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
749 TAILQ_REMOVE(&t->repo_commits, rc, entry);
750 gotweb_free_repo_commit(rc);
752 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
753 TAILQ_REMOVE(&t->repo_tags, rt, entry);
754 gotweb_free_repo_tag(rt);
756 gotweb_free_repo_dir(t->repo_dir);
757 gotweb_free_querystring(t->qs);
763 const struct got_error *
764 gotweb_render_content_type(struct request *c, const char *type)
766 const char *csp = "default-src 'self'; script-src 'none'; "
767 "object-src 'none';";
770 "Content-Security-Policy: %s\r\n"
771 "Content-Type: %s\r\n\r\n",
776 const struct got_error *
777 gotweb_render_content_type_file(struct request *c, const char *type,
778 const char *file, const char *suffix)
780 fcgi_printf(c, "Content-type: %s\r\n"
781 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
782 type, file, suffix ? suffix : "");
787 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
788 struct gotweb_url *next, int *have_next)
790 struct transport *t = c->t;
791 struct querystring *qs = t->qs;
792 struct server *srv = c->srv;
794 *have_prev = *have_next = 0;
798 if (qs->index_page > 0) {
800 *prev = (struct gotweb_url){
802 .index_page = qs->index_page - 1,
806 if (t->next_disp == srv->max_repos_display &&
807 t->repos_total != (qs->index_page + 1) *
808 srv->max_repos_display) {
810 *next = (struct gotweb_url){
812 .index_page = qs->index_page + 1,
818 if (t->prev_id && qs->commit != NULL &&
819 strcmp(qs->commit, t->prev_id) != 0) {
821 *prev = (struct gotweb_url){
824 .page = qs->page - 1,
826 .commit = t->prev_id,
827 .headref = qs->headref,
832 *next = (struct gotweb_url){
835 .page = qs->page + 1,
837 .commit = t->next_id,
838 .headref = qs->headref,
843 if (t->prev_id && qs->commit != NULL &&
844 strcmp(qs->commit, t->prev_id) != 0) {
846 *prev = (struct gotweb_url){
849 .page = qs->page - 1,
851 .commit = t->prev_id,
852 .headref = qs->headref,
853 .folder = qs->folder,
859 *next = (struct gotweb_url){
862 .page = qs->page + 1,
864 .commit = t->next_id,
865 .headref = qs->headref,
866 .folder = qs->folder,
872 if (t->prev_id && qs->commit != NULL &&
873 strcmp(qs->commit, t->prev_id) != 0) {
875 *prev = (struct gotweb_url){
878 .page = qs->page - 1,
880 .commit = t->prev_id,
881 .headref = qs->headref,
886 *next = (struct gotweb_url){
889 .page = qs->page + 1,
891 .commit = t->next_id,
892 .headref = qs->headref,
899 static const struct got_error *
900 gotweb_render_index(struct request *c)
902 const struct got_error *error = NULL;
903 struct server *srv = c->srv;
904 struct transport *t = c->t;
905 struct querystring *qs = t->qs;
906 struct repo_dir *repo_dir = NULL;
908 struct dirent **sd_dent = NULL;
909 unsigned int d_cnt, d_i, d_disp = 0;
910 unsigned int d_skipped = 0;
913 d = opendir(srv->repos_path);
915 error = got_error_from_errno2("opendir", srv->repos_path);
919 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
922 error = got_error_from_errno2("scandir", srv->repos_path);
926 if (gotweb_render_repo_table_hdr(c->tp) == -1)
929 for (d_i = 0; d_i < d_cnt; d_i++) {
930 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
933 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
934 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
939 error = got_path_dirent_type(&type, srv->repos_path,
943 if (type != DT_DIR) {
948 if (qs->index_page > 0 && (qs->index_page *
949 srv->max_repos_display) > t->prev_disp) {
954 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
958 error = gotweb_load_got_path(c, repo_dir);
959 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
961 gotweb_free_repo_dir(repo_dir);
966 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
972 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
975 gotweb_free_repo_dir(repo_dir);
978 if (d_disp == srv->max_repos_display)
981 t->repos_total = d_cnt - d_skipped;
983 if (srv->max_repos_display == 0)
985 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
987 if (t->repos_total <= srv->max_repos ||
988 t->repos_total <= srv->max_repos_display)
991 if (gotweb_render_navs(c->tp) == -1)
995 for (d_i = 0; d_i < d_cnt; d_i++)
999 if (d != NULL && closedir(d) == EOF && error == NULL)
1000 error = got_error_from_errno("closedir");
1005 should_urlencode(int c)
1007 if (c <= ' ' || c >= 127)
1031 /* needed because the URLs are embedded into the HTML */
1040 gotweb_urlencode(const char *str)
1048 for (s = str; *s; ++s) {
1050 if (should_urlencode(*s))
1054 escaped = calloc(1, len + 1);
1055 if (escaped == NULL)
1059 for (s = str; *s; ++s) {
1060 if (should_urlencode(*s)) {
1061 a = (*s & 0xF0) >> 4;
1065 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1066 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1075 gotweb_action_name(int action)
1110 gotweb_render_url(struct request *c, struct gotweb_url *url)
1112 const char *sep = "?", *action;
1116 action = gotweb_action_name(url->action);
1117 if (action != NULL) {
1118 if (fcgi_printf(c, "?action=%s", action) == -1)
1124 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1130 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1136 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1142 tmp = gotweb_urlencode(url->file);
1145 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1153 tmp = gotweb_urlencode(url->folder);
1156 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1164 tmp = gotweb_urlencode(url->headref);
1167 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1174 if (url->index_page != -1) {
1175 if (fcgi_printf(c, "%sindex_page=%d", sep,
1176 url->index_page) == -1)
1182 tmp = gotweb_urlencode(url->path);
1185 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1192 if (url->page != -1) {
1193 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1202 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1204 struct template *tp = c->tp;
1205 const char *proto = c->https ? "https" : "http";
1207 if (fcgi_puts(tp, proto) == -1 ||
1208 fcgi_puts(tp, "://") == -1 ||
1209 tp_htmlescape(tp, c->server_name) == -1 ||
1210 tp_htmlescape(tp, c->document_uri) == -1)
1213 return gotweb_render_url(c, url);
1216 static struct got_repository *
1217 find_cached_repo(struct server *srv, const char *path)
1221 for (i = 0; i < srv->ncached_repos; i++) {
1222 if (strcmp(srv->cached_repos[i].path, path) == 0)
1223 return srv->cached_repos[i].repo;
1229 static const struct got_error *
1230 cache_repo(struct got_repository **new, struct server *srv,
1231 struct repo_dir *repo_dir, struct socket *sock)
1233 const struct got_error *error = NULL;
1234 struct got_repository *repo;
1235 struct cached_repo *cr;
1238 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1239 cr = &srv->cached_repos[srv->ncached_repos - 1];
1240 error = got_repo_close(cr->repo);
1241 memset(cr, 0, sizeof(*cr));
1242 srv->ncached_repos--;
1245 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1246 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1247 cr = &srv->cached_repos[0];
1250 cr = &srv->cached_repos[srv->ncached_repos];
1253 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1256 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1257 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1262 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1263 >= sizeof(cr->path)) {
1265 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1266 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1268 return got_error(GOT_ERR_NO_SPACE);
1272 srv->ncached_repos++;
1277 static const struct got_error *
1278 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1280 const struct got_error *error = NULL;
1281 struct socket *sock = c->sock;
1282 struct server *srv = c->srv;
1283 struct transport *t = c->t;
1284 struct got_repository *repo = NULL;
1288 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1289 GOTWEB_GIT_DIR) == -1)
1290 return got_error_from_errno("asprintf");
1292 dt = opendir(dir_test);
1296 repo_dir->path = dir_test;
1301 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1302 repo_dir->name) == -1)
1303 return got_error_from_errno("asprintf");
1305 dt = opendir(dir_test);
1307 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1310 repo_dir->path = dir_test;
1315 if (srv->respect_exportok &&
1316 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1317 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1321 repo = find_cached_repo(srv, repo_dir->path);
1323 error = cache_repo(&repo, srv, repo_dir, sock);
1328 error = gotweb_get_repo_description(&repo_dir->description, srv,
1329 repo_dir->path, dirfd(dt));
1332 error = got_get_repo_owner(&repo_dir->owner, c);
1335 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1338 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1342 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1343 error = got_error_from_errno("closedir");
1347 static const struct got_error *
1348 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1350 const struct got_error *error;
1352 *repo_dir = calloc(1, sizeof(**repo_dir));
1353 if (*repo_dir == NULL)
1354 return got_error_from_errno("calloc");
1356 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1357 error = got_error_from_errno("asprintf");
1362 (*repo_dir)->owner = NULL;
1363 (*repo_dir)->description = NULL;
1364 (*repo_dir)->url = NULL;
1365 (*repo_dir)->age = NULL;
1366 (*repo_dir)->path = NULL;
1371 static const struct got_error *
1372 gotweb_get_repo_description(char **description, struct server *srv,
1373 const char *dirpath, int dir)
1375 const struct got_error *error = NULL;
1380 *description = NULL;
1381 if (srv->show_repo_description == 0)
1384 fd = openat(dir, "description", O_RDONLY);
1386 if (errno != ENOENT && errno != EACCES) {
1387 error = got_error_from_errno_fmt("openat %s/%s",
1388 dirpath, "description");
1393 if (fstat(fd, &sb) == -1) {
1394 error = got_error_from_errno_fmt("fstat %s/%s",
1395 dirpath, "description");
1400 if (len > GOTWEBD_MAXDESCRSZ - 1)
1401 len = GOTWEBD_MAXDESCRSZ - 1;
1403 *description = calloc(len + 1, sizeof(**description));
1404 if (*description == NULL) {
1405 error = got_error_from_errno("calloc");
1409 if (read(fd, *description, len) == -1)
1410 error = got_error_from_errno("read");
1412 if (fd != -1 && close(fd) == -1 && error == NULL)
1413 error = got_error_from_errno("close");
1417 static const struct got_error *
1418 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1421 const struct got_error *error = NULL;
1427 if (srv->show_repo_cloneurl == 0)
1430 fd = openat(dir, "cloneurl", O_RDONLY);
1432 if (errno != ENOENT && errno != EACCES) {
1433 error = got_error_from_errno_fmt("openat %s/%s",
1434 dirpath, "cloneurl");
1439 if (fstat(fd, &sb) == -1) {
1440 error = got_error_from_errno_fmt("fstat %s/%s",
1441 dirpath, "cloneurl");
1446 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1447 len = GOTWEBD_MAXCLONEURLSZ - 1;
1449 *url = calloc(len + 1, sizeof(**url));
1451 error = got_error_from_errno("calloc");
1455 if (read(fd, *url, len) == -1)
1456 error = got_error_from_errno("read");
1458 if (fd != -1 && close(fd) == -1 && error == NULL)
1459 error = got_error_from_errno("close");
1463 const struct got_error *
1464 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1467 long long diff_time;
1468 const char *years = "years ago", *months = "months ago";
1469 const char *weeks = "weeks ago", *days = "days ago";
1470 const char *hours = "hours ago", *minutes = "minutes ago";
1471 const char *seconds = "seconds ago", *now = "right now";
1480 diff_time = time(NULL) - committer_time;
1481 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1482 if (asprintf(repo_age, "%lld %s",
1483 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1484 return got_error_from_errno("asprintf");
1485 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1486 if (asprintf(repo_age, "%lld %s",
1487 (diff_time / 60 / 60 / 24 / (365 / 12)),
1489 return got_error_from_errno("asprintf");
1490 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1491 if (asprintf(repo_age, "%lld %s",
1492 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1493 return got_error_from_errno("asprintf");
1494 } else if (diff_time > 60 * 60 * 24 * 2) {
1495 if (asprintf(repo_age, "%lld %s",
1496 (diff_time / 60 / 60 / 24), days) == -1)
1497 return got_error_from_errno("asprintf");
1498 } else if (diff_time > 60 * 60 * 2) {
1499 if (asprintf(repo_age, "%lld %s",
1500 (diff_time / 60 / 60), hours) == -1)
1501 return got_error_from_errno("asprintf");
1502 } else if (diff_time > 60 * 2) {
1503 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1505 return got_error_from_errno("asprintf");
1506 } else if (diff_time > 2) {
1507 if (asprintf(repo_age, "%lld %s", diff_time,
1509 return got_error_from_errno("asprintf");
1511 if (asprintf(repo_age, "%s", now) == -1)
1512 return got_error_from_errno("asprintf");
1516 if (gmtime_r(&committer_time, &tm) == NULL)
1517 return got_error_from_errno("gmtime_r");
1519 s = asctime_r(&tm, datebuf);
1521 return got_error_from_errno("asctime_r");
1523 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1524 return got_error_from_errno("asprintf");
1527 if (gmtime_r(&committer_time, &tm) == NULL)
1528 return got_error_from_errno("gmtime_r");
1530 r = strftime(datebuf, sizeof(datebuf),
1531 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1533 return got_error(GOT_ERR_NO_SPACE);
1535 *repo_age = strdup(datebuf);
1536 if (*repo_age == NULL)
1537 return got_error_from_errno("asprintf");