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>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
50 #include "got_compat.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_process_request(struct request *c)
106 const struct got_error *error = NULL, *error2 = NULL;
107 struct got_blob_object *blob = NULL;
108 struct server *srv = NULL;
109 struct querystring *qs = NULL;
110 struct repo_dir *repo_dir = NULL;
111 struct got_reflist_head refs;
113 uint8_t err[] = "gotwebd experienced an error: ";
114 int r, html = 0, fd = -1;
118 /* init the transport */
119 error = gotweb_init_transport(&c->t);
121 log_warnx("%s: %s", __func__, error->msg);
124 /* don't process any further if client disconnected */
125 if (c->sock->client_status == CLIENT_DISCONNECT)
127 /* get the gotwebd server */
128 srv = gotweb_get_server(c->server_name, c->http_host);
130 log_warnx("%s: error server is NULL", __func__);
134 /* parse our querystring */
135 error = gotweb_init_querystring(&qs);
137 log_warnx("%s: %s", __func__, error->msg);
141 error = gotweb_parse_querystring(&qs, c->querystring);
143 log_warnx("%s: %s", __func__, error->msg);
148 * certain actions require a commit id in the querystring. this stops
149 * bad actors from exploiting this by manually manipulating the
153 if (qs->action == BLAME || qs->action == BLOB ||
154 qs->action == BLOBRAW || qs->action == DIFF) {
155 if (qs->commit == NULL) {
156 error2 = got_error(GOT_ERR_QUERYSTRING);
161 if (qs->action != INDEX) {
162 error = gotweb_init_repo_dir(&repo_dir, qs->path);
165 error = gotweb_load_got_path(c, repo_dir);
166 c->t->repo_dir = repo_dir;
167 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
171 if (qs->action == BLOBRAW) {
176 error = got_get_repo_commits(c, 1);
180 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
185 error = gotweb_render_content_type_file(c,
186 "application/octet-stream", qs->file, NULL);
188 error = gotweb_render_content_type(c, "text/plain");
191 log_warnx("%s: %s", __func__, error->msg);
196 error = got_object_blob_read_block(&len, blob);
201 buf = got_object_blob_get_read_buf(blob);
202 if (fcgi_gen_binary_response(c, buf, len) == -1)
209 if (qs->action == BLOB) {
211 struct gotweb_url url = {
216 .commit = qs->commit,
217 .folder = qs->folder,
221 error = got_get_repo_commits(c, 1);
225 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
229 fcgi_puts(c->tp, "Status: 302\r\n");
230 fcgi_puts(c->tp, "Location: ");
231 gotweb_render_url(c, &url);
232 fcgi_puts(c->tp, "\r\n\r\n");
237 if (qs->action == RSS) {
238 error = gotweb_render_content_type_file(c,
239 "application/rss+xml;charset=utf-8",
240 repo_dir->name, ".rss");
242 log_warnx("%s: %s", __func__, error->msg);
246 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
248 log_warnx("%s: %s", __func__, error->msg);
251 if (gotweb_render_rss(c->tp) == -1)
257 error = gotweb_render_content_type(c, "text/html");
259 log_warnx("%s: %s", __func__, error->msg);
264 if (gotweb_render_header(c->tp) == -1)
274 error = got_get_repo_commits(c, 1);
276 log_warnx("%s: %s", __func__, error->msg);
279 if (gotweb_render_blame(c->tp) == -1)
283 if (gotweb_render_blob(c->tp, blob) == -1)
287 if (gotweb_render_briefs(c->tp) == -1)
291 error = got_get_repo_commits(c, srv->max_commits_display);
293 log_warnx("%s: %s", __func__, error->msg);
296 if (gotweb_render_commits(c->tp) == -1)
300 error = got_get_repo_commits(c, 1);
302 log_warnx("%s: %s", __func__, error->msg);
305 error = got_open_diff_for_output(&fp, &fd, c);
307 log_warnx("%s: %s", __func__, error->msg);
310 if (gotweb_render_diff(c->tp, fp) == -1)
314 error = gotweb_render_index(c);
316 log_warnx("%s: %s", __func__, error->msg);
321 error = got_ref_list(&refs, c->t->repo, "refs/heads",
322 got_ref_cmp_by_name, NULL);
324 log_warnx("%s: got_ref_list: %s", __func__,
329 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
331 log_warnx("%s: got_get_repo_tags: %s", __func__,
335 qs->action = SUMMARY;
336 if (gotweb_render_summary(c->tp, &refs) == -1)
340 error = got_get_repo_tags(c, 1);
342 log_warnx("%s: %s", __func__, error->msg);
345 if (c->t->tag_count == 0) {
346 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
350 if (gotweb_render_tag(c->tp) == -1)
354 error = got_get_repo_tags(c, srv->max_commits_display);
356 log_warnx("%s: %s", __func__, error->msg);
359 if (gotweb_render_tags(c->tp) == -1)
363 error = got_get_repo_commits(c, 1);
365 log_warnx("%s: %s", __func__, error->msg);
368 if (gotweb_render_tree(c->tp) == -1)
373 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
374 "Erorr: Bad Querystring");
382 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
384 if (fcgi_printf(c, "\n%s", err) == -1)
387 if (fcgi_printf(c, "%s", error->msg) == -1)
390 if (fcgi_printf(c, "see daemon logs for details") == -1)
393 if (html && fcgi_printf(c, "</div>\n") == -1)
397 got_object_blob_close(blob);
399 error = got_gotweb_flushfile(fp, fd);
401 log_warnx("%s: got_gotweb_flushfile failure: %s",
402 __func__, error->msg);
407 if (html && srv != NULL)
408 gotweb_render_footer(c->tp);
410 got_ref_list_free(&refs);
414 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
416 struct server *srv = NULL;
418 /* check against the server name first */
419 if (strlen(server_name) > 0)
420 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
421 if (strcmp(srv->name, server_name) == 0)
424 /* check against subdomain second */
425 if (strlen(subdomain) > 0)
426 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
427 if (strcmp(srv->name, subdomain) == 0)
430 /* if those fail, send first server */
431 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
438 const struct got_error *
439 gotweb_init_transport(struct transport **t)
441 const struct got_error *error = NULL;
443 *t = calloc(1, sizeof(**t));
445 return got_error_from_errno2("%s: calloc", __func__);
447 TAILQ_INIT(&(*t)->repo_commits);
448 TAILQ_INIT(&(*t)->repo_tags);
451 (*t)->repo_dir = NULL;
453 (*t)->next_id = NULL;
454 (*t)->prev_id = NULL;
461 static const struct got_error *
462 gotweb_init_querystring(struct querystring **qs)
464 const struct got_error *error = NULL;
466 *qs = calloc(1, sizeof(**qs));
468 return got_error_from_errno2("%s: calloc", __func__);
470 (*qs)->headref = strdup("HEAD");
471 if ((*qs)->headref == NULL) {
474 return got_error_from_errno2("%s: strdup", __func__);
477 (*qs)->action = INDEX;
478 (*qs)->commit = NULL;
480 (*qs)->folder = NULL;
481 (*qs)->index_page = 0;
487 static const struct got_error *
488 gotweb_parse_querystring(struct querystring **qs, char *qst)
490 const struct got_error *error = NULL;
491 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
492 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
499 return got_error_from_errno2("%s: strdup", __func__);
504 while (tok1_pair != NULL) {
505 strsep(&tok1_end, "&");
507 tok2 = strdup(tok1_pair);
510 return got_error_from_errno2("%s: strdup", __func__);
516 while (tok2_pair != NULL) {
517 strsep(&tok2_end, "=");
519 error = gotweb_assign_querystring(qs, tok2_pair,
524 tok2_pair = tok2_end;
527 tok1_pair = tok1_end;
538 * Adapted from usr.sbin/httpd/httpd.c url_decode.
540 static const struct got_error *
541 gotweb_urldecode(char *url)
553 /* Encoding character is followed by two hex chars */
554 if (!isxdigit((unsigned char)p[1]) ||
555 !isxdigit((unsigned char)p[2]) ||
556 (p[1] == '0' && p[2] == '0'))
557 return got_error(GOT_ERR_BAD_QUERYSTRING);
563 * We don't have to validate "hex" because it is
564 * guaranteed to include two hex chars followed by nul.
566 x = strtoul(hex, NULL, 16);
582 static const struct got_error *
583 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
585 const struct got_error *error = NULL;
589 error = gotweb_urldecode(value);
593 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
594 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
597 switch (querystring_keys[el_cnt].element) {
599 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
600 if (strcmp(value, action_keys[a_cnt].name) != 0)
602 else if (strcmp(value,
603 action_keys[a_cnt].name) == 0){
605 action_keys[a_cnt].action;
613 (*qs)->commit = strdup(value);
614 if ((*qs)->commit == NULL) {
615 error = got_error_from_errno2("%s: strdup",
621 (*qs)->file = strdup(value);
622 if ((*qs)->file == NULL) {
623 error = got_error_from_errno2("%s: strdup",
629 (*qs)->folder = strdup(value);
630 if ((*qs)->folder == NULL) {
631 error = got_error_from_errno2("%s: strdup",
637 free((*qs)->headref);
638 (*qs)->headref = strdup(value);
639 if ((*qs)->headref == NULL) {
640 error = got_error_from_errno2("%s: strdup",
646 if (strlen(value) == 0)
648 (*qs)->index_page = strtonum(value, INT64_MIN,
651 error = got_error_from_errno3("%s: strtonum %s",
655 if ((*qs)->index_page < 0)
656 (*qs)->index_page = 0;
659 (*qs)->path = strdup(value);
660 if ((*qs)->path == NULL) {
661 error = got_error_from_errno2("%s: strdup",
667 if (strlen(value) == 0)
669 (*qs)->page = strtonum(value, INT64_MIN,
672 error = got_error_from_errno3("%s: strtonum %s",
688 gotweb_free_repo_tag(struct repo_tag *rt)
693 free(rt->tag_commit);
694 free(rt->commit_msg);
701 gotweb_free_repo_commit(struct repo_commit *rc)
711 free(rc->commit_msg);
717 gotweb_free_querystring(struct querystring *qs)
730 gotweb_free_repo_dir(struct repo_dir *repo_dir)
732 if (repo_dir != NULL) {
733 free(repo_dir->name);
734 free(repo_dir->owner);
735 free(repo_dir->description);
738 free(repo_dir->path);
744 gotweb_free_transport(struct transport *t)
746 struct repo_commit *rc = NULL, *trc = NULL;
747 struct repo_tag *rt = NULL, *trt = NULL;
749 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
750 TAILQ_REMOVE(&t->repo_commits, rc, entry);
751 gotweb_free_repo_commit(rc);
753 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
754 TAILQ_REMOVE(&t->repo_tags, rt, entry);
755 gotweb_free_repo_tag(rt);
757 gotweb_free_repo_dir(t->repo_dir);
758 gotweb_free_querystring(t->qs);
764 const struct got_error *
765 gotweb_render_content_type(struct request *c, const char *type)
767 const char *csp = "default-src 'self'; script-src 'none'; "
768 "object-src 'none';";
771 "Content-Security-Policy: %s\r\n"
772 "Content-Type: %s\r\n\r\n",
777 const struct got_error *
778 gotweb_render_content_type_file(struct request *c, const char *type,
779 const char *file, const char *suffix)
781 fcgi_printf(c, "Content-type: %s\r\n"
782 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
783 type, file, suffix ? suffix : "");
788 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
789 struct gotweb_url *next, int *have_next)
791 struct transport *t = c->t;
792 struct querystring *qs = t->qs;
793 struct server *srv = c->srv;
795 *have_prev = *have_next = 0;
799 if (qs->index_page > 0) {
801 *prev = (struct gotweb_url){
803 .index_page = qs->index_page - 1,
807 if (t->next_disp == srv->max_repos_display &&
808 t->repos_total != (qs->index_page + 1) *
809 srv->max_repos_display) {
811 *next = (struct gotweb_url){
813 .index_page = qs->index_page + 1,
819 if (t->prev_id && qs->commit != NULL &&
820 strcmp(qs->commit, t->prev_id) != 0) {
822 *prev = (struct gotweb_url){
825 .page = qs->page - 1,
827 .commit = t->prev_id,
828 .headref = qs->headref,
833 *next = (struct gotweb_url){
836 .page = qs->page + 1,
838 .commit = t->next_id,
839 .headref = qs->headref,
844 if (t->prev_id && qs->commit != NULL &&
845 strcmp(qs->commit, t->prev_id) != 0) {
847 *prev = (struct gotweb_url){
850 .page = qs->page - 1,
852 .commit = t->prev_id,
853 .headref = qs->headref,
854 .folder = qs->folder,
860 *next = (struct gotweb_url){
863 .page = qs->page + 1,
865 .commit = t->next_id,
866 .headref = qs->headref,
867 .folder = qs->folder,
873 if (t->prev_id && qs->commit != NULL &&
874 strcmp(qs->commit, t->prev_id) != 0) {
876 *prev = (struct gotweb_url){
879 .page = qs->page - 1,
881 .commit = t->prev_id,
882 .headref = qs->headref,
887 *next = (struct gotweb_url){
890 .page = qs->page + 1,
892 .commit = t->next_id,
893 .headref = qs->headref,
900 static const struct got_error *
901 gotweb_render_index(struct request *c)
903 const struct got_error *error = NULL;
904 struct server *srv = c->srv;
905 struct transport *t = c->t;
906 struct querystring *qs = t->qs;
907 struct repo_dir *repo_dir = NULL;
909 struct dirent **sd_dent = NULL;
910 unsigned int d_cnt, d_i, d_disp = 0;
911 unsigned int d_skipped = 0;
914 d = opendir(srv->repos_path);
916 error = got_error_from_errno2("opendir", srv->repos_path);
920 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
923 error = got_error_from_errno2("scandir", srv->repos_path);
927 if (gotweb_render_repo_table_hdr(c->tp) == -1)
930 for (d_i = 0; d_i < d_cnt; d_i++) {
931 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
934 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
935 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
940 error = got_path_dirent_type(&type, srv->repos_path,
944 if (type != DT_DIR) {
949 if (qs->index_page > 0 && (qs->index_page *
950 srv->max_repos_display) > t->prev_disp) {
955 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
959 error = gotweb_load_got_path(c, repo_dir);
960 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
962 gotweb_free_repo_dir(repo_dir);
967 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
973 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
976 gotweb_free_repo_dir(repo_dir);
979 if (d_disp == srv->max_repos_display)
982 t->repos_total = d_cnt - d_skipped;
984 if (srv->max_repos_display == 0)
986 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
988 if (t->repos_total <= srv->max_repos ||
989 t->repos_total <= srv->max_repos_display)
992 if (gotweb_render_navs(c->tp) == -1)
996 for (d_i = 0; d_i < d_cnt; d_i++)
1000 if (d != NULL && closedir(d) == EOF && error == NULL)
1001 error = got_error_from_errno("closedir");
1006 should_urlencode(int c)
1008 if (c <= ' ' || c >= 127)
1032 /* needed because the URLs are embedded into the HTML */
1041 gotweb_urlencode(const char *str)
1049 for (s = str; *s; ++s) {
1051 if (should_urlencode(*s))
1055 escaped = calloc(1, len + 1);
1056 if (escaped == NULL)
1060 for (s = str; *s; ++s) {
1061 if (should_urlencode(*s)) {
1062 a = (*s & 0xF0) >> 4;
1066 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1067 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1076 gotweb_action_name(int action)
1111 gotweb_render_url(struct request *c, struct gotweb_url *url)
1113 const char *sep = "?", *action;
1117 action = gotweb_action_name(url->action);
1118 if (action != NULL) {
1119 if (fcgi_printf(c, "?action=%s", action) == -1)
1125 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1131 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1137 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1143 tmp = gotweb_urlencode(url->file);
1146 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1154 tmp = gotweb_urlencode(url->folder);
1157 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1165 tmp = gotweb_urlencode(url->headref);
1168 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1175 if (url->index_page != -1) {
1176 if (fcgi_printf(c, "%sindex_page=%d", sep,
1177 url->index_page) == -1)
1183 tmp = gotweb_urlencode(url->path);
1186 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1193 if (url->page != -1) {
1194 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1203 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1205 struct template *tp = c->tp;
1206 const char *proto = c->https ? "https" : "http";
1208 if (fcgi_puts(tp, proto) == -1 ||
1209 fcgi_puts(tp, "://") == -1 ||
1210 tp_htmlescape(tp, c->server_name) == -1 ||
1211 tp_htmlescape(tp, c->document_uri) == -1)
1214 return gotweb_render_url(c, url);
1217 static struct got_repository *
1218 find_cached_repo(struct server *srv, const char *path)
1222 for (i = 0; i < srv->ncached_repos; i++) {
1223 if (strcmp(srv->cached_repos[i].path, path) == 0)
1224 return srv->cached_repos[i].repo;
1230 static const struct got_error *
1231 cache_repo(struct got_repository **new, struct server *srv,
1232 struct repo_dir *repo_dir, struct socket *sock)
1234 const struct got_error *error = NULL;
1235 struct got_repository *repo;
1236 struct cached_repo *cr;
1239 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1240 cr = &srv->cached_repos[srv->ncached_repos - 1];
1241 error = got_repo_close(cr->repo);
1242 memset(cr, 0, sizeof(*cr));
1243 srv->ncached_repos--;
1246 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1247 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1248 cr = &srv->cached_repos[0];
1251 cr = &srv->cached_repos[srv->ncached_repos];
1254 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1257 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1258 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1263 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1264 >= sizeof(cr->path)) {
1266 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1267 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1269 return got_error(GOT_ERR_NO_SPACE);
1273 srv->ncached_repos++;
1278 static const struct got_error *
1279 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1281 const struct got_error *error = NULL;
1282 struct socket *sock = c->sock;
1283 struct server *srv = c->srv;
1284 struct transport *t = c->t;
1285 struct got_repository *repo = NULL;
1289 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1290 GOTWEB_GIT_DIR) == -1)
1291 return got_error_from_errno("asprintf");
1293 dt = opendir(dir_test);
1297 repo_dir->path = dir_test;
1302 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1303 repo_dir->name) == -1)
1304 return got_error_from_errno("asprintf");
1306 dt = opendir(dir_test);
1308 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1311 repo_dir->path = dir_test;
1316 if (srv->respect_exportok &&
1317 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1318 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1322 repo = find_cached_repo(srv, repo_dir->path);
1324 error = cache_repo(&repo, srv, repo_dir, sock);
1329 error = gotweb_get_repo_description(&repo_dir->description, srv,
1330 repo_dir->path, dirfd(dt));
1333 error = got_get_repo_owner(&repo_dir->owner, c);
1336 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1339 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1343 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1344 error = got_error_from_errno("closedir");
1348 static const struct got_error *
1349 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1351 const struct got_error *error;
1353 *repo_dir = calloc(1, sizeof(**repo_dir));
1354 if (*repo_dir == NULL)
1355 return got_error_from_errno("calloc");
1357 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1358 error = got_error_from_errno("asprintf");
1363 (*repo_dir)->owner = NULL;
1364 (*repo_dir)->description = NULL;
1365 (*repo_dir)->url = NULL;
1366 (*repo_dir)->age = NULL;
1367 (*repo_dir)->path = NULL;
1372 static const struct got_error *
1373 gotweb_get_repo_description(char **description, struct server *srv,
1374 const char *dirpath, int dir)
1376 const struct got_error *error = NULL;
1381 *description = NULL;
1382 if (srv->show_repo_description == 0)
1385 fd = openat(dir, "description", O_RDONLY);
1387 if (errno != ENOENT && errno != EACCES) {
1388 error = got_error_from_errno_fmt("openat %s/%s",
1389 dirpath, "description");
1394 if (fstat(fd, &sb) == -1) {
1395 error = got_error_from_errno_fmt("fstat %s/%s",
1396 dirpath, "description");
1401 if (len > GOTWEBD_MAXDESCRSZ - 1)
1402 len = GOTWEBD_MAXDESCRSZ - 1;
1404 *description = calloc(len + 1, sizeof(**description));
1405 if (*description == NULL) {
1406 error = got_error_from_errno("calloc");
1410 if (read(fd, *description, len) == -1)
1411 error = got_error_from_errno("read");
1413 if (fd != -1 && close(fd) == -1 && error == NULL)
1414 error = got_error_from_errno("close");
1418 static const struct got_error *
1419 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1422 const struct got_error *error = NULL;
1428 if (srv->show_repo_cloneurl == 0)
1431 fd = openat(dir, "cloneurl", O_RDONLY);
1433 if (errno != ENOENT && errno != EACCES) {
1434 error = got_error_from_errno_fmt("openat %s/%s",
1435 dirpath, "cloneurl");
1440 if (fstat(fd, &sb) == -1) {
1441 error = got_error_from_errno_fmt("fstat %s/%s",
1442 dirpath, "cloneurl");
1447 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1448 len = GOTWEBD_MAXCLONEURLSZ - 1;
1450 *url = calloc(len + 1, sizeof(**url));
1452 error = got_error_from_errno("calloc");
1456 if (read(fd, *url, len) == -1)
1457 error = got_error_from_errno("read");
1459 if (fd != -1 && close(fd) == -1 && error == NULL)
1460 error = got_error_from_errno("close");
1464 const struct got_error *
1465 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1468 long long diff_time;
1469 const char *years = "years ago", *months = "months ago";
1470 const char *weeks = "weeks ago", *days = "days ago";
1471 const char *hours = "hours ago", *minutes = "minutes ago";
1472 const char *seconds = "seconds ago", *now = "right now";
1481 diff_time = time(NULL) - committer_time;
1482 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1483 if (asprintf(repo_age, "%lld %s",
1484 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1485 return got_error_from_errno("asprintf");
1486 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1487 if (asprintf(repo_age, "%lld %s",
1488 (diff_time / 60 / 60 / 24 / (365 / 12)),
1490 return got_error_from_errno("asprintf");
1491 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1492 if (asprintf(repo_age, "%lld %s",
1493 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1494 return got_error_from_errno("asprintf");
1495 } else if (diff_time > 60 * 60 * 24 * 2) {
1496 if (asprintf(repo_age, "%lld %s",
1497 (diff_time / 60 / 60 / 24), days) == -1)
1498 return got_error_from_errno("asprintf");
1499 } else if (diff_time > 60 * 60 * 2) {
1500 if (asprintf(repo_age, "%lld %s",
1501 (diff_time / 60 / 60), hours) == -1)
1502 return got_error_from_errno("asprintf");
1503 } else if (diff_time > 60 * 2) {
1504 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1506 return got_error_from_errno("asprintf");
1507 } else if (diff_time > 2) {
1508 if (asprintf(repo_age, "%lld %s", diff_time,
1510 return got_error_from_errno("asprintf");
1512 if (asprintf(repo_age, "%s", now) == -1)
1513 return got_error_from_errno("asprintf");
1517 if (gmtime_r(&committer_time, &tm) == NULL)
1518 return got_error_from_errno("gmtime_r");
1520 s = asctime_r(&tm, datebuf);
1522 return got_error_from_errno("asctime_r");
1524 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1525 return got_error_from_errno("asprintf");
1528 if (gmtime_r(&committer_time, &tm) == NULL)
1529 return got_error_from_errno("gmtime_r");
1531 r = strftime(datebuf, sizeof(datebuf),
1532 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1534 return got_error(GOT_ERR_NO_SPACE);
1536 *repo_age = strdup(datebuf);
1537 if (*repo_age == NULL)
1538 return got_error_from_errno("asprintf");