all repos — cgit @ 6f2e4400faebd829f905c824400f933fe07f5c30

a hyperfast web frontend for git written in c

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	return 1;
 55}
 56
 57static char *buildpath(const char *base, int baselen, const char *path)
 58{
 59	if (path[0])
 60		return fmtalloc("%.*s%s/", baselen, base, path);
 61	else
 62		return fmtalloc("%.*s/", baselen, base);
 63}
 64
 65static void print_dir(const unsigned char *sha1, const char *base,
 66		      int baselen, const char *path)
 67{
 68	char *fullpath, *slash;
 69	size_t len;
 70
 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);
 76	html_txt(fullpath);
 77	htmlf("</title></head>\n<body>\n<h2>%s", slash);
 78	html_txt(fullpath);
 79	html("</h2>\n<ul>\n");
 80	len = strlen(fullpath);
 81	if (len > 1) {
 82		fullpath[len - 1] = 0;
 83		slash = strrchr(fullpath, '/');
 84		if (slash)
 85			*(slash + 1) = 0;
 86		else
 87			fullpath = NULL;
 88		html("<li>");
 89		cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
 90				fullpath);
 91		html("</li>\n");
 92	}
 93	free(fullpath);
 94}
 95
 96static void print_dir_entry(const unsigned char *sha1, const char *base,
 97			    int baselen, const char *path, unsigned mode)
 98{
 99	char *fullpath;
100
101	fullpath = buildpath(base, baselen, path);
102	if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
103		fullpath[strlen(fullpath) - 1] = 0;
104	html("  <li>");
105	if (S_ISGITLINK(mode)) {
106		cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1));
107	} else
108		cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
109				fullpath);
110	html("</li>\n");
111	free(fullpath);
112}
113
114static void print_dir_tail(void)
115{
116	html(" </ul>\n</body></html>\n");
117}
118
119static int walk_tree(const unsigned char *sha1, struct strbuf *base,
120		const char *pathname, unsigned mode, int stage, void *cbdata)
121{
122	struct walk_tree_context *walk_tree_ctx = cbdata;
123
124	if (base->len == walk_tree_ctx->match_baselen) {
125		if (S_ISREG(mode)) {
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;
132		}
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;
138	}
139
140	return 0;
141}
142
143static int basedir_len(const char *path)
144{
145	char *p = strrchr(path, '/');
146	if (p)
147		return p - path + 1;
148	return 0;
149}
150
151void cgit_print_plain(void)
152{
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
159	};
160	struct pathspec paths = {
161		.nr = 1,
162		.items = &path_items
163	};
164	struct walk_tree_context walk_tree_ctx = {
165		.match = 0
166	};
167
168	if (!rev)
169		rev = ctx.qry.head;
170
171	if (get_sha1(rev, sha1)) {
172		cgit_print_error_page(404, "Not found", "Not found");
173		return;
174	}
175	commit = lookup_commit_reference(sha1);
176	if (!commit || parse_commit(commit)) {
177		cgit_print_error_page(404, "Not found", "Not found");
178		return;
179	}
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;
185	}
186	else
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)
192		print_dir_tail();
193}