Lars Hjemli | ab2ab95 | 2007-02-08 13:53:13 +0100 | [diff] [blame] | 1 | /* ui-snapshot.c: generate snapshot of a commit |
| 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 | ab2ab95 | 2007-02-08 13:53:13 +0100 | [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-snapshot.h" |
Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 11 | #include "html.h" |
Lars Hjemli | a4d1ca1 | 2008-03-24 16:50:57 +0100 | [diff] [blame] | 12 | #include "ui-shared.h" |
Lars Hjemli | ab2ab95 | 2007-02-08 13:53:13 +0100 | [diff] [blame] | 13 | |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 14 | static int write_archive_type(const char *format, const char *hex, const char *prefix) |
| 15 | { |
| 16 | struct argv_array argv = ARGV_ARRAY_INIT; |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 17 | const char **nargv; |
| 18 | int result; |
Jason A. Donenfeld | 973deda | 2013-03-03 23:41:53 -0500 | [diff] [blame] | 19 | argv_array_push(&argv, "snapshot"); |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 20 | argv_array_push(&argv, format); |
| 21 | if (prefix) { |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 22 | struct strbuf buf = STRBUF_INIT; |
| 23 | strbuf_addstr(&buf, prefix); |
| 24 | strbuf_addch(&buf, '/'); |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 25 | argv_array_push(&argv, "--prefix"); |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 26 | argv_array_push(&argv, buf.buf); |
| 27 | strbuf_release(&buf); |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 28 | } |
| 29 | argv_array_push(&argv, hex); |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 30 | /* |
| 31 | * Now we need to copy the pointers to arguments into a new |
| 32 | * structure because write_archive will rearrange its arguments |
| 33 | * which may result in duplicated/missing entries causing leaks |
| 34 | * or double-frees in argv_array_clear. |
| 35 | */ |
| 36 | nargv = xmalloc(sizeof(char *) * (argv.argc + 1)); |
| 37 | /* argv_array guarantees a trailing NULL entry. */ |
| 38 | memcpy(nargv, argv.argv, sizeof(char *) * (argv.argc + 1)); |
| 39 | |
Christian Hesse | 2c9f56f | 2018-08-28 18:27:00 +0200 | [diff] [blame] | 40 | result = write_archive(argv.argc, nargv, NULL, the_repository, NULL, 0); |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 41 | argv_array_clear(&argv); |
| 42 | free(nargv); |
| 43 | return result; |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static int write_tar_archive(const char *hex, const char *prefix) |
| 47 | { |
| 48 | return write_archive_type("--format=tar", hex, prefix); |
| 49 | } |
| 50 | |
| 51 | static int write_zip_archive(const char *hex, const char *prefix) |
| 52 | { |
| 53 | return write_archive_type("--format=zip", hex, prefix); |
| 54 | } |
| 55 | |
| 56 | static int write_compressed_tar_archive(const char *hex, |
| 57 | const char *prefix, |
| 58 | char *filter_argv[]) |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 59 | { |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 60 | int rv; |
John Keeping | 7bd90b8 | 2014-01-12 17:13:52 +0000 | [diff] [blame] | 61 | struct cgit_exec_filter f; |
| 62 | cgit_exec_filter_init(&f, filter_argv[0], filter_argv); |
| 63 | |
| 64 | cgit_open_filter(&f.base); |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 65 | rv = write_tar_archive(hex, prefix); |
John Keeping | 7bd90b8 | 2014-01-12 17:13:52 +0000 | [diff] [blame] | 66 | cgit_close_filter(&f.base); |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 67 | return rv; |
| 68 | } |
| 69 | |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 70 | static int write_tar_gzip_archive(const char *hex, const char *prefix) |
Michael Krelin | 4a92cbb | 2007-07-20 20:58:23 +0200 | [diff] [blame] | 71 | { |
Jason A. Donenfeld | 055e092 | 2012-09-26 02:56:38 +0200 | [diff] [blame] | 72 | char *argv[] = { "gzip", "-n", NULL }; |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 73 | return write_compressed_tar_archive(hex, prefix, argv); |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 74 | } |
Lars Hjemli | 1221adb | 2007-07-23 22:51:45 +0200 | [diff] [blame] | 75 | |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 76 | static int write_tar_bzip2_archive(const char *hex, const char *prefix) |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 77 | { |
Jason A. Donenfeld | 055e092 | 2012-09-26 02:56:38 +0200 | [diff] [blame] | 78 | char *argv[] = { "bzip2", NULL }; |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 79 | return write_compressed_tar_archive(hex, prefix, argv); |
Michael Krelin | 4a92cbb | 2007-07-20 20:58:23 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Hanspeter Portner | 06671f4 | 2019-08-16 23:40:19 +0200 | [diff] [blame] | 82 | static int write_tar_lzip_archive(const char *hex, const char *prefix) |
| 83 | { |
| 84 | char *argv[] = { "lzip", NULL }; |
| 85 | return write_compressed_tar_archive(hex, prefix, argv); |
| 86 | } |
| 87 | |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 88 | static int write_tar_xz_archive(const char *hex, const char *prefix) |
Andreas Wiese | 0642435 | 2009-12-08 22:18:11 +0100 | [diff] [blame] | 89 | { |
Jason A. Donenfeld | 055e092 | 2012-09-26 02:56:38 +0200 | [diff] [blame] | 90 | char *argv[] = { "xz", NULL }; |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 91 | return write_compressed_tar_archive(hex, prefix, argv); |
Andreas Wiese | 0642435 | 2009-12-08 22:18:11 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 94 | const struct cgit_snapshot_format cgit_snapshot_formats[] = { |
Christian Hesse | 7ba4196 | 2018-06-07 21:31:28 +0200 | [diff] [blame] | 95 | /* .tar must remain the 0 index */ |
Christian Hesse | 2f8648f | 2018-06-11 08:26:59 +0200 | [diff] [blame] | 96 | { ".tar", "application/x-tar", write_tar_archive }, |
| 97 | { ".tar.gz", "application/x-gzip", write_tar_gzip_archive }, |
| 98 | { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive }, |
Hanspeter Portner | 06671f4 | 2019-08-16 23:40:19 +0200 | [diff] [blame] | 99 | { ".tar.lz", "application/x-lzip", write_tar_lzip_archive }, |
Christian Hesse | 2f8648f | 2018-06-11 08:26:59 +0200 | [diff] [blame] | 100 | { ".tar.xz", "application/x-xz", write_tar_xz_archive }, |
| 101 | { ".zip", "application/x-zip", write_zip_archive }, |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 102 | { NULL } |
Michael Krelin | f97c707 | 2007-07-18 14:40:03 +0200 | [diff] [blame] | 103 | }; |
Lars Hjemli | ab2ab95 | 2007-02-08 13:53:13 +0100 | [diff] [blame] | 104 | |
John Keeping | c712d5a | 2018-03-31 16:15:48 +0100 | [diff] [blame] | 105 | static struct notes_tree snapshot_sig_notes[ARRAY_SIZE(cgit_snapshot_formats)]; |
| 106 | |
| 107 | const struct object_id *cgit_snapshot_get_sig(const char *ref, |
| 108 | const struct cgit_snapshot_format *f) |
| 109 | { |
| 110 | struct notes_tree *tree; |
| 111 | struct object_id oid; |
| 112 | |
| 113 | if (get_oid(ref, &oid)) |
| 114 | return NULL; |
| 115 | |
| 116 | tree = &snapshot_sig_notes[f - &cgit_snapshot_formats[0]]; |
| 117 | if (!tree->initialized) { |
| 118 | struct strbuf notes_ref = STRBUF_INIT; |
| 119 | |
| 120 | strbuf_addf(¬es_ref, "refs/notes/signatures/%s", |
| 121 | f->suffix + 1); |
| 122 | |
| 123 | init_notes(tree, notes_ref.buf, combine_notes_ignore, 0); |
| 124 | strbuf_release(¬es_ref); |
| 125 | } |
| 126 | |
| 127 | return get_note(tree, &oid); |
| 128 | } |
| 129 | |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 130 | static const struct cgit_snapshot_format *get_format(const char *filename) |
| 131 | { |
| 132 | const struct cgit_snapshot_format *fmt; |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 133 | |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 134 | for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) { |
Christian Hesse | 79c985e | 2014-05-29 17:35:46 +0200 | [diff] [blame] | 135 | if (ends_with(filename, fmt->suffix)) |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 136 | return fmt; |
| 137 | } |
| 138 | return NULL; |
| 139 | } |
| 140 | |
Christian Hesse | 2f8648f | 2018-06-11 08:26:59 +0200 | [diff] [blame] | 141 | const unsigned cgit_snapshot_format_bit(const struct cgit_snapshot_format *f) |
| 142 | { |
| 143 | return BIT(f - &cgit_snapshot_formats[0]); |
| 144 | } |
| 145 | |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 146 | static int make_snapshot(const struct cgit_snapshot_format *format, |
| 147 | const char *hex, const char *prefix, |
| 148 | const char *filename) |
| 149 | { |
Christian Hesse | 406f593 | 2016-09-29 22:12:11 +0200 | [diff] [blame] | 150 | struct object_id oid; |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 151 | |
Christian Hesse | 406f593 | 2016-09-29 22:12:11 +0200 | [diff] [blame] | 152 | if (get_oid(hex, &oid)) { |
John Keeping | 58e827c | 2015-08-14 12:47:08 +0100 | [diff] [blame] | 153 | cgit_print_error_page(404, "Not found", |
| 154 | "Bad object id: %s", hex); |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 155 | return 1; |
| 156 | } |
Christian Hesse | 2c9f56f | 2018-08-28 18:27:00 +0200 | [diff] [blame] | 157 | if (!lookup_commit_reference(the_repository, &oid)) { |
John Keeping | 58e827c | 2015-08-14 12:47:08 +0100 | [diff] [blame] | 158 | cgit_print_error_page(400, "Bad request", |
| 159 | "Not a commit reference: %s", hex); |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 160 | return 1; |
| 161 | } |
Christian Hesse | 406f593 | 2016-09-29 22:12:11 +0200 | [diff] [blame] | 162 | ctx.page.etag = oid_to_hex(&oid); |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 163 | ctx.page.mimetype = xstrdup(format->mimetype); |
| 164 | ctx.page.filename = xstrdup(filename); |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 165 | cgit_print_http_headers(); |
Christian Hesse | 55ebd5e | 2018-11-20 17:31:21 +0100 | [diff] [blame] | 166 | init_archivers(); |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 167 | format->write_func(hex, prefix); |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 168 | return 0; |
| 169 | } |
Lars Hjemli | 1221adb | 2007-07-23 22:51:45 +0200 | [diff] [blame] | 170 | |
John Keeping | c712d5a | 2018-03-31 16:15:48 +0100 | [diff] [blame] | 171 | static int write_sig(const struct cgit_snapshot_format *format, |
| 172 | const char *hex, const char *archive, |
| 173 | const char *filename) |
| 174 | { |
| 175 | const struct object_id *note = cgit_snapshot_get_sig(hex, format); |
| 176 | enum object_type type; |
| 177 | unsigned long size; |
| 178 | char *buf; |
| 179 | |
| 180 | if (!note) { |
| 181 | cgit_print_error_page(404, "Not found", |
| 182 | "No signature for %s", archive); |
| 183 | return 0; |
| 184 | } |
| 185 | |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 186 | buf = read_object_file(note, &type, &size); |
John Keeping | c712d5a | 2018-03-31 16:15:48 +0100 | [diff] [blame] | 187 | if (!buf) { |
| 188 | cgit_print_error_page(404, "Not found", "Not found"); |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | html("X-Content-Type-Options: nosniff\n"); |
| 193 | html("Content-Security-Policy: default-src 'none'\n"); |
| 194 | ctx.page.etag = oid_to_hex(note); |
| 195 | ctx.page.mimetype = xstrdup("application/pgp-signature"); |
| 196 | ctx.page.filename = xstrdup(filename); |
| 197 | cgit_print_http_headers(); |
| 198 | |
| 199 | html_raw(buf, size); |
| 200 | free(buf); |
| 201 | return 0; |
| 202 | } |
| 203 | |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 204 | /* Try to guess the requested revision from the requested snapshot name. |
| 205 | * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become |
| 206 | * "cgit-0.7.2". If this is a valid commit object name we've got a winner. |
| 207 | * Otherwise, if the snapshot name has a prefix matching the result from |
| 208 | * repo_basename(), we strip the basename and any following '-' and '_' |
| 209 | * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once |
| 210 | * more. If this still isn't a valid commit object name, we check if pre- |
Lukas Fleischer | 8c4c2c4 | 2013-04-10 13:04:03 +0200 | [diff] [blame] | 211 | * pending a 'v' or a 'V' to the remaining snapshot name ("0.7.2" -> |
| 212 | * "v0.7.2") gives us something valid. |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 213 | */ |
John Keeping | d85e8a9 | 2018-03-31 15:18:57 +0100 | [diff] [blame] | 214 | static const char *get_ref_from_filename(const struct cgit_repo *repo, |
| 215 | const char *filename, |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 216 | const struct cgit_snapshot_format *format) |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 217 | { |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 218 | const char *reponame; |
Christian Hesse | 406f593 | 2016-09-29 22:12:11 +0200 | [diff] [blame] | 219 | struct object_id oid; |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 220 | struct strbuf snapshot = STRBUF_INIT; |
| 221 | int result = 1; |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 222 | |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 223 | strbuf_addstr(&snapshot, filename); |
| 224 | strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix)); |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 225 | |
Christian Hesse | 406f593 | 2016-09-29 22:12:11 +0200 | [diff] [blame] | 226 | if (get_oid(snapshot.buf, &oid) == 0) |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 227 | goto out; |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 228 | |
John Keeping | c1572bb | 2018-03-31 14:20:01 +0100 | [diff] [blame] | 229 | reponame = cgit_snapshot_prefix(repo); |
Christian Hesse | 79c985e | 2014-05-29 17:35:46 +0200 | [diff] [blame] | 230 | if (starts_with(snapshot.buf, reponame)) { |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 231 | const char *new_start = snapshot.buf; |
| 232 | new_start += strlen(reponame); |
| 233 | while (new_start && (*new_start == '-' || *new_start == '_')) |
| 234 | new_start++; |
| 235 | strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0); |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Christian Hesse | 406f593 | 2016-09-29 22:12:11 +0200 | [diff] [blame] | 238 | if (get_oid(snapshot.buf, &oid) == 0) |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 239 | goto out; |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 240 | |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 241 | strbuf_insert(&snapshot, 0, "v", 1); |
Christian Hesse | 406f593 | 2016-09-29 22:12:11 +0200 | [diff] [blame] | 242 | if (get_oid(snapshot.buf, &oid) == 0) |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 243 | goto out; |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 244 | |
Lukas Fleischer | 8c4c2c4 | 2013-04-10 13:04:03 +0200 | [diff] [blame] | 245 | strbuf_splice(&snapshot, 0, 1, "V", 1); |
Christian Hesse | 406f593 | 2016-09-29 22:12:11 +0200 | [diff] [blame] | 246 | if (get_oid(snapshot.buf, &oid) == 0) |
Lukas Fleischer | 8c4c2c4 | 2013-04-10 13:04:03 +0200 | [diff] [blame] | 247 | goto out; |
| 248 | |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 249 | result = 0; |
| 250 | strbuf_release(&snapshot); |
| 251 | |
| 252 | out: |
| 253 | return result ? strbuf_detach(&snapshot, NULL) : NULL; |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 254 | } |
| 255 | |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 256 | void cgit_print_snapshot(const char *head, const char *hex, |
Lukas Fleischer | 3e9578e | 2014-02-08 14:37:29 +0100 | [diff] [blame] | 257 | const char *filename, int dwim) |
Lars Hjemli | ab2ab95 | 2007-02-08 13:53:13 +0100 | [diff] [blame] | 258 | { |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 259 | const struct cgit_snapshot_format* f; |
John Keeping | c712d5a | 2018-03-31 16:15:48 +0100 | [diff] [blame] | 260 | const char *sig_filename = NULL; |
| 261 | char *adj_filename = NULL; |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 262 | char *prefix = NULL; |
Lars Hjemli | eb45342 | 2007-07-23 00:11:15 +0200 | [diff] [blame] | 263 | |
Lars Hjemli | 6fddad7 | 2009-03-15 08:57:33 +0100 | [diff] [blame] | 264 | if (!filename) { |
John Keeping | fd00e71 | 2015-08-14 12:47:09 +0100 | [diff] [blame] | 265 | cgit_print_error_page(400, "Bad request", |
| 266 | "No snapshot name specified"); |
Lars Hjemli | 6fddad7 | 2009-03-15 08:57:33 +0100 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | |
John Keeping | c712d5a | 2018-03-31 16:15:48 +0100 | [diff] [blame] | 270 | if (ends_with(filename, ".asc")) { |
| 271 | sig_filename = filename; |
| 272 | |
| 273 | /* Strip ".asc" from filename for common format processing */ |
| 274 | adj_filename = xstrdup(filename); |
| 275 | adj_filename[strlen(adj_filename) - 4] = '\0'; |
| 276 | filename = adj_filename; |
| 277 | } |
| 278 | |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 279 | f = get_format(filename); |
Christian Hesse | 7ba4196 | 2018-06-07 21:31:28 +0200 | [diff] [blame] | 280 | if (!f || (!sig_filename && !(ctx.repo->snapshots & cgit_snapshot_format_bit(f)))) { |
John Keeping | fd00e71 | 2015-08-14 12:47:09 +0100 | [diff] [blame] | 281 | cgit_print_error_page(400, "Bad request", |
| 282 | "Unsupported snapshot format: %s", filename); |
Michael Krelin | f97c707 | 2007-07-18 14:40:03 +0200 | [diff] [blame] | 283 | return; |
| 284 | } |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 285 | |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 286 | if (!hex && dwim) { |
John Keeping | d85e8a9 | 2018-03-31 15:18:57 +0100 | [diff] [blame] | 287 | hex = get_ref_from_filename(ctx.repo, filename, f); |
Natanael Copa | c4b45de | 2008-12-02 11:31:34 +0100 | [diff] [blame] | 288 | if (hex == NULL) { |
John Keeping | 048f195 | 2015-08-14 12:47:05 +0100 | [diff] [blame] | 289 | cgit_print_error_page(404, "Not found", "Not found"); |
Natanael Copa | c4b45de | 2008-12-02 11:31:34 +0100 | [diff] [blame] | 290 | return; |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 291 | } |
Natanael Copa | c4b45de | 2008-12-02 11:31:34 +0100 | [diff] [blame] | 292 | prefix = xstrdup(filename); |
| 293 | prefix[strlen(filename) - strlen(f->suffix)] = '\0'; |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 294 | } |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 295 | |
| 296 | if (!hex) |
| 297 | hex = head; |
| 298 | |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 299 | if (!prefix) |
John Keeping | c1572bb | 2018-03-31 14:20:01 +0100 | [diff] [blame] | 300 | prefix = xstrdup(cgit_snapshot_prefix(ctx.repo)); |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 301 | |
John Keeping | c712d5a | 2018-03-31 16:15:48 +0100 | [diff] [blame] | 302 | if (sig_filename) |
| 303 | write_sig(f, hex, filename, sig_filename); |
| 304 | else |
| 305 | make_snapshot(f, hex, prefix, filename); |
| 306 | |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 307 | free(prefix); |
John Keeping | c712d5a | 2018-03-31 16:15:48 +0100 | [diff] [blame] | 308 | free(adj_filename); |
Michael Krelin | f97c707 | 2007-07-18 14:40:03 +0200 | [diff] [blame] | 309 | } |