]> gitweb.ps.run Git - ps-cgit/blob - ui-plain.c
ui-refs.c: Refactor print_tag()
[ps-cgit] / ui-plain.c
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 <stdio.h>
10 #include "cgit.h"
11 #include "ui-plain.h"
12 #include "html.h"
13 #include "ui-shared.h"
14
15 struct walk_tree_context {
16         int match_baselen;
17         int match;
18 };
19
20 static char *get_mimetype_from_file(const char *filename, const char *ext)
21 {
22         static const char *delimiters;
23         char *result;
24         FILE *fd;
25         char line[1024];
26         char *mimetype;
27         char *token;
28
29         if (!filename)
30                 return NULL;
31
32         fd = fopen(filename, "r");
33         if (!fd)
34                 return NULL;
35
36         delimiters = " \t\r\n";
37         result = NULL;
38
39         /* loop over all lines in the file */
40         while (!result && fgets(line, sizeof(line), fd)) {
41                 mimetype = strtok(line, delimiters);
42
43                 /* skip empty lines and comment lines */
44                 if (!mimetype || (mimetype[0] == '#'))
45                         continue;
46
47                 /* loop over all extensions of mimetype */
48                 while ((token = strtok(NULL, delimiters))) {
49                         if (!strcasecmp(ext, token)) {
50                                 result = xstrdup(mimetype);
51                                 break;
52                         }
53                 }
54         }
55         fclose(fd);
56
57         return result;
58 }
59
60 static int print_object(const unsigned char *sha1, const char *path)
61 {
62         enum object_type type;
63         char *buf, *ext;
64         unsigned long size;
65         struct string_list_item *mime;
66         int freemime;
67
68         type = sha1_object_info(sha1, &size);
69         if (type == OBJ_BAD) {
70                 html_status(404, "Not found", 0);
71                 return 0;
72         }
73
74         buf = read_sha1_file(sha1, &type, &size);
75         if (!buf) {
76                 html_status(404, "Not found", 0);
77                 return 0;
78         }
79         ctx.page.mimetype = NULL;
80         ext = strrchr(path, '.');
81         freemime = 0;
82         if (ext && *(++ext)) {
83                 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
84                 if (mime) {
85                         ctx.page.mimetype = (char *)mime->util;
86                 } else {
87                         ctx.page.mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext);
88                         if (ctx.page.mimetype)
89                                 freemime = 1;
90                 }
91         }
92         if (!ctx.page.mimetype) {
93                 if (buffer_is_binary(buf, size))
94                         ctx.page.mimetype = "application/octet-stream";
95                 else
96                         ctx.page.mimetype = "text/plain";
97         }
98         ctx.page.filename = fmt("%s", path);
99         ctx.page.size = size;
100         ctx.page.etag = sha1_to_hex(sha1);
101         cgit_print_http_headers(&ctx);
102         html_raw(buf, size);
103         if (freemime)
104                 free(ctx.page.mimetype);
105         return 1;
106 }
107
108 static char *buildpath(const char *base, int baselen, const char *path)
109 {
110         if (path[0])
111                 return fmt("%.*s%s/", baselen, base, path);
112         else
113                 return fmt("%.*s/", baselen, base);
114 }
115
116 static void print_dir(const unsigned char *sha1, const char *base,
117                       int baselen, const char *path)
118 {
119         char *fullpath, *slash;
120         size_t len;
121
122         fullpath = buildpath(base, baselen, path);
123         slash = (fullpath[0] == '/' ? "" : "/");
124         ctx.page.etag = sha1_to_hex(sha1);
125         cgit_print_http_headers(&ctx);
126         htmlf("<html><head><title>%s", slash);
127         html_txt(fullpath);
128         htmlf("</title></head>\n<body>\n<h2>%s", slash);
129         html_txt(fullpath);
130         html("</h2>\n<ul>\n");
131         len = strlen(fullpath);
132         if (len > 1) {
133                 fullpath[len - 1] = 0;
134                 slash = strrchr(fullpath, '/');
135                 if (slash)
136                         *(slash + 1) = 0;
137                 else
138                         fullpath = NULL;
139                 html("<li>");
140                 cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
141                                 fullpath);
142                 html("</li>\n");
143         }
144 }
145
146 static void print_dir_entry(const unsigned char *sha1, const char *base,
147                             int baselen, const char *path, unsigned mode)
148 {
149         char *fullpath;
150
151         fullpath = buildpath(base, baselen, path);
152         if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
153                 fullpath[strlen(fullpath) - 1] = 0;
154         html("  <li>");
155         if (S_ISGITLINK(mode)) {
156                 cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1));
157         } else
158                 cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
159                                 fullpath);
160         html("</li>\n");
161 }
162
163 static void print_dir_tail(void)
164 {
165         html(" </ul>\n</body></html>\n");
166 }
167
168 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
169                      const char *pathname, unsigned mode, int stage,
170                      void *cbdata)
171 {
172         struct walk_tree_context *walk_tree_ctx = cbdata;
173
174         if (baselen == walk_tree_ctx->match_baselen) {
175                 if (S_ISREG(mode)) {
176                         if (print_object(sha1, pathname))
177                                 walk_tree_ctx->match = 1;
178                 } else if (S_ISDIR(mode)) {
179                         print_dir(sha1, base, baselen, pathname);
180                         walk_tree_ctx->match = 2;
181                         return READ_TREE_RECURSIVE;
182                 }
183         } else if (baselen > walk_tree_ctx->match_baselen) {
184                 print_dir_entry(sha1, base, baselen, pathname, mode);
185                 walk_tree_ctx->match = 2;
186         } else if (S_ISDIR(mode)) {
187                 return READ_TREE_RECURSIVE;
188         }
189
190         return 0;
191 }
192
193 static int basedir_len(const char *path)
194 {
195         char *p = strrchr(path, '/');
196         if (p)
197                 return p - path + 1;
198         return 0;
199 }
200
201 void cgit_print_plain(struct cgit_context *ctx)
202 {
203         const char *rev = ctx->qry.sha1;
204         unsigned char sha1[20];
205         struct commit *commit;
206         struct pathspec_item path_items = {
207                 .match = ctx->qry.path,
208                 .len = ctx->qry.path ? strlen(ctx->qry.path) : 0
209         };
210         struct pathspec paths = {
211                 .nr = 1,
212                 .items = &path_items
213         };
214         struct walk_tree_context walk_tree_ctx = {
215                 .match = 0
216         };
217
218         if (!rev)
219                 rev = ctx->qry.head;
220
221         if (get_sha1(rev, sha1)) {
222                 html_status(404, "Not found", 0);
223                 return;
224         }
225         commit = lookup_commit_reference(sha1);
226         if (!commit || parse_commit(commit)) {
227                 html_status(404, "Not found", 0);
228                 return;
229         }
230         if (!path_items.match) {
231                 path_items.match = "";
232                 walk_tree_ctx.match_baselen = -1;
233                 print_dir(commit->tree->object.sha1, "", 0, "");
234                 walk_tree_ctx.match = 2;
235         }
236         else
237                 walk_tree_ctx.match_baselen = basedir_len(path_items.match);
238         read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
239         if (!walk_tree_ctx.match)
240                 html_status(404, "Not found", 0);
241         else if (walk_tree_ctx.match == 2)
242                 print_dir_tail();
243 }