all repos — cgit @ f5c83d7b5ddceb03e1c6bda2e43c48500c7da9f5

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, *ext;
 23	unsigned long size;
 24	struct string_list_item *mime;
 25	int freemime;
 26
 27	type = sha1_object_info(sha1, &size);
 28	if (type == OBJ_BAD) {
 29		cgit_print_error_page(404, "Not found", "Not found");
 30		return 0;
 31	}
 32
 33	buf = read_sha1_file(sha1, &type, &size);
 34	if (!buf) {
 35		cgit_print_error_page(404, "Not found", "Not found");
 36		return 0;
 37	}
 38	ctx.page.mimetype = NULL;
 39	ext = strrchr(path, '.');
 40	freemime = 0;
 41	if (ext && *(++ext)) {
 42		mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
 43		if (mime) {
 44			ctx.page.mimetype = (char *)mime->util;
 45			ctx.page.charset = NULL;
 46		} else {
 47			ctx.page.mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext);
 48			if (ctx.page.mimetype) {
 49				freemime = 1;
 50				ctx.page.charset = NULL;
 51			}
 52		}
 53	}
 54	if (!ctx.page.mimetype) {
 55		if (buffer_is_binary(buf, size)) {
 56			ctx.page.mimetype = "application/octet-stream";
 57			ctx.page.charset = NULL;
 58		} else {
 59			ctx.page.mimetype = "text/plain";
 60		}
 61	}
 62	ctx.page.filename = path;
 63	ctx.page.size = size;
 64	ctx.page.etag = sha1_to_hex(sha1);
 65	cgit_print_http_headers();
 66	html_raw(buf, size);
 67	/* If we allocated this, then casting away const is safe. */
 68	if (freemime)
 69		free((char*) ctx.page.mimetype);
 70	return 1;
 71}
 72
 73static char *buildpath(const char *base, int baselen, const char *path)
 74{
 75	if (path[0])
 76		return fmtalloc("%.*s%s/", baselen, base, path);
 77	else
 78		return fmtalloc("%.*s/", baselen, base);
 79}
 80
 81static void print_dir(const unsigned char *sha1, const char *base,
 82		      int baselen, const char *path)
 83{
 84	char *fullpath, *slash;
 85	size_t len;
 86
 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);
 92	html_txt(fullpath);
 93	htmlf("</title></head>\n<body>\n<h2>%s", slash);
 94	html_txt(fullpath);
 95	html("</h2>\n<ul>\n");
 96	len = strlen(fullpath);
 97	if (len > 1) {
 98		fullpath[len - 1] = 0;
 99		slash = strrchr(fullpath, '/');
100		if (slash)
101			*(slash + 1) = 0;
102		else
103			fullpath = NULL;
104		html("<li>");
105		cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
106				fullpath);
107		html("</li>\n");
108	}
109	free(fullpath);
110}
111
112static void print_dir_entry(const unsigned char *sha1, const char *base,
113			    int baselen, const char *path, unsigned mode)
114{
115	char *fullpath;
116
117	fullpath = buildpath(base, baselen, path);
118	if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
119		fullpath[strlen(fullpath) - 1] = 0;
120	html("  <li>");
121	if (S_ISGITLINK(mode)) {
122		cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1));
123	} else
124		cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
125				fullpath);
126	html("</li>\n");
127	free(fullpath);
128}
129
130static void print_dir_tail(void)
131{
132	html(" </ul>\n</body></html>\n");
133}
134
135static int walk_tree(const unsigned char *sha1, struct strbuf *base,
136		const char *pathname, unsigned mode, int stage, void *cbdata)
137{
138	struct walk_tree_context *walk_tree_ctx = cbdata;
139
140	if (base->len == walk_tree_ctx->match_baselen) {
141		if (S_ISREG(mode)) {
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;
148		}
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;
154	}
155
156	return 0;
157}
158
159static int basedir_len(const char *path)
160{
161	char *p = strrchr(path, '/');
162	if (p)
163		return p - path + 1;
164	return 0;
165}
166
167void cgit_print_plain(void)
168{
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
175	};
176	struct pathspec paths = {
177		.nr = 1,
178		.items = &path_items
179	};
180	struct walk_tree_context walk_tree_ctx = {
181		.match = 0
182	};
183
184	if (!rev)
185		rev = ctx.qry.head;
186
187	if (get_sha1(rev, sha1)) {
188		cgit_print_error_page(404, "Not found", "Not found");
189		return;
190	}
191	commit = lookup_commit_reference(sha1);
192	if (!commit || parse_commit(commit)) {
193		cgit_print_error_page(404, "Not found", "Not found");
194		return;
195	}
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;
201	}
202	else
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)
208		print_dir_tail();
209}