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"
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 },
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 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
113 if (tp_writes(c->tp, "Location: ") == -1 ||
114 gotweb_render_url(c, location) == -1 ||
115 tp_writes(c->tp, "\r\n") == -1)
119 csp = "Content-Security-Policy: default-src 'self'; "
120 "script-src 'none'; object-src 'none';\r\n";
121 if (tp_writes(c->tp, csp) == -1)
124 if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
127 return tp_writes(c->tp, "\r\n");
131 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
136 r = tp_writef(c->tp, "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 qs->action == PATCH) {
193 if (qs->commit == NULL) {
194 error = got_error(GOT_ERR_BAD_QUERYSTRING);
199 if (qs->action != INDEX) {
200 error = gotweb_init_repo_dir(&repo_dir, qs->path);
203 error = gotweb_load_got_path(c, repo_dir);
204 c->t->repo_dir = repo_dir;
205 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
209 if (qs->action == BLOBRAW || qs->action == BLOB) {
210 error = got_get_repo_commits(c, 1);
214 error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
215 &binary, c, qs->folder, qs->file, qs->commit);
220 switch (qs->action) {
222 error = got_get_repo_commits(c, 1);
224 log_warnx("%s: %s", __func__, error->msg);
227 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
229 gotweb_render_page(c->tp, gotweb_render_blame);
233 struct gotweb_url url = {
238 .commit = qs->commit,
239 .folder = qs->folder,
243 gotweb_reply(c, 302, NULL, &url);
247 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
249 gotweb_render_page(c->tp, gotweb_render_blob);
253 r = gotweb_reply_file(c, "application/octet-stream",
256 r = gotweb_reply(c, 200, "text/plain", NULL);
259 if (template_flush(c->tp) == -1)
263 error = got_object_blob_read_block(&len, c->t->blob);
268 buf = got_object_blob_get_read_buf(c->t->blob);
269 if (fcgi_write(c, buf, len) == -1)
274 error = got_get_repo_commits(c, srv->max_commits_display);
277 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
279 gotweb_render_page(c->tp, gotweb_render_briefs);
282 error = got_get_repo_commits(c, srv->max_commits_display);
284 log_warnx("%s: %s", __func__, error->msg);
287 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
289 gotweb_render_page(c->tp, gotweb_render_commits);
292 error = got_get_repo_commits(c, 1);
294 log_warnx("%s: %s", __func__, error->msg);
297 error = got_open_diff_for_output(&c->t->fp, c);
299 log_warnx("%s: %s", __func__, error->msg);
302 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
304 gotweb_render_page(c->tp, gotweb_render_diff);
307 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
309 if (c->t->nrepos == -1) {
311 error = got_error_from_errno2("scandir",
315 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
317 gotweb_render_page(c->tp, gotweb_render_index);
320 error = got_get_repo_commits(c, 1);
322 log_warnx("%s: %s", __func__, error->msg);
325 error = got_open_diff_for_output(&c->t->fp, c);
327 log_warnx("%s: %s", __func__, error->msg);
330 if (gotweb_reply(c, 200, "text/plain", NULL) == -1)
332 gotweb_render_patch(c->tp);
335 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
338 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
341 gotweb_render_rss(c->tp);
344 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
345 got_ref_cmp_by_name, NULL);
347 log_warnx("%s: got_ref_list: %s", __func__,
351 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
355 error = got_get_repo_tags(c, D_MAXSLTAGDISP);
357 log_warnx("%s: got_get_repo_tags: %s", __func__,
361 qs->action = SUMMARY;
362 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
364 gotweb_render_page(c->tp, gotweb_render_summary);
367 error = got_get_repo_tags(c, 1);
369 log_warnx("%s: %s", __func__, error->msg);
372 if (c->t->tag_count == 0) {
373 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
377 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
379 gotweb_render_page(c->tp, gotweb_render_tag);
382 error = got_get_repo_tags(c, srv->max_commits_display);
384 log_warnx("%s: %s", __func__, error->msg);
387 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
389 gotweb_render_page(c->tp, gotweb_render_tags);
392 error = got_get_repo_commits(c, 1);
394 log_warnx("%s: %s", __func__, error->msg);
397 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
399 gotweb_render_page(c->tp, gotweb_render_tree);
403 error = got_error(GOT_ERR_BAD_QUERYSTRING);
408 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
410 gotweb_render_page(c->tp, gotweb_render_error);
414 gotweb_get_server(const char *server_name)
418 /* check against the server name first */
419 if (*server_name != '\0')
420 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
421 if (strcmp(srv->name, server_name) == 0)
424 /* otherwise, use the first server */
425 return TAILQ_FIRST(&gotwebd_env->servers);
428 const struct got_error *
429 gotweb_init_transport(struct transport **t)
431 const struct got_error *error = NULL;
433 *t = calloc(1, sizeof(**t));
435 return got_error_from_errno2(__func__, "calloc");
437 TAILQ_INIT(&(*t)->repo_commits);
438 TAILQ_INIT(&(*t)->repo_tags);
439 TAILQ_INIT(&(*t)->refs);
446 static const struct got_error *
447 gotweb_init_querystring(struct querystring **qs)
449 const struct got_error *error = NULL;
451 *qs = calloc(1, sizeof(**qs));
453 return got_error_from_errno2(__func__, "calloc");
455 (*qs)->headref = strdup("HEAD");
456 if ((*qs)->headref == NULL) {
459 return got_error_from_errno2(__func__, "strdup");
462 (*qs)->action = INDEX;
467 static const struct got_error *
468 gotweb_parse_querystring(struct querystring **qs, char *qst)
470 const struct got_error *error = NULL;
471 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
472 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
479 return got_error_from_errno2(__func__, "strdup");
484 while (tok1_pair != NULL) {
485 strsep(&tok1_end, "&");
487 tok2 = strdup(tok1_pair);
490 return got_error_from_errno2(__func__, "strdup");
496 while (tok2_pair != NULL) {
497 strsep(&tok2_end, "=");
499 error = gotweb_assign_querystring(qs, tok2_pair,
504 tok2_pair = tok2_end;
507 tok1_pair = tok1_end;
518 * Adapted from usr.sbin/httpd/httpd.c url_decode.
520 static const struct got_error *
521 gotweb_urldecode(char *url)
533 /* Encoding character is followed by two hex chars */
534 if (!isxdigit((unsigned char)p[1]) ||
535 !isxdigit((unsigned char)p[2]) ||
536 (p[1] == '0' && p[2] == '0'))
537 return got_error(GOT_ERR_BAD_QUERYSTRING);
543 * We don't have to validate "hex" because it is
544 * guaranteed to include two hex chars followed by nul.
546 x = strtoul(hex, NULL, 16);
562 static const struct got_error *
563 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
565 const struct got_error *error = NULL;
569 error = gotweb_urldecode(value);
573 for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
574 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
577 switch (querystring_keys[el_cnt].element) {
579 for (a_cnt = 0; a_cnt < nitems(action_keys); a_cnt++) {
580 if (strcmp(value, action_keys[a_cnt].name) != 0)
582 else if (strcmp(value,
583 action_keys[a_cnt].name) == 0){
585 action_keys[a_cnt].action;
593 (*qs)->commit = strdup(value);
594 if ((*qs)->commit == NULL) {
595 error = got_error_from_errno2(__func__,
601 (*qs)->file = strdup(value);
602 if ((*qs)->file == NULL) {
603 error = got_error_from_errno2(__func__,
609 (*qs)->folder = strdup(value);
610 if ((*qs)->folder == NULL) {
611 error = got_error_from_errno2(__func__,
617 free((*qs)->headref);
618 (*qs)->headref = strdup(value);
619 if ((*qs)->headref == NULL) {
620 error = got_error_from_errno2(__func__,
628 (*qs)->index_page = strtonum(value, INT64_MIN,
631 error = got_error_from_errno3(__func__,
635 if ((*qs)->index_page < 0)
636 (*qs)->index_page = 0;
639 (*qs)->path = strdup(value);
640 if ((*qs)->path == NULL) {
641 error = got_error_from_errno2(__func__,
649 (*qs)->page = strtonum(value, INT64_MIN,
652 error = got_error_from_errno3(__func__,
669 gotweb_free_repo_tag(struct repo_tag *rt)
674 free(rt->tag_commit);
675 free(rt->commit_msg);
682 gotweb_free_repo_commit(struct repo_commit *rc)
692 free(rc->commit_msg);
698 gotweb_free_querystring(struct querystring *qs)
711 gotweb_free_repo_dir(struct repo_dir *repo_dir)
713 if (repo_dir != NULL) {
714 free(repo_dir->name);
715 free(repo_dir->owner);
716 free(repo_dir->description);
718 free(repo_dir->path);
724 gotweb_free_transport(struct transport *t)
726 const struct got_error *err;
727 struct repo_commit *rc = NULL, *trc = NULL;
728 struct repo_tag *rt = NULL, *trt = NULL;
731 got_ref_list_free(&t->refs);
732 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
733 TAILQ_REMOVE(&t->repo_commits, rc, entry);
734 gotweb_free_repo_commit(rc);
736 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
737 TAILQ_REMOVE(&t->repo_tags, rt, entry);
738 gotweb_free_repo_tag(rt);
740 gotweb_free_repo_dir(t->repo_dir);
741 gotweb_free_querystring(t->qs);
743 free(t->tags_more_id);
745 got_object_blob_close(t->blob);
747 err = got_gotweb_closefile(t->fp);
749 log_warnx("%s: got_gotweb_closefile failure: %s",
752 if (t->fd != -1 && close(t->fd) == -1)
753 log_warn("%s: close", __func__);
755 for (i = 0; i < t->nrepos; ++i)
760 got_repo_close(t->repo);
765 gotweb_index_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
766 struct gotweb_url *next, int *have_next)
768 struct transport *t = c->t;
769 struct querystring *qs = t->qs;
770 struct server *srv = c->srv;
772 *have_prev = *have_next = 0;
774 if (qs->index_page > 0) {
776 *prev = (struct gotweb_url){
778 .index_page = qs->index_page - 1,
782 if (t->next_disp == srv->max_repos_display &&
783 t->repos_total != (qs->index_page + 1) *
784 srv->max_repos_display) {
786 *next = (struct gotweb_url){
788 .index_page = qs->index_page + 1,
795 gotweb_render_index(struct template *tp)
797 const struct got_error *error = NULL;
798 struct request *c = tp->tp_arg;
799 struct server *srv = c->srv;
800 struct transport *t = c->t;
801 struct querystring *qs = t->qs;
802 struct repo_dir *repo_dir = NULL;
803 struct dirent **sd_dent = t->repos;
804 unsigned int d_i, d_disp = 0;
805 unsigned int d_skipped = 0;
808 if (gotweb_render_repo_table_hdr(c->tp) == -1)
811 for (d_i = 0; d_i < t->nrepos; d_i++) {
812 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
815 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
816 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
821 error = got_path_dirent_type(&type, srv->repos_path,
825 if (type != DT_DIR) {
830 if (qs->index_page > 0 && (qs->index_page *
831 srv->max_repos_display) > t->prev_disp) {
836 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
840 error = gotweb_load_got_path(c, repo_dir);
841 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
842 if (error->code != GOT_ERR_NOT_GIT_REPO)
843 log_warnx("%s: %s: %s", __func__,
844 sd_dent[d_i]->d_name, error->msg);
845 gotweb_free_repo_dir(repo_dir);
854 r = gotweb_render_repo_fragment(c->tp, repo_dir);
855 gotweb_free_repo_dir(repo_dir);
857 got_repo_close(t->repo);
863 if (d_disp == srv->max_repos_display)
866 t->repos_total = t->nrepos - d_skipped;
868 if (srv->max_repos_display == 0)
870 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
872 if (t->repos_total <= srv->max_repos ||
873 t->repos_total <= srv->max_repos_display)
876 if (gotweb_render_navs(c->tp) == -1)
883 should_urlencode(int c)
885 if (c <= ' ' || c >= 127)
909 /* needed because the URLs are embedded into the HTML */
918 gotweb_urlencode(const char *str)
926 for (s = str; *s; ++s) {
928 if (should_urlencode(*s))
932 escaped = calloc(1, len + 1);
937 for (s = str; *s; ++s) {
938 if (should_urlencode(*s)) {
939 a = (*s & 0xF0) >> 4;
943 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
944 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
953 gotweb_action_name(int action)
990 gotweb_render_url(struct request *c, struct gotweb_url *url)
992 const char *sep = "?", *action;
996 action = gotweb_action_name(url->action);
997 if (action != NULL) {
998 if (tp_writef(c->tp, "?action=%s", action) == -1)
1004 if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
1010 if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
1016 if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
1022 tmp = gotweb_urlencode(url->file);
1025 r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1033 tmp = gotweb_urlencode(url->folder);
1036 r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1044 tmp = gotweb_urlencode(url->headref);
1047 r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1054 if (url->index_page != -1) {
1055 if (tp_writef(c->tp, "%sindex_page=%d", sep,
1056 url->index_page) == -1)
1062 tmp = gotweb_urlencode(url->path);
1065 r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1072 if (url->page != -1) {
1073 if (tp_writef(c->tp, "%spage=%d", sep, url->page) == -1)
1082 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1084 struct template *tp = c->tp;
1085 const char *proto = c->https ? "https" : "http";
1087 if (tp_writes(tp, proto) == -1 ||
1088 tp_writes(tp, "://") == -1 ||
1089 tp_htmlescape(tp, c->server_name) == -1 ||
1090 tp_htmlescape(tp, c->document_uri) == -1)
1093 return gotweb_render_url(c, url);
1096 static const struct got_error *
1097 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1099 const struct got_error *error = NULL;
1100 struct socket *sock = c->sock;
1101 struct server *srv = c->srv;
1102 struct transport *t = c->t;
1106 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1107 GOTWEB_GIT_DIR) == -1)
1108 return got_error_from_errno("asprintf");
1110 dt = opendir(dir_test);
1113 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1114 repo_dir->name) == -1)
1115 return got_error_from_errno("asprintf");
1116 dt = opendir(dir_test);
1119 return got_error_path(repo_dir->name,
1120 GOT_ERR_NOT_GIT_REPO);
1124 repo_dir->path = dir_test;
1127 if (srv->respect_exportok &&
1128 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1129 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1133 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1136 error = gotweb_get_repo_description(&repo_dir->description, srv,
1137 repo_dir->path, dirfd(dt));
1140 error = got_get_repo_owner(&repo_dir->owner, c);
1143 if (srv->show_repo_age) {
1144 error = got_get_repo_age(&repo_dir->age, c, NULL);
1148 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1152 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1153 error = got_error_from_errno("closedir");
1154 if (error && t->repo) {
1155 got_repo_close(t->repo);
1161 static const struct got_error *
1162 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1164 const struct got_error *error;
1166 *repo_dir = calloc(1, sizeof(**repo_dir));
1167 if (*repo_dir == NULL)
1168 return got_error_from_errno("calloc");
1170 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1171 error = got_error_from_errno("asprintf");
1176 (*repo_dir)->owner = NULL;
1177 (*repo_dir)->description = NULL;
1178 (*repo_dir)->url = NULL;
1179 (*repo_dir)->path = NULL;
1184 static const struct got_error *
1185 gotweb_get_repo_description(char **description, struct server *srv,
1186 const char *dirpath, int dir)
1188 const struct got_error *error = NULL;
1193 *description = NULL;
1194 if (srv->show_repo_description == 0)
1197 fd = openat(dir, "description", O_RDONLY);
1199 if (errno != ENOENT && errno != EACCES) {
1200 error = got_error_from_errno_fmt("openat %s/%s",
1201 dirpath, "description");
1206 if (fstat(fd, &sb) == -1) {
1207 error = got_error_from_errno_fmt("fstat %s/%s",
1208 dirpath, "description");
1213 if (len > GOTWEBD_MAXDESCRSZ - 1)
1214 len = GOTWEBD_MAXDESCRSZ - 1;
1216 *description = calloc(len + 1, sizeof(**description));
1217 if (*description == NULL) {
1218 error = got_error_from_errno("calloc");
1222 if (read(fd, *description, len) == -1)
1223 error = got_error_from_errno("read");
1225 if (fd != -1 && close(fd) == -1 && error == NULL)
1226 error = got_error_from_errno("close");
1230 static const struct got_error *
1231 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1234 const struct got_error *error = NULL;
1240 if (srv->show_repo_cloneurl == 0)
1243 fd = openat(dir, "cloneurl", O_RDONLY);
1245 if (errno != ENOENT && errno != EACCES) {
1246 error = got_error_from_errno_fmt("openat %s/%s",
1247 dirpath, "cloneurl");
1252 if (fstat(fd, &sb) == -1) {
1253 error = got_error_from_errno_fmt("fstat %s/%s",
1254 dirpath, "cloneurl");
1259 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1260 len = GOTWEBD_MAXCLONEURLSZ - 1;
1262 *url = calloc(len + 1, sizeof(**url));
1264 error = got_error_from_errno("calloc");
1268 if (read(fd, *url, len) == -1)
1269 error = got_error_from_errno("read");
1271 if (fd != -1 && close(fd) == -1 && error == NULL)
1272 error = got_error_from_errno("close");
1277 gotweb_render_age(struct template *tp, time_t committer_time)
1279 struct request *c = tp->tp_arg;
1280 long long diff_time;
1281 const char *years = "years ago", *months = "months ago";
1282 const char *weeks = "weeks ago", *days = "days ago";
1283 const char *hours = "hours ago", *minutes = "minutes ago";
1284 const char *seconds = "seconds ago", *now = "right now";
1286 diff_time = time(NULL) - committer_time;
1287 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1288 if (tp_writef(c->tp, "%lld %s",
1289 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1291 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1292 if (tp_writef(c->tp, "%lld %s",
1293 (diff_time / 60 / 60 / 24 / (365 / 12)),
1296 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1297 if (tp_writef(c->tp, "%lld %s",
1298 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1300 } else if (diff_time > 60 * 60 * 24 * 2) {
1301 if (tp_writef(c->tp, "%lld %s",
1302 (diff_time / 60 / 60 / 24), days) == -1)
1304 } else if (diff_time > 60 * 60 * 2) {
1305 if (tp_writef(c->tp, "%lld %s",
1306 (diff_time / 60 / 60), hours) == -1)
1308 } else if (diff_time > 60 * 2) {
1309 if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1312 } else if (diff_time > 2) {
1313 if (tp_writef(c->tp, "%lld %s", diff_time,
1317 if (tp_writes(tp, now) == -1)