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"
54 static const struct querystring_keys querystring_keys
[] = {
59 { "headref", HEADREF
},
60 { "index_page", INDEX_PAGE
},
64 static const struct action_keys action_keys
[] = {
67 { "blobraw", BLOBRAW
},
69 { "commits", COMMITS
},
74 { "summary", SUMMARY
},
81 static const struct got_error
*gotweb_init_querystring(struct querystring
**);
82 static const struct got_error
*gotweb_parse_querystring(struct querystring
**,
84 static const struct got_error
*gotweb_assign_querystring(struct querystring
**,
86 static int gotweb_render_index(struct template *);
87 static const struct got_error
*gotweb_load_got_path(struct repo_dir
**,
88 const char *, struct request
*);
89 static const struct got_error
*gotweb_get_repo_description(char **,
90 struct server
*, const char *, int);
91 static const struct got_error
*gotweb_get_clone_url(char **, struct server
*,
94 static void gotweb_free_querystring(struct querystring
*);
95 static void gotweb_free_repo_dir(struct repo_dir
*);
97 struct server
*gotweb_get_server(const char *);
100 gotweb_reply(struct request
*c
, int status
, const char *ctype
,
101 struct gotweb_url
*location
)
105 if (status
!= 200 && tp_writef(c
->tp
, "Status: %d\r\n", status
) == -1)
109 if (tp_writes(c
->tp
, "Location: ") == -1 ||
110 gotweb_render_url(c
, location
) == -1 ||
111 tp_writes(c
->tp
, "\r\n") == -1)
115 csp
= "Content-Security-Policy: default-src 'self'; "
116 "script-src 'none'; object-src 'none';\r\n";
117 if (tp_writes(c
->tp
, csp
) == -1)
120 if (ctype
&& tp_writef(c
->tp
, "Content-Type: %s\r\n", ctype
) == -1)
123 return tp_writes(c
->tp
, "\r\n");
127 gotweb_reply_file(struct request
*c
, const char *ctype
, const char *file
,
132 r
= tp_writef(c
->tp
, "Content-Disposition: attachment; "
133 "filename=%s%s\r\n", file
, suffix
? suffix
: "");
136 return gotweb_reply(c
, 200, ctype
, NULL
);
140 gotweb_process_request(struct request
*c
)
142 const struct got_error
*error
= NULL
;
143 struct server
*srv
= NULL
;
144 struct querystring
*qs
= NULL
;
145 struct repo_dir
*repo_dir
= NULL
;
146 const char *rss_ctype
= "application/rss+xml;charset=utf-8";
151 /* init the transport */
152 error
= gotweb_init_transport(&c
->t
);
154 log_warnx("%s: %s", __func__
, error
->msg
);
157 /* don't process any further if client disconnected */
158 if (c
->sock
->client_status
== CLIENT_DISCONNECT
)
160 /* get the gotwebd server */
161 srv
= gotweb_get_server(c
->server_name
);
163 log_warnx("%s: error server is NULL", __func__
);
167 /* parse our querystring */
168 error
= gotweb_init_querystring(&qs
);
170 log_warnx("%s: %s", __func__
, error
->msg
);
174 error
= gotweb_parse_querystring(&qs
, c
->querystring
);
176 log_warnx("%s: %s", __func__
, error
->msg
);
181 * certain actions require a commit id in the querystring. this stops
182 * bad actors from exploiting this by manually manipulating the
186 if (qs
->action
== BLAME
|| qs
->action
== BLOB
||
187 qs
->action
== BLOBRAW
|| qs
->action
== DIFF
||
188 qs
->action
== PATCH
) {
189 if (qs
->commit
== NULL
) {
190 error
= got_error(GOT_ERR_BAD_QUERYSTRING
);
195 if (qs
->action
!= INDEX
) {
196 error
= gotweb_load_got_path(&repo_dir
, qs
->path
, c
);
197 c
->t
->repo_dir
= repo_dir
;
198 if (error
&& error
->code
!= GOT_ERR_LONELY_PACKIDX
)
202 if (qs
->action
== BLOBRAW
|| qs
->action
== BLOB
) {
203 if (qs
->folder
== NULL
|| qs
->file
== NULL
) {
204 error
= got_error(GOT_ERR_BAD_QUERYSTRING
);
208 error
= got_get_repo_commits(c
, 1);
212 error
= got_open_blob_for_output(&c
->t
->blob
, &c
->t
->fd
,
213 &binary
, c
, qs
->folder
, qs
->file
, qs
->commit
);
218 switch (qs
->action
) {
220 if (qs
->folder
== NULL
|| qs
->file
== NULL
) {
221 error
= got_error(GOT_ERR_BAD_QUERYSTRING
);
224 error
= got_get_repo_commits(c
, 1);
226 log_warnx("%s: %s", __func__
, error
->msg
);
229 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
231 gotweb_render_page(c
->tp
, gotweb_render_blame
);
235 struct gotweb_url url
= {
239 .commit
= qs
->commit
,
240 .folder
= qs
->folder
,
244 gotweb_reply(c
, 302, NULL
, &url
);
248 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
250 gotweb_render_page(c
->tp
, gotweb_render_blob
);
254 r
= gotweb_reply_file(c
, "application/octet-stream",
257 r
= gotweb_reply(c
, 200, "text/plain", NULL
);
260 if (template_flush(c
->tp
) == -1)
264 error
= got_object_blob_read_block(&len
, c
->t
->blob
);
269 buf
= got_object_blob_get_read_buf(c
->t
->blob
);
270 if (fcgi_write(c
, buf
, len
) == -1)
275 error
= got_get_repo_commits(c
, srv
->max_commits_display
);
278 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
280 gotweb_render_page(c
->tp
, gotweb_render_briefs
);
283 error
= got_get_repo_commits(c
, srv
->max_commits_display
);
285 log_warnx("%s: %s", __func__
, error
->msg
);
288 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
290 gotweb_render_page(c
->tp
, gotweb_render_commits
);
293 error
= got_get_repo_commits(c
, 1);
295 log_warnx("%s: %s", __func__
, error
->msg
);
298 error
= got_open_diff_for_output(&c
->t
->fp
, c
);
300 log_warnx("%s: %s", __func__
, error
->msg
);
303 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
305 gotweb_render_page(c
->tp
, gotweb_render_diff
);
308 c
->t
->nrepos
= scandir(srv
->repos_path
, &c
->t
->repos
, NULL
,
310 if (c
->t
->nrepos
== -1) {
312 error
= got_error_from_errno2("scandir",
316 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
318 gotweb_render_page(c
->tp
, gotweb_render_index
);
321 error
= got_get_repo_commits(c
, 1);
323 log_warnx("%s: %s", __func__
, error
->msg
);
326 error
= got_open_diff_for_output(&c
->t
->fp
, c
);
328 log_warnx("%s: %s", __func__
, error
->msg
);
331 if (gotweb_reply(c
, 200, "text/plain", NULL
) == -1)
333 gotweb_render_patch(c
->tp
);
336 error
= got_get_repo_tags(c
, D_MAXSLCOMMDISP
);
339 if (gotweb_reply_file(c
, rss_ctype
, repo_dir
->name
, ".rss")
342 gotweb_render_rss(c
->tp
);
345 error
= got_ref_list(&c
->t
->refs
, c
->t
->repo
, "refs/heads",
346 got_ref_cmp_by_name
, NULL
);
348 log_warnx("%s: got_ref_list: %s", __func__
,
352 error
= got_get_repo_commits(c
, srv
->summary_commits_display
);
356 error
= got_get_repo_tags(c
, srv
->summary_tags_display
);
358 log_warnx("%s: got_get_repo_tags: %s", __func__
,
362 qs
->action
= SUMMARY
;
363 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
365 gotweb_render_page(c
->tp
, gotweb_render_summary
);
368 error
= got_get_repo_tags(c
, 1);
370 log_warnx("%s: %s", __func__
, error
->msg
);
373 if (c
->t
->tag_count
== 0) {
374 error
= got_error_msg(GOT_ERR_BAD_OBJ_ID
,
378 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
380 gotweb_render_page(c
->tp
, gotweb_render_tag
);
383 error
= got_get_repo_tags(c
, srv
->max_commits_display
);
385 log_warnx("%s: %s", __func__
, error
->msg
);
388 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
390 gotweb_render_page(c
->tp
, gotweb_render_tags
);
393 error
= got_get_repo_commits(c
, 1);
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_tree
);
404 error
= got_error(GOT_ERR_BAD_QUERYSTRING
);
409 if (gotweb_reply(c
, 400, "text/html", NULL
) == -1)
411 gotweb_render_page(c
->tp
, gotweb_render_error
);
415 gotweb_get_server(const char *server_name
)
419 /* check against the server name first */
420 if (*server_name
!= '\0')
421 TAILQ_FOREACH(srv
, &gotwebd_env
->servers
, entry
)
422 if (strcmp(srv
->name
, server_name
) == 0)
425 /* otherwise, use the first server */
426 return TAILQ_FIRST(&gotwebd_env
->servers
);
429 const struct got_error
*
430 gotweb_init_transport(struct transport
**t
)
432 const struct got_error
*error
= NULL
;
434 *t
= calloc(1, sizeof(**t
));
436 return got_error_from_errno2(__func__
, "calloc");
438 TAILQ_INIT(&(*t
)->repo_commits
);
439 TAILQ_INIT(&(*t
)->repo_tags
);
440 TAILQ_INIT(&(*t
)->refs
);
447 static const struct got_error
*
448 gotweb_init_querystring(struct querystring
**qs
)
450 const struct got_error
*error
= NULL
;
452 *qs
= calloc(1, sizeof(**qs
));
454 return got_error_from_errno2(__func__
, "calloc");
456 (*qs
)->headref
= strdup("HEAD");
457 if ((*qs
)->headref
== NULL
) {
460 return got_error_from_errno2(__func__
, "strdup");
463 (*qs
)->action
= INDEX
;
468 static const struct got_error
*
469 gotweb_parse_querystring(struct querystring
**qs
, char *qst
)
471 const struct got_error
*error
= NULL
;
472 char *tok1
= NULL
, *tok1_pair
= NULL
, *tok1_end
= NULL
;
473 char *tok2
= NULL
, *tok2_pair
= NULL
, *tok2_end
= NULL
;
480 return got_error_from_errno2(__func__
, "strdup");
485 while (tok1_pair
!= NULL
) {
486 strsep(&tok1_end
, "&");
488 tok2
= strdup(tok1_pair
);
491 return got_error_from_errno2(__func__
, "strdup");
497 while (tok2_pair
!= NULL
) {
498 strsep(&tok2_end
, "=");
500 error
= gotweb_assign_querystring(qs
, tok2_pair
,
505 tok2_pair
= tok2_end
;
508 tok1_pair
= tok1_end
;
519 * Adapted from usr.sbin/httpd/httpd.c url_decode.
521 static const struct got_error
*
522 gotweb_urldecode(char *url
)
534 /* Encoding character is followed by two hex chars */
535 if (!isxdigit((unsigned char)p
[1]) ||
536 !isxdigit((unsigned char)p
[2]) ||
537 (p
[1] == '0' && p
[2] == '0'))
538 return got_error(GOT_ERR_BAD_QUERYSTRING
);
544 * We don't have to validate "hex" because it is
545 * guaranteed to include two hex chars followed by nul.
547 x
= strtoul(hex
, NULL
, 16);
563 static const struct got_error
*
564 gotweb_assign_querystring(struct querystring
**qs
, char *key
, char *value
)
566 const struct got_error
*error
= NULL
;
570 error
= gotweb_urldecode(value
);
574 for (el_cnt
= 0; el_cnt
< nitems(querystring_keys
); el_cnt
++) {
575 if (strcmp(key
, querystring_keys
[el_cnt
].name
) != 0)
578 switch (querystring_keys
[el_cnt
].element
) {
580 for (a_cnt
= 0; a_cnt
< nitems(action_keys
); a_cnt
++) {
581 if (strcmp(value
, action_keys
[a_cnt
].name
) != 0)
583 else if (strcmp(value
,
584 action_keys
[a_cnt
].name
) == 0){
586 action_keys
[a_cnt
].action
;
594 (*qs
)->commit
= strdup(value
);
595 if ((*qs
)->commit
== NULL
) {
596 error
= got_error_from_errno2(__func__
,
602 (*qs
)->file
= strdup(value
);
603 if ((*qs
)->file
== NULL
) {
604 error
= got_error_from_errno2(__func__
,
610 (*qs
)->folder
= strdup(value
);
611 if ((*qs
)->folder
== NULL
) {
612 error
= got_error_from_errno2(__func__
,
618 free((*qs
)->headref
);
619 (*qs
)->headref
= strdup(value
);
620 if ((*qs
)->headref
== NULL
) {
621 error
= got_error_from_errno2(__func__
,
629 (*qs
)->index_page
= strtonum(value
, INT64_MIN
,
632 error
= got_error_from_errno3(__func__
,
636 if ((*qs
)->index_page
< 0)
637 (*qs
)->index_page
= 0;
640 (*qs
)->path
= strdup(value
);
641 if ((*qs
)->path
== NULL
) {
642 error
= got_error_from_errno2(__func__
,
657 gotweb_free_repo_tag(struct repo_tag
*rt
)
662 free(rt
->tag_commit
);
663 free(rt
->commit_msg
);
670 gotweb_free_repo_commit(struct repo_commit
*rc
)
680 free(rc
->commit_msg
);
686 gotweb_free_querystring(struct querystring
*qs
)
699 gotweb_free_repo_dir(struct repo_dir
*repo_dir
)
701 if (repo_dir
!= NULL
) {
702 free(repo_dir
->name
);
703 free(repo_dir
->owner
);
704 free(repo_dir
->description
);
706 free(repo_dir
->path
);
712 gotweb_free_transport(struct transport
*t
)
714 const struct got_error
*err
;
715 struct repo_commit
*rc
= NULL
, *trc
= NULL
;
716 struct repo_tag
*rt
= NULL
, *trt
= NULL
;
719 got_ref_list_free(&t
->refs
);
720 TAILQ_FOREACH_SAFE(rc
, &t
->repo_commits
, entry
, trc
) {
721 TAILQ_REMOVE(&t
->repo_commits
, rc
, entry
);
722 gotweb_free_repo_commit(rc
);
724 TAILQ_FOREACH_SAFE(rt
, &t
->repo_tags
, entry
, trt
) {
725 TAILQ_REMOVE(&t
->repo_tags
, rt
, entry
);
726 gotweb_free_repo_tag(rt
);
728 gotweb_free_repo_dir(t
->repo_dir
);
729 gotweb_free_querystring(t
->qs
);
731 free(t
->tags_more_id
);
733 got_object_blob_close(t
->blob
);
735 err
= got_gotweb_closefile(t
->fp
);
737 log_warnx("%s: got_gotweb_closefile failure: %s",
740 if (t
->fd
!= -1 && close(t
->fd
) == -1)
741 log_warn("%s: close", __func__
);
743 for (i
= 0; i
< t
->nrepos
; ++i
)
748 got_repo_close(t
->repo
);
753 gotweb_index_navs(struct request
*c
, struct gotweb_url
*prev
, int *have_prev
,
754 struct gotweb_url
*next
, int *have_next
)
756 struct transport
*t
= c
->t
;
757 struct querystring
*qs
= t
->qs
;
758 struct server
*srv
= c
->srv
;
760 *have_prev
= *have_next
= 0;
762 if (qs
->index_page
> 0) {
764 *prev
= (struct gotweb_url
){
766 .index_page
= qs
->index_page
- 1,
769 if (t
->next_disp
== srv
->max_repos_display
&&
770 t
->repos_total
!= (qs
->index_page
+ 1) *
771 srv
->max_repos_display
) {
773 *next
= (struct gotweb_url
){
775 .index_page
= qs
->index_page
+ 1,
781 gotweb_render_index(struct template *tp
)
783 const struct got_error
*error
= NULL
;
784 struct request
*c
= tp
->tp_arg
;
785 struct server
*srv
= c
->srv
;
786 struct transport
*t
= c
->t
;
787 struct querystring
*qs
= t
->qs
;
788 struct repo_dir
*repo_dir
= NULL
;
789 struct dirent
**sd_dent
= t
->repos
;
790 unsigned int d_i
, d_disp
= 0;
791 unsigned int d_skipped
= 0;
794 if (gotweb_render_repo_table_hdr(c
->tp
) == -1)
797 for (d_i
= 0; d_i
< t
->nrepos
; d_i
++) {
798 if (strcmp(sd_dent
[d_i
]->d_name
, ".") == 0 ||
799 strcmp(sd_dent
[d_i
]->d_name
, "..") == 0) {
804 error
= got_path_dirent_type(&type
, srv
->repos_path
,
808 if (type
!= DT_DIR
) {
813 if (qs
->index_page
> 0 && (qs
->index_page
*
814 srv
->max_repos_display
) > t
->prev_disp
) {
819 error
= gotweb_load_got_path(&repo_dir
, sd_dent
[d_i
]->d_name
,
821 if (error
&& error
->code
!= GOT_ERR_LONELY_PACKIDX
) {
822 if (error
->code
!= GOT_ERR_NOT_GIT_REPO
)
823 log_warnx("%s: %s: %s", __func__
,
824 sd_dent
[d_i
]->d_name
, error
->msg
);
825 gotweb_free_repo_dir(repo_dir
);
834 r
= gotweb_render_repo_fragment(c
->tp
, repo_dir
);
835 gotweb_free_repo_dir(repo_dir
);
837 got_repo_close(t
->repo
);
843 if (d_disp
== srv
->max_repos_display
)
846 t
->repos_total
= t
->nrepos
- d_skipped
;
848 if (srv
->max_repos_display
== 0 ||
849 t
->repos_total
<= srv
->max_repos_display
)
852 if (gotweb_render_navs(c
->tp
) == -1)
859 should_urlencode(int c
)
861 if (c
<= ' ' || c
>= 127)
885 /* needed because the URLs are embedded into the HTML */
894 gotweb_urlencode(const char *str
)
902 for (s
= str
; *s
; ++s
) {
904 if (should_urlencode(*s
))
908 escaped
= calloc(1, len
+ 1);
913 for (s
= str
; *s
; ++s
) {
914 if (should_urlencode(*s
)) {
915 a
= (*s
& 0xF0) >> 4;
919 escaped
[i
++] = a
<= 9 ? ('0' + a
) : ('7' + a
);
920 escaped
[i
++] = b
<= 9 ? ('0' + b
) : ('7' + b
);
929 gotweb_action_name(int action
)
966 gotweb_render_url(struct request
*c
, struct gotweb_url
*url
)
968 const char *sep
= "?", *action
;
972 action
= gotweb_action_name(url
->action
);
973 if (action
!= NULL
) {
974 if (tp_writef(c
->tp
, "?action=%s", action
) == -1)
980 if (tp_writef(c
->tp
, "%scommit=%s", sep
, url
->commit
) == -1)
986 if (tp_writef(c
->tp
, "%sprevid=%s", sep
, url
->previd
) == -1)
992 if (tp_writef(c
->tp
, "%sprevset=%s", sep
, url
->prevset
) == -1)
998 tmp
= gotweb_urlencode(url
->file
);
1001 r
= tp_writef(c
->tp
, "%sfile=%s", sep
, tmp
);
1009 tmp
= gotweb_urlencode(url
->folder
);
1012 r
= tp_writef(c
->tp
, "%sfolder=%s", sep
, tmp
);
1020 tmp
= gotweb_urlencode(url
->headref
);
1023 r
= tp_writef(c
->tp
, "%sheadref=%s", sep
, url
->headref
);
1030 if (url
->index_page
!= -1) {
1031 if (tp_writef(c
->tp
, "%sindex_page=%d", sep
,
1032 url
->index_page
) == -1)
1038 tmp
= gotweb_urlencode(url
->path
);
1041 r
= tp_writef(c
->tp
, "%spath=%s", sep
, tmp
);
1052 gotweb_render_absolute_url(struct request
*c
, struct gotweb_url
*url
)
1054 struct template *tp
= c
->tp
;
1055 const char *proto
= c
->https
? "https" : "http";
1057 if (tp_writes(tp
, proto
) == -1 ||
1058 tp_writes(tp
, "://") == -1 ||
1059 tp_htmlescape(tp
, c
->server_name
) == -1 ||
1060 tp_htmlescape(tp
, c
->document_uri
) == -1)
1063 return gotweb_render_url(c
, url
);
1066 static const struct got_error
*
1067 gotweb_load_got_path(struct repo_dir
**rp
, const char *dir
,
1070 const struct got_error
*error
= NULL
;
1071 struct socket
*sock
= c
->sock
;
1072 struct server
*srv
= c
->srv
;
1073 struct transport
*t
= c
->t
;
1074 struct repo_dir
*repo_dir
;
1078 *rp
= calloc(1, sizeof(**rp
));
1080 return got_error_from_errno("calloc");
1083 if (asprintf(&dir_test
, "%s/%s/%s", srv
->repos_path
, dir
,
1084 GOTWEB_GIT_DIR
) == -1)
1085 return got_error_from_errno("asprintf");
1087 dt
= opendir(dir_test
);
1090 if (asprintf(&dir_test
, "%s/%s", srv
->repos_path
, dir
) == -1)
1091 return got_error_from_errno("asprintf");
1092 dt
= opendir(dir_test
);
1095 if (asprintf(&dir_test
, "%s/%s%s", srv
->repos_path
,
1096 dir
, GOTWEB_GIT_DIR
) == -1)
1097 return got_error_from_errno("asprintf");
1098 dt
= opendir(dir_test
);
1101 return got_error_path(dir
,
1102 GOT_ERR_NOT_GIT_REPO
);
1107 repo_dir
->path
= dir_test
;
1110 repo_dir
->name
= strdup(repo_dir
->path
+ strlen(srv
->repos_path
) + 1);
1111 if (repo_dir
->name
== NULL
) {
1112 error
= got_error_from_errno("strdup");
1116 if (srv
->respect_exportok
&&
1117 faccessat(dirfd(dt
), "git-daemon-export-ok", F_OK
, 0) == -1) {
1118 error
= got_error_path(repo_dir
->name
, GOT_ERR_NOT_GIT_REPO
);
1122 error
= got_repo_open(&t
->repo
, repo_dir
->path
, NULL
, sock
->pack_fds
);
1125 error
= gotweb_get_repo_description(&repo_dir
->description
, srv
,
1126 repo_dir
->path
, dirfd(dt
));
1129 error
= got_get_repo_owner(&repo_dir
->owner
, c
);
1132 if (srv
->show_repo_age
) {
1133 error
= got_get_repo_age(&repo_dir
->age
, c
, NULL
);
1137 error
= gotweb_get_clone_url(&repo_dir
->url
, srv
, repo_dir
->path
,
1141 if (dt
!= NULL
&& closedir(dt
) == EOF
&& error
== NULL
)
1142 error
= got_error_from_errno("closedir");
1143 if (error
&& t
->repo
) {
1144 got_repo_close(t
->repo
);
1150 static const struct got_error
*
1151 gotweb_get_repo_description(char **description
, struct server
*srv
,
1152 const char *dirpath
, int dir
)
1154 const struct got_error
*error
= NULL
;
1159 *description
= NULL
;
1160 if (srv
->show_repo_description
== 0)
1163 fd
= openat(dir
, "description", O_RDONLY
);
1165 if (errno
!= ENOENT
&& errno
!= EACCES
) {
1166 error
= got_error_from_errno_fmt("openat %s/%s",
1167 dirpath
, "description");
1172 if (fstat(fd
, &sb
) == -1) {
1173 error
= got_error_from_errno_fmt("fstat %s/%s",
1174 dirpath
, "description");
1179 if (len
> GOTWEBD_MAXDESCRSZ
- 1)
1180 len
= GOTWEBD_MAXDESCRSZ
- 1;
1182 *description
= calloc(len
+ 1, sizeof(**description
));
1183 if (*description
== NULL
) {
1184 error
= got_error_from_errno("calloc");
1188 if (read(fd
, *description
, len
) == -1)
1189 error
= got_error_from_errno("read");
1191 if (fd
!= -1 && close(fd
) == -1 && error
== NULL
)
1192 error
= got_error_from_errno("close");
1196 static const struct got_error
*
1197 gotweb_get_clone_url(char **url
, struct server
*srv
, const char *dirpath
,
1200 const struct got_error
*error
= NULL
;
1206 if (srv
->show_repo_cloneurl
== 0)
1209 fd
= openat(dir
, "cloneurl", O_RDONLY
);
1211 if (errno
!= ENOENT
&& errno
!= EACCES
) {
1212 error
= got_error_from_errno_fmt("openat %s/%s",
1213 dirpath
, "cloneurl");
1218 if (fstat(fd
, &sb
) == -1) {
1219 error
= got_error_from_errno_fmt("fstat %s/%s",
1220 dirpath
, "cloneurl");
1225 if (len
> GOTWEBD_MAXCLONEURLSZ
- 1)
1226 len
= GOTWEBD_MAXCLONEURLSZ
- 1;
1228 *url
= calloc(len
+ 1, sizeof(**url
));
1230 error
= got_error_from_errno("calloc");
1234 if (read(fd
, *url
, len
) == -1)
1235 error
= got_error_from_errno("read");
1237 if (fd
!= -1 && close(fd
) == -1 && error
== NULL
)
1238 error
= got_error_from_errno("close");
1243 gotweb_render_age(struct template *tp
, time_t committer_time
)
1245 struct request
*c
= tp
->tp_arg
;
1246 long long diff_time
;
1247 const char *years
= "years ago", *months
= "months ago";
1248 const char *weeks
= "weeks ago", *days
= "days ago";
1249 const char *hours
= "hours ago", *minutes
= "minutes ago";
1250 const char *seconds
= "seconds ago", *now
= "right now";
1252 diff_time
= time(NULL
) - committer_time
;
1253 if (diff_time
> 60 * 60 * 24 * 365 * 2) {
1254 if (tp_writef(c
->tp
, "%lld %s",
1255 (diff_time
/ 60 / 60 / 24 / 365), years
) == -1)
1257 } else if (diff_time
> 60 * 60 * 24 * (365 / 12) * 2) {
1258 if (tp_writef(c
->tp
, "%lld %s",
1259 (diff_time
/ 60 / 60 / 24 / (365 / 12)),
1262 } else if (diff_time
> 60 * 60 * 24 * 7 * 2) {
1263 if (tp_writef(c
->tp
, "%lld %s",
1264 (diff_time
/ 60 / 60 / 24 / 7), weeks
) == -1)
1266 } else if (diff_time
> 60 * 60 * 24 * 2) {
1267 if (tp_writef(c
->tp
, "%lld %s",
1268 (diff_time
/ 60 / 60 / 24), days
) == -1)
1270 } else if (diff_time
> 60 * 60 * 2) {
1271 if (tp_writef(c
->tp
, "%lld %s",
1272 (diff_time
/ 60 / 60), hours
) == -1)
1274 } else if (diff_time
> 60 * 2) {
1275 if (tp_writef(c
->tp
, "%lld %s", (diff_time
/ 60),
1278 } else if (diff_time
> 2) {
1279 if (tp_writef(c
->tp
, "%lld %s", diff_time
,
1283 if (tp_writes(tp
, now
) == -1)