1 /* ui-plain.c: functions for output of plain blobs by path
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
12 #include "ui-shared.h"
14 struct walk_tree_context
{
19 static int print_object(const unsigned char *sha1
, const char *path
)
21 enum object_type type
;
25 type
= sha1_object_info(sha1
, &size
);
26 if (type
== OBJ_BAD
) {
27 cgit_print_error_page(404, "Not found", "Not found");
31 buf
= read_sha1_file(sha1
, &type
, &size
);
33 cgit_print_error_page(404, "Not found", "Not found");
37 mimetype
= get_mimetype_for_filename(path
);
38 ctx
.page
.mimetype
= mimetype
;
40 if (!ctx
.page
.mimetype
) {
41 if (buffer_is_binary(buf
, size
)) {
42 ctx
.page
.mimetype
= "application/octet-stream";
43 ctx
.page
.charset
= NULL
;
45 ctx
.page
.mimetype
= "text/plain";
48 ctx
.page
.filename
= path
;
50 ctx
.page
.etag
= sha1_to_hex(sha1
);
51 cgit_print_http_headers();
58 static char *buildpath(const char *base
, int baselen
, const char *path
)
61 return fmtalloc("%.*s%s/", baselen
, base
, path
);
63 return fmtalloc("%.*s/", baselen
, base
);
66 static void print_dir(const unsigned char *sha1
, const char *base
,
67 int baselen
, const char *path
)
69 char *fullpath
, *slash
;
72 fullpath
= buildpath(base
, baselen
, path
);
73 slash
= (fullpath
[0] == '/' ? "" : "/");
74 ctx
.page
.etag
= sha1_to_hex(sha1
);
75 cgit_print_http_headers();
76 htmlf("<html><head><title>%s", slash
);
78 htmlf("</title></head>\n<body>\n<h2>%s", slash
);
80 html("</h2>\n<ul>\n");
81 len
= strlen(fullpath
);
83 fullpath
[len
- 1] = 0;
84 slash
= strrchr(fullpath
, '/');
92 cgit_plain_link("../", NULL
, NULL
, ctx
.qry
.head
, ctx
.qry
.sha1
,
99 static void print_dir_entry(const unsigned char *sha1
, const char *base
,
100 int baselen
, const char *path
, unsigned mode
)
104 fullpath
= buildpath(base
, baselen
, path
);
105 if (!S_ISDIR(mode
) && !S_ISGITLINK(mode
))
106 fullpath
[strlen(fullpath
) - 1] = 0;
108 if (S_ISGITLINK(mode
)) {
109 cgit_submodule_link(NULL
, fullpath
, sha1_to_hex(sha1
));
111 cgit_plain_link(path
, NULL
, NULL
, ctx
.qry
.head
, ctx
.qry
.sha1
,
117 static void print_dir_tail(void)
119 html(" </ul>\n</body></html>\n");
122 static int walk_tree(const unsigned char *sha1
, struct strbuf
*base
,
123 const char *pathname
, unsigned mode
, int stage
, void *cbdata
)
125 struct walk_tree_context
*walk_tree_ctx
= cbdata
;
127 if (base
->len
== walk_tree_ctx
->match_baselen
) {
129 if (print_object(sha1
, pathname
))
130 walk_tree_ctx
->match
= 1;
131 } else if (S_ISDIR(mode
)) {
132 print_dir(sha1
, base
->buf
, base
->len
, pathname
);
133 walk_tree_ctx
->match
= 2;
134 return READ_TREE_RECURSIVE
;
136 } else if (base
->len
> walk_tree_ctx
->match_baselen
) {
137 print_dir_entry(sha1
, base
->buf
, base
->len
, pathname
, mode
);
138 walk_tree_ctx
->match
= 2;
139 } else if (S_ISDIR(mode
)) {
140 return READ_TREE_RECURSIVE
;
146 static int basedir_len(const char *path
)
148 char *p
= strrchr(path
, '/');
154 void cgit_print_plain(void)
156 const char *rev
= ctx
.qry
.sha1
;
157 unsigned char sha1
[20];
158 struct commit
*commit
;
159 struct pathspec_item path_items
= {
160 .match
= ctx
.qry
.path
,
161 .len
= ctx
.qry
.path
? strlen(ctx
.qry
.path
) : 0
163 struct pathspec paths
= {
167 struct walk_tree_context walk_tree_ctx
= {
174 if (get_sha1(rev
, sha1
)) {
175 cgit_print_error_page(404, "Not found", "Not found");
178 commit
= lookup_commit_reference(sha1
);
179 if (!commit
|| parse_commit(commit
)) {
180 cgit_print_error_page(404, "Not found", "Not found");
183 if (!path_items
.match
) {
184 path_items
.match
= "";
185 walk_tree_ctx
.match_baselen
= -1;
186 print_dir(commit
->tree
->object
.sha1
, "", 0, "");
187 walk_tree_ctx
.match
= 2;
190 walk_tree_ctx
.match_baselen
= basedir_len(path_items
.match
);
191 read_tree_recursive(commit
->tree
, "", 0, 0, &paths
, walk_tree
, &walk_tree_ctx
);
192 if (!walk_tree_ctx
.match
)
193 cgit_print_error_page(404, "Not found", "Not found");
194 else if (walk_tree_ctx
.match
== 2)