ui-plain.c (view raw)
1/* ui-plain.c: functions for output of plain blobs by path
2 *
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
10#include "ui-plain.h"
11#include "html.h"
12#include "ui-shared.h"
13
14struct walk_tree_context {
15 int match_baselen;
16 int match;
17};
18
19static int print_object(const unsigned char *sha1, const char *path)
20{
21 enum object_type type;
22 char *buf, *mimetype;
23 unsigned long size;
24
25 type = sha1_object_info(sha1, &size);
26 if (type == OBJ_BAD) {
27 cgit_print_error_page(404, "Not found", "Not found");
28 return 0;
29 }
30
31 buf = read_sha1_file(sha1, &type, &size);
32 if (!buf) {
33 cgit_print_error_page(404, "Not found", "Not found");
34 return 0;
35 }
36
37 mimetype = get_mimetype_for_filename(path);
38 ctx.page.mimetype = mimetype;
39
40 if (!ctx.page.mimetype) {
41 if (buffer_is_binary(buf, size)) {
42 ctx.page.mimetype = "application/octet-stream";
43 ctx.page.charset = NULL;
44 } else {
45 ctx.page.mimetype = "text/plain";
46 }
47 }
48 ctx.page.filename = path;
49 ctx.page.size = size;
50 ctx.page.etag = sha1_to_hex(sha1);
51 cgit_print_http_headers();
52 html_raw(buf, size);
53 free(mimetype);
54 free(buf);
55 return 1;
56}
57
58static char *buildpath(const char *base, int baselen, const char *path)
59{
60 if (path[0])
61 return fmtalloc("%.*s%s/", baselen, base, path);
62 else
63 return fmtalloc("%.*s/", baselen, base);
64}
65
66static void print_dir(const unsigned char *sha1, const char *base,
67 int baselen, const char *path)
68{
69 char *fullpath, *slash;
70 size_t len;
71
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);
77 html_txt(fullpath);
78 htmlf("</title></head>\n<body>\n<h2>%s", slash);
79 html_txt(fullpath);
80 html("</h2>\n<ul>\n");
81 len = strlen(fullpath);
82 if (len > 1) {
83 fullpath[len - 1] = 0;
84 slash = strrchr(fullpath, '/');
85 if (slash)
86 *(slash + 1) = 0;
87 else
88 fullpath = NULL;
89 html("<li>");
90 cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
91 fullpath);
92 html("</li>\n");
93 }
94 free(fullpath);
95}
96
97static void print_dir_entry(const unsigned char *sha1, const char *base,
98 int baselen, const char *path, unsigned mode)
99{
100 char *fullpath;
101
102 fullpath = buildpath(base, baselen, path);
103 if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
104 fullpath[strlen(fullpath) - 1] = 0;
105 html(" <li>");
106 if (S_ISGITLINK(mode)) {
107 cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1));
108 } else
109 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
110 fullpath);
111 html("</li>\n");
112 free(fullpath);
113}
114
115static void print_dir_tail(void)
116{
117 html(" </ul>\n</body></html>\n");
118}
119
120static int walk_tree(const unsigned char *sha1, struct strbuf *base,
121 const char *pathname, unsigned mode, int stage, void *cbdata)
122{
123 struct walk_tree_context *walk_tree_ctx = cbdata;
124
125 if (base->len == walk_tree_ctx->match_baselen) {
126 if (S_ISREG(mode)) {
127 if (print_object(sha1, pathname))
128 walk_tree_ctx->match = 1;
129 } else if (S_ISDIR(mode)) {
130 print_dir(sha1, base->buf, base->len, pathname);
131 walk_tree_ctx->match = 2;
132 return READ_TREE_RECURSIVE;
133 }
134 } else if (base->len > walk_tree_ctx->match_baselen) {
135 print_dir_entry(sha1, base->buf, base->len, pathname, mode);
136 walk_tree_ctx->match = 2;
137 } else if (S_ISDIR(mode)) {
138 return READ_TREE_RECURSIVE;
139 }
140
141 return 0;
142}
143
144static int basedir_len(const char *path)
145{
146 char *p = strrchr(path, '/');
147 if (p)
148 return p - path + 1;
149 return 0;
150}
151
152void cgit_print_plain(void)
153{
154 const char *rev = ctx.qry.sha1;
155 unsigned char sha1[20];
156 struct commit *commit;
157 struct pathspec_item path_items = {
158 .match = ctx.qry.path,
159 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
160 };
161 struct pathspec paths = {
162 .nr = 1,
163 .items = &path_items
164 };
165 struct walk_tree_context walk_tree_ctx = {
166 .match = 0
167 };
168
169 if (!rev)
170 rev = ctx.qry.head;
171
172 if (get_sha1(rev, sha1)) {
173 cgit_print_error_page(404, "Not found", "Not found");
174 return;
175 }
176 commit = lookup_commit_reference(sha1);
177 if (!commit || parse_commit(commit)) {
178 cgit_print_error_page(404, "Not found", "Not found");
179 return;
180 }
181 if (!path_items.match) {
182 path_items.match = "";
183 walk_tree_ctx.match_baselen = -1;
184 print_dir(commit->tree->object.sha1, "", 0, "");
185 walk_tree_ctx.match = 2;
186 }
187 else
188 walk_tree_ctx.match_baselen = basedir_len(path_items.match);
189 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
190 if (!walk_tree_ctx.match)
191 cgit_print_error_page(404, "Not found", "Not found");
192 else if (walk_tree_ctx.match == 2)
193 print_dir_tail();
194}