blob: 556d3ed4125aac62a6d307d10b8869cc544d6118 [file] [log] [blame]
Lars Hjemliab2ab952007-02-08 13:53:13 +01001/* ui-snapshot.c: generate snapshot of a commit
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemliab2ab952007-02-08 13:53:13 +01004 *
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-snapshot.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010011#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010012#include "ui-shared.h"
Lars Hjemliab2ab952007-02-08 13:53:13 +010013
John Keeping2ab1cd92013-03-02 12:32:12 +000014static int write_archive_type(const char *format, const char *hex, const char *prefix)
15{
16 struct argv_array argv = ARGV_ARRAY_INIT;
John Keepingfb3655d2013-04-06 10:28:57 +010017 const char **nargv;
18 int result;
Jason A. Donenfeld973deda2013-03-03 23:41:53 -050019 argv_array_push(&argv, "snapshot");
John Keeping2ab1cd92013-03-02 12:32:12 +000020 argv_array_push(&argv, format);
21 if (prefix) {
John Keepingfb3655d2013-04-06 10:28:57 +010022 struct strbuf buf = STRBUF_INIT;
23 strbuf_addstr(&buf, prefix);
24 strbuf_addch(&buf, '/');
John Keeping2ab1cd92013-03-02 12:32:12 +000025 argv_array_push(&argv, "--prefix");
John Keepingfb3655d2013-04-06 10:28:57 +010026 argv_array_push(&argv, buf.buf);
27 strbuf_release(&buf);
John Keeping2ab1cd92013-03-02 12:32:12 +000028 }
29 argv_array_push(&argv, hex);
John Keepingfb3655d2013-04-06 10:28:57 +010030 /*
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 Hesse2c9f56f2018-08-28 18:27:00 +020040 result = write_archive(argv.argc, nargv, NULL, the_repository, NULL, 0);
John Keepingfb3655d2013-04-06 10:28:57 +010041 argv_array_clear(&argv);
42 free(nargv);
43 return result;
John Keeping2ab1cd92013-03-02 12:32:12 +000044}
45
46static int write_tar_archive(const char *hex, const char *prefix)
47{
48 return write_archive_type("--format=tar", hex, prefix);
49}
50
51static int write_zip_archive(const char *hex, const char *prefix)
52{
53 return write_archive_type("--format=zip", hex, prefix);
54}
55
56static int write_compressed_tar_archive(const char *hex,
57 const char *prefix,
58 char *filter_argv[])
Michael Krelin18a99bd2007-07-21 02:05:34 +020059{
Michael Krelin18a99bd2007-07-21 02:05:34 +020060 int rv;
John Keeping7bd90b82014-01-12 17:13:52 +000061 struct cgit_exec_filter f;
62 cgit_exec_filter_init(&f, filter_argv[0], filter_argv);
63
64 cgit_open_filter(&f.base);
John Keeping2ab1cd92013-03-02 12:32:12 +000065 rv = write_tar_archive(hex, prefix);
John Keeping7bd90b82014-01-12 17:13:52 +000066 cgit_close_filter(&f.base);
Michael Krelin18a99bd2007-07-21 02:05:34 +020067 return rv;
68}
69
John Keeping2ab1cd92013-03-02 12:32:12 +000070static int write_tar_gzip_archive(const char *hex, const char *prefix)
Michael Krelin4a92cbb2007-07-20 20:58:23 +020071{
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020072 char *argv[] = { "gzip", "-n", NULL };
John Keeping2ab1cd92013-03-02 12:32:12 +000073 return write_compressed_tar_archive(hex, prefix, argv);
Michael Krelin18a99bd2007-07-21 02:05:34 +020074}
Lars Hjemli1221adb2007-07-23 22:51:45 +020075
John Keeping2ab1cd92013-03-02 12:32:12 +000076static int write_tar_bzip2_archive(const char *hex, const char *prefix)
Michael Krelin18a99bd2007-07-21 02:05:34 +020077{
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020078 char *argv[] = { "bzip2", NULL };
John Keeping2ab1cd92013-03-02 12:32:12 +000079 return write_compressed_tar_archive(hex, prefix, argv);
Michael Krelin4a92cbb2007-07-20 20:58:23 +020080}
81
Hanspeter Portner06671f42019-08-16 23:40:19 +020082static 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 Keeping2ab1cd92013-03-02 12:32:12 +000088static int write_tar_xz_archive(const char *hex, const char *prefix)
Andreas Wiese06424352009-12-08 22:18:11 +010089{
Jason A. Donenfeld055e0922012-09-26 02:56:38 +020090 char *argv[] = { "xz", NULL };
John Keeping2ab1cd92013-03-02 12:32:12 +000091 return write_compressed_tar_archive(hex, prefix, argv);
Andreas Wiese06424352009-12-08 22:18:11 +010092}
93
Christian Hesse892ba8c2020-02-26 09:12:21 +010094static int write_tar_zstd_archive(const char *hex, const char *prefix)
95{
96 char *argv[] = { "zstd", "-T0", NULL };
97 return write_compressed_tar_archive(hex, prefix, argv);
98}
99
Lars Hjemlif34478c2008-03-24 16:00:27 +0100100const struct cgit_snapshot_format cgit_snapshot_formats[] = {
Christian Hesse7ba41962018-06-07 21:31:28 +0200101 /* .tar must remain the 0 index */
Christian Hesse2f8648f2018-06-11 08:26:59 +0200102 { ".tar", "application/x-tar", write_tar_archive },
103 { ".tar.gz", "application/x-gzip", write_tar_gzip_archive },
104 { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive },
Hanspeter Portner06671f42019-08-16 23:40:19 +0200105 { ".tar.lz", "application/x-lzip", write_tar_lzip_archive },
Christian Hesse2f8648f2018-06-11 08:26:59 +0200106 { ".tar.xz", "application/x-xz", write_tar_xz_archive },
Christian Hesse892ba8c2020-02-26 09:12:21 +0100107 { ".tar.zst", "application/x-zstd", write_tar_zstd_archive },
Christian Hesse2f8648f2018-06-11 08:26:59 +0200108 { ".zip", "application/x-zip", write_zip_archive },
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500109 { NULL }
Michael Krelinf97c7072007-07-18 14:40:03 +0200110};
Lars Hjemliab2ab952007-02-08 13:53:13 +0100111
John Keepingc712d5a2018-03-31 16:15:48 +0100112static struct notes_tree snapshot_sig_notes[ARRAY_SIZE(cgit_snapshot_formats)];
113
114const struct object_id *cgit_snapshot_get_sig(const char *ref,
115 const struct cgit_snapshot_format *f)
116{
117 struct notes_tree *tree;
118 struct object_id oid;
119
120 if (get_oid(ref, &oid))
121 return NULL;
122
123 tree = &snapshot_sig_notes[f - &cgit_snapshot_formats[0]];
124 if (!tree->initialized) {
125 struct strbuf notes_ref = STRBUF_INIT;
126
127 strbuf_addf(&notes_ref, "refs/notes/signatures/%s",
128 f->suffix + 1);
129
130 init_notes(tree, notes_ref.buf, combine_notes_ignore, 0);
131 strbuf_release(&notes_ref);
132 }
133
134 return get_note(tree, &oid);
135}
136
Lars Hjemlied7ff092008-10-11 20:09:42 +0200137static const struct cgit_snapshot_format *get_format(const char *filename)
138{
139 const struct cgit_snapshot_format *fmt;
Lars Hjemlied7ff092008-10-11 20:09:42 +0200140
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500141 for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
Christian Hesse79c985e2014-05-29 17:35:46 +0200142 if (ends_with(filename, fmt->suffix))
Lars Hjemlied7ff092008-10-11 20:09:42 +0200143 return fmt;
144 }
145 return NULL;
146}
147
Christian Hesse2f8648f2018-06-11 08:26:59 +0200148const unsigned cgit_snapshot_format_bit(const struct cgit_snapshot_format *f)
149{
150 return BIT(f - &cgit_snapshot_formats[0]);
151}
152
Lars Hjemlif34478c2008-03-24 16:00:27 +0100153static int make_snapshot(const struct cgit_snapshot_format *format,
154 const char *hex, const char *prefix,
155 const char *filename)
156{
Christian Hesse406f5932016-09-29 22:12:11 +0200157 struct object_id oid;
Lars Hjemlif34478c2008-03-24 16:00:27 +0100158
Christian Hesse406f5932016-09-29 22:12:11 +0200159 if (get_oid(hex, &oid)) {
John Keeping58e827c2015-08-14 12:47:08 +0100160 cgit_print_error_page(404, "Not found",
161 "Bad object id: %s", hex);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100162 return 1;
163 }
Christian Hesse2c9f56f2018-08-28 18:27:00 +0200164 if (!lookup_commit_reference(the_repository, &oid)) {
John Keeping58e827c2015-08-14 12:47:08 +0100165 cgit_print_error_page(400, "Bad request",
166 "Not a commit reference: %s", hex);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100167 return 1;
168 }
Christian Hesse406f5932016-09-29 22:12:11 +0200169 ctx.page.etag = oid_to_hex(&oid);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100170 ctx.page.mimetype = xstrdup(format->mimetype);
171 ctx.page.filename = xstrdup(filename);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100172 cgit_print_http_headers();
Christian Hesse55ebd5e2018-11-20 17:31:21 +0100173 init_archivers();
John Keeping2ab1cd92013-03-02 12:32:12 +0000174 format->write_func(hex, prefix);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100175 return 0;
176}
Lars Hjemli1221adb2007-07-23 22:51:45 +0200177
John Keepingc712d5a2018-03-31 16:15:48 +0100178static int write_sig(const struct cgit_snapshot_format *format,
179 const char *hex, const char *archive,
180 const char *filename)
181{
182 const struct object_id *note = cgit_snapshot_get_sig(hex, format);
183 enum object_type type;
184 unsigned long size;
185 char *buf;
186
187 if (!note) {
188 cgit_print_error_page(404, "Not found",
189 "No signature for %s", archive);
190 return 0;
191 }
192
Christian Hesse255b78f2018-06-04 18:49:28 +0200193 buf = read_object_file(note, &type, &size);
John Keepingc712d5a2018-03-31 16:15:48 +0100194 if (!buf) {
195 cgit_print_error_page(404, "Not found", "Not found");
196 return 0;
197 }
198
199 html("X-Content-Type-Options: nosniff\n");
200 html("Content-Security-Policy: default-src 'none'\n");
201 ctx.page.etag = oid_to_hex(note);
202 ctx.page.mimetype = xstrdup("application/pgp-signature");
203 ctx.page.filename = xstrdup(filename);
204 cgit_print_http_headers();
205
206 html_raw(buf, size);
207 free(buf);
208 return 0;
209}
210
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100211/* Try to guess the requested revision from the requested snapshot name.
212 * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
213 * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
214 * Otherwise, if the snapshot name has a prefix matching the result from
215 * repo_basename(), we strip the basename and any following '-' and '_'
216 * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
217 * more. If this still isn't a valid commit object name, we check if pre-
Lukas Fleischer8c4c2c42013-04-10 13:04:03 +0200218 * pending a 'v' or a 'V' to the remaining snapshot name ("0.7.2" ->
219 * "v0.7.2") gives us something valid.
Lars Hjemlied7ff092008-10-11 20:09:42 +0200220 */
John Keepingd85e8a92018-03-31 15:18:57 +0100221static const char *get_ref_from_filename(const struct cgit_repo *repo,
222 const char *filename,
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100223 const struct cgit_snapshot_format *format)
Lars Hjemlied7ff092008-10-11 20:09:42 +0200224{
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100225 const char *reponame;
Christian Hesse406f5932016-09-29 22:12:11 +0200226 struct object_id oid;
John Keepingfb3655d2013-04-06 10:28:57 +0100227 struct strbuf snapshot = STRBUF_INIT;
228 int result = 1;
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100229
John Keepingfb3655d2013-04-06 10:28:57 +0100230 strbuf_addstr(&snapshot, filename);
231 strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix));
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100232
Christian Hesse406f5932016-09-29 22:12:11 +0200233 if (get_oid(snapshot.buf, &oid) == 0)
John Keepingfb3655d2013-04-06 10:28:57 +0100234 goto out;
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100235
John Keepingc1572bb2018-03-31 14:20:01 +0100236 reponame = cgit_snapshot_prefix(repo);
Christian Hesse79c985e2014-05-29 17:35:46 +0200237 if (starts_with(snapshot.buf, reponame)) {
John Keepingfb3655d2013-04-06 10:28:57 +0100238 const char *new_start = snapshot.buf;
239 new_start += strlen(reponame);
240 while (new_start && (*new_start == '-' || *new_start == '_'))
241 new_start++;
242 strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0);
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100243 }
244
Christian Hesse406f5932016-09-29 22:12:11 +0200245 if (get_oid(snapshot.buf, &oid) == 0)
John Keepingfb3655d2013-04-06 10:28:57 +0100246 goto out;
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100247
John Keepingfb3655d2013-04-06 10:28:57 +0100248 strbuf_insert(&snapshot, 0, "v", 1);
Christian Hesse406f5932016-09-29 22:12:11 +0200249 if (get_oid(snapshot.buf, &oid) == 0)
John Keepingfb3655d2013-04-06 10:28:57 +0100250 goto out;
Lars Hjemli4b4f8d12008-12-01 19:13:44 +0100251
Lukas Fleischer8c4c2c42013-04-10 13:04:03 +0200252 strbuf_splice(&snapshot, 0, 1, "V", 1);
Christian Hesse406f5932016-09-29 22:12:11 +0200253 if (get_oid(snapshot.buf, &oid) == 0)
Lukas Fleischer8c4c2c42013-04-10 13:04:03 +0200254 goto out;
255
John Keepingfb3655d2013-04-06 10:28:57 +0100256 result = 0;
257 strbuf_release(&snapshot);
258
259out:
260 return result ? strbuf_detach(&snapshot, NULL) : NULL;
Lars Hjemlied7ff092008-10-11 20:09:42 +0200261}
262
Natanael Copa314d9ea2008-11-29 21:49:07 -0800263void cgit_print_snapshot(const char *head, const char *hex,
Lukas Fleischer3e9578e2014-02-08 14:37:29 +0100264 const char *filename, int dwim)
Lars Hjemliab2ab952007-02-08 13:53:13 +0100265{
Lars Hjemlif34478c2008-03-24 16:00:27 +0100266 const struct cgit_snapshot_format* f;
John Keepingc712d5a2018-03-31 16:15:48 +0100267 const char *sig_filename = NULL;
268 char *adj_filename = NULL;
Natanael Copa314d9ea2008-11-29 21:49:07 -0800269 char *prefix = NULL;
Lars Hjemlieb453422007-07-23 00:11:15 +0200270
Lars Hjemli6fddad72009-03-15 08:57:33 +0100271 if (!filename) {
John Keepingfd00e712015-08-14 12:47:09 +0100272 cgit_print_error_page(400, "Bad request",
273 "No snapshot name specified");
Lars Hjemli6fddad72009-03-15 08:57:33 +0100274 return;
275 }
276
John Keepingc712d5a2018-03-31 16:15:48 +0100277 if (ends_with(filename, ".asc")) {
278 sig_filename = filename;
279
280 /* Strip ".asc" from filename for common format processing */
281 adj_filename = xstrdup(filename);
282 adj_filename[strlen(adj_filename) - 4] = '\0';
283 filename = adj_filename;
284 }
285
Lars Hjemlied7ff092008-10-11 20:09:42 +0200286 f = get_format(filename);
Christian Hesse7ba41962018-06-07 21:31:28 +0200287 if (!f || (!sig_filename && !(ctx.repo->snapshots & cgit_snapshot_format_bit(f)))) {
John Keepingfd00e712015-08-14 12:47:09 +0100288 cgit_print_error_page(400, "Bad request",
289 "Unsupported snapshot format: %s", filename);
Michael Krelinf97c7072007-07-18 14:40:03 +0200290 return;
291 }
Lars Hjemlied7ff092008-10-11 20:09:42 +0200292
Natanael Copa314d9ea2008-11-29 21:49:07 -0800293 if (!hex && dwim) {
John Keepingd85e8a92018-03-31 15:18:57 +0100294 hex = get_ref_from_filename(ctx.repo, filename, f);
Natanael Copac4b45de2008-12-02 11:31:34 +0100295 if (hex == NULL) {
John Keeping048f1952015-08-14 12:47:05 +0100296 cgit_print_error_page(404, "Not found", "Not found");
Natanael Copac4b45de2008-12-02 11:31:34 +0100297 return;
Natanael Copa314d9ea2008-11-29 21:49:07 -0800298 }
Natanael Copac4b45de2008-12-02 11:31:34 +0100299 prefix = xstrdup(filename);
300 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
Natanael Copa314d9ea2008-11-29 21:49:07 -0800301 }
Lars Hjemlied7ff092008-10-11 20:09:42 +0200302
303 if (!hex)
304 hex = head;
305
Natanael Copa314d9ea2008-11-29 21:49:07 -0800306 if (!prefix)
John Keepingc1572bb2018-03-31 14:20:01 +0100307 prefix = xstrdup(cgit_snapshot_prefix(ctx.repo));
Natanael Copa314d9ea2008-11-29 21:49:07 -0800308
John Keepingc712d5a2018-03-31 16:15:48 +0100309 if (sig_filename)
310 write_sig(f, hex, filename, sig_filename);
311 else
312 make_snapshot(f, hex, prefix, filename);
313
Natanael Copa314d9ea2008-11-29 21:49:07 -0800314 free(prefix);
John Keepingc712d5a2018-03-31 16:15:48 +0100315 free(adj_filename);
Michael Krelinf97c7072007-07-18 14:40:03 +0200316}