Blob


1 /*
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>
7 *
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.
11 *
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.
19 */
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <fcntl.h>
32 #include <imsg.h>
33 #include <sha1.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
51 #include "proc.h"
52 #include "gotwebd.h"
53 #include "tmpl.h"
55 static const struct querystring_keys querystring_keys[] = {
56 { "action", ACTION },
57 { "commit", COMMIT },
58 { "file", RFILE },
59 { "folder", FOLDER },
60 { "headref", HEADREF },
61 { "index_page", INDEX_PAGE },
62 { "path", PATH },
63 { "page", PAGE },
64 };
66 static const struct action_keys action_keys[] = {
67 { "blame", BLAME },
68 { "blob", BLOB },
69 { "blobraw", BLOBRAW },
70 { "briefs", BRIEFS },
71 { "commits", COMMITS },
72 { "diff", DIFF },
73 { "error", ERR },
74 { "index", INDEX },
75 { "summary", SUMMARY },
76 { "tag", TAG },
77 { "tags", TAGS },
78 { "tree", TREE },
79 { "rss", RSS },
80 };
82 static const struct got_error *gotweb_init_querystring(struct querystring **);
83 static const struct got_error *gotweb_parse_querystring(struct querystring **,
84 char *);
85 static const struct got_error *gotweb_assign_querystring(struct querystring **,
86 char *, char *);
87 static const struct got_error *gotweb_render_index(struct request *);
88 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
89 const char *);
90 static const struct got_error *gotweb_load_got_path(struct request *c,
91 struct repo_dir *);
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 *,
95 const char *, int);
96 static const struct got_error *gotweb_render_blame(struct request *);
97 static const struct got_error *gotweb_render_diff(struct request *);
98 static const struct got_error *gotweb_render_summary(struct request *);
99 static const struct got_error *gotweb_render_tags(struct request *);
100 static const struct got_error *gotweb_render_branches(struct request *);
102 static void gotweb_free_querystring(struct querystring *);
103 static void gotweb_free_repo_dir(struct repo_dir *);
105 struct server *gotweb_get_server(uint8_t *, uint8_t *);
107 void
108 gotweb_process_request(struct request *c)
110 const struct got_error *error = NULL, *error2 = NULL;
111 struct got_blob_object *blob = NULL;
112 struct server *srv = NULL;
113 struct querystring *qs = NULL;
114 struct repo_dir *repo_dir = NULL;
115 uint8_t err[] = "gotwebd experienced an error: ";
116 int r, html = 0, fd = -1;
118 /* init the transport */
119 error = gotweb_init_transport(&c->t);
120 if (error) {
121 log_warnx("%s: %s", __func__, error->msg);
122 return;
124 /* don't process any further if client disconnected */
125 if (c->sock->client_status == CLIENT_DISCONNECT)
126 return;
127 /* get the gotwebd server */
128 srv = gotweb_get_server(c->server_name, c->http_host);
129 if (srv == NULL) {
130 log_warnx("%s: error server is NULL", __func__);
131 goto err;
133 c->srv = srv;
134 /* parse our querystring */
135 error = gotweb_init_querystring(&qs);
136 if (error) {
137 log_warnx("%s: %s", __func__, error->msg);
138 goto err;
140 c->t->qs = qs;
141 error = gotweb_parse_querystring(&qs, c->querystring);
142 if (error) {
143 log_warnx("%s: %s", __func__, error->msg);
144 goto err;
147 /*
148 * certain actions require a commit id in the querystring. this stops
149 * bad actors from exploiting this by manually manipulating the
150 * querystring.
151 */
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);
157 goto render;
161 if (qs->action != INDEX) {
162 error = gotweb_init_repo_dir(&repo_dir, qs->path);
163 if (error)
164 goto done;
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)
168 goto err;
171 if (qs->action == BLOBRAW) {
172 error = got_get_repo_commits(c, 1);
173 if (error)
174 goto done;
175 error = got_output_file_blob(c);
176 if (error) {
177 log_warnx("%s: %s", __func__, error->msg);
178 goto err;
180 goto done;
183 if (qs->action == BLOB) {
184 int binary;
185 struct gotweb_url url = {
186 .index_page = -1,
187 .page = -1,
188 .action = BLOBRAW,
189 .path = qs->path,
190 .commit = qs->commit,
191 .folder = qs->folder,
192 .file = qs->file,
193 };
195 error = got_get_repo_commits(c, 1);
196 if (error)
197 goto done;
199 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
200 if (error2)
201 goto render;
202 if (binary) {
203 fcgi_puts(c->tp, "Status: 302\r\n");
204 fcgi_puts(c->tp, "Location: ");
205 gotweb_render_url(c, &url);
206 fcgi_puts(c->tp, "\r\n\r\n");
207 goto done;
211 if (qs->action == RSS) {
212 error = gotweb_render_content_type_file(c,
213 "application/rss+xml;charset=utf-8",
214 repo_dir->name, ".rss");
215 if (error) {
216 log_warnx("%s: %s", __func__, error->msg);
217 goto err;
220 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
221 if (error) {
222 log_warnx("%s: %s", __func__, error->msg);
223 goto err;
225 if (gotweb_render_rss(c->tp) == -1)
226 goto err;
227 goto done;
230 render:
231 error = gotweb_render_content_type(c, "text/html");
232 if (error) {
233 log_warnx("%s: %s", __func__, error->msg);
234 goto err;
236 html = 1;
238 if (gotweb_render_header(c->tp) == -1)
239 goto err;
241 if (error2) {
242 error = error2;
243 goto err;
246 switch(qs->action) {
247 case BLAME:
248 error = gotweb_render_blame(c);
249 if (error) {
250 log_warnx("%s: %s", __func__, error->msg);
251 goto err;
253 break;
254 case BLOB:
255 if (gotweb_render_blob(c->tp, blob) == -1)
256 goto err;
257 break;
258 case BRIEFS:
259 if (gotweb_render_briefs(c->tp) == -1)
260 goto err;
261 break;
262 case COMMITS:
263 error = got_get_repo_commits(c, srv->max_commits_display);
264 if (error) {
265 log_warnx("%s: %s", __func__, error->msg);
266 goto err;
268 if (gotweb_render_commits(c->tp) == -1)
269 goto err;
270 break;
271 case DIFF:
272 error = gotweb_render_diff(c);
273 if (error) {
274 log_warnx("%s: %s", __func__, error->msg);
275 goto err;
277 break;
278 case INDEX:
279 error = gotweb_render_index(c);
280 if (error) {
281 log_warnx("%s: %s", __func__, error->msg);
282 goto err;
284 break;
285 case SUMMARY:
286 error = gotweb_render_summary(c);
287 if (error) {
288 log_warnx("%s: %s", __func__, error->msg);
289 goto err;
291 break;
292 case TAG:
293 error = got_get_repo_tags(c, 1);
294 if (error) {
295 log_warnx("%s: %s", __func__, error->msg);
296 goto err;
298 if (c->t->tag_count == 0) {
299 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
300 "bad commit id");
301 goto err;
303 if (gotweb_render_tag(c->tp) == -1)
304 goto done;
305 break;
306 case TAGS:
307 error = gotweb_render_tags(c);
308 if (error) {
309 log_warnx("%s: %s", __func__, error->msg);
310 goto err;
312 break;
313 case TREE:
314 error = got_get_repo_commits(c, 1);
315 if (error) {
316 log_warnx("%s: %s", __func__, error->msg);
317 goto err;
319 if (gotweb_render_tree(c->tp) == -1)
320 goto err;
321 break;
322 case ERR:
323 default:
324 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
325 "Erorr: Bad Querystring");
326 if (r == -1)
327 goto err;
328 break;
331 goto done;
332 err:
333 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
334 return;
335 if (fcgi_printf(c, "\n%s", err) == -1)
336 return;
337 if (error) {
338 if (fcgi_printf(c, "%s", error->msg) == -1)
339 return;
340 } else {
341 if (fcgi_printf(c, "see daemon logs for details") == -1)
342 return;
344 if (html && fcgi_printf(c, "</div>\n") == -1)
345 return;
346 done:
347 if (blob)
348 got_object_blob_close(blob);
349 if (fd != -1)
350 close(fd);
351 if (html && srv != NULL)
352 gotweb_render_footer(c->tp);
355 struct server *
356 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
358 struct server *srv = NULL;
360 /* check against the server name first */
361 if (strlen(server_name) > 0)
362 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
363 if (strcmp(srv->name, server_name) == 0)
364 goto done;
366 /* check against subdomain second */
367 if (strlen(subdomain) > 0)
368 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
369 if (strcmp(srv->name, subdomain) == 0)
370 goto done;
372 /* if those fail, send first server */
373 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
374 if (srv != NULL)
375 break;
376 done:
377 return srv;
378 };
380 const struct got_error *
381 gotweb_init_transport(struct transport **t)
383 const struct got_error *error = NULL;
385 *t = calloc(1, sizeof(**t));
386 if (*t == NULL)
387 return got_error_from_errno2("%s: calloc", __func__);
389 TAILQ_INIT(&(*t)->repo_commits);
390 TAILQ_INIT(&(*t)->repo_tags);
392 (*t)->repo = NULL;
393 (*t)->repo_dir = NULL;
394 (*t)->qs = NULL;
395 (*t)->next_id = NULL;
396 (*t)->prev_id = NULL;
397 (*t)->next_disp = 0;
398 (*t)->prev_disp = 0;
400 return error;
403 static const struct got_error *
404 gotweb_init_querystring(struct querystring **qs)
406 const struct got_error *error = NULL;
408 *qs = calloc(1, sizeof(**qs));
409 if (*qs == NULL)
410 return got_error_from_errno2("%s: calloc", __func__);
412 (*qs)->headref = strdup("HEAD");
413 if ((*qs)->headref == NULL) {
414 free(*qs);
415 *qs = NULL;
416 return got_error_from_errno2("%s: strdup", __func__);
419 (*qs)->action = INDEX;
420 (*qs)->commit = NULL;
421 (*qs)->file = NULL;
422 (*qs)->folder = NULL;
423 (*qs)->index_page = 0;
424 (*qs)->path = NULL;
426 return error;
429 static const struct got_error *
430 gotweb_parse_querystring(struct querystring **qs, char *qst)
432 const struct got_error *error = NULL;
433 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
434 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
436 if (qst == NULL)
437 return error;
439 tok1 = strdup(qst);
440 if (tok1 == NULL)
441 return got_error_from_errno2("%s: strdup", __func__);
443 tok1_pair = tok1;
444 tok1_end = tok1;
446 while (tok1_pair != NULL) {
447 strsep(&tok1_end, "&");
449 tok2 = strdup(tok1_pair);
450 if (tok2 == NULL) {
451 free(tok1);
452 return got_error_from_errno2("%s: strdup", __func__);
455 tok2_pair = tok2;
456 tok2_end = tok2;
458 while (tok2_pair != NULL) {
459 strsep(&tok2_end, "=");
460 if (tok2_end) {
461 error = gotweb_assign_querystring(qs, tok2_pair,
462 tok2_end);
463 if (error)
464 goto err;
466 tok2_pair = tok2_end;
468 free(tok2);
469 tok1_pair = tok1_end;
471 free(tok1);
472 return error;
473 err:
474 free(tok2);
475 free(tok1);
476 return error;
479 /*
480 * Adapted from usr.sbin/httpd/httpd.c url_decode.
481 */
482 static const struct got_error *
483 gotweb_urldecode(char *url)
485 char *p, *q;
486 char hex[3];
487 unsigned long x;
489 hex[2] = '\0';
490 p = q = url;
492 while (*p != '\0') {
493 switch (*p) {
494 case '%':
495 /* Encoding character is followed by two hex chars */
496 if (!isxdigit((unsigned char)p[1]) ||
497 !isxdigit((unsigned char)p[2]) ||
498 (p[1] == '0' && p[2] == '0'))
499 return got_error(GOT_ERR_BAD_QUERYSTRING);
501 hex[0] = p[1];
502 hex[1] = p[2];
504 /*
505 * We don't have to validate "hex" because it is
506 * guaranteed to include two hex chars followed by nul.
507 */
508 x = strtoul(hex, NULL, 16);
509 *q = (char)x;
510 p += 2;
511 break;
512 default:
513 *q = *p;
514 break;
516 p++;
517 q++;
519 *q = '\0';
521 return NULL;
524 static const struct got_error *
525 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
527 const struct got_error *error = NULL;
528 const char *errstr;
529 int a_cnt, el_cnt;
531 error = gotweb_urldecode(value);
532 if (error)
533 return error;
535 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
536 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
537 continue;
539 switch (querystring_keys[el_cnt].element) {
540 case ACTION:
541 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
542 if (strcmp(value, action_keys[a_cnt].name) != 0)
543 continue;
544 else if (strcmp(value,
545 action_keys[a_cnt].name) == 0){
546 (*qs)->action =
547 action_keys[a_cnt].action;
548 goto qa_found;
551 (*qs)->action = ERR;
552 qa_found:
553 break;
554 case COMMIT:
555 (*qs)->commit = strdup(value);
556 if ((*qs)->commit == NULL) {
557 error = got_error_from_errno2("%s: strdup",
558 __func__);
559 goto done;
561 break;
562 case RFILE:
563 (*qs)->file = strdup(value);
564 if ((*qs)->file == NULL) {
565 error = got_error_from_errno2("%s: strdup",
566 __func__);
567 goto done;
569 break;
570 case FOLDER:
571 (*qs)->folder = strdup(value);
572 if ((*qs)->folder == NULL) {
573 error = got_error_from_errno2("%s: strdup",
574 __func__);
575 goto done;
577 break;
578 case HEADREF:
579 free((*qs)->headref);
580 (*qs)->headref = strdup(value);
581 if ((*qs)->headref == NULL) {
582 error = got_error_from_errno2("%s: strdup",
583 __func__);
584 goto done;
586 break;
587 case INDEX_PAGE:
588 if (strlen(value) == 0)
589 break;
590 (*qs)->index_page = strtonum(value, INT64_MIN,
591 INT64_MAX, &errstr);
592 if (errstr) {
593 error = got_error_from_errno3("%s: strtonum %s",
594 __func__, errstr);
595 goto done;
597 if ((*qs)->index_page < 0)
598 (*qs)->index_page = 0;
599 break;
600 case PATH:
601 (*qs)->path = strdup(value);
602 if ((*qs)->path == NULL) {
603 error = got_error_from_errno2("%s: strdup",
604 __func__);
605 goto done;
607 break;
608 case PAGE:
609 if (strlen(value) == 0)
610 break;
611 (*qs)->page = strtonum(value, INT64_MIN,
612 INT64_MAX, &errstr);
613 if (errstr) {
614 error = got_error_from_errno3("%s: strtonum %s",
615 __func__, errstr);
616 goto done;
618 if ((*qs)->page < 0)
619 (*qs)->page = 0;
620 break;
621 default:
622 break;
625 done:
626 return error;
629 void
630 gotweb_free_repo_tag(struct repo_tag *rt)
632 if (rt != NULL) {
633 free(rt->commit_id);
634 free(rt->tag_name);
635 free(rt->tag_commit);
636 free(rt->commit_msg);
637 free(rt->tagger);
639 free(rt);
642 void
643 gotweb_free_repo_commit(struct repo_commit *rc)
645 if (rc != NULL) {
646 free(rc->path);
647 free(rc->refs_str);
648 free(rc->commit_id);
649 free(rc->parent_id);
650 free(rc->tree_id);
651 free(rc->author);
652 free(rc->committer);
653 free(rc->commit_msg);
655 free(rc);
658 static void
659 gotweb_free_querystring(struct querystring *qs)
661 if (qs != NULL) {
662 free(qs->commit);
663 free(qs->file);
664 free(qs->folder);
665 free(qs->headref);
666 free(qs->path);
668 free(qs);
671 static void
672 gotweb_free_repo_dir(struct repo_dir *repo_dir)
674 if (repo_dir != NULL) {
675 free(repo_dir->name);
676 free(repo_dir->owner);
677 free(repo_dir->description);
678 free(repo_dir->url);
679 free(repo_dir->age);
680 free(repo_dir->path);
682 free(repo_dir);
685 void
686 gotweb_free_transport(struct transport *t)
688 struct repo_commit *rc = NULL, *trc = NULL;
689 struct repo_tag *rt = NULL, *trt = NULL;
691 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
692 TAILQ_REMOVE(&t->repo_commits, rc, entry);
693 gotweb_free_repo_commit(rc);
695 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
696 TAILQ_REMOVE(&t->repo_tags, rt, entry);
697 gotweb_free_repo_tag(rt);
699 gotweb_free_repo_dir(t->repo_dir);
700 gotweb_free_querystring(t->qs);
701 free(t->next_id);
702 free(t->prev_id);
703 free(t);
706 const struct got_error *
707 gotweb_render_content_type(struct request *c, const char *type)
709 const char *csp = "default-src 'self'; script-src 'none'; "
710 "object-src 'none';";
712 fcgi_printf(c,
713 "Content-Security-Policy: %s\r\n"
714 "Content-Type: %s\r\n\r\n",
715 csp, type);
716 return NULL;
719 const struct got_error *
720 gotweb_render_content_type_file(struct request *c, const char *type,
721 const char *file, const char *suffix)
723 fcgi_printf(c, "Content-type: %s\r\n"
724 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
725 type, file, suffix ? suffix : "");
726 return NULL;
729 void
730 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
731 struct gotweb_url *next, int *have_next)
733 struct transport *t = c->t;
734 struct querystring *qs = t->qs;
735 struct server *srv = c->srv;
737 *have_prev = *have_next = 0;
739 switch(qs->action) {
740 case INDEX:
741 if (qs->index_page > 0) {
742 *have_prev = 1;
743 *prev = (struct gotweb_url){
744 .action = -1,
745 .index_page = qs->index_page - 1,
746 .page = -1,
747 };
749 if (t->next_disp == srv->max_repos_display &&
750 t->repos_total != (qs->index_page + 1) *
751 srv->max_repos_display) {
752 *have_next = 1;
753 *next = (struct gotweb_url){
754 .action = -1,
755 .index_page = qs->index_page + 1,
756 .page = -1,
757 };
759 break;
760 case BRIEFS:
761 if (t->prev_id && qs->commit != NULL &&
762 strcmp(qs->commit, t->prev_id) != 0) {
763 *have_prev = 1;
764 *prev = (struct gotweb_url){
765 .action = BRIEFS,
766 .index_page = -1,
767 .page = qs->page - 1,
768 .path = qs->path,
769 .commit = t->prev_id,
770 .headref = qs->headref,
771 };
773 if (t->next_id) {
774 *have_next = 1;
775 *next = (struct gotweb_url){
776 .action = BRIEFS,
777 .index_page = -1,
778 .page = qs->page + 1,
779 .path = qs->path,
780 .commit = t->next_id,
781 .headref = qs->headref,
782 };
784 break;
785 case COMMITS:
786 if (t->prev_id && qs->commit != NULL &&
787 strcmp(qs->commit, t->prev_id) != 0) {
788 *have_prev = 1;
789 *prev = (struct gotweb_url){
790 .action = COMMITS,
791 .index_page = -1,
792 .page = qs->page - 1,
793 .path = qs->path,
794 .commit = t->prev_id,
795 .headref = qs->headref,
796 .folder = qs->folder,
797 .file = qs->file,
798 };
800 if (t->next_id) {
801 *have_next = 1;
802 *next = (struct gotweb_url){
803 .action = COMMITS,
804 .index_page = -1,
805 .page = qs->page + 1,
806 .path = qs->path,
807 .commit = t->next_id,
808 .headref = qs->headref,
809 .folder = qs->folder,
810 .file = qs->file,
811 };
813 break;
814 case TAGS:
815 if (t->prev_id && qs->commit != NULL &&
816 strcmp(qs->commit, t->prev_id) != 0) {
817 *have_prev = 1;
818 *prev = (struct gotweb_url){
819 .action = TAGS,
820 .index_page = -1,
821 .page = qs->page - 1,
822 .path = qs->path,
823 .commit = t->prev_id,
824 .headref = qs->headref,
825 };
827 if (t->next_id) {
828 *have_next = 1;
829 *next = (struct gotweb_url){
830 .action = TAGS,
831 .index_page = -1,
832 .page = qs->page + 1,
833 .path = qs->path,
834 .commit = t->next_id,
835 .headref = qs->headref,
836 };
838 break;
842 static const struct got_error *
843 gotweb_render_index(struct request *c)
845 const struct got_error *error = NULL;
846 struct server *srv = c->srv;
847 struct transport *t = c->t;
848 struct querystring *qs = t->qs;
849 struct repo_dir *repo_dir = NULL;
850 DIR *d;
851 struct dirent **sd_dent = NULL;
852 unsigned int d_cnt, d_i, d_disp = 0;
853 unsigned int d_skipped = 0;
854 int type;
856 d = opendir(srv->repos_path);
857 if (d == NULL) {
858 error = got_error_from_errno2("opendir", srv->repos_path);
859 return error;
862 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
863 if (d_cnt == -1) {
864 sd_dent = NULL;
865 error = got_error_from_errno2("scandir", srv->repos_path);
866 goto done;
869 if (gotweb_render_repo_table_hdr(c->tp) == -1)
870 goto done;
872 for (d_i = 0; d_i < d_cnt; d_i++) {
873 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
874 break;
876 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
877 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
878 d_skipped++;
879 continue;
882 error = got_path_dirent_type(&type, srv->repos_path,
883 sd_dent[d_i]);
884 if (error)
885 goto done;
886 if (type != DT_DIR) {
887 d_skipped++;
888 continue;
891 if (qs->index_page > 0 && (qs->index_page *
892 srv->max_repos_display) > t->prev_disp) {
893 t->prev_disp++;
894 continue;
897 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
898 if (error)
899 goto done;
901 error = gotweb_load_got_path(c, repo_dir);
902 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
903 error = NULL;
904 gotweb_free_repo_dir(repo_dir);
905 repo_dir = NULL;
906 d_skipped++;
907 continue;
909 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
910 goto done;
912 d_disp++;
913 t->prev_disp++;
915 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
916 goto done;
918 gotweb_free_repo_dir(repo_dir);
919 repo_dir = NULL;
920 t->next_disp++;
921 if (d_disp == srv->max_repos_display)
922 break;
924 t->repos_total = d_cnt - d_skipped;
926 if (srv->max_repos_display == 0)
927 goto done;
928 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
929 goto done;
930 if (t->repos_total <= srv->max_repos ||
931 t->repos_total <= srv->max_repos_display)
932 goto done;
934 if (gotweb_render_navs(c->tp) == -1)
935 goto done;
936 done:
937 if (sd_dent) {
938 for (d_i = 0; d_i < d_cnt; d_i++)
939 free(sd_dent[d_i]);
940 free(sd_dent);
942 if (d != NULL && closedir(d) == EOF && error == NULL)
943 error = got_error_from_errno("closedir");
944 return error;
947 static const struct got_error *
948 gotweb_render_blame(struct request *c)
950 const struct got_error *error = NULL;
951 struct transport *t = c->t;
952 struct repo_commit *rc = NULL;
953 char *age = NULL, *msg = NULL;
954 int r;
956 error = got_get_repo_commits(c, 1);
957 if (error)
958 return error;
960 rc = TAILQ_FIRST(&t->repo_commits);
962 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
963 if (error)
964 goto done;
965 error = gotweb_escape_html(&msg, rc->commit_msg);
966 if (error)
967 goto done;
969 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
970 "<div id='blame_title'>Blame</div>\n"
971 "</div>\n" /* #blame_title_wrapper */
972 "<div id='blame_content'>\n"
973 "<div id='blame_header_wrapper'>\n"
974 "<div id='blame_header'>\n"
975 "<div class='header_age_title'>Date:</div>\n"
976 "<div class='header_age'>%s</div>\n"
977 "<div id='header_commit_msg_title'>Message:</div>\n"
978 "<div id='header_commit_msg'>%s</div>\n"
979 "</div>\n" /* #blame_header */
980 "</div>\n" /* #blame_header_wrapper */
981 "<div class='dotted_line'></div>\n"
982 "<div id='blame'>\n",
983 age,
984 msg);
985 if (r == -1)
986 goto done;
988 error = got_output_file_blame(c);
989 if (error)
990 goto done;
992 fcgi_printf(c, "</div>\n" /* #blame */
993 "</div>\n"); /* #blame_content */
994 done:
995 free(age);
996 free(msg);
997 return error;
1000 static const struct got_error *
1001 gotweb_render_branches(struct request *c)
1003 const struct got_error *error = NULL;
1004 struct got_reflist_head refs;
1005 struct got_reflist_entry *re;
1006 struct transport *t = c->t;
1007 struct querystring *qs = t->qs;
1008 struct got_repository *repo = t->repo;
1009 char *escaped_refname = NULL;
1010 char *age = NULL;
1011 int r;
1013 TAILQ_INIT(&refs);
1015 error = got_ref_list(&refs, repo, "refs/heads",
1016 got_ref_cmp_by_name, NULL);
1017 if (error)
1018 goto done;
1020 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1021 "<div id='branches_title'>Branches</div>\n"
1022 "</div>\n" /* #branches_title_wrapper */
1023 "<div id='branches_content'>\n");
1024 if (r == -1)
1025 goto done;
1027 TAILQ_FOREACH(re, &refs, entry) {
1028 const char *refname = NULL;
1030 if (got_ref_is_symbolic(re->ref))
1031 continue;
1033 refname = got_ref_get_name(re->ref);
1034 if (refname == NULL) {
1035 error = got_error_from_errno("strdup");
1036 goto done;
1038 if (strncmp(refname, "refs/heads/", 11) != 0)
1039 continue;
1041 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1042 if (error)
1043 goto done;
1045 if (strncmp(refname, "refs/heads/", 11) == 0)
1046 refname += 11;
1047 error = gotweb_escape_html(&escaped_refname, refname);
1048 if (error)
1049 goto done;
1051 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1052 "<div class='branches_age'>%s</div>\n"
1053 "<div class='branches_space'>&nbsp;</div>\n"
1054 "<div class='branch'>", age);
1055 if (r == -1)
1056 goto done;
1058 r = gotweb_link(c, &(struct gotweb_url){
1059 .action = SUMMARY,
1060 .index_page = -1,
1061 .page = -1,
1062 .path = qs->path,
1063 .headref = refname,
1064 }, "%s", escaped_refname);
1065 if (r == -1)
1066 goto done;
1068 if (fcgi_printf(c, "</div>\n" /* .branch */
1069 "<div class='navs_wrapper'>\n"
1070 "<div class='navs'>") == -1)
1071 goto done;
1073 r = gotweb_link(c, &(struct gotweb_url){
1074 .action = SUMMARY,
1075 .index_page = -1,
1076 .page = -1,
1077 .path = qs->path,
1078 .headref = refname,
1079 }, "summary");
1080 if (r == -1)
1081 goto done;
1083 if (fcgi_printf(c, " | ") == -1)
1084 goto done;
1086 r = gotweb_link(c, &(struct gotweb_url){
1087 .action = BRIEFS,
1088 .index_page = -1,
1089 .page = -1,
1090 .path = qs->path,
1091 .headref = refname,
1092 }, "commit briefs");
1093 if (r == -1)
1094 goto done;
1096 if (fcgi_printf(c, " | ") == -1)
1097 goto done;
1099 r = gotweb_link(c, &(struct gotweb_url){
1100 .action = COMMITS,
1101 .index_page = -1,
1102 .page = -1,
1103 .path = qs->path,
1104 .headref = refname,
1105 }, "commits");
1106 if (r == -1)
1107 goto done;
1109 r = fcgi_printf(c, "</div>\n" /* .navs */
1110 "</div>\n" /* .navs_wrapper */
1111 "<div class='dotted_line'></div>\n"
1112 "</div>\n"); /* .branches_wrapper */
1113 if (r == -1)
1114 goto done;
1116 free(age);
1117 age = NULL;
1118 free(escaped_refname);
1119 escaped_refname = NULL;
1121 fcgi_printf(c, "</div>\n"); /* #branches_content */
1122 done:
1123 free(age);
1124 free(escaped_refname);
1125 got_ref_list_free(&refs);
1126 return error;
1129 static const struct got_error *
1130 gotweb_render_diff(struct request *c)
1132 const struct got_error *error = NULL;
1133 struct transport *t = c->t;
1134 struct repo_commit *rc = NULL;
1135 char *age = NULL, *author = NULL, *msg = NULL;
1136 int r;
1138 error = got_get_repo_commits(c, 1);
1139 if (error)
1140 return error;
1142 rc = TAILQ_FIRST(&t->repo_commits);
1144 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1145 if (error)
1146 goto done;
1147 error = gotweb_escape_html(&author, rc->author);
1148 if (error)
1149 goto done;
1150 error = gotweb_escape_html(&msg, rc->commit_msg);
1151 if (error)
1152 goto done;
1154 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1155 "<div id='diff_title'>Commit Diff</div>\n"
1156 "</div>\n" /* #diff_title_wrapper */
1157 "<div id='diff_content'>\n"
1158 "<div id='diff_header_wrapper'>\n"
1159 "<div id='diff_header'>\n"
1160 "<div id='header_diff_title'>Diff:</div>\n"
1161 "<div id='header_diff'>%s<br />%s</div>\n"
1162 "<div class='header_commit_title'>Commit:</div>\n"
1163 "<div class='header_commit'>%s</div>\n"
1164 "<div id='header_tree_title'>Tree:</div>\n"
1165 "<div id='header_tree'>%s</div>\n"
1166 "<div class='header_author_title'>Author:</div>\n"
1167 "<div class='header_author'>%s</div>\n"
1168 "<div class='header_age_title'>Date:</div>\n"
1169 "<div class='header_age'>%s</div>\n"
1170 "<div id='header_commit_msg_title'>Message:</div>\n"
1171 "<div id='header_commit_msg'>%s</div>\n"
1172 "</div>\n" /* #diff_header */
1173 "</div>\n" /* #diff_header_wrapper */
1174 "<div class='dotted_line'></div>\n"
1175 "<div id='diff'>\n",
1176 rc->parent_id, rc->commit_id,
1177 rc->commit_id,
1178 rc->tree_id,
1179 author,
1180 age,
1181 msg);
1182 if (r == -1)
1183 goto done;
1185 error = got_output_repo_diff(c);
1186 if (error)
1187 goto done;
1189 fcgi_printf(c, "</div>\n"); /* #diff */
1190 fcgi_printf(c, "</div>\n"); /* #diff_content */
1191 done:
1192 free(age);
1193 free(author);
1194 free(msg);
1195 return error;
1198 static const struct got_error *
1199 gotweb_render_summary(struct request *c)
1201 const struct got_error *error = NULL;
1202 struct transport *t = c->t;
1203 struct server *srv = c->srv;
1204 int r;
1206 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1207 goto done;
1209 if (srv->show_repo_description) {
1210 r = fcgi_printf(c,
1211 "<div id='description_title'>Description:</div>\n"
1212 "<div id='description'>%s</div>\n",
1213 t->repo_dir->description ? t->repo_dir->description : "");
1214 if (r == -1)
1215 goto done;
1218 if (srv->show_repo_owner) {
1219 r = fcgi_printf(c,
1220 "<div id='repo_owner_title'>Owner:</div>\n"
1221 "<div id='repo_owner'>%s</div>\n",
1222 t->repo_dir->owner ? t->repo_dir->owner : "");
1223 if (r == -1)
1224 goto done;
1227 if (srv->show_repo_age) {
1228 r = fcgi_printf(c,
1229 "<div id='last_change_title'>Last Change:</div>\n"
1230 "<div id='last_change'>%s</div>\n",
1231 t->repo_dir->age);
1232 if (r == -1)
1233 goto done;
1236 if (srv->show_repo_cloneurl) {
1237 r = fcgi_printf(c,
1238 "<div id='cloneurl_title'>Clone URL:</div>\n"
1239 "<div id='cloneurl'>%s</div>\n",
1240 t->repo_dir->url ? t->repo_dir->url : "");
1241 if (r == -1)
1242 goto done;
1245 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1246 if (r == -1)
1247 goto done;
1249 if (gotweb_render_briefs(c->tp) == -1)
1250 goto done;
1252 error = gotweb_render_tags(c);
1253 if (error) {
1254 log_warnx("%s: %s", __func__, error->msg);
1255 goto done;
1258 error = gotweb_render_branches(c);
1259 if (error)
1260 log_warnx("%s: %s", __func__, error->msg);
1261 done:
1262 return error;
1265 static const struct got_error *
1266 gotweb_render_tags(struct request *c)
1268 const struct got_error *error = NULL;
1269 struct server *srv = c->srv;
1270 struct transport *t = c->t;
1271 struct querystring *qs = t->qs;
1273 if (qs->action == BRIEFS) {
1274 qs->action = TAGS;
1275 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1276 } else
1277 error = got_get_repo_tags(c, srv->max_commits_display);
1278 if (error)
1279 goto done;
1281 if (gotweb_render_tags_tmpl(c->tp) == -1)
1282 goto done;
1284 done:
1285 return error;
1288 const struct got_error *
1289 gotweb_escape_html(char **escaped_html, const char *orig_html)
1291 const struct got_error *error = NULL;
1292 struct escape_pair {
1293 char c;
1294 const char *s;
1295 } esc[] = {
1296 { '>', "&gt;" },
1297 { '<', "&lt;" },
1298 { '&', "&amp;" },
1299 { '"', "&quot;" },
1300 { '\'', "&apos;" },
1301 { '\n', "<br />" },
1303 size_t orig_len, len;
1304 int i, j, x;
1306 orig_len = strlen(orig_html);
1307 len = orig_len;
1308 for (i = 0; i < orig_len; i++) {
1309 for (j = 0; j < nitems(esc); j++) {
1310 if (orig_html[i] != esc[j].c)
1311 continue;
1312 len += strlen(esc[j].s) - 1 /* escaped char */;
1316 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1317 if (*escaped_html == NULL)
1318 return got_error_from_errno("calloc");
1320 x = 0;
1321 for (i = 0; i < orig_len; i++) {
1322 int escaped = 0;
1323 for (j = 0; j < nitems(esc); j++) {
1324 if (orig_html[i] != esc[j].c)
1325 continue;
1327 if (strlcat(*escaped_html, esc[j].s, len + 1)
1328 >= len + 1) {
1329 error = got_error(GOT_ERR_NO_SPACE);
1330 goto done;
1332 x += strlen(esc[j].s);
1333 escaped = 1;
1334 break;
1336 if (!escaped) {
1337 (*escaped_html)[x] = orig_html[i];
1338 x++;
1341 done:
1342 if (error) {
1343 free(*escaped_html);
1344 *escaped_html = NULL;
1345 } else {
1346 (*escaped_html)[x] = '\0';
1349 return error;
1352 static inline int
1353 should_urlencode(int c)
1355 if (c <= ' ' || c >= 127)
1356 return 1;
1358 switch (c) {
1359 /* gen-delim */
1360 case ':':
1361 case '/':
1362 case '?':
1363 case '#':
1364 case '[':
1365 case ']':
1366 case '@':
1367 /* sub-delims */
1368 case '!':
1369 case '$':
1370 case '&':
1371 case '\'':
1372 case '(':
1373 case ')':
1374 case '*':
1375 case '+':
1376 case ',':
1377 case ';':
1378 case '=':
1379 /* needed because the URLs are embedded into the HTML */
1380 case '\"':
1381 return 1;
1382 default:
1383 return 0;
1387 static char *
1388 gotweb_urlencode(const char *str)
1390 const char *s;
1391 char *escaped;
1392 size_t i, len;
1393 int a, b;
1395 len = 0;
1396 for (s = str; *s; ++s) {
1397 len++;
1398 if (should_urlencode(*s))
1399 len += 2;
1402 escaped = calloc(1, len + 1);
1403 if (escaped == NULL)
1404 return NULL;
1406 i = 0;
1407 for (s = str; *s; ++s) {
1408 if (should_urlencode(*s)) {
1409 a = (*s & 0xF0) >> 4;
1410 b = (*s & 0x0F);
1412 escaped[i++] = '%';
1413 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1414 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1415 } else
1416 escaped[i++] = *s;
1419 return escaped;
1422 const char *
1423 gotweb_action_name(int action)
1425 switch (action) {
1426 case BLAME:
1427 return "blame";
1428 case BLOB:
1429 return "blob";
1430 case BLOBRAW:
1431 return "blobraw";
1432 case BRIEFS:
1433 return "briefs";
1434 case COMMITS:
1435 return "commits";
1436 case DIFF:
1437 return "diff";
1438 case ERR:
1439 return "err";
1440 case INDEX:
1441 return "index";
1442 case SUMMARY:
1443 return "summary";
1444 case TAG:
1445 return "tag";
1446 case TAGS:
1447 return "tags";
1448 case TREE:
1449 return "tree";
1450 case RSS:
1451 return "rss";
1452 default:
1453 return NULL;
1457 int
1458 gotweb_render_url(struct request *c, struct gotweb_url *url)
1460 const char *sep = "?", *action;
1461 char *tmp;
1462 int r;
1464 action = gotweb_action_name(url->action);
1465 if (action != NULL) {
1466 if (fcgi_printf(c, "?action=%s", action) == -1)
1467 return -1;
1468 sep = "&";
1471 if (url->commit) {
1472 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1473 return -1;
1474 sep = "&";
1477 if (url->previd) {
1478 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1479 return -1;
1480 sep = "&";
1483 if (url->prevset) {
1484 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1485 return -1;
1486 sep = "&";
1489 if (url->file) {
1490 tmp = gotweb_urlencode(url->file);
1491 if (tmp == NULL)
1492 return -1;
1493 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1494 free(tmp);
1495 if (r == -1)
1496 return -1;
1497 sep = "&";
1500 if (url->folder) {
1501 tmp = gotweb_urlencode(url->folder);
1502 if (tmp == NULL)
1503 return -1;
1504 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1505 free(tmp);
1506 if (r == -1)
1507 return -1;
1508 sep = "&";
1511 if (url->headref) {
1512 tmp = gotweb_urlencode(url->headref);
1513 if (tmp == NULL)
1514 return -1;
1515 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1516 free(tmp);
1517 if (r == -1)
1518 return -1;
1519 sep = "&";
1522 if (url->index_page != -1) {
1523 if (fcgi_printf(c, "%sindex_page=%d", sep,
1524 url->index_page) == -1)
1525 return -1;
1526 sep = "&";
1529 if (url->path) {
1530 tmp = gotweb_urlencode(url->path);
1531 if (tmp == NULL)
1532 return -1;
1533 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1534 free(tmp);
1535 if (r == -1)
1536 return -1;
1537 sep = "&";
1540 if (url->page != -1) {
1541 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1542 return -1;
1543 sep = "&";
1546 return 0;
1549 int
1550 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1552 struct template *tp = c->tp;
1553 const char *proto = c->https ? "https" : "http";
1555 if (fcgi_puts(tp, proto) == -1 ||
1556 fcgi_puts(tp, "://") == -1 ||
1557 tp_htmlescape(tp, c->server_name) == -1 ||
1558 tp_htmlescape(tp, c->document_uri) == -1)
1559 return -1;
1561 return gotweb_render_url(c, url);
1564 int
1565 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1567 va_list ap;
1568 int r;
1570 if (fcgi_printf(c, "<a href='") == -1)
1571 return -1;
1573 if (gotweb_render_url(c, url) == -1)
1574 return -1;
1576 if (fcgi_printf(c, "'>") == -1)
1577 return -1;
1579 va_start(ap, fmt);
1580 r = fcgi_vprintf(c, fmt, ap);
1581 va_end(ap);
1582 if (r == -1)
1583 return -1;
1585 if (fcgi_printf(c, "</a>"))
1586 return -1;
1587 return 0;
1590 static struct got_repository *
1591 find_cached_repo(struct server *srv, const char *path)
1593 int i;
1595 for (i = 0; i < srv->ncached_repos; i++) {
1596 if (strcmp(srv->cached_repos[i].path, path) == 0)
1597 return srv->cached_repos[i].repo;
1600 return NULL;
1603 static const struct got_error *
1604 cache_repo(struct got_repository **new, struct server *srv,
1605 struct repo_dir *repo_dir, struct socket *sock)
1607 const struct got_error *error = NULL;
1608 struct got_repository *repo;
1609 struct cached_repo *cr;
1610 int evicted = 0;
1612 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1613 cr = &srv->cached_repos[srv->ncached_repos - 1];
1614 error = got_repo_close(cr->repo);
1615 memset(cr, 0, sizeof(*cr));
1616 srv->ncached_repos--;
1617 if (error)
1618 return error;
1619 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1620 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1621 cr = &srv->cached_repos[0];
1622 evicted = 1;
1623 } else {
1624 cr = &srv->cached_repos[srv->ncached_repos];
1627 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1628 if (error) {
1629 if (evicted) {
1630 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1631 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1633 return error;
1636 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1637 >= sizeof(cr->path)) {
1638 if (evicted) {
1639 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1640 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1642 return got_error(GOT_ERR_NO_SPACE);
1645 cr->repo = repo;
1646 srv->ncached_repos++;
1647 *new = repo;
1648 return NULL;
1651 static const struct got_error *
1652 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1654 const struct got_error *error = NULL;
1655 struct socket *sock = c->sock;
1656 struct server *srv = c->srv;
1657 struct transport *t = c->t;
1658 struct got_repository *repo = NULL;
1659 DIR *dt;
1660 char *dir_test;
1662 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1663 GOTWEB_GIT_DIR) == -1)
1664 return got_error_from_errno("asprintf");
1666 dt = opendir(dir_test);
1667 if (dt == NULL) {
1668 free(dir_test);
1669 } else {
1670 repo_dir->path = dir_test;
1671 dir_test = NULL;
1672 goto done;
1675 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1676 repo_dir->name) == -1)
1677 return got_error_from_errno("asprintf");
1679 dt = opendir(dir_test);
1680 if (dt == NULL) {
1681 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1682 goto err;
1683 } else {
1684 repo_dir->path = dir_test;
1685 dir_test = NULL;
1688 done:
1689 if (srv->respect_exportok &&
1690 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1691 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1692 goto err;
1695 repo = find_cached_repo(srv, repo_dir->path);
1696 if (repo == NULL) {
1697 error = cache_repo(&repo, srv, repo_dir, sock);
1698 if (error)
1699 goto err;
1701 t->repo = repo;
1702 error = gotweb_get_repo_description(&repo_dir->description, srv,
1703 repo_dir->path, dirfd(dt));
1704 if (error)
1705 goto err;
1706 error = got_get_repo_owner(&repo_dir->owner, c);
1707 if (error)
1708 goto err;
1709 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1710 if (error)
1711 goto err;
1712 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1713 dirfd(dt));
1714 err:
1715 free(dir_test);
1716 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1717 error = got_error_from_errno("closedir");
1718 return error;
1721 static const struct got_error *
1722 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1724 const struct got_error *error;
1726 *repo_dir = calloc(1, sizeof(**repo_dir));
1727 if (*repo_dir == NULL)
1728 return got_error_from_errno("calloc");
1730 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1731 error = got_error_from_errno("asprintf");
1732 free(*repo_dir);
1733 *repo_dir = NULL;
1734 return error;
1736 (*repo_dir)->owner = NULL;
1737 (*repo_dir)->description = NULL;
1738 (*repo_dir)->url = NULL;
1739 (*repo_dir)->age = NULL;
1740 (*repo_dir)->path = NULL;
1742 return NULL;
1745 static const struct got_error *
1746 gotweb_get_repo_description(char **description, struct server *srv,
1747 const char *dirpath, int dir)
1749 const struct got_error *error = NULL;
1750 struct stat sb;
1751 int fd = -1;
1752 off_t len;
1754 *description = NULL;
1755 if (srv->show_repo_description == 0)
1756 return NULL;
1758 fd = openat(dir, "description", O_RDONLY);
1759 if (fd == -1) {
1760 if (errno != ENOENT && errno != EACCES) {
1761 error = got_error_from_errno_fmt("openat %s/%s",
1762 dirpath, "description");
1764 goto done;
1767 if (fstat(fd, &sb) == -1) {
1768 error = got_error_from_errno_fmt("fstat %s/%s",
1769 dirpath, "description");
1770 goto done;
1773 len = sb.st_size;
1774 if (len > GOTWEBD_MAXDESCRSZ - 1)
1775 len = GOTWEBD_MAXDESCRSZ - 1;
1777 *description = calloc(len + 1, sizeof(**description));
1778 if (*description == NULL) {
1779 error = got_error_from_errno("calloc");
1780 goto done;
1783 if (read(fd, *description, len) == -1)
1784 error = got_error_from_errno("read");
1785 done:
1786 if (fd != -1 && close(fd) == -1 && error == NULL)
1787 error = got_error_from_errno("close");
1788 return error;
1791 static const struct got_error *
1792 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1793 int dir)
1795 const struct got_error *error = NULL;
1796 struct stat sb;
1797 int fd = -1;
1798 off_t len;
1800 *url = NULL;
1801 if (srv->show_repo_cloneurl == 0)
1802 return NULL;
1804 fd = openat(dir, "cloneurl", O_RDONLY);
1805 if (fd == -1) {
1806 if (errno != ENOENT && errno != EACCES) {
1807 error = got_error_from_errno_fmt("openat %s/%s",
1808 dirpath, "cloneurl");
1810 goto done;
1813 if (fstat(fd, &sb) == -1) {
1814 error = got_error_from_errno_fmt("fstat %s/%s",
1815 dirpath, "cloneurl");
1816 goto done;
1819 len = sb.st_size;
1820 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1821 len = GOTWEBD_MAXCLONEURLSZ - 1;
1823 *url = calloc(len + 1, sizeof(**url));
1824 if (*url == NULL) {
1825 error = got_error_from_errno("calloc");
1826 goto done;
1829 if (read(fd, *url, len) == -1)
1830 error = got_error_from_errno("read");
1831 done:
1832 if (fd != -1 && close(fd) == -1 && error == NULL)
1833 error = got_error_from_errno("close");
1834 return error;
1837 const struct got_error *
1838 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1840 struct tm tm;
1841 long long diff_time;
1842 const char *years = "years ago", *months = "months ago";
1843 const char *weeks = "weeks ago", *days = "days ago";
1844 const char *hours = "hours ago", *minutes = "minutes ago";
1845 const char *seconds = "seconds ago", *now = "right now";
1846 char *s;
1847 char datebuf[64];
1848 size_t r;
1850 *repo_age = NULL;
1852 switch (ref_tm) {
1853 case TM_DIFF:
1854 diff_time = time(NULL) - committer_time;
1855 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1856 if (asprintf(repo_age, "%lld %s",
1857 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1858 return got_error_from_errno("asprintf");
1859 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1860 if (asprintf(repo_age, "%lld %s",
1861 (diff_time / 60 / 60 / 24 / (365 / 12)),
1862 months) == -1)
1863 return got_error_from_errno("asprintf");
1864 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1865 if (asprintf(repo_age, "%lld %s",
1866 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1867 return got_error_from_errno("asprintf");
1868 } else if (diff_time > 60 * 60 * 24 * 2) {
1869 if (asprintf(repo_age, "%lld %s",
1870 (diff_time / 60 / 60 / 24), days) == -1)
1871 return got_error_from_errno("asprintf");
1872 } else if (diff_time > 60 * 60 * 2) {
1873 if (asprintf(repo_age, "%lld %s",
1874 (diff_time / 60 / 60), hours) == -1)
1875 return got_error_from_errno("asprintf");
1876 } else if (diff_time > 60 * 2) {
1877 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1878 minutes) == -1)
1879 return got_error_from_errno("asprintf");
1880 } else if (diff_time > 2) {
1881 if (asprintf(repo_age, "%lld %s", diff_time,
1882 seconds) == -1)
1883 return got_error_from_errno("asprintf");
1884 } else {
1885 if (asprintf(repo_age, "%s", now) == -1)
1886 return got_error_from_errno("asprintf");
1888 break;
1889 case TM_LONG:
1890 if (gmtime_r(&committer_time, &tm) == NULL)
1891 return got_error_from_errno("gmtime_r");
1893 s = asctime_r(&tm, datebuf);
1894 if (s == NULL)
1895 return got_error_from_errno("asctime_r");
1897 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1898 return got_error_from_errno("asprintf");
1899 break;
1900 case TM_RFC822:
1901 if (gmtime_r(&committer_time, &tm) == NULL)
1902 return got_error_from_errno("gmtime_r");
1904 r = strftime(datebuf, sizeof(datebuf),
1905 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1906 if (r == 0)
1907 return got_error(GOT_ERR_NO_SPACE);
1909 *repo_age = strdup(datebuf);
1910 if (*repo_age == NULL)
1911 return got_error_from_errno("asprintf");
1912 break;
1914 return NULL;