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