ui-plain.c (view raw)
1/* ui-plain.c: functions for output of plain blobs by path
2 *
3 * Copyright (C) 2008 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
10#include "html.h"
11#include "ui-shared.h"
12
13int match_baselen;
14int match;
15
16static void print_object(const unsigned char *sha1, const char *path)
17{
18 enum object_type type;
19 char *buf, *ext;
20 unsigned long size;
21 struct string_list_item *mime;
22
23 type = sha1_object_info(sha1, &size);
24 if (type == OBJ_BAD) {
25 html_status(404, "Not found", 0);
26 return;
27 }
28
29 buf = read_sha1_file(sha1, &type, &size);
30 if (!buf) {
31 html_status(404, "Not found", 0);
32 return;
33 }
34 ctx.page.mimetype = NULL;
35 ext = strrchr(path, '.');
36 if (ext && *(++ext)) {
37 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
38 if (mime)
39 ctx.page.mimetype = (char *)mime->util;
40 }
41 if (!ctx.page.mimetype) {
42 if (buffer_is_binary(buf, size))
43 ctx.page.mimetype = "application/octet-stream";
44 else
45 ctx.page.mimetype = "text/plain";
46 }
47 ctx.page.filename = fmt("%s", path);
48 ctx.page.size = size;
49 ctx.page.etag = sha1_to_hex(sha1);
50 cgit_print_http_headers(&ctx);
51 html_raw(buf, size);
52 match = 1;
53}
54
55static char *buildpath(const char *base, int baselen, const char *path)
56{
57 if (path[0])
58 return fmt("%.*s%s/", baselen, base, path);
59 else
60 return fmt("%.*s/", baselen, base);
61}
62
63static void print_dir(const unsigned char *sha1, const char *base,
64 int baselen, const char *path)
65{
66 char *fullpath, *slash;
67 size_t len;
68
69 fullpath = buildpath(base, baselen, path);
70 slash = (fullpath[0] == '/' ? "" : "/");
71 ctx.page.etag = sha1_to_hex(sha1);
72 cgit_print_http_headers(&ctx);
73 htmlf("<html><head><title>%s", slash);
74 html_txt(fullpath);
75 htmlf("</title></head>\n<body>\n<h2>%s", slash);
76 html_txt(fullpath);
77 html("</h2>\n<ul>\n");
78 len = strlen(fullpath);
79 if (len > 1) {
80 fullpath[len - 1] = 0;
81 slash = strrchr(fullpath, '/');
82 if (slash)
83 *(slash + 1) = 0;
84 else
85 fullpath = NULL;
86 html("<li>");
87 cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
88 fullpath);
89 html("</li>\n");
90 }
91 match = 2;
92}
93
94static void print_dir_entry(const unsigned char *sha1, const char *base,
95 int baselen, const char *path, unsigned mode)
96{
97 char *fullpath;
98
99 fullpath = buildpath(base, baselen, path);
100 if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
101 fullpath[strlen(fullpath) - 1] = 0;
102 html(" <li>");
103 if (S_ISGITLINK(mode)) {
104 cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1));
105 } else
106 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
107 fullpath);
108 html("</li>\n");
109 match = 2;
110}
111
112static void print_dir_tail(void)
113{
114 html(" </ul>\n</body></html>\n");
115}
116
117static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
118 const char *pathname, unsigned mode, int stage,
119 void *cbdata)
120{
121 if (baselen == match_baselen) {
122 if (S_ISREG(mode))
123 print_object(sha1, pathname);
124 else if (S_ISDIR(mode)) {
125 print_dir(sha1, base, baselen, pathname);
126 return READ_TREE_RECURSIVE;
127 }
128 }
129 else if (baselen > match_baselen)
130 print_dir_entry(sha1, base, baselen, pathname, mode);
131 else if (S_ISDIR(mode))
132 return READ_TREE_RECURSIVE;
133
134 return 0;
135}
136
137static int basedir_len(const char *path)
138{
139 char *p = strrchr(path, '/');
140 if (p)
141 return p - path + 1;
142 return 0;
143}
144
145void cgit_print_plain(struct cgit_context *ctx)
146{
147 const char *rev = ctx->qry.sha1;
148 unsigned char sha1[20];
149 struct commit *commit;
150 const char *paths[] = {ctx->qry.path, NULL};
151
152 if (!rev)
153 rev = ctx->qry.head;
154
155 if (get_sha1(rev, sha1)) {
156 html_status(404, "Not found", 0);
157 return;
158 }
159 commit = lookup_commit_reference(sha1);
160 if (!commit || parse_commit(commit)) {
161 html_status(404, "Not found", 0);
162 return;
163 }
164 if (!paths[0]) {
165 paths[0] = "";
166 match_baselen = -1;
167 print_dir(commit->tree->object.sha1, "", 0, "");
168 }
169 else
170 match_baselen = basedir_len(paths[0]);
171 read_tree_recursive(commit->tree, "", 0, 0, paths, walk_tree, NULL);
172 if (!match)
173 html_status(404, "Not found", 0);
174 else if (match == 2)
175 print_dir_tail();
176}