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;
24 struct string_list_item *mime;
27 type = sha1_object_info(sha1, &size);
28 if (type == OBJ_BAD) {
29 cgit_print_error_page(404, "Not found", "Not found");
33 buf = read_sha1_file(sha1, &type, &size);
35 cgit_print_error_page(404, "Not found", "Not found");
38 ctx.page.mimetype = NULL;
39 ext = strrchr(path, '.');
41 if (ext && *(++ext)) {
42 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
44 ctx.page.mimetype = (char *)mime->util;
45 ctx.page.charset = NULL;
47 ctx.page.mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext);
48 if (ctx.page.mimetype) {
50 ctx.page.charset = NULL;
54 if (!ctx.page.mimetype) {
55 if (buffer_is_binary(buf, size)) {
56 ctx.page.mimetype = "application/octet-stream";
57 ctx.page.charset = NULL;
59 ctx.page.mimetype = "text/plain";
62 ctx.page.filename = path;
64 ctx.page.etag = sha1_to_hex(sha1);
65 cgit_print_http_headers();
67 /* If we allocated this, then casting away const is safe. */
69 free((char*) ctx.page.mimetype);
73 static char *buildpath(const char *base, int baselen, const char *path)
76 return fmtalloc("%.*s%s/", baselen, base, path);
78 return fmtalloc("%.*s/", baselen, base);
81 static void print_dir(const unsigned char *sha1, const char *base,
82 int baselen, const char *path)
84 char *fullpath, *slash;
87 fullpath = buildpath(base, baselen, path);
88 slash = (fullpath[0] == '/' ? "" : "/");
89 ctx.page.etag = sha1_to_hex(sha1);
90 cgit_print_http_headers();
91 htmlf("<html><head><title>%s", slash);
93 htmlf("</title></head>\n<body>\n<h2>%s", slash);
95 html("</h2>\n<ul>\n");
96 len = strlen(fullpath);
98 fullpath[len - 1] = 0;
99 slash = strrchr(fullpath, '/');
105 cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
112 static void print_dir_entry(const unsigned char *sha1, const char *base,
113 int baselen, const char *path, unsigned mode)
117 fullpath = buildpath(base, baselen, path);
118 if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
119 fullpath[strlen(fullpath) - 1] = 0;
121 if (S_ISGITLINK(mode)) {
122 cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1));
124 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
130 static void print_dir_tail(void)
132 html(" </ul>\n</body></html>\n");
135 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
136 const char *pathname, unsigned mode, int stage, void *cbdata)
138 struct walk_tree_context *walk_tree_ctx = cbdata;
140 if (base->len == walk_tree_ctx->match_baselen) {
142 if (print_object(sha1, pathname))
143 walk_tree_ctx->match = 1;
144 } else if (S_ISDIR(mode)) {
145 print_dir(sha1, base->buf, base->len, pathname);
146 walk_tree_ctx->match = 2;
147 return READ_TREE_RECURSIVE;
149 } else if (base->len > walk_tree_ctx->match_baselen) {
150 print_dir_entry(sha1, base->buf, base->len, pathname, mode);
151 walk_tree_ctx->match = 2;
152 } else if (S_ISDIR(mode)) {
153 return READ_TREE_RECURSIVE;
159 static int basedir_len(const char *path)
161 char *p = strrchr(path, '/');
167 void cgit_print_plain(void)
169 const char *rev = ctx.qry.sha1;
170 unsigned char sha1[20];
171 struct commit *commit;
172 struct pathspec_item path_items = {
173 .match = ctx.qry.path,
174 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
176 struct pathspec paths = {
180 struct walk_tree_context walk_tree_ctx = {
187 if (get_sha1(rev, sha1)) {
188 cgit_print_error_page(404, "Not found", "Not found");
191 commit = lookup_commit_reference(sha1);
192 if (!commit || parse_commit(commit)) {
193 cgit_print_error_page(404, "Not found", "Not found");
196 if (!path_items.match) {
197 path_items.match = "";
198 walk_tree_ctx.match_baselen = -1;
199 print_dir(commit->tree->object.sha1, "", 0, "");
200 walk_tree_ctx.match = 2;
203 walk_tree_ctx.match_baselen = basedir_len(path_items.match);
204 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
205 if (!walk_tree_ctx.match)
206 cgit_print_error_page(404, "Not found", "Not found");
207 else if (walk_tree_ctx.match == 2)