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)
10 #include "ui-snapshot.h"
12 #include "ui-shared.h"
14 static int write_archive_type(const char *format, const char *hex, const char *prefix)
16 struct argv_array argv = ARGV_ARRAY_INIT;
19 argv_array_push(&argv, "snapshot");
20 argv_array_push(&argv, format);
22 struct strbuf buf = STRBUF_INIT;
23 strbuf_addstr(&buf, prefix);
24 strbuf_addch(&buf, '/');
25 argv_array_push(&argv, "--prefix");
26 argv_array_push(&argv, buf.buf);
29 argv_array_push(&argv, hex);
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.
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));
40 result = write_archive(argv.argc, nargv, NULL, the_repository, NULL, 0);
41 argv_array_clear(&argv);
46 static int write_tar_archive(const char *hex, const char *prefix)
48 return write_archive_type("--format=tar", hex, prefix);
51 static int write_zip_archive(const char *hex, const char *prefix)
53 return write_archive_type("--format=zip", hex, prefix);
56 static int write_compressed_tar_archive(const char *hex,
61 struct cgit_exec_filter f;
62 cgit_exec_filter_init(&f, filter_argv[0], filter_argv);
64 cgit_open_filter(&f.base);
65 rv = write_tar_archive(hex, prefix);
66 cgit_close_filter(&f.base);
70 static int write_tar_gzip_archive(const char *hex, const char *prefix)
72 char *argv[] = { "gzip", "-n", NULL };
73 return write_compressed_tar_archive(hex, prefix, argv);
76 static int write_tar_bzip2_archive(const char *hex, const char *prefix)
78 char *argv[] = { "bzip2", NULL };
79 return write_compressed_tar_archive(hex, prefix, argv);
82 static int write_tar_lzip_archive(const char *hex, const char *prefix)
84 char *argv[] = { "lzip", NULL };
85 return write_compressed_tar_archive(hex, prefix, argv);
88 static int write_tar_xz_archive(const char *hex, const char *prefix)
90 char *argv[] = { "xz", NULL };
91 return write_compressed_tar_archive(hex, prefix, argv);
94 const struct cgit_snapshot_format cgit_snapshot_formats[] = {
95 /* .tar must remain the 0 index */
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 },
99 { ".tar.lz", "application/x-lzip", write_tar_lzip_archive },
100 { ".tar.xz", "application/x-xz", write_tar_xz_archive },
101 { ".zip", "application/x-zip", write_zip_archive },
105 static struct notes_tree snapshot_sig_notes[ARRAY_SIZE(cgit_snapshot_formats)];
107 const struct object_id *cgit_snapshot_get_sig(const char *ref,
108 const struct cgit_snapshot_format *f)
110 struct notes_tree *tree;
111 struct object_id oid;
113 if (get_oid(ref, &oid))
116 tree = &snapshot_sig_notes[f - &cgit_snapshot_formats[0]];
117 if (!tree->initialized) {
118 struct strbuf notes_ref = STRBUF_INIT;
120 strbuf_addf(¬es_ref, "refs/notes/signatures/%s",
123 init_notes(tree, notes_ref.buf, combine_notes_ignore, 0);
124 strbuf_release(¬es_ref);
127 return get_note(tree, &oid);
130 static const struct cgit_snapshot_format *get_format(const char *filename)
132 const struct cgit_snapshot_format *fmt;
134 for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
135 if (ends_with(filename, fmt->suffix))
141 const unsigned cgit_snapshot_format_bit(const struct cgit_snapshot_format *f)
143 return BIT(f - &cgit_snapshot_formats[0]);
146 static int make_snapshot(const struct cgit_snapshot_format *format,
147 const char *hex, const char *prefix,
148 const char *filename)
150 struct object_id oid;
152 if (get_oid(hex, &oid)) {
153 cgit_print_error_page(404, "Not found",
154 "Bad object id: %s", hex);
157 if (!lookup_commit_reference(the_repository, &oid)) {
158 cgit_print_error_page(400, "Bad request",
159 "Not a commit reference: %s", hex);
162 ctx.page.etag = oid_to_hex(&oid);
163 ctx.page.mimetype = xstrdup(format->mimetype);
164 ctx.page.filename = xstrdup(filename);
165 cgit_print_http_headers();
167 format->write_func(hex, prefix);
171 static int write_sig(const struct cgit_snapshot_format *format,
172 const char *hex, const char *archive,
173 const char *filename)
175 const struct object_id *note = cgit_snapshot_get_sig(hex, format);
176 enum object_type type;
181 cgit_print_error_page(404, "Not found",
182 "No signature for %s", archive);
186 buf = read_object_file(note, &type, &size);
188 cgit_print_error_page(404, "Not found", "Not found");
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();
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-
211 * pending a 'v' or a 'V' to the remaining snapshot name ("0.7.2" ->
212 * "v0.7.2") gives us something valid.
214 static const char *get_ref_from_filename(const struct cgit_repo *repo,
215 const char *filename,
216 const struct cgit_snapshot_format *format)
218 const char *reponame;
219 struct object_id oid;
220 struct strbuf snapshot = STRBUF_INIT;
223 strbuf_addstr(&snapshot, filename);
224 strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix));
226 if (get_oid(snapshot.buf, &oid) == 0)
229 reponame = cgit_snapshot_prefix(repo);
230 if (starts_with(snapshot.buf, reponame)) {
231 const char *new_start = snapshot.buf;
232 new_start += strlen(reponame);
233 while (new_start && (*new_start == '-' || *new_start == '_'))
235 strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0);
238 if (get_oid(snapshot.buf, &oid) == 0)
241 strbuf_insert(&snapshot, 0, "v", 1);
242 if (get_oid(snapshot.buf, &oid) == 0)
245 strbuf_splice(&snapshot, 0, 1, "V", 1);
246 if (get_oid(snapshot.buf, &oid) == 0)
250 strbuf_release(&snapshot);
253 return result ? strbuf_detach(&snapshot, NULL) : NULL;
256 void cgit_print_snapshot(const char *head, const char *hex,
257 const char *filename, int dwim)
259 const struct cgit_snapshot_format* f;
260 const char *sig_filename = NULL;
261 char *adj_filename = NULL;
265 cgit_print_error_page(400, "Bad request",
266 "No snapshot name specified");
270 if (ends_with(filename, ".asc")) {
271 sig_filename = filename;
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;
279 f = get_format(filename);
280 if (!f || (!sig_filename && !(ctx.repo->snapshots & cgit_snapshot_format_bit(f)))) {
281 cgit_print_error_page(400, "Bad request",
282 "Unsupported snapshot format: %s", filename);
287 hex = get_ref_from_filename(ctx.repo, filename, f);
289 cgit_print_error_page(404, "Not found", "Not found");
292 prefix = xstrdup(filename);
293 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
300 prefix = xstrdup(cgit_snapshot_prefix(ctx.repo));
303 write_sig(f, hex, filename, sig_filename);
305 make_snapshot(f, hex, prefix, filename);