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, '/');
90 cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
97 static void print_dir_entry(const unsigned char *sha1, const char *base,
98 int baselen, const char *path, unsigned mode)
102 fullpath = buildpath(base, baselen, path);
103 if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
104 fullpath[strlen(fullpath) - 1] = 0;
106 if (S_ISGITLINK(mode)) {
107 cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1));
109 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
115 static void print_dir_tail(void)
117 html(" </ul>\n</body></html>\n");
120 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
121 const char *pathname, unsigned mode, int stage, void *cbdata)
123 struct walk_tree_context *walk_tree_ctx = cbdata;
125 if (base->len == walk_tree_ctx->match_baselen) {
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;
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;
144 static int basedir_len(const char *path)
146 char *p = strrchr(path, '/');
152 void cgit_print_plain(void)
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
161 struct pathspec paths = {
165 struct walk_tree_context walk_tree_ctx = {
172 if (get_sha1(rev, sha1)) {
173 cgit_print_error_page(404, "Not found", "Not found");
176 commit = lookup_commit_reference(sha1);
177 if (!commit || parse_commit(commit)) {
178 cgit_print_error_page(404, "Not found", "Not found");
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;
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)