]> git.cameronkatri.com Git - cgit.git/blob - ui-snapshot.c
email-gravatar.py: fix UTF-8
[cgit.git] / ui-snapshot.c
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
14 static 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, 1, NULL, 0);
41 argv_array_clear(&argv);
42 free(nargv);
43 return result;
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[])
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
70 static 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
76 static 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
82 static 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
88 const 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
97 static const struct cgit_snapshot_format *get_format(const char *filename)
98 {
99 const struct cgit_snapshot_format *fmt;
100
101 for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
102 if (!suffixcmp(filename, fmt->suffix))
103 return fmt;
104 }
105 return NULL;
106 }
107
108 static int make_snapshot(const struct cgit_snapshot_format *format,
109 const char *hex, const char *prefix,
110 const char *filename)
111 {
112 unsigned char sha1[20];
113
114 if (get_sha1(hex, sha1)) {
115 cgit_print_error("Bad object id: %s", hex);
116 return 1;
117 }
118 if (!lookup_commit_reference(sha1)) {
119 cgit_print_error("Not a commit reference: %s", hex);
120 return 1;
121 }
122 ctx.page.mimetype = xstrdup(format->mimetype);
123 ctx.page.filename = xstrdup(filename);
124 cgit_print_http_headers(&ctx);
125 format->write_func(hex, prefix);
126 return 0;
127 }
128
129 /* Try to guess the requested revision from the requested snapshot name.
130 * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
131 * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
132 * Otherwise, if the snapshot name has a prefix matching the result from
133 * repo_basename(), we strip the basename and any following '-' and '_'
134 * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
135 * more. If this still isn't a valid commit object name, we check if pre-
136 * pending a 'v' or a 'V' to the remaining snapshot name ("0.7.2" ->
137 * "v0.7.2") gives us something valid.
138 */
139 static const char *get_ref_from_filename(const char *url, const char *filename,
140 const struct cgit_snapshot_format *format)
141 {
142 const char *reponame;
143 unsigned char sha1[20];
144 struct strbuf snapshot = STRBUF_INIT;
145 int result = 1;
146
147 strbuf_addstr(&snapshot, filename);
148 strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix));
149
150 if (get_sha1(snapshot.buf, sha1) == 0)
151 goto out;
152
153 reponame = cgit_repobasename(url);
154 if (prefixcmp(snapshot.buf, reponame) == 0) {
155 const char *new_start = snapshot.buf;
156 new_start += strlen(reponame);
157 while (new_start && (*new_start == '-' || *new_start == '_'))
158 new_start++;
159 strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0);
160 }
161
162 if (get_sha1(snapshot.buf, sha1) == 0)
163 goto out;
164
165 strbuf_insert(&snapshot, 0, "v", 1);
166 if (get_sha1(snapshot.buf, sha1) == 0)
167 goto out;
168
169 strbuf_splice(&snapshot, 0, 1, "V", 1);
170 if (get_sha1(snapshot.buf, sha1) == 0)
171 goto out;
172
173 result = 0;
174 strbuf_release(&snapshot);
175
176 out:
177 return result ? strbuf_detach(&snapshot, NULL) : NULL;
178 }
179
180 __attribute__((format (printf, 1, 2)))
181 static void show_error(char *fmt, ...)
182 {
183 va_list ap;
184
185 ctx.page.mimetype = "text/html";
186 cgit_print_http_headers(&ctx);
187 cgit_print_docstart(&ctx);
188 cgit_print_pageheader(&ctx);
189 va_start(ap, fmt);
190 cgit_vprint_error(fmt, ap);
191 va_end(ap);
192 cgit_print_docend();
193 }
194
195 void cgit_print_snapshot(const char *head, const char *hex,
196 const char *filename, int snapshots, int dwim)
197 {
198 const struct cgit_snapshot_format* f;
199 char *prefix = NULL;
200
201 if (!filename) {
202 show_error("No snapshot name specified");
203 return;
204 }
205
206 f = get_format(filename);
207 if (!f) {
208 show_error("Unsupported snapshot format: %s", filename);
209 return;
210 }
211
212 if (!hex && dwim) {
213 hex = get_ref_from_filename(ctx.repo->url, filename, f);
214 if (hex == NULL) {
215 html_status(404, "Not found", 0);
216 return;
217 }
218 prefix = xstrdup(filename);
219 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
220 }
221
222 if (!hex)
223 hex = head;
224
225 if (!prefix)
226 prefix = xstrdup(cgit_repobasename(ctx.repo->url));
227
228 make_snapshot(f, hex, prefix, filename);
229 free(prefix);
230 }