]> gitweb.ps.run Git - ps-cgit/blob - ui-snapshot.c
git: update to v2.25.1
[ps-cgit] / 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, the_repository, 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         /* .tar must remain the 0 index */
90         { ".tar",       "application/x-tar",    write_tar_archive       },
91         { ".tar.gz",    "application/x-gzip",   write_tar_gzip_archive  },
92         { ".tar.bz2",   "application/x-bzip2",  write_tar_bzip2_archive },
93         { ".tar.xz",    "application/x-xz",     write_tar_xz_archive    },
94         { ".zip",       "application/x-zip",    write_zip_archive       },
95         { NULL }
96 };
97
98 static struct notes_tree snapshot_sig_notes[ARRAY_SIZE(cgit_snapshot_formats)];
99
100 const struct object_id *cgit_snapshot_get_sig(const char *ref,
101                                               const struct cgit_snapshot_format *f)
102 {
103         struct notes_tree *tree;
104         struct object_id oid;
105
106         if (get_oid(ref, &oid))
107                 return NULL;
108
109         tree = &snapshot_sig_notes[f - &cgit_snapshot_formats[0]];
110         if (!tree->initialized) {
111                 struct strbuf notes_ref = STRBUF_INIT;
112
113                 strbuf_addf(&notes_ref, "refs/notes/signatures/%s",
114                             f->suffix + 1);
115
116                 init_notes(tree, notes_ref.buf, combine_notes_ignore, 0);
117                 strbuf_release(&notes_ref);
118         }
119
120         return get_note(tree, &oid);
121 }
122
123 static const struct cgit_snapshot_format *get_format(const char *filename)
124 {
125         const struct cgit_snapshot_format *fmt;
126
127         for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
128                 if (ends_with(filename, fmt->suffix))
129                         return fmt;
130         }
131         return NULL;
132 }
133
134 const unsigned cgit_snapshot_format_bit(const struct cgit_snapshot_format *f)
135 {
136         return BIT(f - &cgit_snapshot_formats[0]);
137 }
138
139 static int make_snapshot(const struct cgit_snapshot_format *format,
140                          const char *hex, const char *prefix,
141                          const char *filename)
142 {
143         struct object_id oid;
144
145         if (get_oid(hex, &oid)) {
146                 cgit_print_error_page(404, "Not found",
147                                 "Bad object id: %s", hex);
148                 return 1;
149         }
150         if (!lookup_commit_reference(the_repository, &oid)) {
151                 cgit_print_error_page(400, "Bad request",
152                                 "Not a commit reference: %s", hex);
153                 return 1;
154         }
155         ctx.page.etag = oid_to_hex(&oid);
156         ctx.page.mimetype = xstrdup(format->mimetype);
157         ctx.page.filename = xstrdup(filename);
158         cgit_print_http_headers();
159         init_archivers();
160         format->write_func(hex, prefix);
161         return 0;
162 }
163
164 static int write_sig(const struct cgit_snapshot_format *format,
165                      const char *hex, const char *archive,
166                      const char *filename)
167 {
168         const struct object_id *note = cgit_snapshot_get_sig(hex, format);
169         enum object_type type;
170         unsigned long size;
171         char *buf;
172
173         if (!note) {
174                 cgit_print_error_page(404, "Not found",
175                                 "No signature for %s", archive);
176                 return 0;
177         }
178
179         buf = read_object_file(note, &type, &size);
180         if (!buf) {
181                 cgit_print_error_page(404, "Not found", "Not found");
182                 return 0;
183         }
184
185         html("X-Content-Type-Options: nosniff\n");
186         html("Content-Security-Policy: default-src 'none'\n");
187         ctx.page.etag = oid_to_hex(note);
188         ctx.page.mimetype = xstrdup("application/pgp-signature");
189         ctx.page.filename = xstrdup(filename);
190         cgit_print_http_headers();
191
192         html_raw(buf, size);
193         free(buf);
194         return 0;
195 }
196
197 /* Try to guess the requested revision from the requested snapshot name.
198  * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
199  * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
200  * Otherwise, if the snapshot name has a prefix matching the result from
201  * repo_basename(), we strip the basename and any following '-' and '_'
202  * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
203  * more. If this still isn't a valid commit object name, we check if pre-
204  * pending a 'v' or a 'V' to the remaining snapshot name ("0.7.2" ->
205  * "v0.7.2") gives us something valid.
206  */
207 static const char *get_ref_from_filename(const struct cgit_repo *repo,
208                                          const char *filename,
209                                          const struct cgit_snapshot_format *format)
210 {
211         const char *reponame;
212         struct object_id oid;
213         struct strbuf snapshot = STRBUF_INIT;
214         int result = 1;
215
216         strbuf_addstr(&snapshot, filename);
217         strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix));
218
219         if (get_oid(snapshot.buf, &oid) == 0)
220                 goto out;
221
222         reponame = cgit_snapshot_prefix(repo);
223         if (starts_with(snapshot.buf, reponame)) {
224                 const char *new_start = snapshot.buf;
225                 new_start += strlen(reponame);
226                 while (new_start && (*new_start == '-' || *new_start == '_'))
227                         new_start++;
228                 strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0);
229         }
230
231         if (get_oid(snapshot.buf, &oid) == 0)
232                 goto out;
233
234         strbuf_insert(&snapshot, 0, "v", 1);
235         if (get_oid(snapshot.buf, &oid) == 0)
236                 goto out;
237
238         strbuf_splice(&snapshot, 0, 1, "V", 1);
239         if (get_oid(snapshot.buf, &oid) == 0)
240                 goto out;
241
242         result = 0;
243         strbuf_release(&snapshot);
244
245 out:
246         return result ? strbuf_detach(&snapshot, NULL) : NULL;
247 }
248
249 void cgit_print_snapshot(const char *head, const char *hex,
250                          const char *filename, int dwim)
251 {
252         const struct cgit_snapshot_format* f;
253         const char *sig_filename = NULL;
254         char *adj_filename = NULL;
255         char *prefix = NULL;
256
257         if (!filename) {
258                 cgit_print_error_page(400, "Bad request",
259                                 "No snapshot name specified");
260                 return;
261         }
262
263         if (ends_with(filename, ".asc")) {
264                 sig_filename = filename;
265
266                 /* Strip ".asc" from filename for common format processing */
267                 adj_filename = xstrdup(filename);
268                 adj_filename[strlen(adj_filename) - 4] = '\0';
269                 filename = adj_filename;
270         }
271
272         f = get_format(filename);
273         if (!f || (!sig_filename && !(ctx.repo->snapshots & cgit_snapshot_format_bit(f)))) {
274                 cgit_print_error_page(400, "Bad request",
275                                 "Unsupported snapshot format: %s", filename);
276                 return;
277         }
278
279         if (!hex && dwim) {
280                 hex = get_ref_from_filename(ctx.repo, filename, f);
281                 if (hex == NULL) {
282                         cgit_print_error_page(404, "Not found", "Not found");
283                         return;
284                 }
285                 prefix = xstrdup(filename);
286                 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
287         }
288
289         if (!hex)
290                 hex = head;
291
292         if (!prefix)
293                 prefix = xstrdup(cgit_snapshot_prefix(ctx.repo));
294
295         if (sig_filename)
296                 write_sig(f, hex, filename, sig_filename);
297         else
298                 make_snapshot(f, hex, prefix, filename);
299
300         free(prefix);
301         free(adj_filename);
302 }