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