summaryrefslogtreecommitdiffabout
authorFerry Huberts <ferry.huberts@pelagic.nl>2011-03-09 07:16:58 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2011-03-26 14:13:35 (UTC)
commit3f1ebd3565afa33196dfc3e8584e04564987e33c (patch) (unidiff)
tree714a4426c035416af4b4cb47bb90aecd4b3851c6
parent4d2a303c3e198c91cb6635eb66fa6f0a6c0277cc (diff)
downloadcgit-3f1ebd3565afa33196dfc3e8584e04564987e33c.zip
cgit-3f1ebd3565afa33196dfc3e8584e04564987e33c.tar.gz
cgit-3f1ebd3565afa33196dfc3e8584e04564987e33c.tar.bz2
source_filter: fix a memory leak
Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl> Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--ui-tree.c2
1 files changed, 2 insertions, 0 deletions
diff --git a/ui-tree.c b/ui-tree.c
index 0b1b531..442b6be 100644
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -1,242 +1,244 @@
1/* ui-tree.c: functions for tree output 1/* ui-tree.c: functions for tree output
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include <ctype.h> 9#include <ctype.h>
10#include "cgit.h" 10#include "cgit.h"
11#include "html.h" 11#include "html.h"
12#include "ui-shared.h" 12#include "ui-shared.h"
13 13
14char *curr_rev; 14char *curr_rev;
15char *match_path; 15char *match_path;
16int header = 0; 16int header = 0;
17 17
18static void print_text_buffer(const char *name, char *buf, unsigned long size) 18static void print_text_buffer(const char *name, char *buf, unsigned long size)
19{ 19{
20 unsigned long lineno, idx; 20 unsigned long lineno, idx;
21 const char *numberfmt = 21 const char *numberfmt =
22 "<a class='no' id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a>\n"; 22 "<a class='no' id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a>\n";
23 23
24 html("<table summary='blob content' class='blob'>\n"); 24 html("<table summary='blob content' class='blob'>\n");
25 25
26 if (ctx.cfg.enable_tree_linenumbers) { 26 if (ctx.cfg.enable_tree_linenumbers) {
27 html("<tr><td class='linenumbers'><pre>"); 27 html("<tr><td class='linenumbers'><pre>");
28 idx = 0; 28 idx = 0;
29 lineno = 0; 29 lineno = 0;
30 30
31 if (size) { 31 if (size) {
32 htmlf(numberfmt, ++lineno); 32 htmlf(numberfmt, ++lineno);
33 while(idx < size - 1) { // skip absolute last newline 33 while(idx < size - 1) { // skip absolute last newline
34 if (buf[idx] == '\n') 34 if (buf[idx] == '\n')
35 htmlf(numberfmt, ++lineno); 35 htmlf(numberfmt, ++lineno);
36 idx++; 36 idx++;
37 } 37 }
38 } 38 }
39 html("</pre></td>\n"); 39 html("</pre></td>\n");
40 } 40 }
41 else { 41 else {
42 html("<tr>\n"); 42 html("<tr>\n");
43 } 43 }
44 44
45 if (ctx.repo->source_filter) { 45 if (ctx.repo->source_filter) {
46 html("<td class='lines'><pre><code>"); 46 html("<td class='lines'><pre><code>");
47 ctx.repo->source_filter->argv[1] = xstrdup(name); 47 ctx.repo->source_filter->argv[1] = xstrdup(name);
48 cgit_open_filter(ctx.repo->source_filter); 48 cgit_open_filter(ctx.repo->source_filter);
49 html_raw(buf, size); 49 html_raw(buf, size);
50 cgit_close_filter(ctx.repo->source_filter); 50 cgit_close_filter(ctx.repo->source_filter);
51 free(ctx.repo->source_filter->argv[1]);
52 ctx.repo->source_filter->argv[1] = NULL;
51 html("</code></pre></td></tr></table>\n"); 53 html("</code></pre></td></tr></table>\n");
52 return; 54 return;
53 } 55 }
54 56
55 html("<td class='lines'><pre><code>"); 57 html("<td class='lines'><pre><code>");
56 html_txt(buf); 58 html_txt(buf);
57 html("</code></pre></td></tr></table>\n"); 59 html("</code></pre></td></tr></table>\n");
58} 60}
59 61
60#define ROWLEN 32 62#define ROWLEN 32
61 63
62static void print_binary_buffer(char *buf, unsigned long size) 64static void print_binary_buffer(char *buf, unsigned long size)
63{ 65{
64 unsigned long ofs, idx; 66 unsigned long ofs, idx;
65 static char ascii[ROWLEN + 1]; 67 static char ascii[ROWLEN + 1];
66 68
67 html("<table summary='blob content' class='bin-blob'>\n"); 69 html("<table summary='blob content' class='bin-blob'>\n");
68 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>"); 70 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
69 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) { 71 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
70 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs); 72 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
71 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++) 73 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
72 htmlf("%*s%02x", 74 htmlf("%*s%02x",
73 idx == 16 ? 4 : 1, "", 75 idx == 16 ? 4 : 1, "",
74 buf[idx] & 0xff); 76 buf[idx] & 0xff);
75 html(" </td><td class='hex'>"); 77 html(" </td><td class='hex'>");
76 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++) 78 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
77 ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.'; 79 ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
78 ascii[idx] = '\0'; 80 ascii[idx] = '\0';
79 html_txt(ascii); 81 html_txt(ascii);
80 html("</td></tr>\n"); 82 html("</td></tr>\n");
81 } 83 }
82 html("</table>\n"); 84 html("</table>\n");
83} 85}
84 86
85static void print_object(const unsigned char *sha1, char *path, const char *basename) 87static void print_object(const unsigned char *sha1, char *path, const char *basename)
86{ 88{
87 enum object_type type; 89 enum object_type type;
88 char *buf; 90 char *buf;
89 unsigned long size; 91 unsigned long size;
90 92
91 type = sha1_object_info(sha1, &size); 93 type = sha1_object_info(sha1, &size);
92 if (type == OBJ_BAD) { 94 if (type == OBJ_BAD) {
93 cgit_print_error(fmt("Bad object name: %s", 95 cgit_print_error(fmt("Bad object name: %s",
94 sha1_to_hex(sha1))); 96 sha1_to_hex(sha1)));
95 return; 97 return;
96 } 98 }
97 99
98 buf = read_sha1_file(sha1, &type, &size); 100 buf = read_sha1_file(sha1, &type, &size);
99 if (!buf) { 101 if (!buf) {
100 cgit_print_error(fmt("Error reading object %s", 102 cgit_print_error(fmt("Error reading object %s",
101 sha1_to_hex(sha1))); 103 sha1_to_hex(sha1)));
102 return; 104 return;
103 } 105 }
104 106
105 htmlf("blob: %s (", sha1_to_hex(sha1)); 107 htmlf("blob: %s (", sha1_to_hex(sha1));
106 cgit_plain_link("plain", NULL, NULL, ctx.qry.head, 108 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
107 curr_rev, path); 109 curr_rev, path);
108 html(")\n"); 110 html(")\n");
109 111
110 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) { 112 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
111 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>", 113 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
112 size / 1024, ctx.cfg.max_blob_size); 114 size / 1024, ctx.cfg.max_blob_size);
113 return; 115 return;
114 } 116 }
115 117
116 if (buffer_is_binary(buf, size)) 118 if (buffer_is_binary(buf, size))
117 print_binary_buffer(buf, size); 119 print_binary_buffer(buf, size);
118 else 120 else
119 print_text_buffer(basename, buf, size); 121 print_text_buffer(basename, buf, size);
120} 122}
121 123
122 124
123static int ls_item(const unsigned char *sha1, const char *base, int baselen, 125static int ls_item(const unsigned char *sha1, const char *base, int baselen,
124 const char *pathname, unsigned int mode, int stage, 126 const char *pathname, unsigned int mode, int stage,
125 void *cbdata) 127 void *cbdata)
126{ 128{
127 char *name; 129 char *name;
128 char *fullpath; 130 char *fullpath;
129 char *class; 131 char *class;
130 enum object_type type; 132 enum object_type type;
131 unsigned long size = 0; 133 unsigned long size = 0;
132 134
133 name = xstrdup(pathname); 135 name = xstrdup(pathname);
134 fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "", 136 fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
135 ctx.qry.path ? "/" : "", name); 137 ctx.qry.path ? "/" : "", name);
136 138
137 if (!S_ISGITLINK(mode)) { 139 if (!S_ISGITLINK(mode)) {
138 type = sha1_object_info(sha1, &size); 140 type = sha1_object_info(sha1, &size);
139 if (type == OBJ_BAD) { 141 if (type == OBJ_BAD) {
140 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>", 142 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
141 name, 143 name,
142 sha1_to_hex(sha1)); 144 sha1_to_hex(sha1));
143 return 0; 145 return 0;
144 } 146 }
145 } 147 }
146 148
147 html("<tr><td class='ls-mode'>"); 149 html("<tr><td class='ls-mode'>");
148 cgit_print_filemode(mode); 150 cgit_print_filemode(mode);
149 html("</td><td>"); 151 html("</td><td>");
150 if (S_ISGITLINK(mode)) { 152 if (S_ISGITLINK(mode)) {
151 htmlf("<a class='ls-mod' href='"); 153 htmlf("<a class='ls-mod' href='");
152 html_attr(fmt(ctx.repo->module_link, 154 html_attr(fmt(ctx.repo->module_link,
153 name, 155 name,
154 sha1_to_hex(sha1))); 156 sha1_to_hex(sha1)));
155 html("'>"); 157 html("'>");
156 html_txt(name); 158 html_txt(name);
157 html("</a>"); 159 html("</a>");
158 } else if (S_ISDIR(mode)) { 160 } else if (S_ISDIR(mode)) {
159 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, 161 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
160 curr_rev, fullpath); 162 curr_rev, fullpath);
161 } else { 163 } else {
162 class = strrchr(name, '.'); 164 class = strrchr(name, '.');
163 if (class != NULL) { 165 if (class != NULL) {
164 class = fmt("ls-blob %s", class + 1); 166 class = fmt("ls-blob %s", class + 1);
165 } else 167 } else
166 class = "ls-blob"; 168 class = "ls-blob";
167 cgit_tree_link(name, NULL, class, ctx.qry.head, 169 cgit_tree_link(name, NULL, class, ctx.qry.head,
168 curr_rev, fullpath); 170 curr_rev, fullpath);
169 } 171 }
170 htmlf("</td><td class='ls-size'>%li</td>", size); 172 htmlf("</td><td class='ls-size'>%li</td>", size);
171 173
172 html("<td>"); 174 html("<td>");
173 cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev, 175 cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev,
174 fullpath, 0, NULL, NULL, ctx.qry.showmsg); 176 fullpath, 0, NULL, NULL, ctx.qry.showmsg);
175 if (ctx.repo->max_stats) 177 if (ctx.repo->max_stats)
176 cgit_stats_link("stats", NULL, "button", ctx.qry.head, 178 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
177 fullpath); 179 fullpath);
178 cgit_plain_link("plain", NULL, "button", ctx.qry.head, curr_rev, 180 cgit_plain_link("plain", NULL, "button", ctx.qry.head, curr_rev,
179 fullpath); 181 fullpath);
180 html("</td></tr>\n"); 182 html("</td></tr>\n");
181 free(name); 183 free(name);
182 return 0; 184 return 0;
183} 185}
184 186
185static void ls_head() 187static void ls_head()
186{ 188{
187 html("<table summary='tree listing' class='list'>\n"); 189 html("<table summary='tree listing' class='list'>\n");
188 html("<tr class='nohover'>"); 190 html("<tr class='nohover'>");
189 html("<th class='left'>Mode</th>"); 191 html("<th class='left'>Mode</th>");
190 html("<th class='left'>Name</th>"); 192 html("<th class='left'>Name</th>");
191 html("<th class='right'>Size</th>"); 193 html("<th class='right'>Size</th>");
192 html("<th/>"); 194 html("<th/>");
193 html("</tr>\n"); 195 html("</tr>\n");
194 header = 1; 196 header = 1;
195} 197}
196 198
197static void ls_tail() 199static void ls_tail()
198{ 200{
199 if (!header) 201 if (!header)
200 return; 202 return;
201 html("</table>\n"); 203 html("</table>\n");
202 header = 0; 204 header = 0;
203} 205}
204 206
205static void ls_tree(const unsigned char *sha1, char *path) 207static void ls_tree(const unsigned char *sha1, char *path)
206{ 208{
207 struct tree *tree; 209 struct tree *tree;
208 210
209 tree = parse_tree_indirect(sha1); 211 tree = parse_tree_indirect(sha1);
210 if (!tree) { 212 if (!tree) {
211 cgit_print_error(fmt("Not a tree object: %s", 213 cgit_print_error(fmt("Not a tree object: %s",
212 sha1_to_hex(sha1))); 214 sha1_to_hex(sha1)));
213 return; 215 return;
214 } 216 }
215 217
216 ls_head(); 218 ls_head();
217 read_tree_recursive(tree, "", 0, 1, NULL, ls_item, NULL); 219 read_tree_recursive(tree, "", 0, 1, NULL, ls_item, NULL);
218 ls_tail(); 220 ls_tail();
219} 221}
220 222
221 223
222static int walk_tree(const unsigned char *sha1, const char *base, int baselen, 224static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
223 const char *pathname, unsigned mode, int stage, 225 const char *pathname, unsigned mode, int stage,
224 void *cbdata) 226 void *cbdata)
225{ 227{
226 static int state; 228 static int state;
227 static char buffer[PATH_MAX]; 229 static char buffer[PATH_MAX];
228 230
229 if (state == 0) { 231 if (state == 0) {
230 memcpy(buffer, base, baselen); 232 memcpy(buffer, base, baselen);
231 strcpy(buffer+baselen, pathname); 233 strcpy(buffer+baselen, pathname);
232 if (strcmp(match_path, buffer)) 234 if (strcmp(match_path, buffer))
233 return READ_TREE_RECURSIVE; 235 return READ_TREE_RECURSIVE;
234 236
235 if (S_ISDIR(mode)) { 237 if (S_ISDIR(mode)) {
236 state = 1; 238 state = 1;
237 ls_head(); 239 ls_head();
238 return READ_TREE_RECURSIVE; 240 return READ_TREE_RECURSIVE;
239 } else { 241 } else {
240 print_object(sha1, buffer, pathname); 242 print_object(sha1, buffer, pathname);
241 return 0; 243 return 0;
242 } 244 }