1 /* ui-snapshot.c: generate snapshot of a commit
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
9 #define USE_THE_REPOSITORY_VARIABLE
12 #include "ui-snapshot.h"
14 #include "ui-shared.h"
16 static int write_archive_type(const char *format, const char *hex, const char *prefix)
18 struct strvec argv = STRVEC_INIT;
21 strvec_push(&argv, "snapshot");
22 strvec_push(&argv, format);
24 struct strbuf buf = STRBUF_INIT;
25 strbuf_addstr(&buf, prefix);
26 strbuf_addch(&buf, '/');
27 strvec_push(&argv, "--prefix");
28 strvec_push(&argv, buf.buf);
31 strvec_push(&argv, hex);
33 * Now we need to copy the pointers to arguments into a new
34 * structure because write_archive will rearrange its arguments
35 * which may result in duplicated/missing entries causing leaks
36 * or double-frees in strvec_clear.
38 nargv = xmalloc(sizeof(char *) * (argv.nr + 1));
39 /* strvec guarantees a trailing NULL entry. */
40 memcpy(nargv, argv.v, sizeof(char *) * (argv.nr + 1));
42 result = write_archive(argv.nr, nargv, NULL, the_repository, NULL, 0);
48 static int write_tar_archive(const char *hex, const char *prefix)
50 return write_archive_type("--format=tar", hex, prefix);
53 static int write_zip_archive(const char *hex, const char *prefix)
55 return write_archive_type("--format=zip", hex, prefix);
58 static int write_compressed_tar_archive(const char *hex,
63 struct cgit_exec_filter f;
64 cgit_exec_filter_init(&f, filter_argv[0], filter_argv);
66 cgit_open_filter(&f.base);
67 rv = write_tar_archive(hex, prefix);
68 cgit_close_filter(&f.base);
72 static int write_tar_gzip_archive(const char *hex, const char *prefix)
74 char *argv[] = { "gzip", "-n", NULL };
75 return write_compressed_tar_archive(hex, prefix, argv);
78 static int write_tar_bzip2_archive(const char *hex, const char *prefix)
80 char *argv[] = { "bzip2", NULL };
81 return write_compressed_tar_archive(hex, prefix, argv);
84 static int write_tar_lzip_archive(const char *hex, const char *prefix)
86 char *argv[] = { "lzip", NULL };
87 return write_compressed_tar_archive(hex, prefix, argv);
90 static int write_tar_xz_archive(const char *hex, const char *prefix)
92 char *argv[] = { "xz", NULL };
93 return write_compressed_tar_archive(hex, prefix, argv);
96 static int write_tar_zstd_archive(const char *hex, const char *prefix)
98 char *argv[] = { "zstd", "-T0", NULL };
99 return write_compressed_tar_archive(hex, prefix, argv);
102 const struct cgit_snapshot_format cgit_snapshot_formats[] = {
103 /* .tar must remain the 0 index */
104 { ".tar", "application/x-tar", write_tar_archive },
105 { ".tar.gz", "application/x-gzip", write_tar_gzip_archive },
106 { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive },
107 { ".tar.lz", "application/x-lzip", write_tar_lzip_archive },
108 { ".tar.xz", "application/x-xz", write_tar_xz_archive },
109 { ".tar.zst", "application/x-zstd", write_tar_zstd_archive },
110 { ".zip", "application/x-zip", write_zip_archive },
114 static struct notes_tree snapshot_sig_notes[ARRAY_SIZE(cgit_snapshot_formats)];
116 const struct object_id *cgit_snapshot_get_sig(const char *ref,
117 const struct cgit_snapshot_format *f)
119 struct notes_tree *tree;
120 struct object_id oid;
122 if (repo_get_oid(the_repository, ref, &oid))
125 tree = &snapshot_sig_notes[f - &cgit_snapshot_formats[0]];
126 if (!tree->initialized) {
127 struct strbuf notes_ref = STRBUF_INIT;
129 strbuf_addf(¬es_ref, "refs/notes/signatures/%s",
132 init_notes(tree, notes_ref.buf, combine_notes_ignore, 0);
133 strbuf_release(¬es_ref);
136 return get_note(tree, &oid);
139 static const struct cgit_snapshot_format *get_format(const char *filename)
141 const struct cgit_snapshot_format *fmt;
143 for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
144 if (ends_with(filename, fmt->suffix))
150 const unsigned cgit_snapshot_format_bit(const struct cgit_snapshot_format *f)
152 return BIT(f - &cgit_snapshot_formats[0]);
155 static int make_snapshot(const struct cgit_snapshot_format *format,
156 const char *hex, const char *prefix,
157 const char *filename)
159 struct object_id oid;
161 if (repo_get_oid(the_repository, hex, &oid)) {
162 cgit_print_error_page(404, "Not found",
163 "Bad object id: %s", hex);
166 if (!lookup_commit_reference(the_repository, &oid)) {
167 cgit_print_error_page(400, "Bad request",
168 "Not a commit reference: %s", hex);
171 ctx.page.etag = oid_to_hex(&oid);
172 ctx.page.mimetype = xstrdup(format->mimetype);
173 ctx.page.filename = xstrdup(filename);
174 cgit_print_http_headers();
176 format->write_func(hex, prefix);
180 static int write_sig(const struct cgit_snapshot_format *format,
181 const char *hex, const char *archive,
182 const char *filename)
184 const struct object_id *note = cgit_snapshot_get_sig(hex, format);
185 enum object_type type;
190 cgit_print_error_page(404, "Not found",
191 "No signature for %s", archive);
195 buf = repo_read_object_file(the_repository, note, &type, &size);
197 cgit_print_error_page(404, "Not found", "Not found");
201 html("X-Content-Type-Options: nosniff\n");
202 html("Content-Security-Policy: default-src 'none'\n");
203 ctx.page.etag = oid_to_hex(note);
204 ctx.page.mimetype = xstrdup("application/pgp-signature");
205 ctx.page.filename = xstrdup(filename);
206 cgit_print_http_headers();
213 /* Try to guess the requested revision from the requested snapshot name.
214 * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
215 * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
216 * Otherwise, if the snapshot name has a prefix matching the result from
217 * repo_basename(), we strip the basename and any following '-' and '_'
218 * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
219 * more. If this still isn't a valid commit object name, we check if pre-
220 * pending a 'v' or a 'V' to the remaining snapshot name ("0.7.2" ->
221 * "v0.7.2") gives us something valid.
223 static const char *get_ref_from_filename(const struct cgit_repo *repo,
224 const char *filename,
225 const struct cgit_snapshot_format *format)
227 const char *reponame;
228 struct object_id oid;
229 struct strbuf snapshot = STRBUF_INIT;
232 strbuf_addstr(&snapshot, filename);
233 strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix));
235 if (repo_get_oid(the_repository, snapshot.buf, &oid) == 0)
238 reponame = cgit_snapshot_prefix(repo);
239 if (starts_with(snapshot.buf, reponame)) {
240 const char *new_start = snapshot.buf;
241 new_start += strlen(reponame);
242 while (new_start && (*new_start == '-' || *new_start == '_'))
244 strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0);
247 if (repo_get_oid(the_repository, snapshot.buf, &oid) == 0)
250 strbuf_insert(&snapshot, 0, "v", 1);
251 if (repo_get_oid(the_repository, snapshot.buf, &oid) == 0)
254 strbuf_splice(&snapshot, 0, 1, "V", 1);
255 if (repo_get_oid(the_repository, snapshot.buf, &oid) == 0)
259 strbuf_release(&snapshot);
262 return result ? strbuf_detach(&snapshot, NULL) : NULL;
265 void cgit_print_snapshot(const char *head, const char *hex,
266 const char *filename, int dwim)
268 const struct cgit_snapshot_format* f;
269 const char *sig_filename = NULL;
270 char *adj_filename = NULL;
274 cgit_print_error_page(400, "Bad request",
275 "No snapshot name specified");
279 if (ends_with(filename, ".asc")) {
280 sig_filename = filename;
282 /* Strip ".asc" from filename for common format processing */
283 adj_filename = xstrdup(filename);
284 adj_filename[strlen(adj_filename) - 4] = '\0';
285 filename = adj_filename;
288 f = get_format(filename);
289 if (!f || (!sig_filename && !(ctx.repo->snapshots & cgit_snapshot_format_bit(f)))) {
290 cgit_print_error_page(400, "Bad request",
291 "Unsupported snapshot format: %s", filename);
296 hex = get_ref_from_filename(ctx.repo, filename, f);
298 cgit_print_error_page(404, "Not found", "Not found");
301 prefix = xstrdup(filename);
302 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
309 prefix = xstrdup(cgit_snapshot_prefix(ctx.repo));
312 write_sig(f, hex, filename, sig_filename);
314 make_snapshot(f, hex, prefix, filename);