2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "got_compat.h"
23 #include <netinet/in.h>
24 #include <sys/queue.h>
26 #include <sys/types.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
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_load_file(char **, const char *,
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
*,
97 static void gotweb_free_querystring(struct querystring
*);
98 static void gotweb_free_repo_dir(struct repo_dir
*);
100 struct server
*gotweb_get_server(const char *);
103 gotweb_reply(struct request
*c
, int status
, const char *ctype
,
104 struct gotweb_url
*location
)
108 if (status
!= 200 && tp_writef(c
->tp
, "Status: %d\r\n", status
) == -1)
112 if (tp_writes(c
->tp
, "Location: ") == -1 ||
113 gotweb_render_url(c
, location
) == -1 ||
114 tp_writes(c
->tp
, "\r\n") == -1)
118 csp
= "Content-Security-Policy: default-src 'self'; "
119 "script-src 'none'; object-src 'none';\r\n";
120 if (tp_writes(c
->tp
, csp
) == -1)
123 if (ctype
&& tp_writef(c
->tp
, "Content-Type: %s\r\n", ctype
) == -1)
126 return tp_writes(c
->tp
, "\r\n");
130 gotweb_reply_file(struct request
*c
, const char *ctype
, const char *file
,
135 r
= tp_writef(c
->tp
, "Content-Disposition: attachment; "
136 "filename=%s%s\r\n", file
, suffix
? suffix
: "");
139 return gotweb_reply(c
, 200, ctype
, NULL
);
143 gotweb_process_request(struct request
*c
)
145 const struct got_error
*error
= NULL
;
146 struct server
*srv
= NULL
;
147 struct querystring
*qs
= NULL
;
148 struct repo_dir
*repo_dir
= NULL
;
149 struct repo_commit
*commit
;
150 const char *rss_ctype
= "application/rss+xml;charset=utf-8";
155 /* init the transport */
156 error
= gotweb_init_transport(&c
->t
);
158 log_warnx("%s: %s", __func__
, error
->msg
);
161 /* 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
;
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 commit
= TAILQ_FIRST(&c
->t
->repo_commits
);
365 if (commit
&& qs
->commit
== NULL
) {
366 qs
->commit
= strdup(commit
->commit_id
);
367 if (qs
->commit
== NULL
) {
368 error
= got_error_from_errno("strdup");
369 log_warn("%s: strdup", __func__
);
373 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
375 gotweb_render_page(c
->tp
, gotweb_render_summary
);
378 error
= got_get_repo_tags(c
, 1);
380 log_warnx("%s: %s", __func__
, error
->msg
);
383 if (TAILQ_EMPTY(&c
->t
->repo_tags
)) {
384 error
= got_error_msg(GOT_ERR_BAD_OBJ_ID
,
388 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
390 gotweb_render_page(c
->tp
, gotweb_render_tag
);
393 error
= got_get_repo_tags(c
, srv
->max_commits_display
);
395 log_warnx("%s: %s", __func__
, error
->msg
);
398 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
400 gotweb_render_page(c
->tp
, gotweb_render_tags
);
403 error
= got_get_repo_commits(c
, 1);
405 log_warnx("%s: %s", __func__
, error
->msg
);
408 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
410 gotweb_render_page(c
->tp
, gotweb_render_tree
);
414 error
= got_error(GOT_ERR_BAD_QUERYSTRING
);
419 if (gotweb_reply(c
, 400, "text/html", NULL
) == -1)
421 gotweb_render_page(c
->tp
, gotweb_render_error
);
425 gotweb_get_server(const char *server_name
)
429 /* check against the server name first */
430 if (*server_name
!= '\0')
431 TAILQ_FOREACH(srv
, &gotwebd_env
->servers
, entry
)
432 if (strcmp(srv
->name
, server_name
) == 0)
435 /* otherwise, use the first server */
436 return TAILQ_FIRST(&gotwebd_env
->servers
);
439 const struct got_error
*
440 gotweb_init_transport(struct transport
**t
)
442 const struct got_error
*error
= NULL
;
444 *t
= calloc(1, sizeof(**t
));
446 return got_error_from_errno2(__func__
, "calloc");
448 TAILQ_INIT(&(*t
)->repo_commits
);
449 TAILQ_INIT(&(*t
)->repo_tags
);
450 TAILQ_INIT(&(*t
)->refs
);
457 static const struct got_error
*
458 gotweb_init_querystring(struct querystring
**qs
)
460 const struct got_error
*error
= NULL
;
462 *qs
= calloc(1, sizeof(**qs
));
464 return got_error_from_errno2(__func__
, "calloc");
466 (*qs
)->headref
= strdup("HEAD");
467 if ((*qs
)->headref
== NULL
) {
470 return got_error_from_errno2(__func__
, "strdup");
473 (*qs
)->action
= INDEX
;
478 static const struct got_error
*
479 gotweb_parse_querystring(struct querystring
*qs
, char *qst
)
481 const struct got_error
*error
= NULL
;
482 char *tok1
= NULL
, *tok1_pair
= NULL
, *tok1_end
= NULL
;
483 char *tok2
= NULL
, *tok2_pair
= NULL
, *tok2_end
= NULL
;
490 return got_error_from_errno2(__func__
, "strdup");
495 while (tok1_pair
!= NULL
) {
496 strsep(&tok1_end
, "&");
498 tok2
= strdup(tok1_pair
);
501 return got_error_from_errno2(__func__
, "strdup");
507 while (tok2_pair
!= NULL
) {
508 strsep(&tok2_end
, "=");
510 error
= gotweb_assign_querystring(qs
, tok2_pair
,
515 tok2_pair
= tok2_end
;
518 tok1_pair
= tok1_end
;
529 * Adapted from usr.sbin/httpd/httpd.c url_decode.
531 static const struct got_error
*
532 gotweb_urldecode(char *url
)
544 /* Encoding character is followed by two hex chars */
545 if (!isxdigit((unsigned char)p
[1]) ||
546 !isxdigit((unsigned char)p
[2]) ||
547 (p
[1] == '0' && p
[2] == '0'))
548 return got_error(GOT_ERR_BAD_QUERYSTRING
);
554 * We don't have to validate "hex" because it is
555 * guaranteed to include two hex chars followed by nul.
557 x
= strtoul(hex
, NULL
, 16);
573 static const struct got_error
*
574 gotweb_assign_querystring(struct querystring
*qs
, char *key
, char *value
)
576 const struct got_error
*error
= NULL
;
580 error
= gotweb_urldecode(value
);
584 for (el_cnt
= 0; el_cnt
< nitems(querystring_keys
); el_cnt
++) {
585 if (strcmp(key
, querystring_keys
[el_cnt
].name
) != 0)
588 switch (querystring_keys
[el_cnt
].element
) {
590 for (a_cnt
= 0; a_cnt
< nitems(action_keys
); a_cnt
++) {
591 if (strcmp(value
, action_keys
[a_cnt
].name
) != 0)
593 qs
->action
= action_keys
[a_cnt
].action
;
600 qs
->commit
= strdup(value
);
601 if (qs
->commit
== NULL
) {
602 error
= got_error_from_errno2(__func__
,
608 qs
->file
= strdup(value
);
609 if (qs
->file
== NULL
) {
610 error
= got_error_from_errno2(__func__
,
616 qs
->folder
= strdup(value
);
617 if (qs
->folder
== NULL
) {
618 error
= got_error_from_errno2(__func__
,
625 qs
->headref
= strdup(value
);
626 if (qs
->headref
== NULL
) {
627 error
= got_error_from_errno2(__func__
,
635 qs
->index_page
= strtonum(value
, INT64_MIN
,
638 error
= got_error_from_errno3(__func__
,
642 if (qs
->index_page
< 0)
646 qs
->path
= strdup(value
);
647 if (qs
->path
== NULL
) {
648 error
= got_error_from_errno2(__func__
,
663 gotweb_free_repo_tag(struct repo_tag
*rt
)
668 free(rt
->tag_commit
);
669 free(rt
->commit_msg
);
676 gotweb_free_repo_commit(struct repo_commit
*rc
)
686 free(rc
->commit_msg
);
692 gotweb_free_querystring(struct querystring
*qs
)
705 gotweb_free_repo_dir(struct repo_dir
*repo_dir
)
707 if (repo_dir
!= NULL
) {
708 free(repo_dir
->name
);
709 free(repo_dir
->owner
);
710 free(repo_dir
->description
);
712 free(repo_dir
->path
);
718 gotweb_free_transport(struct transport
*t
)
720 const struct got_error
*err
;
721 struct repo_commit
*rc
= NULL
, *trc
= NULL
;
722 struct repo_tag
*rt
= NULL
, *trt
= NULL
;
725 got_ref_list_free(&t
->refs
);
726 TAILQ_FOREACH_SAFE(rc
, &t
->repo_commits
, entry
, trc
) {
727 TAILQ_REMOVE(&t
->repo_commits
, rc
, entry
);
728 gotweb_free_repo_commit(rc
);
730 TAILQ_FOREACH_SAFE(rt
, &t
->repo_tags
, entry
, trt
) {
731 TAILQ_REMOVE(&t
->repo_tags
, rt
, entry
);
732 gotweb_free_repo_tag(rt
);
734 gotweb_free_repo_dir(t
->repo_dir
);
735 gotweb_free_querystring(t
->qs
);
737 free(t
->tags_more_id
);
739 got_object_blob_close(t
->blob
);
741 err
= got_gotweb_closefile(t
->fp
);
743 log_warnx("%s: got_gotweb_closefile failure: %s",
746 if (t
->fd
!= -1 && close(t
->fd
) == -1)
747 log_warn("%s: close", __func__
);
749 for (i
= 0; i
< t
->nrepos
; ++i
)
754 got_repo_close(t
->repo
);
759 gotweb_index_navs(struct request
*c
, struct gotweb_url
*prev
, int *have_prev
,
760 struct gotweb_url
*next
, int *have_next
)
762 struct transport
*t
= c
->t
;
763 struct querystring
*qs
= t
->qs
;
764 struct server
*srv
= c
->srv
;
766 *have_prev
= *have_next
= 0;
768 if (qs
->index_page
> 0) {
770 *prev
= (struct gotweb_url
){
772 .index_page
= qs
->index_page
- 1,
775 if (t
->next_disp
== srv
->max_repos_display
&&
776 t
->repos_total
!= (qs
->index_page
+ 1) *
777 srv
->max_repos_display
) {
779 *next
= (struct gotweb_url
){
781 .index_page
= qs
->index_page
+ 1,
787 gotweb_render_index(struct template *tp
)
789 const struct got_error
*error
= NULL
;
790 struct request
*c
= tp
->tp_arg
;
791 struct server
*srv
= c
->srv
;
792 struct transport
*t
= c
->t
;
793 struct querystring
*qs
= t
->qs
;
794 struct repo_dir
*repo_dir
= NULL
;
795 struct dirent
**sd_dent
= t
->repos
;
796 unsigned int d_i
, d_disp
= 0;
797 unsigned int d_skipped
= 0;
800 if (gotweb_render_repo_table_hdr(c
->tp
) == -1)
803 for (d_i
= 0; d_i
< t
->nrepos
; d_i
++) {
804 if (strcmp(sd_dent
[d_i
]->d_name
, ".") == 0 ||
805 strcmp(sd_dent
[d_i
]->d_name
, "..") == 0) {
810 error
= got_path_dirent_type(&type
, srv
->repos_path
,
814 if (type
!= DT_DIR
) {
819 if (qs
->index_page
> 0 && (qs
->index_page
*
820 srv
->max_repos_display
) > t
->prev_disp
) {
825 error
= gotweb_load_got_path(&repo_dir
, sd_dent
[d_i
]->d_name
,
828 if (error
->code
!= GOT_ERR_NOT_GIT_REPO
)
829 log_warnx("%s: %s: %s", __func__
,
830 sd_dent
[d_i
]->d_name
, error
->msg
);
831 gotweb_free_repo_dir(repo_dir
);
840 r
= gotweb_render_repo_fragment(c
->tp
, repo_dir
);
841 gotweb_free_repo_dir(repo_dir
);
843 got_repo_close(t
->repo
);
849 if (d_disp
== srv
->max_repos_display
)
852 t
->repos_total
= t
->nrepos
- d_skipped
;
854 if (srv
->max_repos_display
== 0 ||
855 t
->repos_total
<= srv
->max_repos_display
)
858 if (gotweb_render_navs(c
->tp
) == -1)
865 should_urlencode(int c
)
867 if (c
<= ' ' || c
>= 127)
891 /* needed because the URLs are embedded into the HTML */
900 gotweb_urlencode(const char *str
)
908 for (s
= str
; *s
; ++s
) {
910 if (should_urlencode(*s
))
914 escaped
= calloc(1, len
+ 1);
919 for (s
= str
; *s
; ++s
) {
920 if (should_urlencode(*s
)) {
921 a
= (*s
& 0xF0) >> 4;
925 escaped
[i
++] = a
<= 9 ? ('0' + a
) : ('7' + a
);
926 escaped
[i
++] = b
<= 9 ? ('0' + b
) : ('7' + b
);
935 gotweb_action_name(int action
)
972 gotweb_render_url(struct request
*c
, struct gotweb_url
*url
)
974 const char *sep
= "?", *action
;
978 action
= gotweb_action_name(url
->action
);
979 if (action
!= NULL
) {
980 if (tp_writef(c
->tp
, "?action=%s", action
) == -1)
986 if (tp_writef(c
->tp
, "%scommit=%s", sep
, url
->commit
) == -1)
992 tmp
= gotweb_urlencode(url
->file
);
995 r
= tp_writef(c
->tp
, "%sfile=%s", sep
, tmp
);
1003 tmp
= gotweb_urlencode(url
->folder
);
1006 r
= tp_writef(c
->tp
, "%sfolder=%s", sep
, tmp
);
1014 tmp
= gotweb_urlencode(url
->headref
);
1017 r
= tp_writef(c
->tp
, "%sheadref=%s", sep
, url
->headref
);
1024 if (url
->index_page
!= -1) {
1025 if (tp_writef(c
->tp
, "%sindex_page=%d", sep
,
1026 url
->index_page
) == -1)
1032 tmp
= gotweb_urlencode(url
->path
);
1035 r
= tp_writef(c
->tp
, "%spath=%s", sep
, tmp
);
1046 gotweb_render_absolute_url(struct request
*c
, struct gotweb_url
*url
)
1048 struct template *tp
= c
->tp
;
1049 const char *proto
= c
->https
? "https" : "http";
1051 if (tp_writes(tp
, proto
) == -1 ||
1052 tp_writes(tp
, "://") == -1 ||
1053 tp_htmlescape(tp
, c
->server_name
) == -1 ||
1054 tp_htmlescape(tp
, c
->document_uri
) == -1)
1057 return gotweb_render_url(c
, url
);
1060 static const struct got_error
*
1061 gotweb_load_got_path(struct repo_dir
**rp
, const char *dir
,
1064 const struct got_error
*error
= NULL
;
1065 struct server
*srv
= c
->srv
;
1066 struct transport
*t
= c
->t
;
1067 struct repo_dir
*repo_dir
;
1071 *rp
= calloc(1, sizeof(**rp
));
1073 return got_error_from_errno("calloc");
1076 if (asprintf(&dir_test
, "%s/%s/%s", srv
->repos_path
, dir
,
1077 GOTWEB_GIT_DIR
) == -1)
1078 return got_error_from_errno("asprintf");
1080 dt
= opendir(dir_test
);
1083 if (asprintf(&dir_test
, "%s/%s", srv
->repos_path
, dir
) == -1)
1084 return got_error_from_errno("asprintf");
1085 dt
= opendir(dir_test
);
1088 if (asprintf(&dir_test
, "%s/%s%s", srv
->repos_path
,
1089 dir
, GOTWEB_GIT_DIR
) == -1)
1090 return got_error_from_errno("asprintf");
1091 dt
= opendir(dir_test
);
1094 return got_error_path(dir
,
1095 GOT_ERR_NOT_GIT_REPO
);
1100 repo_dir
->path
= dir_test
;
1103 repo_dir
->name
= strdup(repo_dir
->path
+ strlen(srv
->repos_path
) + 1);
1104 if (repo_dir
->name
== NULL
) {
1105 error
= got_error_from_errno("strdup");
1109 if (srv
->respect_exportok
&&
1110 faccessat(dirfd(dt
), "git-daemon-export-ok", F_OK
, 0) == -1) {
1111 error
= got_error_path(repo_dir
->name
, GOT_ERR_NOT_GIT_REPO
);
1115 error
= got_repo_open(&t
->repo
, repo_dir
->path
, NULL
,
1116 gotwebd_env
->pack_fds
);
1119 error
= gotweb_get_repo_description(&repo_dir
->description
, srv
,
1120 repo_dir
->path
, dirfd(dt
));
1123 if (srv
->show_repo_owner
) {
1124 error
= gotweb_load_file(&repo_dir
->owner
, repo_dir
->path
,
1125 "owner", dirfd(dt
));
1128 if (repo_dir
->owner
== NULL
) {
1129 error
= got_get_repo_owner(&repo_dir
->owner
, c
);
1134 if (srv
->show_repo_age
) {
1135 error
= got_get_repo_age(&repo_dir
->age
, c
, NULL
);
1139 error
= gotweb_get_clone_url(&repo_dir
->url
, srv
, repo_dir
->path
,
1143 if (dt
!= NULL
&& closedir(dt
) == EOF
&& error
== NULL
)
1144 error
= got_error_from_errno("closedir");
1145 if (error
&& t
->repo
) {
1146 got_repo_close(t
->repo
);
1152 static const struct got_error
*
1153 gotweb_load_file(char **str
, const char *dir
, const char *file
, int dirfd
)
1155 const struct got_error
*error
= NULL
;
1162 fd
= openat(dirfd
, file
, O_RDONLY
);
1164 if (errno
== ENOENT
|| errno
== EACCES
)
1166 return got_error_from_errno_fmt("openat %s/%s", dir
, file
);
1169 if (fstat(fd
, &sb
) == -1) {
1170 error
= got_error_from_errno_fmt("fstat %s/%s", dir
, file
);
1175 if (len
> GOTWEBD_MAXDESCRSZ
- 1)
1176 len
= GOTWEBD_MAXDESCRSZ
- 1;
1178 *str
= calloc(len
+ 1, 1);
1180 error
= got_error_from_errno("calloc");
1184 if (read(fd
, *str
, len
) == -1)
1185 error
= got_error_from_errno("read");
1187 if (fd
!= -1 && close(fd
) == -1 && error
== NULL
)
1188 error
= got_error_from_errno("close");
1192 static const struct got_error
*
1193 gotweb_get_repo_description(char **description
, struct server
*srv
,
1194 const char *dirpath
, int dir
)
1196 *description
= NULL
;
1197 if (srv
->show_repo_description
== 0)
1200 return gotweb_load_file(description
, dirpath
, "description", dir
);
1203 static const struct got_error
*
1204 gotweb_get_clone_url(char **url
, struct server
*srv
, const char *dirpath
,
1208 if (srv
->show_repo_cloneurl
== 0)
1211 return gotweb_load_file(url
, dirpath
, "cloneurl", dir
);
1215 gotweb_render_age(struct template *tp
, time_t committer_time
)
1217 struct request
*c
= tp
->tp_arg
;
1218 long long diff_time
;
1219 const char *years
= "years ago", *months
= "months ago";
1220 const char *weeks
= "weeks ago", *days
= "days ago";
1221 const char *hours
= "hours ago", *minutes
= "minutes ago";
1222 const char *seconds
= "seconds ago", *now
= "right now";
1224 diff_time
= time(NULL
) - committer_time
;
1225 if (diff_time
> 60 * 60 * 24 * 365 * 2) {
1226 if (tp_writef(c
->tp
, "%lld %s",
1227 (diff_time
/ 60 / 60 / 24 / 365), years
) == -1)
1229 } else if (diff_time
> 60 * 60 * 24 * (365 / 12) * 2) {
1230 if (tp_writef(c
->tp
, "%lld %s",
1231 (diff_time
/ 60 / 60 / 24 / (365 / 12)),
1234 } else if (diff_time
> 60 * 60 * 24 * 7 * 2) {
1235 if (tp_writef(c
->tp
, "%lld %s",
1236 (diff_time
/ 60 / 60 / 24 / 7), weeks
) == -1)
1238 } else if (diff_time
> 60 * 60 * 24 * 2) {
1239 if (tp_writef(c
->tp
, "%lld %s",
1240 (diff_time
/ 60 / 60 / 24), days
) == -1)
1242 } else if (diff_time
> 60 * 60 * 2) {
1243 if (tp_writef(c
->tp
, "%lld %s",
1244 (diff_time
/ 60 / 60), hours
) == -1)
1246 } else if (diff_time
> 60 * 2) {
1247 if (tp_writef(c
->tp
, "%lld %s", (diff_time
/ 60),
1250 } else if (diff_time
> 2) {
1251 if (tp_writef(c
->tp
, "%lld %s", diff_time
,
1255 if (tp_writes(tp
, now
) == -1)