ui-shared: fix resource leak: free allocation from cgit_hosturl
[cgit.git] / ui-tree.c
blobd71503da1f7691329946e481434f1998eb811454
1 /* ui-tree.c: functions for tree output
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)
7 */
9 #include "cgit.h"
10 #include "ui-tree.h"
11 #include "html.h"
12 #include "ui-shared.h"
14 struct walk_tree_context {
15 char *curr_rev;
16 char *match_path;
17 int state;
20 static void print_text_buffer(const char *name, char *buf, unsigned long size)
22 unsigned long lineno, idx;
23 const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
25 html("<table summary='blob content' class='blob'>\n");
27 if (ctx.cfg.enable_tree_linenumbers) {
28 html("<tr><td class='linenumbers'><pre>");
29 idx = 0;
30 lineno = 0;
32 if (size) {
33 htmlf(numberfmt, ++lineno);
34 while (idx < size - 1) { // skip absolute last newline
35 if (buf[idx] == '\n')
36 htmlf(numberfmt, ++lineno);
37 idx++;
40 html("</pre></td>\n");
42 else {
43 html("<tr>\n");
46 if (ctx.repo->source_filter) {
47 char *filter_arg = xstrdup(name);
48 html("<td class='lines'><pre><code>");
49 cgit_open_filter(ctx.repo->source_filter, filter_arg);
50 html_raw(buf, size);
51 cgit_close_filter(ctx.repo->source_filter);
52 free(filter_arg);
53 html("</code></pre></td></tr></table>\n");
54 return;
57 html("<td class='lines'><pre><code>");
58 html_txt(buf);
59 html("</code></pre></td></tr></table>\n");
62 #define ROWLEN 32
64 static void print_binary_buffer(char *buf, unsigned long size)
66 unsigned long ofs, idx;
67 static char ascii[ROWLEN + 1];
69 html("<table summary='blob content' class='bin-blob'>\n");
70 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
71 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
72 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
73 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
74 htmlf("%*s%02x",
75 idx == 16 ? 4 : 1, "",
76 buf[idx] & 0xff);
77 html(" </td><td class='hex'>");
78 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
79 ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
80 ascii[idx] = '\0';
81 html_txt(ascii);
82 html("</td></tr>\n");
84 html("</table>\n");
87 static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
89 enum object_type type;
90 char *buf;
91 unsigned long size;
93 type = sha1_object_info(sha1, &size);
94 if (type == OBJ_BAD) {
95 cgit_print_error_page(404, "Not found",
96 "Bad object name: %s", sha1_to_hex(sha1));
97 return;
100 buf = read_sha1_file(sha1, &type, &size);
101 if (!buf) {
102 cgit_print_error_page(500, "Internal server error",
103 "Error reading object %s", sha1_to_hex(sha1));
104 return;
107 cgit_print_layout_start();
108 htmlf("blob: %s (", sha1_to_hex(sha1));
109 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
110 rev, path);
111 html(")\n");
113 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
114 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
115 size / 1024, ctx.cfg.max_blob_size);
116 return;
119 if (buffer_is_binary(buf, size))
120 print_binary_buffer(buf, size);
121 else
122 print_text_buffer(basename, buf, size);
126 static int ls_item(const unsigned char *sha1, struct strbuf *base,
127 const char *pathname, unsigned mode, int stage, void *cbdata)
129 struct walk_tree_context *walk_tree_ctx = cbdata;
130 char *name;
131 struct strbuf fullpath = STRBUF_INIT;
132 struct strbuf class = STRBUF_INIT;
133 enum object_type type;
134 unsigned long size = 0;
136 name = xstrdup(pathname);
137 strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
138 ctx.qry.path ? "/" : "", name);
140 if (!S_ISGITLINK(mode)) {
141 type = sha1_object_info(sha1, &size);
142 if (type == OBJ_BAD) {
143 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
144 name,
145 sha1_to_hex(sha1));
146 free(name);
147 return 0;
151 html("<tr><td class='ls-mode'>");
152 cgit_print_filemode(mode);
153 html("</td><td>");
154 if (S_ISGITLINK(mode)) {
155 cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1));
156 } else if (S_ISDIR(mode)) {
157 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
158 walk_tree_ctx->curr_rev, fullpath.buf);
159 } else {
160 char *ext = strrchr(name, '.');
161 strbuf_addstr(&class, "ls-blob");
162 if (ext)
163 strbuf_addf(&class, " %s", ext + 1);
164 cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
165 walk_tree_ctx->curr_rev, fullpath.buf);
167 htmlf("</td><td class='ls-size'>%li</td>", size);
169 html("<td>");
170 cgit_log_link("log", NULL, "button", ctx.qry.head,
171 walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
172 ctx.qry.showmsg, 0);
173 if (ctx.repo->max_stats)
174 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
175 fullpath.buf);
176 if (!S_ISGITLINK(mode))
177 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
178 walk_tree_ctx->curr_rev, fullpath.buf);
179 html("</td></tr>\n");
180 free(name);
181 strbuf_release(&fullpath);
182 strbuf_release(&class);
183 return 0;
186 static void ls_head(void)
188 cgit_print_layout_start();
189 html("<table summary='tree listing' class='list'>\n");
190 html("<tr class='nohover'>");
191 html("<th class='left'>Mode</th>");
192 html("<th class='left'>Name</th>");
193 html("<th class='right'>Size</th>");
194 html("<th/>");
195 html("</tr>\n");
198 static void ls_tail(void)
200 html("</table>\n");
201 cgit_print_layout_end();
204 static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
206 struct tree *tree;
207 struct pathspec paths = {
208 .nr = 0
211 tree = parse_tree_indirect(sha1);
212 if (!tree) {
213 cgit_print_error_page(404, "Not found",
214 "Not a tree object: %s", sha1_to_hex(sha1));
215 return;
218 ls_head();
219 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
220 ls_tail();
224 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
225 const char *pathname, unsigned mode, int stage, void *cbdata)
227 struct walk_tree_context *walk_tree_ctx = cbdata;
228 static char buffer[PATH_MAX];
230 if (walk_tree_ctx->state == 0) {
231 memcpy(buffer, base->buf, base->len);
232 strcpy(buffer + base->len, pathname);
233 if (strcmp(walk_tree_ctx->match_path, buffer))
234 return READ_TREE_RECURSIVE;
236 if (S_ISDIR(mode)) {
237 walk_tree_ctx->state = 1;
238 ls_head();
239 return READ_TREE_RECURSIVE;
240 } else {
241 walk_tree_ctx->state = 2;
242 print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
243 return 0;
246 ls_item(sha1, base, pathname, mode, stage, walk_tree_ctx);
247 return 0;
251 * Show a tree or a blob
252 * rev: the commit pointing at the root tree object
253 * path: path to tree or blob
255 void cgit_print_tree(const char *rev, char *path)
257 unsigned char sha1[20];
258 struct commit *commit;
259 struct pathspec_item path_items = {
260 .match = path,
261 .len = path ? strlen(path) : 0
263 struct pathspec paths = {
264 .nr = path ? 1 : 0,
265 .items = &path_items
267 struct walk_tree_context walk_tree_ctx = {
268 .match_path = path,
269 .state = 0
272 if (!rev)
273 rev = ctx.qry.head;
275 if (get_sha1(rev, sha1)) {
276 cgit_print_error_page(404, "Not found",
277 "Invalid revision name: %s", rev);
278 return;
280 commit = lookup_commit_reference(sha1);
281 if (!commit || parse_commit(commit)) {
282 cgit_print_error_page(404, "Not found",
283 "Invalid commit reference: %s", rev);
284 return;
287 walk_tree_ctx.curr_rev = xstrdup(rev);
289 if (path == NULL) {
290 ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
291 goto cleanup;
294 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
295 if (walk_tree_ctx.state == 1)
296 ls_tail();
297 else if (walk_tree_ctx.state == 2)
298 cgit_print_layout_end();
299 else
300 cgit_print_error_page(404, "Not found", "Path not found");
302 cleanup:
303 free(walk_tree_ctx.curr_rev);