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 <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_diff.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
50 #include "got_compat.h"
52 #include "proc.h"
53 #include "gotwebd.h"
54 #include "tmpl.h"
56 static const struct querystring_keys querystring_keys[] = {
57 { "action", ACTION },
58 { "commit", COMMIT },
59 { "file", RFILE },
60 { "folder", FOLDER },
61 { "headref", HEADREF },
62 { "index_page", INDEX_PAGE },
63 { "path", PATH },
64 { "page", PAGE },
65 };
67 static const struct action_keys action_keys[] = {
68 { "blame", BLAME },
69 { "blob", BLOB },
70 { "blobraw", BLOBRAW },
71 { "briefs", BRIEFS },
72 { "commits", COMMITS },
73 { "diff", DIFF },
74 { "error", ERR },
75 { "index", INDEX },
76 { "summary", SUMMARY },
77 { "tag", TAG },
78 { "tags", TAGS },
79 { "tree", TREE },
80 { "rss", RSS },
81 };
83 static const struct got_error *gotweb_init_querystring(struct querystring **);
84 static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 char *);
86 static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 char *, char *);
88 static const struct got_error *gotweb_render_index(struct request *);
89 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 const char *);
91 static const struct got_error *gotweb_load_got_path(struct request *c,
92 struct repo_dir *);
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 *,
96 const char *, int);
97 static const struct got_error *gotweb_render_blame(struct request *);
98 static const struct got_error *gotweb_render_diff(struct request *);
99 static const struct got_error *gotweb_render_summary(struct request *);
100 static const struct got_error *gotweb_render_tags(struct request *);
101 static const struct got_error *gotweb_render_branches(struct request *);
103 static void gotweb_free_querystring(struct querystring *);
104 static void gotweb_free_repo_dir(struct repo_dir *);
106 struct server *gotweb_get_server(uint8_t *, uint8_t *);
108 void
109 gotweb_process_request(struct request *c)
111 const struct got_error *error = NULL, *error2 = NULL;
112 struct got_blob_object *blob = NULL;
113 struct server *srv = NULL;
114 struct querystring *qs = NULL;
115 struct repo_dir *repo_dir = NULL;
116 uint8_t err[] = "gotwebd experienced an error: ";
117 int r, html = 0, fd = -1;
119 /* init the transport */
120 error = gotweb_init_transport(&c->t);
121 if (error) {
122 log_warnx("%s: %s", __func__, error->msg);
123 return;
125 /* don't process any further if client disconnected */
126 if (c->sock->client_status == CLIENT_DISCONNECT)
127 return;
128 /* get the gotwebd server */
129 srv = gotweb_get_server(c->server_name, c->http_host);
130 if (srv == NULL) {
131 log_warnx("%s: error server is NULL", __func__);
132 goto err;
134 c->srv = srv;
135 /* parse our querystring */
136 error = gotweb_init_querystring(&qs);
137 if (error) {
138 log_warnx("%s: %s", __func__, error->msg);
139 goto err;
141 c->t->qs = qs;
142 error = gotweb_parse_querystring(&qs, c->querystring);
143 if (error) {
144 log_warnx("%s: %s", __func__, error->msg);
145 goto err;
148 /*
149 * certain actions require a commit id in the querystring. this stops
150 * bad actors from exploiting this by manually manipulating the
151 * querystring.
152 */
154 if (qs->action == BLAME || qs->action == BLOB ||
155 qs->action == BLOBRAW || qs->action == DIFF) {
156 if (qs->commit == NULL) {
157 error2 = got_error(GOT_ERR_QUERYSTRING);
158 goto render;
162 if (qs->action != INDEX) {
163 error = gotweb_init_repo_dir(&repo_dir, qs->path);
164 if (error)
165 goto done;
166 error = gotweb_load_got_path(c, repo_dir);
167 c->t->repo_dir = repo_dir;
168 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
169 goto err;
172 if (qs->action == BLOBRAW) {
173 error = got_get_repo_commits(c, 1);
174 if (error)
175 goto done;
176 error = got_output_file_blob(c);
177 if (error) {
178 log_warnx("%s: %s", __func__, error->msg);
179 goto err;
181 goto done;
184 if (qs->action == BLOB) {
185 int binary;
186 struct gotweb_url url = {
187 .index_page = -1,
188 .page = -1,
189 .action = BLOBRAW,
190 .path = qs->path,
191 .commit = qs->commit,
192 .folder = qs->folder,
193 .file = qs->file,
194 };
196 error = got_get_repo_commits(c, 1);
197 if (error)
198 goto done;
200 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
201 if (error2)
202 goto render;
203 if (binary) {
204 fcgi_puts(c->tp, "Status: 302\r\n");
205 fcgi_puts(c->tp, "Location: ");
206 gotweb_render_url(c, &url);
207 fcgi_puts(c->tp, "\r\n\r\n");
208 goto done;
212 if (qs->action == RSS) {
213 error = gotweb_render_content_type_file(c,
214 "application/rss+xml;charset=utf-8",
215 repo_dir->name, ".rss");
216 if (error) {
217 log_warnx("%s: %s", __func__, error->msg);
218 goto err;
221 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
222 if (error) {
223 log_warnx("%s: %s", __func__, error->msg);
224 goto err;
226 if (gotweb_render_rss(c->tp) == -1)
227 goto err;
228 goto done;
231 render:
232 error = gotweb_render_content_type(c, "text/html");
233 if (error) {
234 log_warnx("%s: %s", __func__, error->msg);
235 goto err;
237 html = 1;
239 if (gotweb_render_header(c->tp) == -1)
240 goto err;
242 if (error2) {
243 error = error2;
244 goto err;
247 switch(qs->action) {
248 case BLAME:
249 error = gotweb_render_blame(c);
250 if (error) {
251 log_warnx("%s: %s", __func__, error->msg);
252 goto err;
254 break;
255 case BLOB:
256 if (gotweb_render_blob(c->tp, blob) == -1)
257 goto err;
258 break;
259 case BRIEFS:
260 if (gotweb_render_briefs(c->tp) == -1)
261 goto err;
262 break;
263 case COMMITS:
264 error = got_get_repo_commits(c, srv->max_commits_display);
265 if (error) {
266 log_warnx("%s: %s", __func__, error->msg);
267 goto err;
269 if (gotweb_render_commits(c->tp) == -1)
270 goto err;
271 break;
272 case DIFF:
273 error = gotweb_render_diff(c);
274 if (error) {
275 log_warnx("%s: %s", __func__, error->msg);
276 goto err;
278 break;
279 case INDEX:
280 error = gotweb_render_index(c);
281 if (error) {
282 log_warnx("%s: %s", __func__, error->msg);
283 goto err;
285 break;
286 case SUMMARY:
287 error = gotweb_render_summary(c);
288 if (error) {
289 log_warnx("%s: %s", __func__, error->msg);
290 goto err;
292 break;
293 case TAG:
294 error = got_get_repo_tags(c, 1);
295 if (error) {
296 log_warnx("%s: %s", __func__, error->msg);
297 goto err;
299 if (c->t->tag_count == 0) {
300 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
301 "bad commit id");
302 goto err;
304 if (gotweb_render_tag(c->tp) == -1)
305 goto done;
306 break;
307 case TAGS:
308 error = gotweb_render_tags(c);
309 if (error) {
310 log_warnx("%s: %s", __func__, error->msg);
311 goto err;
313 break;
314 case TREE:
315 error = got_get_repo_commits(c, 1);
316 if (error) {
317 log_warnx("%s: %s", __func__, error->msg);
318 goto err;
320 if (gotweb_render_tree(c->tp) == -1)
321 goto err;
322 break;
323 case ERR:
324 default:
325 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
326 "Erorr: Bad Querystring");
327 if (r == -1)
328 goto err;
329 break;
332 goto done;
333 err:
334 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
335 return;
336 if (fcgi_printf(c, "\n%s", err) == -1)
337 return;
338 if (error) {
339 if (fcgi_printf(c, "%s", error->msg) == -1)
340 return;
341 } else {
342 if (fcgi_printf(c, "see daemon logs for details") == -1)
343 return;
345 if (html && fcgi_printf(c, "</div>\n") == -1)
346 return;
347 done:
348 if (blob)
349 got_object_blob_close(blob);
350 if (fd != -1)
351 close(fd);
352 if (html && srv != NULL)
353 gotweb_render_footer(c->tp);
356 struct server *
357 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
359 struct server *srv = NULL;
361 /* check against the server name first */
362 if (strlen(server_name) > 0)
363 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
364 if (strcmp(srv->name, server_name) == 0)
365 goto done;
367 /* check against subdomain second */
368 if (strlen(subdomain) > 0)
369 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
370 if (strcmp(srv->name, subdomain) == 0)
371 goto done;
373 /* if those fail, send first server */
374 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
375 if (srv != NULL)
376 break;
377 done:
378 return srv;
379 };
381 const struct got_error *
382 gotweb_init_transport(struct transport **t)
384 const struct got_error *error = NULL;
386 *t = calloc(1, sizeof(**t));
387 if (*t == NULL)
388 return got_error_from_errno2("%s: calloc", __func__);
390 TAILQ_INIT(&(*t)->repo_commits);
391 TAILQ_INIT(&(*t)->repo_tags);
393 (*t)->repo = NULL;
394 (*t)->repo_dir = NULL;
395 (*t)->qs = NULL;
396 (*t)->next_id = NULL;
397 (*t)->prev_id = NULL;
398 (*t)->next_disp = 0;
399 (*t)->prev_disp = 0;
401 return error;
404 static const struct got_error *
405 gotweb_init_querystring(struct querystring **qs)
407 const struct got_error *error = NULL;
409 *qs = calloc(1, sizeof(**qs));
410 if (*qs == NULL)
411 return got_error_from_errno2("%s: calloc", __func__);
413 (*qs)->headref = strdup("HEAD");
414 if ((*qs)->headref == NULL) {
415 free(*qs);
416 *qs = NULL;
417 return got_error_from_errno2("%s: strdup", __func__);
420 (*qs)->action = INDEX;
421 (*qs)->commit = NULL;
422 (*qs)->file = NULL;
423 (*qs)->folder = NULL;
424 (*qs)->index_page = 0;
425 (*qs)->path = NULL;
427 return error;
430 static const struct got_error *
431 gotweb_parse_querystring(struct querystring **qs, char *qst)
433 const struct got_error *error = NULL;
434 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
435 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
437 if (qst == NULL)
438 return error;
440 tok1 = strdup(qst);
441 if (tok1 == NULL)
442 return got_error_from_errno2("%s: strdup", __func__);
444 tok1_pair = tok1;
445 tok1_end = tok1;
447 while (tok1_pair != NULL) {
448 strsep(&tok1_end, "&");
450 tok2 = strdup(tok1_pair);
451 if (tok2 == NULL) {
452 free(tok1);
453 return got_error_from_errno2("%s: strdup", __func__);
456 tok2_pair = tok2;
457 tok2_end = tok2;
459 while (tok2_pair != NULL) {
460 strsep(&tok2_end, "=");
461 if (tok2_end) {
462 error = gotweb_assign_querystring(qs, tok2_pair,
463 tok2_end);
464 if (error)
465 goto err;
467 tok2_pair = tok2_end;
469 free(tok2);
470 tok1_pair = tok1_end;
472 free(tok1);
473 return error;
474 err:
475 free(tok2);
476 free(tok1);
477 return error;
480 /*
481 * Adapted from usr.sbin/httpd/httpd.c url_decode.
482 */
483 static const struct got_error *
484 gotweb_urldecode(char *url)
486 char *p, *q;
487 char hex[3];
488 unsigned long x;
490 hex[2] = '\0';
491 p = q = url;
493 while (*p != '\0') {
494 switch (*p) {
495 case '%':
496 /* Encoding character is followed by two hex chars */
497 if (!isxdigit((unsigned char)p[1]) ||
498 !isxdigit((unsigned char)p[2]) ||
499 (p[1] == '0' && p[2] == '0'))
500 return got_error(GOT_ERR_BAD_QUERYSTRING);
502 hex[0] = p[1];
503 hex[1] = p[2];
505 /*
506 * We don't have to validate "hex" because it is
507 * guaranteed to include two hex chars followed by nul.
508 */
509 x = strtoul(hex, NULL, 16);
510 *q = (char)x;
511 p += 2;
512 break;
513 default:
514 *q = *p;
515 break;
517 p++;
518 q++;
520 *q = '\0';
522 return NULL;
525 static const struct got_error *
526 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
528 const struct got_error *error = NULL;
529 const char *errstr;
530 int a_cnt, el_cnt;
532 error = gotweb_urldecode(value);
533 if (error)
534 return error;
536 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
537 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
538 continue;
540 switch (querystring_keys[el_cnt].element) {
541 case ACTION:
542 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
543 if (strcmp(value, action_keys[a_cnt].name) != 0)
544 continue;
545 else if (strcmp(value,
546 action_keys[a_cnt].name) == 0){
547 (*qs)->action =
548 action_keys[a_cnt].action;
549 goto qa_found;
552 (*qs)->action = ERR;
553 qa_found:
554 break;
555 case COMMIT:
556 (*qs)->commit = strdup(value);
557 if ((*qs)->commit == NULL) {
558 error = got_error_from_errno2("%s: strdup",
559 __func__);
560 goto done;
562 break;
563 case RFILE:
564 (*qs)->file = strdup(value);
565 if ((*qs)->file == NULL) {
566 error = got_error_from_errno2("%s: strdup",
567 __func__);
568 goto done;
570 break;
571 case FOLDER:
572 (*qs)->folder = strdup(value);
573 if ((*qs)->folder == NULL) {
574 error = got_error_from_errno2("%s: strdup",
575 __func__);
576 goto done;
578 break;
579 case HEADREF:
580 free((*qs)->headref);
581 (*qs)->headref = strdup(value);
582 if ((*qs)->headref == NULL) {
583 error = got_error_from_errno2("%s: strdup",
584 __func__);
585 goto done;
587 break;
588 case INDEX_PAGE:
589 if (strlen(value) == 0)
590 break;
591 (*qs)->index_page = strtonum(value, INT64_MIN,
592 INT64_MAX, &errstr);
593 if (errstr) {
594 error = got_error_from_errno3("%s: strtonum %s",
595 __func__, errstr);
596 goto done;
598 if ((*qs)->index_page < 0)
599 (*qs)->index_page = 0;
600 break;
601 case PATH:
602 (*qs)->path = strdup(value);
603 if ((*qs)->path == NULL) {
604 error = got_error_from_errno2("%s: strdup",
605 __func__);
606 goto done;
608 break;
609 case PAGE:
610 if (strlen(value) == 0)
611 break;
612 (*qs)->page = strtonum(value, INT64_MIN,
613 INT64_MAX, &errstr);
614 if (errstr) {
615 error = got_error_from_errno3("%s: strtonum %s",
616 __func__, errstr);
617 goto done;
619 if ((*qs)->page < 0)
620 (*qs)->page = 0;
621 break;
622 default:
623 break;
626 done:
627 return error;
630 void
631 gotweb_free_repo_tag(struct repo_tag *rt)
633 if (rt != NULL) {
634 free(rt->commit_id);
635 free(rt->tag_name);
636 free(rt->tag_commit);
637 free(rt->commit_msg);
638 free(rt->tagger);
640 free(rt);
643 void
644 gotweb_free_repo_commit(struct repo_commit *rc)
646 if (rc != NULL) {
647 free(rc->path);
648 free(rc->refs_str);
649 free(rc->commit_id);
650 free(rc->parent_id);
651 free(rc->tree_id);
652 free(rc->author);
653 free(rc->committer);
654 free(rc->commit_msg);
656 free(rc);
659 static void
660 gotweb_free_querystring(struct querystring *qs)
662 if (qs != NULL) {
663 free(qs->commit);
664 free(qs->file);
665 free(qs->folder);
666 free(qs->headref);
667 free(qs->path);
669 free(qs);
672 static void
673 gotweb_free_repo_dir(struct repo_dir *repo_dir)
675 if (repo_dir != NULL) {
676 free(repo_dir->name);
677 free(repo_dir->owner);
678 free(repo_dir->description);
679 free(repo_dir->url);
680 free(repo_dir->age);
681 free(repo_dir->path);
683 free(repo_dir);
686 void
687 gotweb_free_transport(struct transport *t)
689 struct repo_commit *rc = NULL, *trc = NULL;
690 struct repo_tag *rt = NULL, *trt = NULL;
692 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
693 TAILQ_REMOVE(&t->repo_commits, rc, entry);
694 gotweb_free_repo_commit(rc);
696 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
697 TAILQ_REMOVE(&t->repo_tags, rt, entry);
698 gotweb_free_repo_tag(rt);
700 gotweb_free_repo_dir(t->repo_dir);
701 gotweb_free_querystring(t->qs);
702 free(t->next_id);
703 free(t->prev_id);
704 free(t);
707 const struct got_error *
708 gotweb_render_content_type(struct request *c, const char *type)
710 const char *csp = "default-src 'self'; script-src 'none'; "
711 "object-src 'none';";
713 fcgi_printf(c,
714 "Content-Security-Policy: %s\r\n"
715 "Content-Type: %s\r\n\r\n",
716 csp, type);
717 return NULL;
720 const struct got_error *
721 gotweb_render_content_type_file(struct request *c, const char *type,
722 const char *file, const char *suffix)
724 fcgi_printf(c, "Content-type: %s\r\n"
725 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
726 type, file, suffix ? suffix : "");
727 return NULL;
730 void
731 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
732 struct gotweb_url *next, int *have_next)
734 struct transport *t = c->t;
735 struct querystring *qs = t->qs;
736 struct server *srv = c->srv;
738 *have_prev = *have_next = 0;
740 switch(qs->action) {
741 case INDEX:
742 if (qs->index_page > 0) {
743 *have_prev = 1;
744 *prev = (struct gotweb_url){
745 .action = -1,
746 .index_page = qs->index_page - 1,
747 .page = -1,
748 };
750 if (t->next_disp == srv->max_repos_display &&
751 t->repos_total != (qs->index_page + 1) *
752 srv->max_repos_display) {
753 *have_next = 1;
754 *next = (struct gotweb_url){
755 .action = -1,
756 .index_page = qs->index_page + 1,
757 .page = -1,
758 };
760 break;
761 case BRIEFS:
762 if (t->prev_id && qs->commit != NULL &&
763 strcmp(qs->commit, t->prev_id) != 0) {
764 *have_prev = 1;
765 *prev = (struct gotweb_url){
766 .action = BRIEFS,
767 .index_page = -1,
768 .page = qs->page - 1,
769 .path = qs->path,
770 .commit = t->prev_id,
771 .headref = qs->headref,
772 };
774 if (t->next_id) {
775 *have_next = 1;
776 *next = (struct gotweb_url){
777 .action = BRIEFS,
778 .index_page = -1,
779 .page = qs->page + 1,
780 .path = qs->path,
781 .commit = t->next_id,
782 .headref = qs->headref,
783 };
785 break;
786 case COMMITS:
787 if (t->prev_id && qs->commit != NULL &&
788 strcmp(qs->commit, t->prev_id) != 0) {
789 *have_prev = 1;
790 *prev = (struct gotweb_url){
791 .action = COMMITS,
792 .index_page = -1,
793 .page = qs->page - 1,
794 .path = qs->path,
795 .commit = t->prev_id,
796 .headref = qs->headref,
797 .folder = qs->folder,
798 .file = qs->file,
799 };
801 if (t->next_id) {
802 *have_next = 1;
803 *next = (struct gotweb_url){
804 .action = COMMITS,
805 .index_page = -1,
806 .page = qs->page + 1,
807 .path = qs->path,
808 .commit = t->next_id,
809 .headref = qs->headref,
810 .folder = qs->folder,
811 .file = qs->file,
812 };
814 break;
815 case TAGS:
816 if (t->prev_id && qs->commit != NULL &&
817 strcmp(qs->commit, t->prev_id) != 0) {
818 *have_prev = 1;
819 *prev = (struct gotweb_url){
820 .action = TAGS,
821 .index_page = -1,
822 .page = qs->page - 1,
823 .path = qs->path,
824 .commit = t->prev_id,
825 .headref = qs->headref,
826 };
828 if (t->next_id) {
829 *have_next = 1;
830 *next = (struct gotweb_url){
831 .action = TAGS,
832 .index_page = -1,
833 .page = qs->page + 1,
834 .path = qs->path,
835 .commit = t->next_id,
836 .headref = qs->headref,
837 };
839 break;
843 static const struct got_error *
844 gotweb_render_index(struct request *c)
846 const struct got_error *error = NULL;
847 struct server *srv = c->srv;
848 struct transport *t = c->t;
849 struct querystring *qs = t->qs;
850 struct repo_dir *repo_dir = NULL;
851 DIR *d;
852 struct dirent **sd_dent = NULL;
853 unsigned int d_cnt, d_i, d_disp = 0;
854 unsigned int d_skipped = 0;
855 int type;
857 d = opendir(srv->repos_path);
858 if (d == NULL) {
859 error = got_error_from_errno2("opendir", srv->repos_path);
860 return error;
863 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
864 if (d_cnt == -1) {
865 sd_dent = NULL;
866 error = got_error_from_errno2("scandir", srv->repos_path);
867 goto done;
870 if (gotweb_render_repo_table_hdr(c->tp) == -1)
871 goto done;
873 for (d_i = 0; d_i < d_cnt; d_i++) {
874 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
875 break;
877 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
878 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
879 d_skipped++;
880 continue;
883 error = got_path_dirent_type(&type, srv->repos_path,
884 sd_dent[d_i]);
885 if (error)
886 goto done;
887 if (type != DT_DIR) {
888 d_skipped++;
889 continue;
892 if (qs->index_page > 0 && (qs->index_page *
893 srv->max_repos_display) > t->prev_disp) {
894 t->prev_disp++;
895 continue;
898 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
899 if (error)
900 goto done;
902 error = gotweb_load_got_path(c, repo_dir);
903 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
904 error = NULL;
905 gotweb_free_repo_dir(repo_dir);
906 repo_dir = NULL;
907 d_skipped++;
908 continue;
910 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
911 goto done;
913 d_disp++;
914 t->prev_disp++;
916 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
917 goto done;
919 gotweb_free_repo_dir(repo_dir);
920 repo_dir = NULL;
921 t->next_disp++;
922 if (d_disp == srv->max_repos_display)
923 break;
925 t->repos_total = d_cnt - d_skipped;
927 if (srv->max_repos_display == 0)
928 goto done;
929 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
930 goto done;
931 if (t->repos_total <= srv->max_repos ||
932 t->repos_total <= srv->max_repos_display)
933 goto done;
935 if (gotweb_render_navs(c->tp) == -1)
936 goto done;
937 done:
938 if (sd_dent) {
939 for (d_i = 0; d_i < d_cnt; d_i++)
940 free(sd_dent[d_i]);
941 free(sd_dent);
943 if (d != NULL && closedir(d) == EOF && error == NULL)
944 error = got_error_from_errno("closedir");
945 return error;
948 static const struct got_error *
949 gotweb_render_blame(struct request *c)
951 const struct got_error *error = NULL;
952 struct transport *t = c->t;
953 struct repo_commit *rc = NULL;
954 char *age = NULL, *msg = NULL;
955 int r;
957 error = got_get_repo_commits(c, 1);
958 if (error)
959 return error;
961 rc = TAILQ_FIRST(&t->repo_commits);
963 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
964 if (error)
965 goto done;
966 error = gotweb_escape_html(&msg, rc->commit_msg);
967 if (error)
968 goto done;
970 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
971 "<div id='blame_title'>Blame</div>\n"
972 "</div>\n" /* #blame_title_wrapper */
973 "<div id='blame_content'>\n"
974 "<div id='blame_header_wrapper'>\n"
975 "<div id='blame_header'>\n"
976 "<div class='header_age_title'>Date:</div>\n"
977 "<div class='header_age'>%s</div>\n"
978 "<div id='header_commit_msg_title'>Message:</div>\n"
979 "<div id='header_commit_msg'>%s</div>\n"
980 "</div>\n" /* #blame_header */
981 "</div>\n" /* #blame_header_wrapper */
982 "<div class='dotted_line'></div>\n"
983 "<div id='blame'>\n",
984 age,
985 msg);
986 if (r == -1)
987 goto done;
989 error = got_output_file_blame(c);
990 if (error)
991 goto done;
993 fcgi_printf(c, "</div>\n" /* #blame */
994 "</div>\n"); /* #blame_content */
995 done:
996 free(age);
997 free(msg);
998 return error;
1001 static const struct got_error *
1002 gotweb_render_branches(struct request *c)
1004 const struct got_error *error = NULL;
1005 struct got_reflist_head refs;
1006 struct got_reflist_entry *re;
1007 struct transport *t = c->t;
1008 struct querystring *qs = t->qs;
1009 struct got_repository *repo = t->repo;
1010 char *escaped_refname = NULL;
1011 char *age = NULL;
1012 int r;
1014 TAILQ_INIT(&refs);
1016 error = got_ref_list(&refs, repo, "refs/heads",
1017 got_ref_cmp_by_name, NULL);
1018 if (error)
1019 goto done;
1021 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1022 "<div id='branches_title'>Branches</div>\n"
1023 "</div>\n" /* #branches_title_wrapper */
1024 "<div id='branches_content'>\n");
1025 if (r == -1)
1026 goto done;
1028 TAILQ_FOREACH(re, &refs, entry) {
1029 const char *refname = NULL;
1031 if (got_ref_is_symbolic(re->ref))
1032 continue;
1034 refname = got_ref_get_name(re->ref);
1035 if (refname == NULL) {
1036 error = got_error_from_errno("strdup");
1037 goto done;
1039 if (strncmp(refname, "refs/heads/", 11) != 0)
1040 continue;
1042 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1043 if (error)
1044 goto done;
1046 if (strncmp(refname, "refs/heads/", 11) == 0)
1047 refname += 11;
1048 error = gotweb_escape_html(&escaped_refname, refname);
1049 if (error)
1050 goto done;
1052 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1053 "<div class='branches_age'>%s</div>\n"
1054 "<div class='branches_space'>&nbsp;</div>\n"
1055 "<div class='branch'>", age);
1056 if (r == -1)
1057 goto done;
1059 r = gotweb_link(c, &(struct gotweb_url){
1060 .action = SUMMARY,
1061 .index_page = -1,
1062 .page = -1,
1063 .path = qs->path,
1064 .headref = refname,
1065 }, "%s", escaped_refname);
1066 if (r == -1)
1067 goto done;
1069 if (fcgi_printf(c, "</div>\n" /* .branch */
1070 "<div class='navs_wrapper'>\n"
1071 "<div class='navs'>") == -1)
1072 goto done;
1074 r = gotweb_link(c, &(struct gotweb_url){
1075 .action = SUMMARY,
1076 .index_page = -1,
1077 .page = -1,
1078 .path = qs->path,
1079 .headref = refname,
1080 }, "summary");
1081 if (r == -1)
1082 goto done;
1084 if (fcgi_printf(c, " | ") == -1)
1085 goto done;
1087 r = gotweb_link(c, &(struct gotweb_url){
1088 .action = BRIEFS,
1089 .index_page = -1,
1090 .page = -1,
1091 .path = qs->path,
1092 .headref = refname,
1093 }, "commit briefs");
1094 if (r == -1)
1095 goto done;
1097 if (fcgi_printf(c, " | ") == -1)
1098 goto done;
1100 r = gotweb_link(c, &(struct gotweb_url){
1101 .action = COMMITS,
1102 .index_page = -1,
1103 .page = -1,
1104 .path = qs->path,
1105 .headref = refname,
1106 }, "commits");
1107 if (r == -1)
1108 goto done;
1110 r = fcgi_printf(c, "</div>\n" /* .navs */
1111 "</div>\n" /* .navs_wrapper */
1112 "<div class='dotted_line'></div>\n"
1113 "</div>\n"); /* .branches_wrapper */
1114 if (r == -1)
1115 goto done;
1117 free(age);
1118 age = NULL;
1119 free(escaped_refname);
1120 escaped_refname = NULL;
1122 fcgi_printf(c, "</div>\n"); /* #branches_content */
1123 done:
1124 free(age);
1125 free(escaped_refname);
1126 got_ref_list_free(&refs);
1127 return error;
1130 static const struct got_error *
1131 gotweb_render_diff(struct request *c)
1133 const struct got_error *error = NULL;
1134 struct transport *t = c->t;
1135 struct repo_commit *rc = NULL;
1136 char *age = NULL, *author = NULL, *msg = NULL;
1137 int r;
1139 error = got_get_repo_commits(c, 1);
1140 if (error)
1141 return error;
1143 rc = TAILQ_FIRST(&t->repo_commits);
1145 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1146 if (error)
1147 goto done;
1148 error = gotweb_escape_html(&author, rc->author);
1149 if (error)
1150 goto done;
1151 error = gotweb_escape_html(&msg, rc->commit_msg);
1152 if (error)
1153 goto done;
1155 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1156 "<div id='diff_title'>Commit Diff</div>\n"
1157 "</div>\n" /* #diff_title_wrapper */
1158 "<div id='diff_content'>\n"
1159 "<div id='diff_header_wrapper'>\n"
1160 "<div id='diff_header'>\n"
1161 "<div id='header_diff_title'>Diff:</div>\n"
1162 "<div id='header_diff'>%s<br />%s</div>\n"
1163 "<div class='header_commit_title'>Commit:</div>\n"
1164 "<div class='header_commit'>%s</div>\n"
1165 "<div id='header_tree_title'>Tree:</div>\n"
1166 "<div id='header_tree'>%s</div>\n"
1167 "<div class='header_author_title'>Author:</div>\n"
1168 "<div class='header_author'>%s</div>\n"
1169 "<div class='header_age_title'>Date:</div>\n"
1170 "<div class='header_age'>%s</div>\n"
1171 "<div id='header_commit_msg_title'>Message:</div>\n"
1172 "<div id='header_commit_msg'>%s</div>\n"
1173 "</div>\n" /* #diff_header */
1174 "</div>\n" /* #diff_header_wrapper */
1175 "<div class='dotted_line'></div>\n"
1176 "<div id='diff'>\n",
1177 rc->parent_id, rc->commit_id,
1178 rc->commit_id,
1179 rc->tree_id,
1180 author,
1181 age,
1182 msg);
1183 if (r == -1)
1184 goto done;
1186 error = got_output_repo_diff(c);
1187 if (error)
1188 goto done;
1190 fcgi_printf(c, "</div>\n"); /* #diff */
1191 fcgi_printf(c, "</div>\n"); /* #diff_content */
1192 done:
1193 free(age);
1194 free(author);
1195 free(msg);
1196 return error;
1199 static const struct got_error *
1200 gotweb_render_summary(struct request *c)
1202 const struct got_error *error = NULL;
1203 struct transport *t = c->t;
1204 struct server *srv = c->srv;
1205 int r;
1207 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1208 goto done;
1210 if (srv->show_repo_description) {
1211 r = fcgi_printf(c,
1212 "<div id='description_title'>Description:</div>\n"
1213 "<div id='description'>%s</div>\n",
1214 t->repo_dir->description ? t->repo_dir->description : "");
1215 if (r == -1)
1216 goto done;
1219 if (srv->show_repo_owner) {
1220 r = fcgi_printf(c,
1221 "<div id='repo_owner_title'>Owner:</div>\n"
1222 "<div id='repo_owner'>%s</div>\n",
1223 t->repo_dir->owner ? t->repo_dir->owner : "");
1224 if (r == -1)
1225 goto done;
1228 if (srv->show_repo_age) {
1229 r = fcgi_printf(c,
1230 "<div id='last_change_title'>Last Change:</div>\n"
1231 "<div id='last_change'>%s</div>\n",
1232 t->repo_dir->age);
1233 if (r == -1)
1234 goto done;
1237 if (srv->show_repo_cloneurl) {
1238 r = fcgi_printf(c,
1239 "<div id='cloneurl_title'>Clone URL:</div>\n"
1240 "<div id='cloneurl'>%s</div>\n",
1241 t->repo_dir->url ? t->repo_dir->url : "");
1242 if (r == -1)
1243 goto done;
1246 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1247 if (r == -1)
1248 goto done;
1250 if (gotweb_render_briefs(c->tp) == -1)
1251 goto done;
1253 error = gotweb_render_tags(c);
1254 if (error) {
1255 log_warnx("%s: %s", __func__, error->msg);
1256 goto done;
1259 error = gotweb_render_branches(c);
1260 if (error)
1261 log_warnx("%s: %s", __func__, error->msg);
1262 done:
1263 return error;
1266 static const struct got_error *
1267 gotweb_render_tags(struct request *c)
1269 const struct got_error *error = NULL;
1270 struct server *srv = c->srv;
1271 struct transport *t = c->t;
1272 struct querystring *qs = t->qs;
1274 if (qs->action == BRIEFS) {
1275 qs->action = TAGS;
1276 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1277 } else
1278 error = got_get_repo_tags(c, srv->max_commits_display);
1279 if (error)
1280 goto done;
1282 if (gotweb_render_tags_tmpl(c->tp) == -1)
1283 goto done;
1285 done:
1286 return error;
1289 const struct got_error *
1290 gotweb_escape_html(char **escaped_html, const char *orig_html)
1292 const struct got_error *error = NULL;
1293 struct escape_pair {
1294 char c;
1295 const char *s;
1296 } esc[] = {
1297 { '>', "&gt;" },
1298 { '<', "&lt;" },
1299 { '&', "&amp;" },
1300 { '"', "&quot;" },
1301 { '\'', "&apos;" },
1302 { '\n', "<br />" },
1304 size_t orig_len, len;
1305 int i, j, x;
1307 orig_len = strlen(orig_html);
1308 len = orig_len;
1309 for (i = 0; i < orig_len; i++) {
1310 for (j = 0; j < nitems(esc); j++) {
1311 if (orig_html[i] != esc[j].c)
1312 continue;
1313 len += strlen(esc[j].s) - 1 /* escaped char */;
1317 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1318 if (*escaped_html == NULL)
1319 return got_error_from_errno("calloc");
1321 x = 0;
1322 for (i = 0; i < orig_len; i++) {
1323 int escaped = 0;
1324 for (j = 0; j < nitems(esc); j++) {
1325 if (orig_html[i] != esc[j].c)
1326 continue;
1328 if (strlcat(*escaped_html, esc[j].s, len + 1)
1329 >= len + 1) {
1330 error = got_error(GOT_ERR_NO_SPACE);
1331 goto done;
1333 x += strlen(esc[j].s);
1334 escaped = 1;
1335 break;
1337 if (!escaped) {
1338 (*escaped_html)[x] = orig_html[i];
1339 x++;
1342 done:
1343 if (error) {
1344 free(*escaped_html);
1345 *escaped_html = NULL;
1346 } else {
1347 (*escaped_html)[x] = '\0';
1350 return error;
1353 static inline int
1354 should_urlencode(int c)
1356 if (c <= ' ' || c >= 127)
1357 return 1;
1359 switch (c) {
1360 /* gen-delim */
1361 case ':':
1362 case '/':
1363 case '?':
1364 case '#':
1365 case '[':
1366 case ']':
1367 case '@':
1368 /* sub-delims */
1369 case '!':
1370 case '$':
1371 case '&':
1372 case '\'':
1373 case '(':
1374 case ')':
1375 case '*':
1376 case '+':
1377 case ',':
1378 case ';':
1379 case '=':
1380 /* needed because the URLs are embedded into the HTML */
1381 case '\"':
1382 return 1;
1383 default:
1384 return 0;
1388 static char *
1389 gotweb_urlencode(const char *str)
1391 const char *s;
1392 char *escaped;
1393 size_t i, len;
1394 int a, b;
1396 len = 0;
1397 for (s = str; *s; ++s) {
1398 len++;
1399 if (should_urlencode(*s))
1400 len += 2;
1403 escaped = calloc(1, len + 1);
1404 if (escaped == NULL)
1405 return NULL;
1407 i = 0;
1408 for (s = str; *s; ++s) {
1409 if (should_urlencode(*s)) {
1410 a = (*s & 0xF0) >> 4;
1411 b = (*s & 0x0F);
1413 escaped[i++] = '%';
1414 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1415 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1416 } else
1417 escaped[i++] = *s;
1420 return escaped;
1423 const char *
1424 gotweb_action_name(int action)
1426 switch (action) {
1427 case BLAME:
1428 return "blame";
1429 case BLOB:
1430 return "blob";
1431 case BLOBRAW:
1432 return "blobraw";
1433 case BRIEFS:
1434 return "briefs";
1435 case COMMITS:
1436 return "commits";
1437 case DIFF:
1438 return "diff";
1439 case ERR:
1440 return "err";
1441 case INDEX:
1442 return "index";
1443 case SUMMARY:
1444 return "summary";
1445 case TAG:
1446 return "tag";
1447 case TAGS:
1448 return "tags";
1449 case TREE:
1450 return "tree";
1451 case RSS:
1452 return "rss";
1453 default:
1454 return NULL;
1458 int
1459 gotweb_render_url(struct request *c, struct gotweb_url *url)
1461 const char *sep = "?", *action;
1462 char *tmp;
1463 int r;
1465 action = gotweb_action_name(url->action);
1466 if (action != NULL) {
1467 if (fcgi_printf(c, "?action=%s", action) == -1)
1468 return -1;
1469 sep = "&";
1472 if (url->commit) {
1473 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1474 return -1;
1475 sep = "&";
1478 if (url->previd) {
1479 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1480 return -1;
1481 sep = "&";
1484 if (url->prevset) {
1485 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1486 return -1;
1487 sep = "&";
1490 if (url->file) {
1491 tmp = gotweb_urlencode(url->file);
1492 if (tmp == NULL)
1493 return -1;
1494 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1495 free(tmp);
1496 if (r == -1)
1497 return -1;
1498 sep = "&";
1501 if (url->folder) {
1502 tmp = gotweb_urlencode(url->folder);
1503 if (tmp == NULL)
1504 return -1;
1505 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1506 free(tmp);
1507 if (r == -1)
1508 return -1;
1509 sep = "&";
1512 if (url->headref) {
1513 tmp = gotweb_urlencode(url->headref);
1514 if (tmp == NULL)
1515 return -1;
1516 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1517 free(tmp);
1518 if (r == -1)
1519 return -1;
1520 sep = "&";
1523 if (url->index_page != -1) {
1524 if (fcgi_printf(c, "%sindex_page=%d", sep,
1525 url->index_page) == -1)
1526 return -1;
1527 sep = "&";
1530 if (url->path) {
1531 tmp = gotweb_urlencode(url->path);
1532 if (tmp == NULL)
1533 return -1;
1534 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1535 free(tmp);
1536 if (r == -1)
1537 return -1;
1538 sep = "&";
1541 if (url->page != -1) {
1542 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1543 return -1;
1544 sep = "&";
1547 return 0;
1550 int
1551 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1553 struct template *tp = c->tp;
1554 const char *proto = c->https ? "https" : "http";
1556 if (fcgi_puts(tp, proto) == -1 ||
1557 fcgi_puts(tp, "://") == -1 ||
1558 tp_htmlescape(tp, c->server_name) == -1 ||
1559 tp_htmlescape(tp, c->document_uri) == -1)
1560 return -1;
1562 return gotweb_render_url(c, url);
1565 int
1566 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1568 va_list ap;
1569 int r;
1571 if (fcgi_printf(c, "<a href='") == -1)
1572 return -1;
1574 if (gotweb_render_url(c, url) == -1)
1575 return -1;
1577 if (fcgi_printf(c, "'>") == -1)
1578 return -1;
1580 va_start(ap, fmt);
1581 r = fcgi_vprintf(c, fmt, ap);
1582 va_end(ap);
1583 if (r == -1)
1584 return -1;
1586 if (fcgi_printf(c, "</a>"))
1587 return -1;
1588 return 0;
1591 static struct got_repository *
1592 find_cached_repo(struct server *srv, const char *path)
1594 int i;
1596 for (i = 0; i < srv->ncached_repos; i++) {
1597 if (strcmp(srv->cached_repos[i].path, path) == 0)
1598 return srv->cached_repos[i].repo;
1601 return NULL;
1604 static const struct got_error *
1605 cache_repo(struct got_repository **new, struct server *srv,
1606 struct repo_dir *repo_dir, struct socket *sock)
1608 const struct got_error *error = NULL;
1609 struct got_repository *repo;
1610 struct cached_repo *cr;
1611 int evicted = 0;
1613 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1614 cr = &srv->cached_repos[srv->ncached_repos - 1];
1615 error = got_repo_close(cr->repo);
1616 memset(cr, 0, sizeof(*cr));
1617 srv->ncached_repos--;
1618 if (error)
1619 return error;
1620 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1621 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1622 cr = &srv->cached_repos[0];
1623 evicted = 1;
1624 } else {
1625 cr = &srv->cached_repos[srv->ncached_repos];
1628 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1629 if (error) {
1630 if (evicted) {
1631 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1632 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1634 return error;
1637 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1638 >= sizeof(cr->path)) {
1639 if (evicted) {
1640 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1641 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1643 return got_error(GOT_ERR_NO_SPACE);
1646 cr->repo = repo;
1647 srv->ncached_repos++;
1648 *new = repo;
1649 return NULL;
1652 static const struct got_error *
1653 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1655 const struct got_error *error = NULL;
1656 struct socket *sock = c->sock;
1657 struct server *srv = c->srv;
1658 struct transport *t = c->t;
1659 struct got_repository *repo = NULL;
1660 DIR *dt;
1661 char *dir_test;
1663 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1664 GOTWEB_GIT_DIR) == -1)
1665 return got_error_from_errno("asprintf");
1667 dt = opendir(dir_test);
1668 if (dt == NULL) {
1669 free(dir_test);
1670 } else {
1671 repo_dir->path = dir_test;
1672 dir_test = NULL;
1673 goto done;
1676 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1677 repo_dir->name) == -1)
1678 return got_error_from_errno("asprintf");
1680 dt = opendir(dir_test);
1681 if (dt == NULL) {
1682 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1683 goto err;
1684 } else {
1685 repo_dir->path = dir_test;
1686 dir_test = NULL;
1689 done:
1690 if (srv->respect_exportok &&
1691 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1692 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1693 goto err;
1696 repo = find_cached_repo(srv, repo_dir->path);
1697 if (repo == NULL) {
1698 error = cache_repo(&repo, srv, repo_dir, sock);
1699 if (error)
1700 goto err;
1702 t->repo = repo;
1703 error = gotweb_get_repo_description(&repo_dir->description, srv,
1704 repo_dir->path, dirfd(dt));
1705 if (error)
1706 goto err;
1707 error = got_get_repo_owner(&repo_dir->owner, c);
1708 if (error)
1709 goto err;
1710 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1711 if (error)
1712 goto err;
1713 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1714 dirfd(dt));
1715 err:
1716 free(dir_test);
1717 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1718 error = got_error_from_errno("closedir");
1719 return error;
1722 static const struct got_error *
1723 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1725 const struct got_error *error;
1727 *repo_dir = calloc(1, sizeof(**repo_dir));
1728 if (*repo_dir == NULL)
1729 return got_error_from_errno("calloc");
1731 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1732 error = got_error_from_errno("asprintf");
1733 free(*repo_dir);
1734 *repo_dir = NULL;
1735 return error;
1737 (*repo_dir)->owner = NULL;
1738 (*repo_dir)->description = NULL;
1739 (*repo_dir)->url = NULL;
1740 (*repo_dir)->age = NULL;
1741 (*repo_dir)->path = NULL;
1743 return NULL;
1746 static const struct got_error *
1747 gotweb_get_repo_description(char **description, struct server *srv,
1748 const char *dirpath, int dir)
1750 const struct got_error *error = NULL;
1751 struct stat sb;
1752 int fd = -1;
1753 off_t len;
1755 *description = NULL;
1756 if (srv->show_repo_description == 0)
1757 return NULL;
1759 fd = openat(dir, "description", O_RDONLY);
1760 if (fd == -1) {
1761 if (errno != ENOENT && errno != EACCES) {
1762 error = got_error_from_errno_fmt("openat %s/%s",
1763 dirpath, "description");
1765 goto done;
1768 if (fstat(fd, &sb) == -1) {
1769 error = got_error_from_errno_fmt("fstat %s/%s",
1770 dirpath, "description");
1771 goto done;
1774 len = sb.st_size;
1775 if (len > GOTWEBD_MAXDESCRSZ - 1)
1776 len = GOTWEBD_MAXDESCRSZ - 1;
1778 *description = calloc(len + 1, sizeof(**description));
1779 if (*description == NULL) {
1780 error = got_error_from_errno("calloc");
1781 goto done;
1784 if (read(fd, *description, len) == -1)
1785 error = got_error_from_errno("read");
1786 done:
1787 if (fd != -1 && close(fd) == -1 && error == NULL)
1788 error = got_error_from_errno("close");
1789 return error;
1792 static const struct got_error *
1793 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1794 int dir)
1796 const struct got_error *error = NULL;
1797 struct stat sb;
1798 int fd = -1;
1799 off_t len;
1801 *url = NULL;
1802 if (srv->show_repo_cloneurl == 0)
1803 return NULL;
1805 fd = openat(dir, "cloneurl", O_RDONLY);
1806 if (fd == -1) {
1807 if (errno != ENOENT && errno != EACCES) {
1808 error = got_error_from_errno_fmt("openat %s/%s",
1809 dirpath, "cloneurl");
1811 goto done;
1814 if (fstat(fd, &sb) == -1) {
1815 error = got_error_from_errno_fmt("fstat %s/%s",
1816 dirpath, "cloneurl");
1817 goto done;
1820 len = sb.st_size;
1821 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1822 len = GOTWEBD_MAXCLONEURLSZ - 1;
1824 *url = calloc(len + 1, sizeof(**url));
1825 if (*url == NULL) {
1826 error = got_error_from_errno("calloc");
1827 goto done;
1830 if (read(fd, *url, len) == -1)
1831 error = got_error_from_errno("read");
1832 done:
1833 if (fd != -1 && close(fd) == -1 && error == NULL)
1834 error = got_error_from_errno("close");
1835 return error;
1838 const struct got_error *
1839 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1841 struct tm tm;
1842 long long diff_time;
1843 const char *years = "years ago", *months = "months ago";
1844 const char *weeks = "weeks ago", *days = "days ago";
1845 const char *hours = "hours ago", *minutes = "minutes ago";
1846 const char *seconds = "seconds ago", *now = "right now";
1847 char *s;
1848 char datebuf[64];
1849 size_t r;
1851 *repo_age = NULL;
1853 switch (ref_tm) {
1854 case TM_DIFF:
1855 diff_time = time(NULL) - committer_time;
1856 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1857 if (asprintf(repo_age, "%lld %s",
1858 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1859 return got_error_from_errno("asprintf");
1860 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1861 if (asprintf(repo_age, "%lld %s",
1862 (diff_time / 60 / 60 / 24 / (365 / 12)),
1863 months) == -1)
1864 return got_error_from_errno("asprintf");
1865 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1866 if (asprintf(repo_age, "%lld %s",
1867 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1868 return got_error_from_errno("asprintf");
1869 } else if (diff_time > 60 * 60 * 24 * 2) {
1870 if (asprintf(repo_age, "%lld %s",
1871 (diff_time / 60 / 60 / 24), days) == -1)
1872 return got_error_from_errno("asprintf");
1873 } else if (diff_time > 60 * 60 * 2) {
1874 if (asprintf(repo_age, "%lld %s",
1875 (diff_time / 60 / 60), hours) == -1)
1876 return got_error_from_errno("asprintf");
1877 } else if (diff_time > 60 * 2) {
1878 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1879 minutes) == -1)
1880 return got_error_from_errno("asprintf");
1881 } else if (diff_time > 2) {
1882 if (asprintf(repo_age, "%lld %s", diff_time,
1883 seconds) == -1)
1884 return got_error_from_errno("asprintf");
1885 } else {
1886 if (asprintf(repo_age, "%s", now) == -1)
1887 return got_error_from_errno("asprintf");
1889 break;
1890 case TM_LONG:
1891 if (gmtime_r(&committer_time, &tm) == NULL)
1892 return got_error_from_errno("gmtime_r");
1894 s = asctime_r(&tm, datebuf);
1895 if (s == NULL)
1896 return got_error_from_errno("asctime_r");
1898 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1899 return got_error_from_errno("asprintf");
1900 break;
1901 case TM_RFC822:
1902 if (gmtime_r(&committer_time, &tm) == NULL)
1903 return got_error_from_errno("gmtime_r");
1905 r = strftime(datebuf, sizeof(datebuf),
1906 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1907 if (r == 0)
1908 return got_error(GOT_ERR_NO_SPACE);
1910 *repo_age = strdup(datebuf);
1911 if (*repo_age == NULL)
1912 return got_error_from_errno("asprintf");
1913 break;
1915 return NULL;