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"
54 static const struct querystring_keys querystring_keys[] = {
59 { "headref", HEADREF },
60 { "index_page", INDEX_PAGE },
65 static const struct action_keys action_keys[] = {
68 { "blobraw", BLOBRAW },
70 { "commits", COMMITS },
74 { "summary", SUMMARY },
81 static const struct got_error *gotweb_init_querystring(struct querystring **);
82 static const struct got_error *gotweb_parse_querystring(struct querystring **,
84 static const struct got_error *gotweb_assign_querystring(struct querystring **,
86 static int gotweb_render_index(struct template *);
87 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
89 static const struct got_error *gotweb_load_got_path(struct request *c,
91 static const struct got_error *gotweb_get_repo_description(char **,
92 struct server *, const char *, int);
93 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 static void gotweb_free_querystring(struct querystring *);
97 static void gotweb_free_repo_dir(struct repo_dir *);
99 struct server *gotweb_get_server(const char *);
102 gotweb_reply(struct request *c, int status, const char *ctype,
103 struct gotweb_url *location)
107 if (status != 200 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
111 if (tp_writes(c->tp, "Location: ") == -1 ||
112 gotweb_render_url(c, location) == -1 ||
113 tp_writes(c->tp, "\r\n") == -1)
117 csp = "Content-Security-Policy: default-src 'self'; "
118 "script-src 'none'; object-src 'none';\r\n";
119 if (tp_writes(c->tp, csp) == -1)
122 if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
125 return tp_writes(c->tp, "\r\n");
129 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
134 r = tp_writef(c->tp, "Content-Disposition: attachment; "
135 "filename=%s%s\r\n", file, suffix ? suffix : "");
138 return gotweb_reply(c, 200, ctype, NULL);
142 gotweb_process_request(struct request *c)
144 const struct got_error *error = NULL;
145 struct server *srv = NULL;
146 struct querystring *qs = NULL;
147 struct repo_dir *repo_dir = NULL;
148 const char *rss_ctype = "application/rss+xml;charset=utf-8";
153 /* init the transport */
154 error = gotweb_init_transport(&c->t);
156 log_warnx("%s: %s", __func__, error->msg);
159 /* don't process any further if client disconnected */
160 if (c->sock->client_status == CLIENT_DISCONNECT)
162 /* get the gotwebd server */
163 srv = gotweb_get_server(c->server_name);
165 log_warnx("%s: error server is NULL", __func__);
169 /* parse our querystring */
170 error = gotweb_init_querystring(&qs);
172 log_warnx("%s: %s", __func__, error->msg);
176 error = gotweb_parse_querystring(&qs, c->querystring);
178 log_warnx("%s: %s", __func__, error->msg);
183 * certain actions require a commit id in the querystring. this stops
184 * bad actors from exploiting this by manually manipulating the
188 if (qs->action == BLAME || qs->action == BLOB ||
189 qs->action == BLOBRAW || qs->action == DIFF) {
190 if (qs->commit == NULL) {
191 error = got_error(GOT_ERR_BAD_QUERYSTRING);
196 if (qs->action != INDEX) {
197 error = gotweb_init_repo_dir(&repo_dir, qs->path);
200 error = gotweb_load_got_path(c, repo_dir);
201 c->t->repo_dir = repo_dir;
202 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
206 if (qs->action == BLOBRAW || qs->action == BLOB) {
207 error = got_get_repo_commits(c, 1);
211 error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
217 switch (qs->action) {
219 error = got_get_repo_commits(c, 1);
221 log_warnx("%s: %s", __func__, error->msg);
224 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
226 gotweb_render_page(c->tp, gotweb_render_blame);
230 struct gotweb_url url = {
235 .commit = qs->commit,
236 .folder = qs->folder,
240 gotweb_reply(c, 302, NULL, &url);
244 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
246 gotweb_render_page(c->tp, gotweb_render_blob);
250 r = gotweb_reply_file(c, "application/octet-stream",
253 r = gotweb_reply(c, 200, "text/plain", NULL);
256 if (template_flush(c->tp) == -1)
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_write(c, buf, len) == -1)
271 error = got_get_repo_commits(c, srv->max_commits_display);
274 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
276 gotweb_render_page(c->tp, gotweb_render_briefs);
279 error = got_get_repo_commits(c, srv->max_commits_display);
281 log_warnx("%s: %s", __func__, error->msg);
284 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
286 gotweb_render_page(c->tp, gotweb_render_commits);
289 error = got_get_repo_commits(c, 1);
291 log_warnx("%s: %s", __func__, error->msg);
294 error = got_open_diff_for_output(&c->t->fp, c);
296 log_warnx("%s: %s", __func__, error->msg);
299 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
301 gotweb_render_page(c->tp, gotweb_render_diff);
304 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
306 if (c->t->nrepos == -1) {
308 error = got_error_from_errno2("scandir",
312 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
314 gotweb_render_page(c->tp, gotweb_render_index);
317 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
320 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
323 gotweb_render_rss(c->tp);
326 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
327 got_ref_cmp_by_name, NULL);
329 log_warnx("%s: got_ref_list: %s", __func__,
333 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
337 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
339 log_warnx("%s: got_get_repo_tags: %s", __func__,
343 qs->action = SUMMARY;
344 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
346 gotweb_render_page(c->tp, gotweb_render_summary);
349 error = got_get_repo_tags(c, 1);
351 log_warnx("%s: %s", __func__, error->msg);
354 if (c->t->tag_count == 0) {
355 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
359 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
361 gotweb_render_page(c->tp, gotweb_render_tag);
364 error = got_get_repo_tags(c, srv->max_commits_display);
366 log_warnx("%s: %s", __func__, error->msg);
369 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
371 gotweb_render_page(c->tp, gotweb_render_tags);
374 error = got_get_repo_commits(c, 1);
376 log_warnx("%s: %s", __func__, error->msg);
379 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
381 gotweb_render_page(c->tp, gotweb_render_tree);
385 error = got_error(GOT_ERR_BAD_QUERYSTRING);
390 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
392 gotweb_render_page(c->tp, gotweb_render_error);
396 gotweb_get_server(const char *server_name)
400 /* check against the server name first */
401 if (*server_name != '\0')
402 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
403 if (strcmp(srv->name, server_name) == 0)
406 /* otherwise, use the first server */
407 return TAILQ_FIRST(&gotwebd_env->servers);
410 const struct got_error *
411 gotweb_init_transport(struct transport **t)
413 const struct got_error *error = NULL;
415 *t = calloc(1, sizeof(**t));
417 return got_error_from_errno2(__func__, "calloc");
419 TAILQ_INIT(&(*t)->repo_commits);
420 TAILQ_INIT(&(*t)->repo_tags);
421 TAILQ_INIT(&(*t)->refs);
428 static const struct got_error *
429 gotweb_init_querystring(struct querystring **qs)
431 const struct got_error *error = NULL;
433 *qs = calloc(1, sizeof(**qs));
435 return got_error_from_errno2(__func__, "calloc");
437 (*qs)->headref = strdup("HEAD");
438 if ((*qs)->headref == NULL) {
441 return got_error_from_errno2(__func__, "strdup");
444 (*qs)->action = INDEX;
445 (*qs)->commit = NULL;
447 (*qs)->folder = NULL;
448 (*qs)->index_page = 0;
454 static const struct got_error *
455 gotweb_parse_querystring(struct querystring **qs, char *qst)
457 const struct got_error *error = NULL;
458 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
459 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
466 return got_error_from_errno2(__func__, "strdup");
471 while (tok1_pair != NULL) {
472 strsep(&tok1_end, "&");
474 tok2 = strdup(tok1_pair);
477 return got_error_from_errno2(__func__, "strdup");
483 while (tok2_pair != NULL) {
484 strsep(&tok2_end, "=");
486 error = gotweb_assign_querystring(qs, tok2_pair,
491 tok2_pair = tok2_end;
494 tok1_pair = tok1_end;
505 * Adapted from usr.sbin/httpd/httpd.c url_decode.
507 static const struct got_error *
508 gotweb_urldecode(char *url)
520 /* Encoding character is followed by two hex chars */
521 if (!isxdigit((unsigned char)p[1]) ||
522 !isxdigit((unsigned char)p[2]) ||
523 (p[1] == '0' && p[2] == '0'))
524 return got_error(GOT_ERR_BAD_QUERYSTRING);
530 * We don't have to validate "hex" because it is
531 * guaranteed to include two hex chars followed by nul.
533 x = strtoul(hex, NULL, 16);
549 static const struct got_error *
550 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
552 const struct got_error *error = NULL;
556 error = gotweb_urldecode(value);
560 for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
561 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
564 switch (querystring_keys[el_cnt].element) {
566 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
567 if (strcmp(value, action_keys[a_cnt].name) != 0)
569 else if (strcmp(value,
570 action_keys[a_cnt].name) == 0){
572 action_keys[a_cnt].action;
580 (*qs)->commit = strdup(value);
581 if ((*qs)->commit == NULL) {
582 error = got_error_from_errno2(__func__,
588 (*qs)->file = strdup(value);
589 if ((*qs)->file == NULL) {
590 error = got_error_from_errno2(__func__,
596 (*qs)->folder = strdup(value);
597 if ((*qs)->folder == NULL) {
598 error = got_error_from_errno2(__func__,
604 free((*qs)->headref);
605 (*qs)->headref = strdup(value);
606 if ((*qs)->headref == NULL) {
607 error = got_error_from_errno2(__func__,
615 (*qs)->index_page = strtonum(value, INT64_MIN,
618 error = got_error_from_errno3(__func__,
622 if ((*qs)->index_page < 0)
623 (*qs)->index_page = 0;
626 (*qs)->path = strdup(value);
627 if ((*qs)->path == NULL) {
628 error = got_error_from_errno2(__func__,
636 (*qs)->page = strtonum(value, INT64_MIN,
639 error = got_error_from_errno3(__func__,
656 gotweb_free_repo_tag(struct repo_tag *rt)
661 free(rt->tag_commit);
662 free(rt->commit_msg);
669 gotweb_free_repo_commit(struct repo_commit *rc)
679 free(rc->commit_msg);
685 gotweb_free_querystring(struct querystring *qs)
698 gotweb_free_repo_dir(struct repo_dir *repo_dir)
700 if (repo_dir != NULL) {
701 free(repo_dir->name);
702 free(repo_dir->owner);
703 free(repo_dir->description);
705 free(repo_dir->path);
711 gotweb_free_transport(struct transport *t)
713 const struct got_error *err;
714 struct repo_commit *rc = NULL, *trc = NULL;
715 struct repo_tag *rt = NULL, *trt = NULL;
718 got_ref_list_free(&t->refs);
719 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
720 TAILQ_REMOVE(&t->repo_commits, rc, entry);
721 gotweb_free_repo_commit(rc);
723 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
724 TAILQ_REMOVE(&t->repo_tags, rt, entry);
725 gotweb_free_repo_tag(rt);
727 gotweb_free_repo_dir(t->repo_dir);
728 gotweb_free_querystring(t->qs);
733 got_object_blob_close(t->blob);
735 err = got_gotweb_closefile(t->fp);
737 log_warnx("%s: got_gotweb_closefile failure: %s",
740 if (t->fd != -1 && close(t->fd) == -1)
741 log_warn("%s: close", __func__);
743 for (i = 0; i < t->nrepos; ++i)
748 got_repo_close(t->repo);
753 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
754 struct gotweb_url *next, int *have_next)
756 struct transport *t = c->t;
757 struct querystring *qs = t->qs;
758 struct server *srv = c->srv;
760 *have_prev = *have_next = 0;
764 if (qs->index_page > 0) {
766 *prev = (struct gotweb_url){
768 .index_page = qs->index_page - 1,
772 if (t->next_disp == srv->max_repos_display &&
773 t->repos_total != (qs->index_page + 1) *
774 srv->max_repos_display) {
776 *next = (struct gotweb_url){
778 .index_page = qs->index_page + 1,
784 if (t->prev_id && qs->commit != NULL &&
785 strcmp(qs->commit, t->prev_id) != 0) {
787 *prev = (struct gotweb_url){
790 .page = qs->page - 1,
792 .commit = t->prev_id,
793 .headref = qs->headref,
798 *next = (struct gotweb_url){
801 .page = qs->page + 1,
803 .commit = t->next_id,
804 .headref = qs->headref,
812 gotweb_render_index(struct template *tp)
814 const struct got_error *error = NULL;
815 struct request *c = tp->tp_arg;
816 struct server *srv = c->srv;
817 struct transport *t = c->t;
818 struct querystring *qs = t->qs;
819 struct repo_dir *repo_dir = NULL;
820 struct dirent **sd_dent = t->repos;
821 unsigned int d_i, d_disp = 0;
822 unsigned int d_skipped = 0;
825 if (gotweb_render_repo_table_hdr(c->tp) == -1)
828 for (d_i = 0; d_i < t->nrepos; d_i++) {
829 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
832 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
833 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
838 error = got_path_dirent_type(&type, srv->repos_path,
842 if (type != DT_DIR) {
847 if (qs->index_page > 0 && (qs->index_page *
848 srv->max_repos_display) > t->prev_disp) {
853 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
857 error = gotweb_load_got_path(c, repo_dir);
858 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
859 if (error->code != GOT_ERR_NOT_GIT_REPO)
860 log_warnx("%s: %s: %s", __func__,
861 sd_dent[d_i]->d_name, error->msg);
862 gotweb_free_repo_dir(repo_dir);
871 r = gotweb_render_repo_fragment(c->tp, repo_dir);
872 gotweb_free_repo_dir(repo_dir);
874 got_repo_close(t->repo);
880 if (d_disp == srv->max_repos_display)
883 t->repos_total = t->nrepos - d_skipped;
885 if (srv->max_repos_display == 0)
887 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
889 if (t->repos_total <= srv->max_repos ||
890 t->repos_total <= srv->max_repos_display)
893 if (gotweb_render_navs(c->tp) == -1)
900 should_urlencode(int c)
902 if (c <= ' ' || c >= 127)
926 /* needed because the URLs are embedded into the HTML */
935 gotweb_urlencode(const char *str)
943 for (s = str; *s; ++s) {
945 if (should_urlencode(*s))
949 escaped = calloc(1, len + 1);
954 for (s = str; *s; ++s) {
955 if (should_urlencode(*s)) {
956 a = (*s & 0xF0) >> 4;
960 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
961 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
970 gotweb_action_name(int action)
1005 gotweb_render_url(struct request *c, struct gotweb_url *url)
1007 const char *sep = "?", *action;
1011 action = gotweb_action_name(url->action);
1012 if (action != NULL) {
1013 if (tp_writef(c->tp, "?action=%s", action) == -1)
1019 if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
1025 if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
1031 if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
1037 tmp = gotweb_urlencode(url->file);
1040 r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1048 tmp = gotweb_urlencode(url->folder);
1051 r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1059 tmp = gotweb_urlencode(url->headref);
1062 r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1069 if (url->index_page != -1) {
1070 if (tp_writef(c->tp, "%sindex_page=%d", sep,
1071 url->index_page) == -1)
1077 tmp = gotweb_urlencode(url->path);
1080 r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1087 if (url->page != -1) {
1088 if (tp_writef(c->tp, "%spage=%d", sep, url->page) == -1)
1097 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1099 struct template *tp = c->tp;
1100 const char *proto = c->https ? "https" : "http";
1102 if (tp_writes(tp, proto) == -1 ||
1103 tp_writes(tp, "://") == -1 ||
1104 tp_htmlescape(tp, c->server_name) == -1 ||
1105 tp_htmlescape(tp, c->document_uri) == -1)
1108 return gotweb_render_url(c, url);
1111 static const struct got_error *
1112 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1114 const struct got_error *error = NULL;
1115 struct socket *sock = c->sock;
1116 struct server *srv = c->srv;
1117 struct transport *t = c->t;
1121 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1122 GOTWEB_GIT_DIR) == -1)
1123 return got_error_from_errno("asprintf");
1125 dt = opendir(dir_test);
1129 repo_dir->path = dir_test;
1134 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1135 repo_dir->name) == -1) {
1136 error = got_error_from_errno("asprintf");
1140 dt = opendir(dir_test);
1142 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1145 repo_dir->path = dir_test;
1150 if (srv->respect_exportok &&
1151 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1152 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1156 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1159 error = gotweb_get_repo_description(&repo_dir->description, srv,
1160 repo_dir->path, dirfd(dt));
1163 error = got_get_repo_owner(&repo_dir->owner, c);
1166 if (srv->show_repo_age) {
1167 error = got_get_repo_age(&repo_dir->age, c, NULL);
1171 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1175 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1176 error = got_error_from_errno("closedir");
1177 if (error && t->repo) {
1178 got_repo_close(t->repo);
1184 static const struct got_error *
1185 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1187 const struct got_error *error;
1189 *repo_dir = calloc(1, sizeof(**repo_dir));
1190 if (*repo_dir == NULL)
1191 return got_error_from_errno("calloc");
1193 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1194 error = got_error_from_errno("asprintf");
1199 (*repo_dir)->owner = NULL;
1200 (*repo_dir)->description = NULL;
1201 (*repo_dir)->url = NULL;
1202 (*repo_dir)->path = NULL;
1207 static const struct got_error *
1208 gotweb_get_repo_description(char **description, struct server *srv,
1209 const char *dirpath, int dir)
1211 const struct got_error *error = NULL;
1216 *description = NULL;
1217 if (srv->show_repo_description == 0)
1220 fd = openat(dir, "description", O_RDONLY);
1222 if (errno != ENOENT && errno != EACCES) {
1223 error = got_error_from_errno_fmt("openat %s/%s",
1224 dirpath, "description");
1229 if (fstat(fd, &sb) == -1) {
1230 error = got_error_from_errno_fmt("fstat %s/%s",
1231 dirpath, "description");
1236 if (len > GOTWEBD_MAXDESCRSZ - 1)
1237 len = GOTWEBD_MAXDESCRSZ - 1;
1239 *description = calloc(len + 1, sizeof(**description));
1240 if (*description == NULL) {
1241 error = got_error_from_errno("calloc");
1245 if (read(fd, *description, len) == -1)
1246 error = got_error_from_errno("read");
1248 if (fd != -1 && close(fd) == -1 && error == NULL)
1249 error = got_error_from_errno("close");
1253 static const struct got_error *
1254 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1257 const struct got_error *error = NULL;
1263 if (srv->show_repo_cloneurl == 0)
1266 fd = openat(dir, "cloneurl", O_RDONLY);
1268 if (errno != ENOENT && errno != EACCES) {
1269 error = got_error_from_errno_fmt("openat %s/%s",
1270 dirpath, "cloneurl");
1275 if (fstat(fd, &sb) == -1) {
1276 error = got_error_from_errno_fmt("fstat %s/%s",
1277 dirpath, "cloneurl");
1282 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1283 len = GOTWEBD_MAXCLONEURLSZ - 1;
1285 *url = calloc(len + 1, sizeof(**url));
1287 error = got_error_from_errno("calloc");
1291 if (read(fd, *url, 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");
1300 gotweb_render_age(struct template *tp, time_t committer_time)
1302 struct request *c = tp->tp_arg;
1303 long long diff_time;
1304 const char *years = "years ago", *months = "months ago";
1305 const char *weeks = "weeks ago", *days = "days ago";
1306 const char *hours = "hours ago", *minutes = "minutes ago";
1307 const char *seconds = "seconds ago", *now = "right now";
1309 diff_time = time(NULL) - committer_time;
1310 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1311 if (tp_writef(c->tp, "%lld %s",
1312 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1314 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1315 if (tp_writef(c->tp, "%lld %s",
1316 (diff_time / 60 / 60 / 24 / (365 / 12)),
1319 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1320 if (tp_writef(c->tp, "%lld %s",
1321 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1323 } else if (diff_time > 60 * 60 * 24 * 2) {
1324 if (tp_writef(c->tp, "%lld %s",
1325 (diff_time / 60 / 60 / 24), days) == -1)
1327 } else if (diff_time > 60 * 60 * 2) {
1328 if (tp_writef(c->tp, "%lld %s",
1329 (diff_time / 60 / 60), hours) == -1)
1331 } else if (diff_time > 60 * 2) {
1332 if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1335 } else if (diff_time > 2) {
1336 if (tp_writef(c->tp, "%lld %s", diff_time,
1340 if (tp_writes(tp, now) == -1)