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 },
65 static const struct action_keys action_keys[] = {
68 { "blobraw", BLOBRAW },
70 { "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 int gotweb_render_index(struct template *);
88 static const struct got_error *gotweb_load_got_path(struct repo_dir **,
89 const char *, struct request *);
90 static const struct got_error *gotweb_get_repo_description(char **,
91 struct server *, const char *, int);
92 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
95 static void gotweb_free_querystring(struct querystring *);
96 static void gotweb_free_repo_dir(struct repo_dir *);
98 struct server *gotweb_get_server(const char *);
101 gotweb_reply(struct request *c, int status, const char *ctype,
102 struct gotweb_url *location)
106 if (status != 200 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
110 if (tp_writes(c->tp, "Location: ") == -1 ||
111 gotweb_render_url(c, location) == -1 ||
112 tp_writes(c->tp, "\r\n") == -1)
116 csp = "Content-Security-Policy: default-src 'self'; "
117 "script-src 'none'; object-src 'none';\r\n";
118 if (tp_writes(c->tp, csp) == -1)
121 if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
124 return tp_writes(c->tp, "\r\n");
128 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
133 r = tp_writef(c->tp, "Content-Disposition: attachment; "
134 "filename=%s%s\r\n", file, suffix ? suffix : "");
137 return gotweb_reply(c, 200, ctype, NULL);
141 gotweb_process_request(struct request *c)
143 const struct got_error *error = NULL;
144 struct server *srv = NULL;
145 struct querystring *qs = NULL;
146 struct repo_dir *repo_dir = NULL;
147 const char *rss_ctype = "application/rss+xml;charset=utf-8";
152 /* init the transport */
153 error = gotweb_init_transport(&c->t);
155 log_warnx("%s: %s", __func__, error->msg);
158 /* don't process any further if client disconnected */
159 if (c->sock->client_status == CLIENT_DISCONNECT)
161 /* get the gotwebd server */
162 srv = gotweb_get_server(c->server_name);
164 log_warnx("%s: error server is NULL", __func__);
168 /* parse our querystring */
169 error = gotweb_init_querystring(&qs);
171 log_warnx("%s: %s", __func__, error->msg);
175 error = gotweb_parse_querystring(&qs, c->querystring);
177 log_warnx("%s: %s", __func__, error->msg);
182 * certain actions require a commit id in the querystring. this stops
183 * bad actors from exploiting this by manually manipulating the
187 if (qs->action == BLAME || qs->action == BLOB ||
188 qs->action == BLOBRAW || qs->action == DIFF ||
189 qs->action == PATCH) {
190 if (qs->commit == NULL) {
191 error = got_error(GOT_ERR_BAD_QUERYSTRING);
196 if (qs->action != INDEX) {
197 error = gotweb_load_got_path(&repo_dir, qs->path, c);
198 c->t->repo_dir = repo_dir;
199 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
203 if (qs->action == BLOBRAW || qs->action == BLOB) {
204 if (qs->folder == NULL || qs->file == NULL) {
205 error = got_error(GOT_ERR_BAD_QUERYSTRING);
209 error = got_get_repo_commits(c, 1);
213 error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
214 &binary, c, qs->folder, qs->file, qs->commit);
219 switch (qs->action) {
221 if (qs->folder == NULL || qs->file == NULL) {
222 error = got_error(GOT_ERR_BAD_QUERYSTRING);
225 error = got_get_repo_commits(c, 1);
227 log_warnx("%s: %s", __func__, error->msg);
230 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
232 gotweb_render_page(c->tp, gotweb_render_blame);
236 struct gotweb_url url = {
240 .commit = qs->commit,
241 .folder = qs->folder,
245 gotweb_reply(c, 302, NULL, &url);
249 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
251 gotweb_render_page(c->tp, gotweb_render_blob);
255 r = gotweb_reply_file(c, "application/octet-stream",
258 r = gotweb_reply(c, 200, "text/plain", NULL);
261 if (template_flush(c->tp) == -1)
265 error = got_object_blob_read_block(&len, c->t->blob);
270 buf = got_object_blob_get_read_buf(c->t->blob);
271 if (fcgi_write(c, buf, len) == -1)
276 error = got_get_repo_commits(c, srv->max_commits_display);
279 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
281 gotweb_render_page(c->tp, gotweb_render_briefs);
284 error = got_get_repo_commits(c, srv->max_commits_display);
286 log_warnx("%s: %s", __func__, error->msg);
289 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
291 gotweb_render_page(c->tp, gotweb_render_commits);
294 error = got_get_repo_commits(c, 1);
296 log_warnx("%s: %s", __func__, error->msg);
299 error = got_open_diff_for_output(&c->t->fp, c);
301 log_warnx("%s: %s", __func__, error->msg);
304 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
306 gotweb_render_page(c->tp, gotweb_render_diff);
309 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
311 if (c->t->nrepos == -1) {
313 error = got_error_from_errno2("scandir",
317 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
319 gotweb_render_page(c->tp, gotweb_render_index);
322 error = got_get_repo_commits(c, 1);
324 log_warnx("%s: %s", __func__, error->msg);
327 error = got_open_diff_for_output(&c->t->fp, c);
329 log_warnx("%s: %s", __func__, error->msg);
332 if (gotweb_reply(c, 200, "text/plain", NULL) == -1)
334 gotweb_render_patch(c->tp);
337 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
340 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
343 gotweb_render_rss(c->tp);
346 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
347 got_ref_cmp_by_name, NULL);
349 log_warnx("%s: got_ref_list: %s", __func__,
353 error = got_get_repo_commits(c, srv->summary_commits_display);
357 error = got_get_repo_tags(c, srv->summary_tags_display);
359 log_warnx("%s: got_get_repo_tags: %s", __func__,
363 qs->action = SUMMARY;
364 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
366 gotweb_render_page(c->tp, gotweb_render_summary);
369 error = got_get_repo_tags(c, 1);
371 log_warnx("%s: %s", __func__, error->msg);
374 if (c->t->tag_count == 0) {
375 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
379 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
381 gotweb_render_page(c->tp, gotweb_render_tag);
384 error = got_get_repo_tags(c, srv->max_commits_display);
386 log_warnx("%s: %s", __func__, error->msg);
389 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
391 gotweb_render_page(c->tp, gotweb_render_tags);
394 error = got_get_repo_commits(c, 1);
396 log_warnx("%s: %s", __func__, error->msg);
399 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
401 gotweb_render_page(c->tp, gotweb_render_tree);
405 error = got_error(GOT_ERR_BAD_QUERYSTRING);
410 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
412 gotweb_render_page(c->tp, gotweb_render_error);
416 gotweb_get_server(const char *server_name)
420 /* check against the server name first */
421 if (*server_name != '\0')
422 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
423 if (strcmp(srv->name, server_name) == 0)
426 /* otherwise, use the first server */
427 return TAILQ_FIRST(&gotwebd_env->servers);
430 const struct got_error *
431 gotweb_init_transport(struct transport **t)
433 const struct got_error *error = NULL;
435 *t = calloc(1, sizeof(**t));
437 return got_error_from_errno2(__func__, "calloc");
439 TAILQ_INIT(&(*t)->repo_commits);
440 TAILQ_INIT(&(*t)->repo_tags);
441 TAILQ_INIT(&(*t)->refs);
448 static const struct got_error *
449 gotweb_init_querystring(struct querystring **qs)
451 const struct got_error *error = NULL;
453 *qs = calloc(1, sizeof(**qs));
455 return got_error_from_errno2(__func__, "calloc");
457 (*qs)->headref = strdup("HEAD");
458 if ((*qs)->headref == NULL) {
461 return got_error_from_errno2(__func__, "strdup");
464 (*qs)->action = INDEX;
469 static const struct got_error *
470 gotweb_parse_querystring(struct querystring **qs, char *qst)
472 const struct got_error *error = NULL;
473 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
474 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
481 return got_error_from_errno2(__func__, "strdup");
486 while (tok1_pair != NULL) {
487 strsep(&tok1_end, "&");
489 tok2 = strdup(tok1_pair);
492 return got_error_from_errno2(__func__, "strdup");
498 while (tok2_pair != NULL) {
499 strsep(&tok2_end, "=");
501 error = gotweb_assign_querystring(qs, tok2_pair,
506 tok2_pair = tok2_end;
509 tok1_pair = tok1_end;
520 * Adapted from usr.sbin/httpd/httpd.c url_decode.
522 static const struct got_error *
523 gotweb_urldecode(char *url)
535 /* Encoding character is followed by two hex chars */
536 if (!isxdigit((unsigned char)p[1]) ||
537 !isxdigit((unsigned char)p[2]) ||
538 (p[1] == '0' && p[2] == '0'))
539 return got_error(GOT_ERR_BAD_QUERYSTRING);
545 * We don't have to validate "hex" because it is
546 * guaranteed to include two hex chars followed by nul.
548 x = strtoul(hex, NULL, 16);
564 static const struct got_error *
565 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
567 const struct got_error *error = NULL;
571 error = gotweb_urldecode(value);
575 for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
576 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
579 switch (querystring_keys[el_cnt].element) {
581 for (a_cnt = 0; a_cnt < nitems(action_keys); a_cnt++) {
582 if (strcmp(value, action_keys[a_cnt].name) != 0)
584 else if (strcmp(value,
585 action_keys[a_cnt].name) == 0){
587 action_keys[a_cnt].action;
595 (*qs)->commit = strdup(value);
596 if ((*qs)->commit == NULL) {
597 error = got_error_from_errno2(__func__,
603 (*qs)->file = strdup(value);
604 if ((*qs)->file == NULL) {
605 error = got_error_from_errno2(__func__,
611 (*qs)->folder = strdup(value);
612 if ((*qs)->folder == NULL) {
613 error = got_error_from_errno2(__func__,
619 free((*qs)->headref);
620 (*qs)->headref = strdup(value);
621 if ((*qs)->headref == NULL) {
622 error = got_error_from_errno2(__func__,
630 (*qs)->index_page = strtonum(value, INT64_MIN,
633 error = got_error_from_errno3(__func__,
637 if ((*qs)->index_page < 0)
638 (*qs)->index_page = 0;
641 (*qs)->path = strdup(value);
642 if ((*qs)->path == NULL) {
643 error = got_error_from_errno2(__func__,
658 gotweb_free_repo_tag(struct repo_tag *rt)
663 free(rt->tag_commit);
664 free(rt->commit_msg);
671 gotweb_free_repo_commit(struct repo_commit *rc)
681 free(rc->commit_msg);
687 gotweb_free_querystring(struct querystring *qs)
700 gotweb_free_repo_dir(struct repo_dir *repo_dir)
702 if (repo_dir != NULL) {
703 free(repo_dir->name);
704 free(repo_dir->owner);
705 free(repo_dir->description);
707 free(repo_dir->path);
713 gotweb_free_transport(struct transport *t)
715 const struct got_error *err;
716 struct repo_commit *rc = NULL, *trc = NULL;
717 struct repo_tag *rt = NULL, *trt = NULL;
720 got_ref_list_free(&t->refs);
721 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
722 TAILQ_REMOVE(&t->repo_commits, rc, entry);
723 gotweb_free_repo_commit(rc);
725 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
726 TAILQ_REMOVE(&t->repo_tags, rt, entry);
727 gotweb_free_repo_tag(rt);
729 gotweb_free_repo_dir(t->repo_dir);
730 gotweb_free_querystring(t->qs);
732 free(t->tags_more_id);
734 got_object_blob_close(t->blob);
736 err = got_gotweb_closefile(t->fp);
738 log_warnx("%s: got_gotweb_closefile failure: %s",
741 if (t->fd != -1 && close(t->fd) == -1)
742 log_warn("%s: close", __func__);
744 for (i = 0; i < t->nrepos; ++i)
749 got_repo_close(t->repo);
754 gotweb_index_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
755 struct gotweb_url *next, int *have_next)
757 struct transport *t = c->t;
758 struct querystring *qs = t->qs;
759 struct server *srv = c->srv;
761 *have_prev = *have_next = 0;
763 if (qs->index_page > 0) {
765 *prev = (struct gotweb_url){
767 .index_page = qs->index_page - 1,
770 if (t->next_disp == srv->max_repos_display &&
771 t->repos_total != (qs->index_page + 1) *
772 srv->max_repos_display) {
774 *next = (struct gotweb_url){
776 .index_page = qs->index_page + 1,
782 gotweb_render_index(struct template *tp)
784 const struct got_error *error = NULL;
785 struct request *c = tp->tp_arg;
786 struct server *srv = c->srv;
787 struct transport *t = c->t;
788 struct querystring *qs = t->qs;
789 struct repo_dir *repo_dir = NULL;
790 struct dirent **sd_dent = t->repos;
791 unsigned int d_i, d_disp = 0;
792 unsigned int d_skipped = 0;
795 if (gotweb_render_repo_table_hdr(c->tp) == -1)
798 for (d_i = 0; d_i < t->nrepos; d_i++) {
799 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
800 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
805 error = got_path_dirent_type(&type, srv->repos_path,
809 if (type != DT_DIR) {
814 if (qs->index_page > 0 && (qs->index_page *
815 srv->max_repos_display) > t->prev_disp) {
820 error = gotweb_load_got_path(&repo_dir, sd_dent[d_i]->d_name,
822 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
823 if (error->code != GOT_ERR_NOT_GIT_REPO)
824 log_warnx("%s: %s: %s", __func__,
825 sd_dent[d_i]->d_name, error->msg);
826 gotweb_free_repo_dir(repo_dir);
835 r = gotweb_render_repo_fragment(c->tp, repo_dir);
836 gotweb_free_repo_dir(repo_dir);
838 got_repo_close(t->repo);
844 if (d_disp == srv->max_repos_display)
847 t->repos_total = t->nrepos - d_skipped;
849 if (srv->max_repos_display == 0 ||
850 t->repos_total <= srv->max_repos_display)
853 if (gotweb_render_navs(c->tp) == -1)
860 should_urlencode(int c)
862 if (c <= ' ' || c >= 127)
886 /* needed because the URLs are embedded into the HTML */
895 gotweb_urlencode(const char *str)
903 for (s = str; *s; ++s) {
905 if (should_urlencode(*s))
909 escaped = calloc(1, len + 1);
914 for (s = str; *s; ++s) {
915 if (should_urlencode(*s)) {
916 a = (*s & 0xF0) >> 4;
920 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
921 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
930 gotweb_action_name(int action)
967 gotweb_render_url(struct request *c, struct gotweb_url *url)
969 const char *sep = "?", *action;
973 action = gotweb_action_name(url->action);
974 if (action != NULL) {
975 if (tp_writef(c->tp, "?action=%s", action) == -1)
981 if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
987 if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
993 if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
999 tmp = gotweb_urlencode(url->file);
1002 r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1010 tmp = gotweb_urlencode(url->folder);
1013 r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1021 tmp = gotweb_urlencode(url->headref);
1024 r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1031 if (url->index_page != -1) {
1032 if (tp_writef(c->tp, "%sindex_page=%d", sep,
1033 url->index_page) == -1)
1039 tmp = gotweb_urlencode(url->path);
1042 r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1053 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1055 struct template *tp = c->tp;
1056 const char *proto = c->https ? "https" : "http";
1058 if (tp_writes(tp, proto) == -1 ||
1059 tp_writes(tp, "://") == -1 ||
1060 tp_htmlescape(tp, c->server_name) == -1 ||
1061 tp_htmlescape(tp, c->document_uri) == -1)
1064 return gotweb_render_url(c, url);
1067 static const struct got_error *
1068 gotweb_load_got_path(struct repo_dir **rp, const char *dir,
1071 const struct got_error *error = NULL;
1072 struct socket *sock = c->sock;
1073 struct server *srv = c->srv;
1074 struct transport *t = c->t;
1075 struct repo_dir *repo_dir;
1079 *rp = calloc(1, sizeof(**rp));
1081 return got_error_from_errno("calloc");
1084 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, dir,
1085 GOTWEB_GIT_DIR) == -1)
1086 return got_error_from_errno("asprintf");
1088 dt = opendir(dir_test);
1091 if (asprintf(&dir_test, "%s/%s", srv->repos_path, dir) == -1)
1092 return got_error_from_errno("asprintf");
1093 dt = opendir(dir_test);
1096 if (asprintf(&dir_test, "%s/%s%s", srv->repos_path,
1097 dir, GOTWEB_GIT_DIR) == -1)
1098 return got_error_from_errno("asprintf");
1099 dt = opendir(dir_test);
1102 return got_error_path(dir,
1103 GOT_ERR_NOT_GIT_REPO);
1108 repo_dir->path = dir_test;
1111 repo_dir->name = strdup(repo_dir->path + strlen(srv->repos_path) + 1);
1112 if (repo_dir->name == NULL) {
1113 error = got_error_from_errno("strdup");
1117 if (srv->respect_exportok &&
1118 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1119 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1123 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1126 error = gotweb_get_repo_description(&repo_dir->description, srv,
1127 repo_dir->path, dirfd(dt));
1130 error = got_get_repo_owner(&repo_dir->owner, c);
1133 if (srv->show_repo_age) {
1134 error = got_get_repo_age(&repo_dir->age, c, NULL);
1138 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1142 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1143 error = got_error_from_errno("closedir");
1144 if (error && t->repo) {
1145 got_repo_close(t->repo);
1151 static const struct got_error *
1152 gotweb_get_repo_description(char **description, struct server *srv,
1153 const char *dirpath, int dir)
1155 const struct got_error *error = NULL;
1160 *description = NULL;
1161 if (srv->show_repo_description == 0)
1164 fd = openat(dir, "description", O_RDONLY);
1166 if (errno != ENOENT && errno != EACCES) {
1167 error = got_error_from_errno_fmt("openat %s/%s",
1168 dirpath, "description");
1173 if (fstat(fd, &sb) == -1) {
1174 error = got_error_from_errno_fmt("fstat %s/%s",
1175 dirpath, "description");
1180 if (len > GOTWEBD_MAXDESCRSZ - 1)
1181 len = GOTWEBD_MAXDESCRSZ - 1;
1183 *description = calloc(len + 1, sizeof(**description));
1184 if (*description == NULL) {
1185 error = got_error_from_errno("calloc");
1189 if (read(fd, *description, len) == -1)
1190 error = got_error_from_errno("read");
1192 if (fd != -1 && close(fd) == -1 && error == NULL)
1193 error = got_error_from_errno("close");
1197 static const struct got_error *
1198 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1201 const struct got_error *error = NULL;
1207 if (srv->show_repo_cloneurl == 0)
1210 fd = openat(dir, "cloneurl", O_RDONLY);
1212 if (errno != ENOENT && errno != EACCES) {
1213 error = got_error_from_errno_fmt("openat %s/%s",
1214 dirpath, "cloneurl");
1219 if (fstat(fd, &sb) == -1) {
1220 error = got_error_from_errno_fmt("fstat %s/%s",
1221 dirpath, "cloneurl");
1226 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1227 len = GOTWEBD_MAXCLONEURLSZ - 1;
1229 *url = calloc(len + 1, sizeof(**url));
1231 error = got_error_from_errno("calloc");
1235 if (read(fd, *url, len) == -1)
1236 error = got_error_from_errno("read");
1238 if (fd != -1 && close(fd) == -1 && error == NULL)
1239 error = got_error_from_errno("close");
1244 gotweb_render_age(struct template *tp, time_t committer_time)
1246 struct request *c = tp->tp_arg;
1247 long long diff_time;
1248 const char *years = "years ago", *months = "months ago";
1249 const char *weeks = "weeks ago", *days = "days ago";
1250 const char *hours = "hours ago", *minutes = "minutes ago";
1251 const char *seconds = "seconds ago", *now = "right now";
1253 diff_time = time(NULL) - committer_time;
1254 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1255 if (tp_writef(c->tp, "%lld %s",
1256 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1258 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1259 if (tp_writef(c->tp, "%lld %s",
1260 (diff_time / 60 / 60 / 24 / (365 / 12)),
1263 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1264 if (tp_writef(c->tp, "%lld %s",
1265 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1267 } else if (diff_time > 60 * 60 * 24 * 2) {
1268 if (tp_writef(c->tp, "%lld %s",
1269 (diff_time / 60 / 60 / 24), days) == -1)
1271 } else if (diff_time > 60 * 60 * 2) {
1272 if (tp_writef(c->tp, "%lld %s",
1273 (diff_time / 60 / 60), hours) == -1)
1275 } else if (diff_time > 60 * 2) {
1276 if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1279 } else if (diff_time > 2) {
1280 if (tp_writef(c->tp, "%lld %s", diff_time,
1284 if (tp_writes(tp, now) == -1)