]> gitweb.ps.run Git - ps-cgit/blob - ui-snapshot.c
git: update to v2.46.0
[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 #define USE_THE_REPOSITORY_VARIABLE
10
11 #include "cgit.h"
12 #include "ui-snapshot.h"
13 #include "html.h"
14 #include "ui-shared.h"
15
16 static int write_archive_type(const char *format, const char *hex, const char *prefix)
17 {
18         struct strvec argv = STRVEC_INIT;
19         const char **nargv;
20         int result;
21         strvec_push(&argv, "snapshot");
22         strvec_push(&argv, format);
23         if (prefix) {
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);
29                 strbuf_release(&buf);
30         }
31         strvec_push(&argv, hex);
32         /*
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.
37          */
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));
41
42         result = write_archive(argv.nr, nargv, NULL, the_repository, NULL, 0);
43         strvec_clear(&argv);
44         free(nargv);
45         return result;
46 }
47
48 static int write_tar_archive(const char *hex, const char *prefix)
49 {
50         return write_archive_type("--format=tar", hex, prefix);
51 }
52
53 static int write_zip_archive(const char *hex, const char *prefix)
54 {
55         return write_archive_type("--format=zip", hex, prefix);
56 }
57
58 static int write_compressed_tar_archive(const char *hex,
59                                         const char *prefix,
60                                         char *filter_argv[])
61 {
62         int rv;
63         struct cgit_exec_filter f;
64         cgit_exec_filter_init(&f, filter_argv[0], filter_argv);
65
66         cgit_open_filter(&f.base);
67         rv = write_tar_archive(hex, prefix);
68         cgit_close_filter(&f.base);
69         return rv;
70 }
71
72 static int write_tar_gzip_archive(const char *hex, const char *prefix)
73 {
74         char *argv[] = { "gzip", "-n", NULL };
75         return write_compressed_tar_archive(hex, prefix, argv);
76 }
77
78 static int write_tar_bzip2_archive(const char *hex, const char *prefix)
79 {
80         char *argv[] = { "bzip2", NULL };
81         return write_compressed_tar_archive(hex, prefix, argv);
82 }
83
84 static int write_tar_lzip_archive(const char *hex, const char *prefix)
85 {
86         char *argv[] = { "lzip", NULL };
87         return write_compressed_tar_archive(hex, prefix, argv);
88 }
89
90 static int write_tar_xz_archive(const char *hex, const char *prefix)
91 {
92         char *argv[] = { "xz", NULL };
93         return write_compressed_tar_archive(hex, prefix, argv);
94 }
95
96 static int write_tar_zstd_archive(const char *hex, const char *prefix)
97 {
98         char *argv[] = { "zstd", "-T0", NULL };
99         return write_compressed_tar_archive(hex, prefix, argv);
100 }
101
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       },
111         { NULL }
112 };
113
114 static struct notes_tree snapshot_sig_notes[ARRAY_SIZE(cgit_snapshot_formats)];
115
116 const struct object_id *cgit_snapshot_get_sig(const char *ref,
117                                               const struct cgit_snapshot_format *f)
118 {
119         struct notes_tree *tree;
120         struct object_id oid;
121
122         if (repo_get_oid(the_repository, ref, &oid))
123                 return NULL;
124
125         tree = &snapshot_sig_notes[f - &cgit_snapshot_formats[0]];
126         if (!tree->initialized) {
127                 struct strbuf notes_ref = STRBUF_INIT;
128
129                 strbuf_addf(&notes_ref, "refs/notes/signatures/%s",
130                             f->suffix + 1);
131
132                 init_notes(tree, notes_ref.buf, combine_notes_ignore, 0);
133                 strbuf_release(&notes_ref);
134         }
135
136         return get_note(tree, &oid);
137 }
138
139 static const struct cgit_snapshot_format *get_format(const char *filename)
140 {
141         const struct cgit_snapshot_format *fmt;
142
143         for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
144                 if (ends_with(filename, fmt->suffix))
145                         return fmt;
146         }
147         return NULL;
148 }
149
150 const unsigned cgit_snapshot_format_bit(const struct cgit_snapshot_format *f)
151 {
152         return BIT(f - &cgit_snapshot_formats[0]);
153 }
154
155 static int make_snapshot(const struct cgit_snapshot_format *format,
156                          const char *hex, const char *prefix,
157                          const char *filename)
158 {
159         struct object_id oid;
160
161         if (repo_get_oid(the_repository, hex, &oid)) {
162                 cgit_print_error_page(404, "Not found",
163                                 "Bad object id: %s", hex);
164                 return 1;
165         }
166         if (!lookup_commit_reference(the_repository, &oid)) {
167                 cgit_print_error_page(400, "Bad request",
168                                 "Not a commit reference: %s", hex);
169                 return 1;
170         }
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();
175         init_archivers();
176         format->write_func(hex, prefix);
177         return 0;
178 }
179
180 static int write_sig(const struct cgit_snapshot_format *format,
181                      const char *hex, const char *archive,
182                      const char *filename)
183 {
184         const struct object_id *note = cgit_snapshot_get_sig(hex, format);
185         enum object_type type;
186         unsigned long size;
187         char *buf;
188
189         if (!note) {
190                 cgit_print_error_page(404, "Not found",
191                                 "No signature for %s", archive);
192                 return 0;
193         }
194
195         buf = repo_read_object_file(the_repository, note, &type, &size);
196         if (!buf) {
197                 cgit_print_error_page(404, "Not found", "Not found");
198                 return 0;
199         }
200
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();
207
208         html_raw(buf, size);
209         free(buf);
210         return 0;
211 }
212
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.
222  */
223 static const char *get_ref_from_filename(const struct cgit_repo *repo,
224                                          const char *filename,
225                                          const struct cgit_snapshot_format *format)
226 {
227         const char *reponame;
228         struct object_id oid;
229         struct strbuf snapshot = STRBUF_INIT;
230         int result = 1;
231
232         strbuf_addstr(&snapshot, filename);
233         strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix));
234
235         if (repo_get_oid(the_repository, snapshot.buf, &oid) == 0)
236                 goto out;
237
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 == '_'))
243                         new_start++;
244                 strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0);
245         }
246
247         if (repo_get_oid(the_repository, snapshot.buf, &oid) == 0)
248                 goto out;
249
250         strbuf_insert(&snapshot, 0, "v", 1);
251         if (repo_get_oid(the_repository, snapshot.buf, &oid) == 0)
252                 goto out;
253
254         strbuf_splice(&snapshot, 0, 1, "V", 1);
255         if (repo_get_oid(the_repository, snapshot.buf, &oid) == 0)
256                 goto out;
257
258         result = 0;
259         strbuf_release(&snapshot);
260
261 out:
262         return result ? strbuf_detach(&snapshot, NULL) : NULL;
263 }
264
265 void cgit_print_snapshot(const char *head, const char *hex,
266                          const char *filename, int dwim)
267 {
268         const struct cgit_snapshot_format* f;
269         const char *sig_filename = NULL;
270         char *adj_filename = NULL;
271         char *prefix = NULL;
272
273         if (!filename) {
274                 cgit_print_error_page(400, "Bad request",
275                                 "No snapshot name specified");
276                 return;
277         }
278
279         if (ends_with(filename, ".asc")) {
280                 sig_filename = filename;
281
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;
286         }
287
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);
292                 return;
293         }
294
295         if (!hex && dwim) {
296                 hex = get_ref_from_filename(ctx.repo, filename, f);
297                 if (hex == NULL) {
298                         cgit_print_error_page(404, "Not found", "Not found");
299                         return;
300                 }
301                 prefix = xstrdup(filename);
302                 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
303         }
304
305         if (!hex)
306                 hex = head;
307
308         if (!prefix)
309                 prefix = xstrdup(cgit_snapshot_prefix(ctx.repo));
310
311         if (sig_filename)
312                 write_sig(f, hex, filename, sig_filename);
313         else
314                 make_snapshot(f, hex, prefix, filename);
315
316         free(prefix);
317         free(adj_filename);
318 }