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_init_repo_dir(struct repo_dir
**,
89 static const struct got_error
*gotweb_load_got_path(struct request
*c
,
91 static const struct got_error
*gotweb_get_repo_description(char **,
92 struct server
*, const char *, int);
93 static const struct got_error
*gotweb_get_clone_url(char **, struct server
*,
96 static void gotweb_free_querystring(struct querystring
*);
97 static void gotweb_free_repo_dir(struct repo_dir
*);
99 struct server
*gotweb_get_server(const char *);
102 gotweb_reply(struct request
*c
, int status
, const char *ctype
,
103 struct gotweb_url
*location
)
107 if (status
!= 200 && tp_writef(c
->tp
, "Status: %d\r\n", status
) == -1)
111 if (tp_writes(c
->tp
, "Location: ") == -1 ||
112 gotweb_render_url(c
, location
) == -1 ||
113 tp_writes(c
->tp
, "\r\n") == -1)
117 csp
= "Content-Security-Policy: default-src 'self'; "
118 "script-src 'none'; object-src 'none';\r\n";
119 if (tp_writes(c
->tp
, csp
) == -1)
122 if (ctype
&& tp_writef(c
->tp
, "Content-Type: %s\r\n", ctype
) == -1)
125 return tp_writes(c
->tp
, "\r\n");
129 gotweb_reply_file(struct request
*c
, const char *ctype
, const char *file
,
134 r
= tp_writef(c
->tp
, "Content-Disposition: attachment; "
135 "filename=%s%s\r\n", file
, suffix
? suffix
: "");
138 return gotweb_reply(c
, 200, ctype
, NULL
);
142 gotweb_process_request(struct request
*c
)
144 const struct got_error
*error
= NULL
;
145 struct server
*srv
= NULL
;
146 struct querystring
*qs
= NULL
;
147 struct repo_dir
*repo_dir
= NULL
;
148 const char *rss_ctype
= "application/rss+xml;charset=utf-8";
153 /* init the transport */
154 error
= gotweb_init_transport(&c
->t
);
156 log_warnx("%s: %s", __func__
, error
->msg
);
159 /* don't process any further if client disconnected */
160 if (c
->sock
->client_status
== CLIENT_DISCONNECT
)
162 /* get the gotwebd server */
163 srv
= gotweb_get_server(c
->server_name
);
165 log_warnx("%s: error server is NULL", __func__
);
169 /* parse our querystring */
170 error
= gotweb_init_querystring(&qs
);
172 log_warnx("%s: %s", __func__
, error
->msg
);
176 error
= gotweb_parse_querystring(&qs
, c
->querystring
);
178 log_warnx("%s: %s", __func__
, error
->msg
);
183 * certain actions require a commit id in the querystring. this stops
184 * bad actors from exploiting this by manually manipulating the
188 if (qs
->action
== BLAME
|| qs
->action
== BLOB
||
189 qs
->action
== BLOBRAW
|| qs
->action
== DIFF
||
190 qs
->action
== PATCH
) {
191 if (qs
->commit
== NULL
) {
192 error
= got_error(GOT_ERR_BAD_QUERYSTRING
);
197 if (qs
->action
!= INDEX
) {
198 error
= gotweb_init_repo_dir(&repo_dir
, qs
->path
);
201 error
= gotweb_load_got_path(c
, repo_dir
);
202 c
->t
->repo_dir
= repo_dir
;
203 if (error
&& error
->code
!= GOT_ERR_LONELY_PACKIDX
)
207 if (qs
->action
== BLOBRAW
|| qs
->action
== BLOB
) {
208 if (qs
->folder
== NULL
|| qs
->file
== NULL
) {
209 error
= got_error(GOT_ERR_BAD_QUERYSTRING
);
213 error
= got_get_repo_commits(c
, 1);
217 error
= got_open_blob_for_output(&c
->t
->blob
, &c
->t
->fd
,
218 &binary
, c
, qs
->folder
, qs
->file
, qs
->commit
);
223 switch (qs
->action
) {
225 if (qs
->folder
== NULL
|| qs
->file
== NULL
) {
226 error
= got_error(GOT_ERR_BAD_QUERYSTRING
);
229 error
= got_get_repo_commits(c
, 1);
231 log_warnx("%s: %s", __func__
, error
->msg
);
234 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
236 gotweb_render_page(c
->tp
, gotweb_render_blame
);
240 struct gotweb_url url
= {
244 .commit
= qs
->commit
,
245 .folder
= qs
->folder
,
249 gotweb_reply(c
, 302, NULL
, &url
);
253 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
255 gotweb_render_page(c
->tp
, gotweb_render_blob
);
259 r
= gotweb_reply_file(c
, "application/octet-stream",
262 r
= gotweb_reply(c
, 200, "text/plain", NULL
);
265 if (template_flush(c
->tp
) == -1)
269 error
= got_object_blob_read_block(&len
, c
->t
->blob
);
274 buf
= got_object_blob_get_read_buf(c
->t
->blob
);
275 if (fcgi_write(c
, buf
, len
) == -1)
280 error
= got_get_repo_commits(c
, srv
->max_commits_display
);
283 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
285 gotweb_render_page(c
->tp
, gotweb_render_briefs
);
288 error
= got_get_repo_commits(c
, srv
->max_commits_display
);
290 log_warnx("%s: %s", __func__
, error
->msg
);
293 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
295 gotweb_render_page(c
->tp
, gotweb_render_commits
);
298 error
= got_get_repo_commits(c
, 1);
300 log_warnx("%s: %s", __func__
, error
->msg
);
303 error
= got_open_diff_for_output(&c
->t
->fp
, c
);
305 log_warnx("%s: %s", __func__
, error
->msg
);
308 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
310 gotweb_render_page(c
->tp
, gotweb_render_diff
);
313 c
->t
->nrepos
= scandir(srv
->repos_path
, &c
->t
->repos
, NULL
,
315 if (c
->t
->nrepos
== -1) {
317 error
= got_error_from_errno2("scandir",
321 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
323 gotweb_render_page(c
->tp
, gotweb_render_index
);
326 error
= got_get_repo_commits(c
, 1);
328 log_warnx("%s: %s", __func__
, error
->msg
);
331 error
= got_open_diff_for_output(&c
->t
->fp
, c
);
333 log_warnx("%s: %s", __func__
, error
->msg
);
336 if (gotweb_reply(c
, 200, "text/plain", NULL
) == -1)
338 gotweb_render_patch(c
->tp
);
341 error
= got_get_repo_tags(c
, D_MAXSLCOMMDISP
);
344 if (gotweb_reply_file(c
, rss_ctype
, repo_dir
->name
, ".rss")
347 gotweb_render_rss(c
->tp
);
350 error
= got_ref_list(&c
->t
->refs
, c
->t
->repo
, "refs/heads",
351 got_ref_cmp_by_name
, NULL
);
353 log_warnx("%s: got_ref_list: %s", __func__
,
357 error
= got_get_repo_commits(c
, srv
->summary_commits_display
);
361 error
= got_get_repo_tags(c
, srv
->summary_tags_display
);
363 log_warnx("%s: got_get_repo_tags: %s", __func__
,
367 qs
->action
= SUMMARY
;
368 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
370 gotweb_render_page(c
->tp
, gotweb_render_summary
);
373 error
= got_get_repo_tags(c
, 1);
375 log_warnx("%s: %s", __func__
, error
->msg
);
378 if (c
->t
->tag_count
== 0) {
379 error
= got_error_msg(GOT_ERR_BAD_OBJ_ID
,
383 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
385 gotweb_render_page(c
->tp
, gotweb_render_tag
);
388 error
= got_get_repo_tags(c
, srv
->max_commits_display
);
390 log_warnx("%s: %s", __func__
, error
->msg
);
393 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
395 gotweb_render_page(c
->tp
, gotweb_render_tags
);
398 error
= got_get_repo_commits(c
, 1);
400 log_warnx("%s: %s", __func__
, error
->msg
);
403 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
405 gotweb_render_page(c
->tp
, gotweb_render_tree
);
409 error
= got_error(GOT_ERR_BAD_QUERYSTRING
);
414 if (gotweb_reply(c
, 400, "text/html", NULL
) == -1)
416 gotweb_render_page(c
->tp
, gotweb_render_error
);
420 gotweb_get_server(const char *server_name
)
424 /* check against the server name first */
425 if (*server_name
!= '\0')
426 TAILQ_FOREACH(srv
, &gotwebd_env
->servers
, entry
)
427 if (strcmp(srv
->name
, server_name
) == 0)
430 /* otherwise, use the first server */
431 return TAILQ_FIRST(&gotwebd_env
->servers
);
434 const struct got_error
*
435 gotweb_init_transport(struct transport
**t
)
437 const struct got_error
*error
= NULL
;
439 *t
= calloc(1, sizeof(**t
));
441 return got_error_from_errno2(__func__
, "calloc");
443 TAILQ_INIT(&(*t
)->repo_commits
);
444 TAILQ_INIT(&(*t
)->repo_tags
);
445 TAILQ_INIT(&(*t
)->refs
);
452 static const struct got_error
*
453 gotweb_init_querystring(struct querystring
**qs
)
455 const struct got_error
*error
= NULL
;
457 *qs
= calloc(1, sizeof(**qs
));
459 return got_error_from_errno2(__func__
, "calloc");
461 (*qs
)->headref
= strdup("HEAD");
462 if ((*qs
)->headref
== NULL
) {
465 return got_error_from_errno2(__func__
, "strdup");
468 (*qs
)->action
= INDEX
;
473 static const struct got_error
*
474 gotweb_parse_querystring(struct querystring
**qs
, char *qst
)
476 const struct got_error
*error
= NULL
;
477 char *tok1
= NULL
, *tok1_pair
= NULL
, *tok1_end
= NULL
;
478 char *tok2
= NULL
, *tok2_pair
= NULL
, *tok2_end
= NULL
;
485 return got_error_from_errno2(__func__
, "strdup");
490 while (tok1_pair
!= NULL
) {
491 strsep(&tok1_end
, "&");
493 tok2
= strdup(tok1_pair
);
496 return got_error_from_errno2(__func__
, "strdup");
502 while (tok2_pair
!= NULL
) {
503 strsep(&tok2_end
, "=");
505 error
= gotweb_assign_querystring(qs
, tok2_pair
,
510 tok2_pair
= tok2_end
;
513 tok1_pair
= tok1_end
;
524 * Adapted from usr.sbin/httpd/httpd.c url_decode.
526 static const struct got_error
*
527 gotweb_urldecode(char *url
)
539 /* Encoding character is followed by two hex chars */
540 if (!isxdigit((unsigned char)p
[1]) ||
541 !isxdigit((unsigned char)p
[2]) ||
542 (p
[1] == '0' && p
[2] == '0'))
543 return got_error(GOT_ERR_BAD_QUERYSTRING
);
549 * We don't have to validate "hex" because it is
550 * guaranteed to include two hex chars followed by nul.
552 x
= strtoul(hex
, NULL
, 16);
568 static const struct got_error
*
569 gotweb_assign_querystring(struct querystring
**qs
, char *key
, char *value
)
571 const struct got_error
*error
= NULL
;
575 error
= gotweb_urldecode(value
);
579 for (el_cnt
= 0; el_cnt
< nitems(querystring_keys
); el_cnt
++) {
580 if (strcmp(key
, querystring_keys
[el_cnt
].name
) != 0)
583 switch (querystring_keys
[el_cnt
].element
) {
585 for (a_cnt
= 0; a_cnt
< nitems(action_keys
); a_cnt
++) {
586 if (strcmp(value
, action_keys
[a_cnt
].name
) != 0)
588 else if (strcmp(value
,
589 action_keys
[a_cnt
].name
) == 0){
591 action_keys
[a_cnt
].action
;
599 (*qs
)->commit
= strdup(value
);
600 if ((*qs
)->commit
== NULL
) {
601 error
= got_error_from_errno2(__func__
,
607 (*qs
)->file
= strdup(value
);
608 if ((*qs
)->file
== NULL
) {
609 error
= got_error_from_errno2(__func__
,
615 (*qs
)->folder
= strdup(value
);
616 if ((*qs
)->folder
== NULL
) {
617 error
= got_error_from_errno2(__func__
,
623 free((*qs
)->headref
);
624 (*qs
)->headref
= strdup(value
);
625 if ((*qs
)->headref
== NULL
) {
626 error
= got_error_from_errno2(__func__
,
634 (*qs
)->index_page
= strtonum(value
, INT64_MIN
,
637 error
= got_error_from_errno3(__func__
,
641 if ((*qs
)->index_page
< 0)
642 (*qs
)->index_page
= 0;
645 (*qs
)->path
= strdup(value
);
646 if ((*qs
)->path
== NULL
) {
647 error
= got_error_from_errno2(__func__
,
662 gotweb_free_repo_tag(struct repo_tag
*rt
)
667 free(rt
->tag_commit
);
668 free(rt
->commit_msg
);
675 gotweb_free_repo_commit(struct repo_commit
*rc
)
685 free(rc
->commit_msg
);
691 gotweb_free_querystring(struct querystring
*qs
)
704 gotweb_free_repo_dir(struct repo_dir
*repo_dir
)
706 if (repo_dir
!= NULL
) {
707 free(repo_dir
->name
);
708 free(repo_dir
->owner
);
709 free(repo_dir
->description
);
711 free(repo_dir
->path
);
717 gotweb_free_transport(struct transport
*t
)
719 const struct got_error
*err
;
720 struct repo_commit
*rc
= NULL
, *trc
= NULL
;
721 struct repo_tag
*rt
= NULL
, *trt
= NULL
;
724 got_ref_list_free(&t
->refs
);
725 TAILQ_FOREACH_SAFE(rc
, &t
->repo_commits
, entry
, trc
) {
726 TAILQ_REMOVE(&t
->repo_commits
, rc
, entry
);
727 gotweb_free_repo_commit(rc
);
729 TAILQ_FOREACH_SAFE(rt
, &t
->repo_tags
, entry
, trt
) {
730 TAILQ_REMOVE(&t
->repo_tags
, rt
, entry
);
731 gotweb_free_repo_tag(rt
);
733 gotweb_free_repo_dir(t
->repo_dir
);
734 gotweb_free_querystring(t
->qs
);
736 free(t
->tags_more_id
);
738 got_object_blob_close(t
->blob
);
740 err
= got_gotweb_closefile(t
->fp
);
742 log_warnx("%s: got_gotweb_closefile failure: %s",
745 if (t
->fd
!= -1 && close(t
->fd
) == -1)
746 log_warn("%s: close", __func__
);
748 for (i
= 0; i
< t
->nrepos
; ++i
)
753 got_repo_close(t
->repo
);
758 gotweb_index_navs(struct request
*c
, struct gotweb_url
*prev
, int *have_prev
,
759 struct gotweb_url
*next
, int *have_next
)
761 struct transport
*t
= c
->t
;
762 struct querystring
*qs
= t
->qs
;
763 struct server
*srv
= c
->srv
;
765 *have_prev
= *have_next
= 0;
767 if (qs
->index_page
> 0) {
769 *prev
= (struct gotweb_url
){
771 .index_page
= qs
->index_page
- 1,
774 if (t
->next_disp
== srv
->max_repos_display
&&
775 t
->repos_total
!= (qs
->index_page
+ 1) *
776 srv
->max_repos_display
) {
778 *next
= (struct gotweb_url
){
780 .index_page
= qs
->index_page
+ 1,
786 gotweb_render_index(struct template *tp
)
788 const struct got_error
*error
= NULL
;
789 struct request
*c
= tp
->tp_arg
;
790 struct server
*srv
= c
->srv
;
791 struct transport
*t
= c
->t
;
792 struct querystring
*qs
= t
->qs
;
793 struct repo_dir
*repo_dir
= NULL
;
794 struct dirent
**sd_dent
= t
->repos
;
795 unsigned int d_i
, d_disp
= 0;
796 unsigned int d_skipped
= 0;
799 if (gotweb_render_repo_table_hdr(c
->tp
) == -1)
802 for (d_i
= 0; d_i
< t
->nrepos
; d_i
++) {
803 if (strcmp(sd_dent
[d_i
]->d_name
, ".") == 0 ||
804 strcmp(sd_dent
[d_i
]->d_name
, "..") == 0) {
809 error
= got_path_dirent_type(&type
, srv
->repos_path
,
813 if (type
!= DT_DIR
) {
818 if (qs
->index_page
> 0 && (qs
->index_page
*
819 srv
->max_repos_display
) > t
->prev_disp
) {
824 error
= gotweb_init_repo_dir(&repo_dir
, sd_dent
[d_i
]->d_name
);
828 error
= gotweb_load_got_path(c
, repo_dir
);
829 if (error
&& error
->code
!= GOT_ERR_LONELY_PACKIDX
) {
830 if (error
->code
!= GOT_ERR_NOT_GIT_REPO
)
831 log_warnx("%s: %s: %s", __func__
,
832 sd_dent
[d_i
]->d_name
, error
->msg
);
833 gotweb_free_repo_dir(repo_dir
);
842 r
= gotweb_render_repo_fragment(c
->tp
, repo_dir
);
843 gotweb_free_repo_dir(repo_dir
);
845 got_repo_close(t
->repo
);
851 if (d_disp
== srv
->max_repos_display
)
854 t
->repos_total
= t
->nrepos
- d_skipped
;
856 if (srv
->max_repos_display
== 0 ||
857 t
->repos_total
<= srv
->max_repos_display
)
860 if (gotweb_render_navs(c
->tp
) == -1)
867 should_urlencode(int c
)
869 if (c
<= ' ' || c
>= 127)
893 /* needed because the URLs are embedded into the HTML */
902 gotweb_urlencode(const char *str
)
910 for (s
= str
; *s
; ++s
) {
912 if (should_urlencode(*s
))
916 escaped
= calloc(1, len
+ 1);
921 for (s
= str
; *s
; ++s
) {
922 if (should_urlencode(*s
)) {
923 a
= (*s
& 0xF0) >> 4;
927 escaped
[i
++] = a
<= 9 ? ('0' + a
) : ('7' + a
);
928 escaped
[i
++] = b
<= 9 ? ('0' + b
) : ('7' + b
);
937 gotweb_action_name(int action
)
974 gotweb_render_url(struct request
*c
, struct gotweb_url
*url
)
976 const char *sep
= "?", *action
;
980 action
= gotweb_action_name(url
->action
);
981 if (action
!= NULL
) {
982 if (tp_writef(c
->tp
, "?action=%s", action
) == -1)
988 if (tp_writef(c
->tp
, "%scommit=%s", sep
, url
->commit
) == -1)
994 if (tp_writef(c
->tp
, "%sprevid=%s", sep
, url
->previd
) == -1)
1000 if (tp_writef(c
->tp
, "%sprevset=%s", sep
, url
->prevset
) == -1)
1006 tmp
= gotweb_urlencode(url
->file
);
1009 r
= tp_writef(c
->tp
, "%sfile=%s", sep
, tmp
);
1017 tmp
= gotweb_urlencode(url
->folder
);
1020 r
= tp_writef(c
->tp
, "%sfolder=%s", sep
, tmp
);
1028 tmp
= gotweb_urlencode(url
->headref
);
1031 r
= tp_writef(c
->tp
, "%sheadref=%s", sep
, url
->headref
);
1038 if (url
->index_page
!= -1) {
1039 if (tp_writef(c
->tp
, "%sindex_page=%d", sep
,
1040 url
->index_page
) == -1)
1046 tmp
= gotweb_urlencode(url
->path
);
1049 r
= tp_writef(c
->tp
, "%spath=%s", sep
, tmp
);
1060 gotweb_render_absolute_url(struct request
*c
, struct gotweb_url
*url
)
1062 struct template *tp
= c
->tp
;
1063 const char *proto
= c
->https
? "https" : "http";
1065 if (tp_writes(tp
, proto
) == -1 ||
1066 tp_writes(tp
, "://") == -1 ||
1067 tp_htmlescape(tp
, c
->server_name
) == -1 ||
1068 tp_htmlescape(tp
, c
->document_uri
) == -1)
1071 return gotweb_render_url(c
, url
);
1074 static const struct got_error
*
1075 gotweb_load_got_path(struct request
*c
, struct repo_dir
*repo_dir
)
1077 const struct got_error
*error
= NULL
;
1078 struct socket
*sock
= c
->sock
;
1079 struct server
*srv
= c
->srv
;
1080 struct transport
*t
= c
->t
;
1084 if (asprintf(&dir_test
, "%s/%s/%s", srv
->repos_path
, repo_dir
->name
,
1085 GOTWEB_GIT_DIR
) == -1)
1086 return got_error_from_errno("asprintf");
1088 dt
= opendir(dir_test
);
1091 if (asprintf(&dir_test
, "%s/%s", srv
->repos_path
,
1092 repo_dir
->name
) == -1)
1093 return got_error_from_errno("asprintf");
1094 dt
= opendir(dir_test
);
1097 return got_error_path(repo_dir
->name
,
1098 GOT_ERR_NOT_GIT_REPO
);
1102 repo_dir
->path
= dir_test
;
1105 if (srv
->respect_exportok
&&
1106 faccessat(dirfd(dt
), "git-daemon-export-ok", F_OK
, 0) == -1) {
1107 error
= got_error_path(repo_dir
->name
, GOT_ERR_NOT_GIT_REPO
);
1111 error
= got_repo_open(&t
->repo
, repo_dir
->path
, NULL
, sock
->pack_fds
);
1114 error
= gotweb_get_repo_description(&repo_dir
->description
, srv
,
1115 repo_dir
->path
, dirfd(dt
));
1118 error
= got_get_repo_owner(&repo_dir
->owner
, c
);
1121 if (srv
->show_repo_age
) {
1122 error
= got_get_repo_age(&repo_dir
->age
, c
, NULL
);
1126 error
= gotweb_get_clone_url(&repo_dir
->url
, srv
, repo_dir
->path
,
1130 if (dt
!= NULL
&& closedir(dt
) == EOF
&& error
== NULL
)
1131 error
= got_error_from_errno("closedir");
1132 if (error
&& t
->repo
) {
1133 got_repo_close(t
->repo
);
1139 static const struct got_error
*
1140 gotweb_init_repo_dir(struct repo_dir
**repo_dir
, const char *dir
)
1142 const struct got_error
*error
;
1144 *repo_dir
= calloc(1, sizeof(**repo_dir
));
1145 if (*repo_dir
== NULL
)
1146 return got_error_from_errno("calloc");
1148 if (asprintf(&(*repo_dir
)->name
, "%s", dir
) == -1) {
1149 error
= got_error_from_errno("asprintf");
1154 (*repo_dir
)->owner
= NULL
;
1155 (*repo_dir
)->description
= NULL
;
1156 (*repo_dir
)->url
= NULL
;
1157 (*repo_dir
)->path
= NULL
;
1162 static const struct got_error
*
1163 gotweb_get_repo_description(char **description
, struct server
*srv
,
1164 const char *dirpath
, int dir
)
1166 const struct got_error
*error
= NULL
;
1171 *description
= NULL
;
1172 if (srv
->show_repo_description
== 0)
1175 fd
= openat(dir
, "description", O_RDONLY
);
1177 if (errno
!= ENOENT
&& errno
!= EACCES
) {
1178 error
= got_error_from_errno_fmt("openat %s/%s",
1179 dirpath
, "description");
1184 if (fstat(fd
, &sb
) == -1) {
1185 error
= got_error_from_errno_fmt("fstat %s/%s",
1186 dirpath
, "description");
1191 if (len
> GOTWEBD_MAXDESCRSZ
- 1)
1192 len
= GOTWEBD_MAXDESCRSZ
- 1;
1194 *description
= calloc(len
+ 1, sizeof(**description
));
1195 if (*description
== NULL
) {
1196 error
= got_error_from_errno("calloc");
1200 if (read(fd
, *description
, len
) == -1)
1201 error
= got_error_from_errno("read");
1203 if (fd
!= -1 && close(fd
) == -1 && error
== NULL
)
1204 error
= got_error_from_errno("close");
1208 static const struct got_error
*
1209 gotweb_get_clone_url(char **url
, struct server
*srv
, const char *dirpath
,
1212 const struct got_error
*error
= NULL
;
1218 if (srv
->show_repo_cloneurl
== 0)
1221 fd
= openat(dir
, "cloneurl", O_RDONLY
);
1223 if (errno
!= ENOENT
&& errno
!= EACCES
) {
1224 error
= got_error_from_errno_fmt("openat %s/%s",
1225 dirpath
, "cloneurl");
1230 if (fstat(fd
, &sb
) == -1) {
1231 error
= got_error_from_errno_fmt("fstat %s/%s",
1232 dirpath
, "cloneurl");
1237 if (len
> GOTWEBD_MAXCLONEURLSZ
- 1)
1238 len
= GOTWEBD_MAXCLONEURLSZ
- 1;
1240 *url
= calloc(len
+ 1, sizeof(**url
));
1242 error
= got_error_from_errno("calloc");
1246 if (read(fd
, *url
, len
) == -1)
1247 error
= got_error_from_errno("read");
1249 if (fd
!= -1 && close(fd
) == -1 && error
== NULL
)
1250 error
= got_error_from_errno("close");
1255 gotweb_render_age(struct template *tp
, time_t committer_time
)
1257 struct request
*c
= tp
->tp_arg
;
1258 long long diff_time
;
1259 const char *years
= "years ago", *months
= "months ago";
1260 const char *weeks
= "weeks ago", *days
= "days ago";
1261 const char *hours
= "hours ago", *minutes
= "minutes ago";
1262 const char *seconds
= "seconds ago", *now
= "right now";
1264 diff_time
= time(NULL
) - committer_time
;
1265 if (diff_time
> 60 * 60 * 24 * 365 * 2) {
1266 if (tp_writef(c
->tp
, "%lld %s",
1267 (diff_time
/ 60 / 60 / 24 / 365), years
) == -1)
1269 } else if (diff_time
> 60 * 60 * 24 * (365 / 12) * 2) {
1270 if (tp_writef(c
->tp
, "%lld %s",
1271 (diff_time
/ 60 / 60 / 24 / (365 / 12)),
1274 } else if (diff_time
> 60 * 60 * 24 * 7 * 2) {
1275 if (tp_writef(c
->tp
, "%lld %s",
1276 (diff_time
/ 60 / 60 / 24 / 7), weeks
) == -1)
1278 } else if (diff_time
> 60 * 60 * 24 * 2) {
1279 if (tp_writef(c
->tp
, "%lld %s",
1280 (diff_time
/ 60 / 60 / 24), days
) == -1)
1282 } else if (diff_time
> 60 * 60 * 2) {
1283 if (tp_writef(c
->tp
, "%lld %s",
1284 (diff_time
/ 60 / 60), hours
) == -1)
1286 } else if (diff_time
> 60 * 2) {
1287 if (tp_writef(c
->tp
, "%lld %s", (diff_time
/ 60),
1290 } else if (diff_time
> 2) {
1291 if (tp_writef(c
->tp
, "%lld %s", diff_time
,
1295 if (tp_writes(tp
, now
) == -1)