blob: 0dd1a8b9129b2ffcdc983d300f83e2ab066e6432 [file] [log] [blame]
Lars Hjemlie5da4bc2008-08-06 10:53:50 +02001/* ui-plain.c: functions for output of plain blobs by path
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemlie5da4bc2008-08-06 10:53:50 +02004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010010#include "ui-plain.h"
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020011#include "html.h"
12#include "ui-shared.h"
13
Lukas Fleischerb1db30c2013-03-03 17:27:54 +010014struct walk_tree_context {
15 int match_baselen;
16 int match;
17};
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020018
Lukas Fleischera6317502013-03-03 17:10:19 +010019static int print_object(const unsigned char *sha1, const char *path)
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020020{
21 enum object_type type;
Christian Hesseaa943bc2015-08-16 14:53:52 +020022 char *buf, *mimetype;
Ramsay Jonesbdd4a562008-11-04 19:22:08 +000023 unsigned long size;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020024
25 type = sha1_object_info(sha1, &size);
26 if (type == OBJ_BAD) {
John Keeping2b3e76a2015-08-14 12:47:04 +010027 cgit_print_error_page(404, "Not found", "Not found");
Lukas Fleischera6317502013-03-03 17:10:19 +010028 return 0;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020029 }
30
31 buf = read_sha1_file(sha1, &type, &size);
32 if (!buf) {
John Keeping2b3e76a2015-08-14 12:47:04 +010033 cgit_print_error_page(404, "Not found", "Not found");
Lukas Fleischera6317502013-03-03 17:10:19 +010034 return 0;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020035 }
Christian Hesseaa943bc2015-08-16 14:53:52 +020036
37 mimetype = get_mimetype_for_filename(path);
38 ctx.page.mimetype = mimetype;
39
Lars Hjemlic4d46c72009-02-13 20:43:30 +010040 if (!ctx.page.mimetype) {
John Keeping407f71c2013-10-06 12:14:41 +010041 if (buffer_is_binary(buf, size)) {
Lars Hjemlic4d46c72009-02-13 20:43:30 +010042 ctx.page.mimetype = "application/octet-stream";
John Keeping407f71c2013-10-06 12:14:41 +010043 ctx.page.charset = NULL;
44 } else {
Lars Hjemlic4d46c72009-02-13 20:43:30 +010045 ctx.page.mimetype = "text/plain";
John Keeping407f71c2013-10-06 12:14:41 +010046 }
Lars Hjemlic4d46c72009-02-13 20:43:30 +010047 }
John Keeping42d54762013-04-06 10:49:22 +010048 ctx.page.filename = path;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020049 ctx.page.size = size;
Lars Hjemli488a2142009-02-19 22:38:36 +010050 ctx.page.etag = sha1_to_hex(sha1);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010051 cgit_print_http_headers();
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020052 html_raw(buf, size);
Christian Hesseaa943bc2015-08-16 14:53:52 +020053 free(mimetype);
Lukas Fleischera6317502013-03-03 17:10:19 +010054 return 1;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +020055}
56
Lars Hjemli7f88d202011-06-12 20:49:35 +000057static char *buildpath(const char *base, int baselen, const char *path)
Mark Lodato6c1a7362010-01-31 14:25:03 -050058{
Lars Hjemli7f88d202011-06-12 20:49:35 +000059 if (path[0])
John Keepingfb3655d2013-04-06 10:28:57 +010060 return fmtalloc("%.*s%s/", baselen, base, path);
Mark Lodato6c1a7362010-01-31 14:25:03 -050061 else
John Keepingfb3655d2013-04-06 10:28:57 +010062 return fmtalloc("%.*s/", baselen, base);
Lars Hjemli7f88d202011-06-12 20:49:35 +000063}
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] == '/' ? "" : "/");
Mark Lodato6c1a7362010-01-31 14:25:03 -050073 ctx.page.etag = sha1_to_hex(sha1);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010074 cgit_print_http_headers();
Lars Hjemli7f88d202011-06-12 20:49:35 +000075 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 }
John Keepingfb3655d2013-04-06 10:28:57 +010093 free(fullpath);
Mark Lodato6c1a7362010-01-31 14:25:03 -050094}
95
Lars Hjemli7f88d202011-06-12 20:49:35 +000096static void print_dir_entry(const unsigned char *sha1, const char *base,
97 int baselen, const char *path, unsigned mode)
Mark Lodato6c1a7362010-01-31 14:25:03 -050098{
Lars Hjemli7f88d202011-06-12 20:49:35 +000099 char *fullpath;
100
101 fullpath = buildpath(base, baselen, path);
Lars Hjemli74218572011-06-15 10:10:41 +0200102 if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
Lars Hjemli7f88d202011-06-12 20:49:35 +0000103 fullpath[strlen(fullpath) - 1] = 0;
104 html(" <li>");
Lars Hjemli74218572011-06-15 10:10:41 +0200105 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);
Lars Hjemli7f88d202011-06-12 20:49:35 +0000110 html("</li>\n");
John Keepingfb3655d2013-04-06 10:28:57 +0100111 free(fullpath);
Mark Lodato6c1a7362010-01-31 14:25:03 -0500112}
113
114static void print_dir_tail(void)
115{
116 html(" </ul>\n</body></html>\n");
117}
118
Christian Hesse7358f632015-02-07 14:18:28 +0100119static int walk_tree(const unsigned char *sha1, struct strbuf *base,
120 const char *pathname, unsigned mode, int stage, void *cbdata)
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200121{
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100122 struct walk_tree_context *walk_tree_ctx = cbdata;
123
Christian Hesse7358f632015-02-07 14:18:28 +0100124 if (base->len == walk_tree_ctx->match_baselen) {
Lukas Fleischera6317502013-03-03 17:10:19 +0100125 if (S_ISREG(mode)) {
126 if (print_object(sha1, pathname))
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100127 walk_tree_ctx->match = 1;
Lukas Fleischera6317502013-03-03 17:10:19 +0100128 } else if (S_ISDIR(mode)) {
Christian Hesse7358f632015-02-07 14:18:28 +0100129 print_dir(sha1, base->buf, base->len, pathname);
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100130 walk_tree_ctx->match = 2;
Mark Lodato6c1a7362010-01-31 14:25:03 -0500131 return READ_TREE_RECURSIVE;
132 }
Christian Hesse7358f632015-02-07 14:18:28 +0100133 } else if (base->len > walk_tree_ctx->match_baselen) {
134 print_dir_entry(sha1, base->buf, base->len, pathname, mode);
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100135 walk_tree_ctx->match = 2;
Lukas Fleischera6317502013-03-03 17:10:19 +0100136 } else if (S_ISDIR(mode)) {
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200137 return READ_TREE_RECURSIVE;
Lukas Fleischera6317502013-03-03 17:10:19 +0100138 }
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200139
Mark Lodato74ebf822010-01-31 01:07:41 -0500140 return 0;
141}
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200142
Mark Lodato74ebf822010-01-31 01:07:41 -0500143static int basedir_len(const char *path)
144{
145 char *p = strrchr(path, '/');
146 if (p)
147 return p - path + 1;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200148 return 0;
149}
150
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100151void cgit_print_plain(void)
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200152{
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100153 const char *rev = ctx.qry.sha1;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200154 unsigned char sha1[20];
155 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +0000156 struct pathspec_item path_items = {
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100157 .match = ctx.qry.path,
158 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
John Keepingc1633c62013-03-02 12:32:11 +0000159 };
160 struct pathspec paths = {
161 .nr = 1,
162 .items = &path_items
163 };
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100164 struct walk_tree_context walk_tree_ctx = {
165 .match = 0
166 };
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200167
168 if (!rev)
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100169 rev = ctx.qry.head;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200170
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200171 if (get_sha1(rev, sha1)) {
John Keeping2b3e76a2015-08-14 12:47:04 +0100172 cgit_print_error_page(404, "Not found", "Not found");
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200173 return;
174 }
175 commit = lookup_commit_reference(sha1);
176 if (!commit || parse_commit(commit)) {
John Keeping2b3e76a2015-08-14 12:47:04 +0100177 cgit_print_error_page(404, "Not found", "Not found");
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200178 return;
179 }
John Keepingc1633c62013-03-02 12:32:11 +0000180 if (!path_items.match) {
181 path_items.match = "";
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100182 walk_tree_ctx.match_baselen = -1;
Lars Hjemli7f88d202011-06-12 20:49:35 +0000183 print_dir(commit->tree->object.sha1, "", 0, "");
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100184 walk_tree_ctx.match = 2;
Mark Lodato6c1a7362010-01-31 14:25:03 -0500185 }
186 else
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100187 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)
John Keeping2b3e76a2015-08-14 12:47:04 +0100190 cgit_print_error_page(404, "Not found", "Not found");
Lukas Fleischerb1db30c2013-03-03 17:27:54 +0100191 else if (walk_tree_ctx.match == 2)
Mark Lodato6c1a7362010-01-31 14:25:03 -0500192 print_dir_tail();
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200193}