]> gitweb.ps.run Git - ps-cgit/blob - ui-snapshot.c
Convert cgit_print_error to a variadic function
[ps-cgit] / ui-snapshot.c
1 /* ui-snapshot.c: generate snapshot of a commit
2  *
3  * Copyright (C) 2006 Lars Hjemli
4  * Copyright (C) 2012 Jason A. Donenfeld <Jason@zx2c4.com>
5  *
6  * Licensed under GNU General Public License v2
7  *   (see COPYING for full license text)
8  */
9
10 #include "cgit.h"
11 #include "ui-snapshot.h"
12 #include "html.h"
13 #include "ui-shared.h"
14
15 static int write_archive_type(const char *format, const char *hex, const char *prefix)
16 {
17         struct argv_array argv = ARGV_ARRAY_INIT;
18         argv_array_push(&argv, "snapshot");
19         argv_array_push(&argv, format);
20         if (prefix) {
21                 argv_array_push(&argv, "--prefix");
22                 argv_array_push(&argv, fmt("%s/", prefix));
23         }
24         argv_array_push(&argv, hex);
25         return write_archive(argv.argc, argv.argv, NULL, 1, NULL, 0);
26 }
27
28 static int write_tar_archive(const char *hex, const char *prefix)
29 {
30         return write_archive_type("--format=tar", hex, prefix);
31 }
32
33 static int write_zip_archive(const char *hex, const char *prefix)
34 {
35         return write_archive_type("--format=zip", hex, prefix);
36 }
37
38 static int write_compressed_tar_archive(const char *hex,
39                                         const char *prefix,
40                                         char *filter_argv[])
41 {
42         int rv;
43         struct cgit_filter f;
44
45         f.cmd = filter_argv[0];
46         f.argv = filter_argv;
47         cgit_open_filter(&f);
48         rv = write_tar_archive(hex, prefix);
49         cgit_close_filter(&f);
50         return rv;
51 }
52
53 static int write_tar_gzip_archive(const char *hex, const char *prefix)
54 {
55         char *argv[] = { "gzip", "-n", NULL };
56         return write_compressed_tar_archive(hex, prefix, argv);
57 }
58
59 static int write_tar_bzip2_archive(const char *hex, const char *prefix)
60 {
61         char *argv[] = { "bzip2", NULL };
62         return write_compressed_tar_archive(hex, prefix, argv);
63 }
64
65 static int write_tar_xz_archive(const char *hex, const char *prefix)
66 {
67         char *argv[] = { "xz", NULL };
68         return write_compressed_tar_archive(hex, prefix, argv);
69 }
70
71 const struct cgit_snapshot_format cgit_snapshot_formats[] = {
72         { ".zip", "application/x-zip", write_zip_archive, 0x01 },
73         { ".tar.gz", "application/x-gzip", write_tar_gzip_archive, 0x02 },
74         { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive, 0x04 },
75         { ".tar", "application/x-tar", write_tar_archive, 0x08 },
76         { ".tar.xz", "application/x-xz", write_tar_xz_archive, 0x10 },
77         { NULL }
78 };
79
80 static const struct cgit_snapshot_format *get_format(const char *filename)
81 {
82         const struct cgit_snapshot_format *fmt;
83         int fl, sl;
84
85         fl = strlen(filename);
86         for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
87                 sl = strlen(fmt->suffix);
88                 if (sl >= fl)
89                         continue;
90                 if (!strcmp(fmt->suffix, filename + fl - sl))
91                         return fmt;
92         }
93         return NULL;
94 }
95
96 static int make_snapshot(const struct cgit_snapshot_format *format,
97                          const char *hex, const char *prefix,
98                          const char *filename)
99 {
100         unsigned char sha1[20];
101
102         if (get_sha1(hex, sha1)) {
103                 cgit_print_error("Bad object id: %s", hex);
104                 return 1;
105         }
106         if (!lookup_commit_reference(sha1)) {
107                 cgit_print_error("Not a commit reference: %s", hex);
108                 return 1;
109         }
110         ctx.page.mimetype = xstrdup(format->mimetype);
111         ctx.page.filename = xstrdup(filename);
112         cgit_print_http_headers(&ctx);
113         format->write_func(hex, prefix);
114         return 0;
115 }
116
117 /* Try to guess the requested revision from the requested snapshot name.
118  * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
119  * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
120  * Otherwise, if the snapshot name has a prefix matching the result from
121  * repo_basename(), we strip the basename and any following '-' and '_'
122  * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
123  * more. If this still isn't a valid commit object name, we check if pre-
124  * pending a 'v' to the remaining snapshot name ("0.7.2" -> "v0.7.2") gives
125  * us something valid.
126  */
127 static const char *get_ref_from_filename(const char *url, const char *filename,
128                                          const struct cgit_snapshot_format *format)
129 {
130         const char *reponame;
131         unsigned char sha1[20];
132         char *snapshot;
133
134         snapshot = xstrdup(filename);
135         snapshot[strlen(snapshot) - strlen(format->suffix)] = '\0';
136
137         if (get_sha1(snapshot, sha1) == 0)
138                 return snapshot;
139
140         reponame = cgit_repobasename(url);
141         if (prefixcmp(snapshot, reponame) == 0) {
142                 snapshot += strlen(reponame);
143                 while (snapshot && (*snapshot == '-' || *snapshot == '_'))
144                         snapshot++;
145         }
146
147         if (get_sha1(snapshot, sha1) == 0)
148                 return snapshot;
149
150         snapshot = fmt("v%s", snapshot);
151         if (get_sha1(snapshot, sha1) == 0)
152                 return snapshot;
153
154         return NULL;
155 }
156
157 __attribute__((format (printf, 1, 2)))
158 static void show_error(char *fmt, ...)
159 {
160         va_list ap;
161
162         ctx.page.mimetype = "text/html";
163         cgit_print_http_headers(&ctx);
164         cgit_print_docstart(&ctx);
165         cgit_print_pageheader(&ctx);
166         va_start(ap, fmt);
167         cgit_vprint_error(fmt, ap);
168         va_end(ap);
169         cgit_print_docend();
170 }
171
172 void cgit_print_snapshot(const char *head, const char *hex,
173                          const char *filename, int snapshots, int dwim)
174 {
175         const struct cgit_snapshot_format* f;
176         char *prefix = NULL;
177
178         if (!filename) {
179                 show_error("No snapshot name specified");
180                 return;
181         }
182
183         f = get_format(filename);
184         if (!f) {
185                 show_error("Unsupported snapshot format: %s", filename);
186                 return;
187         }
188
189         if (!hex && dwim) {
190                 hex = get_ref_from_filename(ctx.repo->url, filename, f);
191                 if (hex == NULL) {
192                         html_status(404, "Not found", 0);
193                         return;
194                 }
195                 prefix = xstrdup(filename);
196                 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
197         }
198
199         if (!hex)
200                 hex = head;
201
202         if (!prefix)
203                 prefix = xstrdup(cgit_repobasename(ctx.repo->url));
204
205         make_snapshot(f, hex, prefix, filename);
206         free(prefix);
207 }