Lars Hjemli | ab2ab95 | 2007-02-08 13:53:13 +0100 | [diff] [blame] | 1 | /* ui-snapshot.c: generate snapshot of a commit |
| 2 | * |
| 3 | * Copyright (C) 2006 Lars Hjemli |
Jason A. Donenfeld | 055e092 | 2012-09-26 02:56:38 +0200 | [diff] [blame] | 4 | * Copyright (C) 2012 Jason A. Donenfeld <Jason@zx2c4.com> |
Lars Hjemli | ab2ab95 | 2007-02-08 13:53:13 +0100 | [diff] [blame] | 5 | * |
| 6 | * Licensed under GNU General Public License v2 |
| 7 | * (see COPYING for full license text) |
| 8 | */ |
| 9 | |
| 10 | #include "cgit.h" |
John Keeping | 8f20879 | 2013-04-06 11:37:59 +0100 | [diff] [blame] | 11 | #include "ui-snapshot.h" |
Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 12 | #include "html.h" |
Lars Hjemli | a4d1ca1 | 2008-03-24 16:50:57 +0100 | [diff] [blame] | 13 | #include "ui-shared.h" |
Lars Hjemli | ab2ab95 | 2007-02-08 13:53:13 +0100 | [diff] [blame] | 14 | |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 15 | static int write_archive_type(const char *format, const char *hex, const char *prefix) |
| 16 | { |
| 17 | struct argv_array argv = ARGV_ARRAY_INIT; |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 18 | const char **nargv; |
| 19 | int result; |
Jason A. Donenfeld | 973deda | 2013-03-03 23:41:53 -0500 | [diff] [blame] | 20 | argv_array_push(&argv, "snapshot"); |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 21 | argv_array_push(&argv, format); |
| 22 | if (prefix) { |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 23 | struct strbuf buf = STRBUF_INIT; |
| 24 | strbuf_addstr(&buf, prefix); |
| 25 | strbuf_addch(&buf, '/'); |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 26 | argv_array_push(&argv, "--prefix"); |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 27 | argv_array_push(&argv, buf.buf); |
| 28 | strbuf_release(&buf); |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 29 | } |
| 30 | argv_array_push(&argv, hex); |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 31 | /* |
| 32 | * Now we need to copy the pointers to arguments into a new |
| 33 | * structure because write_archive will rearrange its arguments |
| 34 | * which may result in duplicated/missing entries causing leaks |
| 35 | * or double-frees in argv_array_clear. |
| 36 | */ |
| 37 | nargv = xmalloc(sizeof(char *) * (argv.argc + 1)); |
| 38 | /* argv_array guarantees a trailing NULL entry. */ |
| 39 | memcpy(nargv, argv.argv, sizeof(char *) * (argv.argc + 1)); |
| 40 | |
| 41 | result = write_archive(argv.argc, nargv, NULL, 1, NULL, 0); |
| 42 | argv_array_clear(&argv); |
| 43 | free(nargv); |
| 44 | return result; |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int write_tar_archive(const char *hex, const char *prefix) |
| 48 | { |
| 49 | return write_archive_type("--format=tar", hex, prefix); |
| 50 | } |
| 51 | |
| 52 | static int write_zip_archive(const char *hex, const char *prefix) |
| 53 | { |
| 54 | return write_archive_type("--format=zip", hex, prefix); |
| 55 | } |
| 56 | |
| 57 | static int write_compressed_tar_archive(const char *hex, |
| 58 | const char *prefix, |
| 59 | char *filter_argv[]) |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 60 | { |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 61 | int rv; |
Lars Hjemli | 18dfbdc | 2009-07-31 15:52:57 +0200 | [diff] [blame] | 62 | struct cgit_filter f; |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 63 | |
Jason A. Donenfeld | 055e092 | 2012-09-26 02:56:38 +0200 | [diff] [blame] | 64 | f.cmd = filter_argv[0]; |
| 65 | f.argv = filter_argv; |
Lars Hjemli | 3ec6b30 | 2011-06-06 19:29:58 +0000 | [diff] [blame] | 66 | cgit_open_filter(&f); |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 67 | rv = write_tar_archive(hex, prefix); |
Lars Hjemli | 18dfbdc | 2009-07-31 15:52:57 +0200 | [diff] [blame] | 68 | cgit_close_filter(&f); |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 69 | return rv; |
| 70 | } |
| 71 | |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 72 | static int write_tar_gzip_archive(const char *hex, const char *prefix) |
Michael Krelin | 4a92cbb | 2007-07-20 20:58:23 +0200 | [diff] [blame] | 73 | { |
Jason A. Donenfeld | 055e092 | 2012-09-26 02:56:38 +0200 | [diff] [blame] | 74 | char *argv[] = { "gzip", "-n", NULL }; |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 75 | return write_compressed_tar_archive(hex, prefix, argv); |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 76 | } |
Lars Hjemli | 1221adb | 2007-07-23 22:51:45 +0200 | [diff] [blame] | 77 | |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 78 | static int write_tar_bzip2_archive(const char *hex, const char *prefix) |
Michael Krelin | 18a99bd | 2007-07-21 02:05:34 +0200 | [diff] [blame] | 79 | { |
Jason A. Donenfeld | 055e092 | 2012-09-26 02:56:38 +0200 | [diff] [blame] | 80 | char *argv[] = { "bzip2", NULL }; |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 81 | return write_compressed_tar_archive(hex, prefix, argv); |
Michael Krelin | 4a92cbb | 2007-07-20 20:58:23 +0200 | [diff] [blame] | 82 | } |
| 83 | |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 84 | static int write_tar_xz_archive(const char *hex, const char *prefix) |
Andreas Wiese | 0642435 | 2009-12-08 22:18:11 +0100 | [diff] [blame] | 85 | { |
Jason A. Donenfeld | 055e092 | 2012-09-26 02:56:38 +0200 | [diff] [blame] | 86 | char *argv[] = { "xz", NULL }; |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 87 | return write_compressed_tar_archive(hex, prefix, argv); |
Andreas Wiese | 0642435 | 2009-12-08 22:18:11 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 90 | const struct cgit_snapshot_format cgit_snapshot_formats[] = { |
Andreas Wiese | 0642435 | 2009-12-08 22:18:11 +0100 | [diff] [blame] | 91 | { ".zip", "application/x-zip", write_zip_archive, 0x01 }, |
| 92 | { ".tar.gz", "application/x-gzip", write_tar_gzip_archive, 0x02 }, |
| 93 | { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive, 0x04 }, |
| 94 | { ".tar", "application/x-tar", write_tar_archive, 0x08 }, |
| 95 | { ".tar.xz", "application/x-xz", write_tar_xz_archive, 0x10 }, |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 96 | { NULL } |
Michael Krelin | f97c707 | 2007-07-18 14:40:03 +0200 | [diff] [blame] | 97 | }; |
Lars Hjemli | ab2ab95 | 2007-02-08 13:53:13 +0100 | [diff] [blame] | 98 | |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 99 | static const struct cgit_snapshot_format *get_format(const char *filename) |
| 100 | { |
| 101 | const struct cgit_snapshot_format *fmt; |
| 102 | int fl, sl; |
| 103 | |
| 104 | fl = strlen(filename); |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 105 | for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) { |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 106 | sl = strlen(fmt->suffix); |
| 107 | if (sl >= fl) |
| 108 | continue; |
| 109 | if (!strcmp(fmt->suffix, filename + fl - sl)) |
| 110 | return fmt; |
| 111 | } |
| 112 | return NULL; |
| 113 | } |
| 114 | |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 115 | static int make_snapshot(const struct cgit_snapshot_format *format, |
| 116 | const char *hex, const char *prefix, |
| 117 | const char *filename) |
| 118 | { |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 119 | unsigned char sha1[20]; |
| 120 | |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 121 | if (get_sha1(hex, sha1)) { |
John Keeping | ed5bd30 | 2013-04-06 11:23:52 +0100 | [diff] [blame] | 122 | cgit_print_error("Bad object id: %s", hex); |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 123 | return 1; |
| 124 | } |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 125 | if (!lookup_commit_reference(sha1)) { |
John Keeping | ed5bd30 | 2013-04-06 11:23:52 +0100 | [diff] [blame] | 126 | cgit_print_error("Not a commit reference: %s", hex); |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 127 | return 1; |
| 128 | } |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 129 | ctx.page.mimetype = xstrdup(format->mimetype); |
| 130 | ctx.page.filename = xstrdup(filename); |
| 131 | cgit_print_http_headers(&ctx); |
John Keeping | 2ab1cd9 | 2013-03-02 12:32:12 +0000 | [diff] [blame] | 132 | format->write_func(hex, prefix); |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 133 | return 0; |
| 134 | } |
Lars Hjemli | 1221adb | 2007-07-23 22:51:45 +0200 | [diff] [blame] | 135 | |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 136 | /* Try to guess the requested revision from the requested snapshot name. |
| 137 | * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become |
| 138 | * "cgit-0.7.2". If this is a valid commit object name we've got a winner. |
| 139 | * Otherwise, if the snapshot name has a prefix matching the result from |
| 140 | * repo_basename(), we strip the basename and any following '-' and '_' |
| 141 | * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once |
| 142 | * more. If this still isn't a valid commit object name, we check if pre- |
Lukas Fleischer | 8c4c2c4 | 2013-04-10 13:04:03 +0200 | [diff] [blame^] | 143 | * pending a 'v' or a 'V' to the remaining snapshot name ("0.7.2" -> |
| 144 | * "v0.7.2") gives us something valid. |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 145 | */ |
| 146 | static const char *get_ref_from_filename(const char *url, const char *filename, |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 147 | const struct cgit_snapshot_format *format) |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 148 | { |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 149 | const char *reponame; |
| 150 | unsigned char sha1[20]; |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 151 | struct strbuf snapshot = STRBUF_INIT; |
| 152 | int result = 1; |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 153 | |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 154 | strbuf_addstr(&snapshot, filename); |
| 155 | strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix)); |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 156 | |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 157 | if (get_sha1(snapshot.buf, sha1) == 0) |
| 158 | goto out; |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 159 | |
| 160 | reponame = cgit_repobasename(url); |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 161 | if (prefixcmp(snapshot.buf, reponame) == 0) { |
| 162 | const char *new_start = snapshot.buf; |
| 163 | new_start += strlen(reponame); |
| 164 | while (new_start && (*new_start == '-' || *new_start == '_')) |
| 165 | new_start++; |
| 166 | strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0); |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 167 | } |
| 168 | |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 169 | if (get_sha1(snapshot.buf, sha1) == 0) |
| 170 | goto out; |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 171 | |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 172 | strbuf_insert(&snapshot, 0, "v", 1); |
| 173 | if (get_sha1(snapshot.buf, sha1) == 0) |
| 174 | goto out; |
Lars Hjemli | 4b4f8d1 | 2008-12-01 19:13:44 +0100 | [diff] [blame] | 175 | |
Lukas Fleischer | 8c4c2c4 | 2013-04-10 13:04:03 +0200 | [diff] [blame^] | 176 | strbuf_splice(&snapshot, 0, 1, "V", 1); |
| 177 | if (get_sha1(snapshot.buf, sha1) == 0) |
| 178 | goto out; |
| 179 | |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 180 | result = 0; |
| 181 | strbuf_release(&snapshot); |
| 182 | |
| 183 | out: |
| 184 | return result ? strbuf_detach(&snapshot, NULL) : NULL; |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 185 | } |
| 186 | |
John Keeping | ed5bd30 | 2013-04-06 11:23:52 +0100 | [diff] [blame] | 187 | __attribute__((format (printf, 1, 2))) |
| 188 | static void show_error(char *fmt, ...) |
Lars Hjemli | 6fddad7 | 2009-03-15 08:57:33 +0100 | [diff] [blame] | 189 | { |
John Keeping | ed5bd30 | 2013-04-06 11:23:52 +0100 | [diff] [blame] | 190 | va_list ap; |
| 191 | |
Lars Hjemli | 6fddad7 | 2009-03-15 08:57:33 +0100 | [diff] [blame] | 192 | ctx.page.mimetype = "text/html"; |
| 193 | cgit_print_http_headers(&ctx); |
| 194 | cgit_print_docstart(&ctx); |
| 195 | cgit_print_pageheader(&ctx); |
John Keeping | ed5bd30 | 2013-04-06 11:23:52 +0100 | [diff] [blame] | 196 | va_start(ap, fmt); |
| 197 | cgit_vprint_error(fmt, ap); |
| 198 | va_end(ap); |
Lars Hjemli | 6fddad7 | 2009-03-15 08:57:33 +0100 | [diff] [blame] | 199 | cgit_print_docend(); |
| 200 | } |
| 201 | |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 202 | void cgit_print_snapshot(const char *head, const char *hex, |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 203 | const char *filename, int snapshots, int dwim) |
Lars Hjemli | ab2ab95 | 2007-02-08 13:53:13 +0100 | [diff] [blame] | 204 | { |
Lars Hjemli | f34478c | 2008-03-24 16:00:27 +0100 | [diff] [blame] | 205 | const struct cgit_snapshot_format* f; |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 206 | char *prefix = NULL; |
Lars Hjemli | eb45342 | 2007-07-23 00:11:15 +0200 | [diff] [blame] | 207 | |
Lars Hjemli | 6fddad7 | 2009-03-15 08:57:33 +0100 | [diff] [blame] | 208 | if (!filename) { |
| 209 | show_error("No snapshot name specified"); |
| 210 | return; |
| 211 | } |
| 212 | |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 213 | f = get_format(filename); |
| 214 | if (!f) { |
John Keeping | ed5bd30 | 2013-04-06 11:23:52 +0100 | [diff] [blame] | 215 | show_error("Unsupported snapshot format: %s", filename); |
Michael Krelin | f97c707 | 2007-07-18 14:40:03 +0200 | [diff] [blame] | 216 | return; |
| 217 | } |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 218 | |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 219 | if (!hex && dwim) { |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 220 | hex = get_ref_from_filename(ctx.repo->url, filename, f); |
Natanael Copa | c4b45de | 2008-12-02 11:31:34 +0100 | [diff] [blame] | 221 | if (hex == NULL) { |
| 222 | html_status(404, "Not found", 0); |
| 223 | return; |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 224 | } |
Natanael Copa | c4b45de | 2008-12-02 11:31:34 +0100 | [diff] [blame] | 225 | prefix = xstrdup(filename); |
| 226 | prefix[strlen(filename) - strlen(f->suffix)] = '\0'; |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 227 | } |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 228 | |
| 229 | if (!hex) |
| 230 | hex = head; |
| 231 | |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 232 | if (!prefix) |
| 233 | prefix = xstrdup(cgit_repobasename(ctx.repo->url)); |
| 234 | |
Lars Hjemli | ed7ff09 | 2008-10-11 20:09:42 +0200 | [diff] [blame] | 235 | make_snapshot(f, hex, prefix, filename); |
Natanael Copa | 314d9ea | 2008-11-29 21:49:07 -0800 | [diff] [blame] | 236 | free(prefix); |
Michael Krelin | f97c707 | 2007-07-18 14:40:03 +0200 | [diff] [blame] | 237 | } |