all repos — cgit @ c712d5ac434b9ee8cb4e63a173a2538e1878637f

a hyperfast web frontend for git written in c

ui-snapshot.c (view raw)

  1/* ui-snapshot.c: generate snapshot of a commit
  2 *
  3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
  4 *
  5 * Licensed under GNU General Public License v2
  6 *   (see COPYING for full license text)
  7 */
  8
  9#include "cgit.h"
 10#include "ui-snapshot.h"
 11#include "html.h"
 12#include "ui-shared.h"
 13
 14static int write_archive_type(const char *format, const char *hex, const char *prefix)
 15{
 16	struct argv_array argv = ARGV_ARRAY_INIT;
 17	const char **nargv;
 18	int result;
 19	argv_array_push(&argv, "snapshot");
 20	argv_array_push(&argv, format);
 21	if (prefix) {
 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);
 27		strbuf_release(&buf);
 28	}
 29	argv_array_push(&argv, hex);
 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
 40	result = write_archive(argv.argc, nargv, NULL, NULL, 0);
 41	argv_array_clear(&argv);
 42	free(nargv);
 43	return result;
 44}
 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[])
 59{
 60	int rv;
 61	struct cgit_exec_filter f;
 62	cgit_exec_filter_init(&f, filter_argv[0], filter_argv);
 63
 64	cgit_open_filter(&f.base);
 65	rv = write_tar_archive(hex, prefix);
 66	cgit_close_filter(&f.base);
 67	return rv;
 68}
 69
 70static int write_tar_gzip_archive(const char *hex, const char *prefix)
 71{
 72	char *argv[] = { "gzip", "-n", NULL };
 73	return write_compressed_tar_archive(hex, prefix, argv);
 74}
 75
 76static int write_tar_bzip2_archive(const char *hex, const char *prefix)
 77{
 78	char *argv[] = { "bzip2", NULL };
 79	return write_compressed_tar_archive(hex, prefix, argv);
 80}
 81
 82static int write_tar_xz_archive(const char *hex, const char *prefix)
 83{
 84	char *argv[] = { "xz", NULL };
 85	return write_compressed_tar_archive(hex, prefix, argv);
 86}
 87
 88const struct cgit_snapshot_format cgit_snapshot_formats[] = {
 89	{ ".zip", "application/x-zip", write_zip_archive, 0x01 },
 90	{ ".tar.gz", "application/x-gzip", write_tar_gzip_archive, 0x02 },
 91	{ ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive, 0x04 },
 92	{ ".tar", "application/x-tar", write_tar_archive, 0x08 },
 93	{ ".tar.xz", "application/x-xz", write_tar_xz_archive, 0x10 },
 94	{ NULL }
 95};
 96
 97static struct notes_tree snapshot_sig_notes[ARRAY_SIZE(cgit_snapshot_formats)];
 98
 99const struct object_id *cgit_snapshot_get_sig(const char *ref,
100					      const struct cgit_snapshot_format *f)
101{
102	struct notes_tree *tree;
103	struct object_id oid;
104
105	if (get_oid(ref, &oid))
106		return NULL;
107
108	tree = &snapshot_sig_notes[f - &cgit_snapshot_formats[0]];
109	if (!tree->initialized) {
110		struct strbuf notes_ref = STRBUF_INIT;
111
112		strbuf_addf(&notes_ref, "refs/notes/signatures/%s",
113			    f->suffix + 1);
114
115		init_notes(tree, notes_ref.buf, combine_notes_ignore, 0);
116		strbuf_release(&notes_ref);
117	}
118
119	return get_note(tree, &oid);
120}
121
122static const struct cgit_snapshot_format *get_format(const char *filename)
123{
124	const struct cgit_snapshot_format *fmt;
125
126	for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
127		if (ends_with(filename, fmt->suffix))
128			return fmt;
129	}
130	return NULL;
131}
132
133static int make_snapshot(const struct cgit_snapshot_format *format,
134			 const char *hex, const char *prefix,
135			 const char *filename)
136{
137	struct object_id oid;
138
139	if (get_oid(hex, &oid)) {
140		cgit_print_error_page(404, "Not found",
141				"Bad object id: %s", hex);
142		return 1;
143	}
144	if (!lookup_commit_reference(&oid)) {
145		cgit_print_error_page(400, "Bad request",
146				"Not a commit reference: %s", hex);
147		return 1;
148	}
149	ctx.page.etag = oid_to_hex(&oid);
150	ctx.page.mimetype = xstrdup(format->mimetype);
151	ctx.page.filename = xstrdup(filename);
152	cgit_print_http_headers();
153	format->write_func(hex, prefix);
154	return 0;
155}
156
157static int write_sig(const struct cgit_snapshot_format *format,
158		     const char *hex, const char *archive,
159		     const char *filename)
160{
161	const struct object_id *note = cgit_snapshot_get_sig(hex, format);
162	enum object_type type;
163	unsigned long size;
164	char *buf;
165
166	if (!note) {
167		cgit_print_error_page(404, "Not found",
168				"No signature for %s", archive);
169		return 0;
170	}
171
172	buf = read_sha1_file(note->hash, &type, &size);
173	if (!buf) {
174		cgit_print_error_page(404, "Not found", "Not found");
175		return 0;
176	}
177
178	html("X-Content-Type-Options: nosniff\n");
179	html("Content-Security-Policy: default-src 'none'\n");
180	ctx.page.etag = oid_to_hex(note);
181	ctx.page.mimetype = xstrdup("application/pgp-signature");
182	ctx.page.filename = xstrdup(filename);
183	cgit_print_http_headers();
184
185	html_raw(buf, size);
186	free(buf);
187	return 0;
188}
189
190/* Try to guess the requested revision from the requested snapshot name.
191 * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
192 * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
193 * Otherwise, if the snapshot name has a prefix matching the result from
194 * repo_basename(), we strip the basename and any following '-' and '_'
195 * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
196 * more. If this still isn't a valid commit object name, we check if pre-
197 * pending a 'v' or a 'V' to the remaining snapshot name ("0.7.2" ->
198 * "v0.7.2") gives us something valid.
199 */
200static const char *get_ref_from_filename(const struct cgit_repo *repo,
201					 const char *filename,
202					 const struct cgit_snapshot_format *format)
203{
204	const char *reponame;
205	struct object_id oid;
206	struct strbuf snapshot = STRBUF_INIT;
207	int result = 1;
208
209	strbuf_addstr(&snapshot, filename);
210	strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix));
211
212	if (get_oid(snapshot.buf, &oid) == 0)
213		goto out;
214
215	reponame = cgit_snapshot_prefix(repo);
216	if (starts_with(snapshot.buf, reponame)) {
217		const char *new_start = snapshot.buf;
218		new_start += strlen(reponame);
219		while (new_start && (*new_start == '-' || *new_start == '_'))
220			new_start++;
221		strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0);
222	}
223
224	if (get_oid(snapshot.buf, &oid) == 0)
225		goto out;
226
227	strbuf_insert(&snapshot, 0, "v", 1);
228	if (get_oid(snapshot.buf, &oid) == 0)
229		goto out;
230
231	strbuf_splice(&snapshot, 0, 1, "V", 1);
232	if (get_oid(snapshot.buf, &oid) == 0)
233		goto out;
234
235	result = 0;
236	strbuf_release(&snapshot);
237
238out:
239	return result ? strbuf_detach(&snapshot, NULL) : NULL;
240}
241
242void cgit_print_snapshot(const char *head, const char *hex,
243			 const char *filename, int dwim)
244{
245	const struct cgit_snapshot_format* f;
246	const char *sig_filename = NULL;
247	char *adj_filename = NULL;
248	char *prefix = NULL;
249
250	if (!filename) {
251		cgit_print_error_page(400, "Bad request",
252				"No snapshot name specified");
253		return;
254	}
255
256	if (ends_with(filename, ".asc")) {
257		sig_filename = filename;
258
259		/* Strip ".asc" from filename for common format processing */
260		adj_filename = xstrdup(filename);
261		adj_filename[strlen(adj_filename) - 4] = '\0';
262		filename = adj_filename;
263	}
264
265	f = get_format(filename);
266	if (!f || !(ctx.repo->snapshots & f->bit)) {
267		cgit_print_error_page(400, "Bad request",
268				"Unsupported snapshot format: %s", filename);
269		return;
270	}
271
272	if (!hex && dwim) {
273		hex = get_ref_from_filename(ctx.repo, filename, f);
274		if (hex == NULL) {
275			cgit_print_error_page(404, "Not found", "Not found");
276			return;
277		}
278		prefix = xstrdup(filename);
279		prefix[strlen(filename) - strlen(f->suffix)] = '\0';
280	}
281
282	if (!hex)
283		hex = head;
284
285	if (!prefix)
286		prefix = xstrdup(cgit_snapshot_prefix(ctx.repo));
287
288	if (sig_filename)
289		write_sig(f, hex, filename, sig_filename);
290	else
291		make_snapshot(f, hex, prefix, filename);
292
293	free(prefix);
294	free(adj_filename);
295}