Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 1 | /* ui-plain.c: functions for output of plain blobs by path |
| 2 | * |
Lukas Fleischer | f7f26f8 | 2014-01-08 15:10:49 +0100 | [diff] [blame] | 3 | * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
| 9 | #include "cgit.h" |
John Keeping | 8f20879 | 2013-04-06 11:37:59 +0100 | [diff] [blame] | 10 | #include "ui-plain.h" |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 11 | #include "html.h" |
| 12 | #include "ui-shared.h" |
| 13 | |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 14 | struct walk_tree_context { |
| 15 | int match_baselen; |
| 16 | int match; |
| 17 | }; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 18 | |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 19 | static int print_object(const unsigned char *sha1, const char *path) |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 20 | { |
| 21 | enum object_type type; |
Christian Hesse | aa943bc | 2015-08-16 14:53:52 +0200 | [diff] [blame] | 22 | char *buf, *mimetype; |
Ramsay Jones | bdd4a56 | 2008-11-04 19:22:08 +0000 | [diff] [blame] | 23 | unsigned long size; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 24 | |
| 25 | type = sha1_object_info(sha1, &size); |
| 26 | if (type == OBJ_BAD) { |
John Keeping | 2b3e76a | 2015-08-14 12:47:04 +0100 | [diff] [blame] | 27 | cgit_print_error_page(404, "Not found", "Not found"); |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 28 | return 0; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | buf = read_sha1_file(sha1, &type, &size); |
| 32 | if (!buf) { |
John Keeping | 2b3e76a | 2015-08-14 12:47:04 +0100 | [diff] [blame] | 33 | cgit_print_error_page(404, "Not found", "Not found"); |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 34 | return 0; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 35 | } |
Christian Hesse | aa943bc | 2015-08-16 14:53:52 +0200 | [diff] [blame] | 36 | |
| 37 | mimetype = get_mimetype_for_filename(path); |
| 38 | ctx.page.mimetype = mimetype; |
| 39 | |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 40 | if (!ctx.page.mimetype) { |
John Keeping | 407f71c | 2013-10-06 12:14:41 +0100 | [diff] [blame] | 41 | if (buffer_is_binary(buf, size)) { |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 42 | ctx.page.mimetype = "application/octet-stream"; |
John Keeping | 407f71c | 2013-10-06 12:14:41 +0100 | [diff] [blame] | 43 | ctx.page.charset = NULL; |
| 44 | } else { |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 45 | ctx.page.mimetype = "text/plain"; |
John Keeping | 407f71c | 2013-10-06 12:14:41 +0100 | [diff] [blame] | 46 | } |
Lars Hjemli | c4d46c7 | 2009-02-13 20:43:30 +0100 | [diff] [blame] | 47 | } |
John Keeping | 42d5476 | 2013-04-06 10:49:22 +0100 | [diff] [blame] | 48 | ctx.page.filename = path; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 49 | ctx.page.size = size; |
Lars Hjemli | 488a214 | 2009-02-19 22:38:36 +0100 | [diff] [blame] | 50 | ctx.page.etag = sha1_to_hex(sha1); |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 51 | cgit_print_http_headers(); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 52 | html_raw(buf, size); |
Christian Hesse | aa943bc | 2015-08-16 14:53:52 +0200 | [diff] [blame] | 53 | free(mimetype); |
Christian Hesse | 979db79 | 2015-10-09 14:55:49 +0200 | [diff] [blame] | 54 | free(buf); |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 55 | return 1; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 58 | static char *buildpath(const char *base, int baselen, const char *path) |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 59 | { |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 60 | if (path[0]) |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 61 | return fmtalloc("%.*s%s/", baselen, base, path); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 62 | else |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 63 | return fmtalloc("%.*s/", baselen, base); |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static void print_dir(const unsigned char *sha1, const char *base, |
| 67 | int baselen, const char *path) |
| 68 | { |
| 69 | char *fullpath, *slash; |
| 70 | size_t len; |
| 71 | |
| 72 | fullpath = buildpath(base, baselen, path); |
| 73 | slash = (fullpath[0] == '/' ? "" : "/"); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 74 | ctx.page.etag = sha1_to_hex(sha1); |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 75 | cgit_print_http_headers(); |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 76 | htmlf("<html><head><title>%s", slash); |
| 77 | html_txt(fullpath); |
| 78 | htmlf("</title></head>\n<body>\n<h2>%s", slash); |
| 79 | html_txt(fullpath); |
| 80 | html("</h2>\n<ul>\n"); |
| 81 | len = strlen(fullpath); |
| 82 | if (len > 1) { |
| 83 | fullpath[len - 1] = 0; |
| 84 | slash = strrchr(fullpath, '/'); |
| 85 | if (slash) |
| 86 | *(slash + 1) = 0; |
Christian Hesse | 08a2b81 | 2015-10-09 14:55:50 +0200 | [diff] [blame] | 87 | else { |
| 88 | free(fullpath); |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 89 | fullpath = NULL; |
Christian Hesse | 08a2b81 | 2015-10-09 14:55:50 +0200 | [diff] [blame] | 90 | } |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 91 | html("<li>"); |
| 92 | cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1, |
| 93 | fullpath); |
| 94 | html("</li>\n"); |
| 95 | } |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 96 | free(fullpath); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 99 | static void print_dir_entry(const unsigned char *sha1, const char *base, |
| 100 | int baselen, const char *path, unsigned mode) |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 101 | { |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 102 | char *fullpath; |
| 103 | |
| 104 | fullpath = buildpath(base, baselen, path); |
Lars Hjemli | 7421857 | 2011-06-15 10:10:41 +0200 | [diff] [blame] | 105 | if (!S_ISDIR(mode) && !S_ISGITLINK(mode)) |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 106 | fullpath[strlen(fullpath) - 1] = 0; |
| 107 | html(" <li>"); |
Lars Hjemli | 7421857 | 2011-06-15 10:10:41 +0200 | [diff] [blame] | 108 | if (S_ISGITLINK(mode)) { |
| 109 | cgit_submodule_link(NULL, fullpath, sha1_to_hex(sha1)); |
| 110 | } else |
| 111 | cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1, |
| 112 | fullpath); |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 113 | html("</li>\n"); |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 114 | free(fullpath); |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void print_dir_tail(void) |
| 118 | { |
| 119 | html(" </ul>\n</body></html>\n"); |
| 120 | } |
| 121 | |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 122 | static int walk_tree(const unsigned char *sha1, struct strbuf *base, |
| 123 | const char *pathname, unsigned mode, int stage, void *cbdata) |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 124 | { |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 125 | struct walk_tree_context *walk_tree_ctx = cbdata; |
| 126 | |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 127 | if (base->len == walk_tree_ctx->match_baselen) { |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 128 | if (S_ISREG(mode)) { |
| 129 | if (print_object(sha1, pathname)) |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 130 | walk_tree_ctx->match = 1; |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 131 | } else if (S_ISDIR(mode)) { |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 132 | print_dir(sha1, base->buf, base->len, pathname); |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 133 | walk_tree_ctx->match = 2; |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 134 | return READ_TREE_RECURSIVE; |
| 135 | } |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 136 | } else if (base->len > walk_tree_ctx->match_baselen) { |
| 137 | print_dir_entry(sha1, base->buf, base->len, pathname, mode); |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 138 | walk_tree_ctx->match = 2; |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 139 | } else if (S_ISDIR(mode)) { |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 140 | return READ_TREE_RECURSIVE; |
Lukas Fleischer | a631750 | 2013-03-03 17:10:19 +0100 | [diff] [blame] | 141 | } |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 142 | |
Mark Lodato | 74ebf82 | 2010-01-31 01:07:41 -0500 | [diff] [blame] | 143 | return 0; |
| 144 | } |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 145 | |
Mark Lodato | 74ebf82 | 2010-01-31 01:07:41 -0500 | [diff] [blame] | 146 | static int basedir_len(const char *path) |
| 147 | { |
| 148 | char *p = strrchr(path, '/'); |
| 149 | if (p) |
| 150 | return p - path + 1; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 154 | void cgit_print_plain(void) |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 155 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 156 | const char *rev = ctx.qry.sha1; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 157 | unsigned char sha1[20]; |
| 158 | struct commit *commit; |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 159 | struct pathspec_item path_items = { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 160 | .match = ctx.qry.path, |
| 161 | .len = ctx.qry.path ? strlen(ctx.qry.path) : 0 |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 162 | }; |
| 163 | struct pathspec paths = { |
| 164 | .nr = 1, |
| 165 | .items = &path_items |
| 166 | }; |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 167 | struct walk_tree_context walk_tree_ctx = { |
| 168 | .match = 0 |
| 169 | }; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 170 | |
| 171 | if (!rev) |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 172 | rev = ctx.qry.head; |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 173 | |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 174 | if (get_sha1(rev, sha1)) { |
John Keeping | 2b3e76a | 2015-08-14 12:47:04 +0100 | [diff] [blame] | 175 | cgit_print_error_page(404, "Not found", "Not found"); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | commit = lookup_commit_reference(sha1); |
| 179 | if (!commit || parse_commit(commit)) { |
John Keeping | 2b3e76a | 2015-08-14 12:47:04 +0100 | [diff] [blame] | 180 | cgit_print_error_page(404, "Not found", "Not found"); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 181 | return; |
| 182 | } |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 183 | if (!path_items.match) { |
| 184 | path_items.match = ""; |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 185 | walk_tree_ctx.match_baselen = -1; |
Lars Hjemli | 7f88d20 | 2011-06-12 20:49:35 +0000 | [diff] [blame] | 186 | print_dir(commit->tree->object.sha1, "", 0, ""); |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 187 | walk_tree_ctx.match = 2; |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 188 | } |
| 189 | else |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 190 | walk_tree_ctx.match_baselen = basedir_len(path_items.match); |
| 191 | read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); |
| 192 | if (!walk_tree_ctx.match) |
John Keeping | 2b3e76a | 2015-08-14 12:47:04 +0100 | [diff] [blame] | 193 | cgit_print_error_page(404, "Not found", "Not found"); |
Lukas Fleischer | b1db30c | 2013-03-03 17:27:54 +0100 | [diff] [blame] | 194 | else if (walk_tree_ctx.match == 2) |
Mark Lodato | 6c1a736 | 2010-01-31 14:25:03 -0500 | [diff] [blame] | 195 | print_dir_tail(); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 196 | } |