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();
57 static char *buildpath(const char *base, int baselen, const char *path)
60 return fmtalloc("%.*s%s/", baselen, base, path);
62 return fmtalloc("%.*s/", baselen, base);
65 static void print_dir(const unsigned char *sha1, const char *base,
66 int baselen, const char *path)
68 char *fullpath, *slash;
71 fullpath = buildpath(base, baselen, path);
72 slash = (fullpath[0] == '/' ? "" : "/");
73 ctx.page.etag = sha1_to_hex(sha1);
74 cgit_print_http_headers();
75 htmlf("<html><head><title>%s", slash);
77 htmlf("</title></head>\n<body>\n<h2>%s", slash);
79 html("</h2>\n<ul>\n");
80 len = strlen(fullpath);
82 fullpath[len - 1] = 0;
83 slash = strrchr(fullpath, '/');
89 cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
96 static void print_dir_entry(const unsigned char *sha1, const char *base,
97 int baselen, const char *path, unsigned mode)
101 fullpath = buildpath(base, baselen, path);
102 if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
103 fullpath[strlen(fullpath) - 1] = 0;
105 if (S_ISGITLINK(mode)) {
106 cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1));
108 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
114 static void print_dir_tail(void)
116 html(" </ul>\n</body></html>\n");
119 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
120 const char *pathname, unsigned mode, int stage, void *cbdata)
122 struct walk_tree_context *walk_tree_ctx = cbdata;
124 if (base->len == walk_tree_ctx->match_baselen) {
126 if (print_object(sha1, pathname))
127 walk_tree_ctx->match = 1;
128 } else if (S_ISDIR(mode)) {
129 print_dir(sha1, base->buf, base->len, pathname);
130 walk_tree_ctx->match = 2;
131 return READ_TREE_RECURSIVE;
133 } else if (base->len > walk_tree_ctx->match_baselen) {
134 print_dir_entry(sha1, base->buf, base->len, pathname, mode);
135 walk_tree_ctx->match = 2;
136 } else if (S_ISDIR(mode)) {
137 return READ_TREE_RECURSIVE;
143 static int basedir_len(const char *path)
145 char *p = strrchr(path, '/');
151 void cgit_print_plain(void)
153 const char *rev = ctx.qry.sha1;
154 unsigned char sha1[20];
155 struct commit *commit;
156 struct pathspec_item path_items = {
157 .match = ctx.qry.path,
158 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
160 struct pathspec paths = {
164 struct walk_tree_context walk_tree_ctx = {
171 if (get_sha1(rev, sha1)) {
172 cgit_print_error_page(404, "Not found", "Not found");
175 commit = lookup_commit_reference(sha1);
176 if (!commit || parse_commit(commit)) {
177 cgit_print_error_page(404, "Not found", "Not found");
180 if (!path_items.match) {
181 path_items.match = "";
182 walk_tree_ctx.match_baselen = -1;
183 print_dir(commit->tree->object.sha1, "", 0, "");
184 walk_tree_ctx.match = 2;
187 walk_tree_ctx.match_baselen = basedir_len(path_items.match);
188 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
189 if (!walk_tree_ctx.match)
190 cgit_print_error_page(404, "Not found", "Not found");
191 else if (walk_tree_ctx.match == 2)