Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006 Rene Scharfe |
| 3 | */ |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 4 | #include "cache.h" |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 5 | #include "archive.h" |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 6 | #include "streaming.h" |
René Scharfe | 88182ba | 2012-09-18 21:46:56 +0200 | [diff] [blame] | 7 | #include "utf8.h" |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 8 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 9 | static int zip_date; |
| 10 | static int zip_time; |
| 11 | |
| 12 | static unsigned char *zip_dir; |
| 13 | static unsigned int zip_dir_size; |
| 14 | |
| 15 | static unsigned int zip_offset; |
| 16 | static unsigned int zip_dir_offset; |
| 17 | static unsigned int zip_dir_entries; |
| 18 | |
| 19 | #define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024) |
René Scharfe | 88182ba | 2012-09-18 21:46:56 +0200 | [diff] [blame] | 20 | #define ZIP_STREAM (1 << 3) |
| 21 | #define ZIP_UTF8 (1 << 11) |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 22 | |
| 23 | struct zip_local_header { |
| 24 | unsigned char magic[4]; |
| 25 | unsigned char version[2]; |
| 26 | unsigned char flags[2]; |
| 27 | unsigned char compression_method[2]; |
| 28 | unsigned char mtime[2]; |
| 29 | unsigned char mdate[2]; |
| 30 | unsigned char crc32[4]; |
| 31 | unsigned char compressed_size[4]; |
| 32 | unsigned char size[4]; |
| 33 | unsigned char filename_length[2]; |
| 34 | unsigned char extra_length[2]; |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 35 | unsigned char _end[1]; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 38 | struct zip_data_desc { |
| 39 | unsigned char magic[4]; |
| 40 | unsigned char crc32[4]; |
| 41 | unsigned char compressed_size[4]; |
| 42 | unsigned char size[4]; |
| 43 | unsigned char _end[1]; |
| 44 | }; |
| 45 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 46 | struct zip_dir_header { |
| 47 | unsigned char magic[4]; |
| 48 | unsigned char creator_version[2]; |
| 49 | unsigned char version[2]; |
| 50 | unsigned char flags[2]; |
| 51 | unsigned char compression_method[2]; |
| 52 | unsigned char mtime[2]; |
| 53 | unsigned char mdate[2]; |
| 54 | unsigned char crc32[4]; |
| 55 | unsigned char compressed_size[4]; |
| 56 | unsigned char size[4]; |
| 57 | unsigned char filename_length[2]; |
| 58 | unsigned char extra_length[2]; |
| 59 | unsigned char comment_length[2]; |
| 60 | unsigned char disk[2]; |
| 61 | unsigned char attr1[2]; |
| 62 | unsigned char attr2[4]; |
| 63 | unsigned char offset[4]; |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 64 | unsigned char _end[1]; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | struct zip_dir_trailer { |
| 68 | unsigned char magic[4]; |
| 69 | unsigned char disk[2]; |
| 70 | unsigned char directory_start_disk[2]; |
| 71 | unsigned char entries_on_this_disk[2]; |
| 72 | unsigned char entries[2]; |
| 73 | unsigned char size[4]; |
| 74 | unsigned char offset[4]; |
| 75 | unsigned char comment_length[2]; |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 76 | unsigned char _end[1]; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
René Scharfe | 227bf59 | 2012-09-24 17:56:23 +0200 | [diff] [blame] | 79 | struct zip_extra_mtime { |
| 80 | unsigned char magic[2]; |
| 81 | unsigned char extra_size[2]; |
| 82 | unsigned char flags[1]; |
| 83 | unsigned char mtime[4]; |
| 84 | unsigned char _end[1]; |
| 85 | }; |
| 86 | |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 87 | /* |
| 88 | * On ARM, padding is added at the end of the struct, so a simple |
| 89 | * sizeof(struct ...) reports two bytes more than the payload size |
| 90 | * we're interested in. |
| 91 | */ |
| 92 | #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end) |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 93 | #define ZIP_DATA_DESC_SIZE offsetof(struct zip_data_desc, _end) |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 94 | #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end) |
| 95 | #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end) |
René Scharfe | 227bf59 | 2012-09-24 17:56:23 +0200 | [diff] [blame] | 96 | #define ZIP_EXTRA_MTIME_SIZE offsetof(struct zip_extra_mtime, _end) |
| 97 | #define ZIP_EXTRA_MTIME_PAYLOAD_SIZE \ |
| 98 | (ZIP_EXTRA_MTIME_SIZE - offsetof(struct zip_extra_mtime, flags)) |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 99 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 100 | static void copy_le16(unsigned char *dest, unsigned int n) |
| 101 | { |
| 102 | dest[0] = 0xff & n; |
| 103 | dest[1] = 0xff & (n >> 010); |
| 104 | } |
| 105 | |
| 106 | static void copy_le32(unsigned char *dest, unsigned int n) |
| 107 | { |
| 108 | dest[0] = 0xff & n; |
| 109 | dest[1] = 0xff & (n >> 010); |
| 110 | dest[2] = 0xff & (n >> 020); |
| 111 | dest[3] = 0xff & (n >> 030); |
| 112 | } |
| 113 | |
René Scharfe | c3c2e1a | 2013-03-15 23:21:51 +0100 | [diff] [blame] | 114 | static void *zlib_deflate_raw(void *data, unsigned long size, |
| 115 | int compression_level, |
| 116 | unsigned long *compressed_size) |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 117 | { |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 118 | git_zstream stream; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 119 | unsigned long maxsize; |
| 120 | void *buffer; |
| 121 | int result; |
| 122 | |
| 123 | memset(&stream, 0, sizeof(stream)); |
René Scharfe | c3c2e1a | 2013-03-15 23:21:51 +0100 | [diff] [blame] | 124 | git_deflate_init_raw(&stream, compression_level); |
Junio C Hamano | 225a6f1 | 2011-06-10 11:18:17 -0700 | [diff] [blame] | 125 | maxsize = git_deflate_bound(&stream, size); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 126 | buffer = xmalloc(maxsize); |
| 127 | |
| 128 | stream.next_in = data; |
| 129 | stream.avail_in = size; |
| 130 | stream.next_out = buffer; |
| 131 | stream.avail_out = maxsize; |
| 132 | |
| 133 | do { |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 134 | result = git_deflate(&stream, Z_FINISH); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 135 | } while (result == Z_OK); |
| 136 | |
| 137 | if (result != Z_STREAM_END) { |
| 138 | free(buffer); |
| 139 | return NULL; |
| 140 | } |
| 141 | |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 142 | git_deflate_end(&stream); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 143 | *compressed_size = stream.total_out; |
| 144 | |
| 145 | return buffer; |
| 146 | } |
| 147 | |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 148 | static void write_zip_data_desc(unsigned long size, |
| 149 | unsigned long compressed_size, |
| 150 | unsigned long crc) |
| 151 | { |
| 152 | struct zip_data_desc trailer; |
| 153 | |
| 154 | copy_le32(trailer.magic, 0x08074b50); |
| 155 | copy_le32(trailer.crc32, crc); |
| 156 | copy_le32(trailer.compressed_size, compressed_size); |
| 157 | copy_le32(trailer.size, size); |
| 158 | write_or_die(1, &trailer, ZIP_DATA_DESC_SIZE); |
| 159 | } |
| 160 | |
René Scharfe | ebf5374 | 2012-05-03 08:51:06 +0700 | [diff] [blame] | 161 | static void set_zip_dir_data_desc(struct zip_dir_header *header, |
| 162 | unsigned long size, |
| 163 | unsigned long compressed_size, |
| 164 | unsigned long crc) |
| 165 | { |
| 166 | copy_le32(header->crc32, crc); |
| 167 | copy_le32(header->compressed_size, compressed_size); |
| 168 | copy_le32(header->size, size); |
| 169 | } |
| 170 | |
| 171 | static void set_zip_header_data_desc(struct zip_local_header *header, |
| 172 | unsigned long size, |
| 173 | unsigned long compressed_size, |
| 174 | unsigned long crc) |
| 175 | { |
| 176 | copy_le32(header->crc32, crc); |
| 177 | copy_le32(header->compressed_size, compressed_size); |
| 178 | copy_le32(header->size, size); |
| 179 | } |
| 180 | |
René Scharfe | 88182ba | 2012-09-18 21:46:56 +0200 | [diff] [blame] | 181 | static int has_only_ascii(const char *s) |
| 182 | { |
| 183 | for (;;) { |
| 184 | int c = *s++; |
| 185 | if (c == '\0') |
| 186 | return 1; |
| 187 | if (!isascii(c)) |
| 188 | return 0; |
| 189 | } |
| 190 | } |
| 191 | |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 192 | #define STREAM_BUFFER_SIZE (1024 * 16) |
| 193 | |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 194 | static int write_zip_entry(struct archiver_args *args, |
Nguyễn Thái Ngọc Duy | 9cb513b | 2012-05-03 08:51:03 +0700 | [diff] [blame] | 195 | const unsigned char *sha1, |
| 196 | const char *path, size_t pathlen, |
| 197 | unsigned int mode) |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 198 | { |
| 199 | struct zip_local_header header; |
| 200 | struct zip_dir_header dirent; |
René Scharfe | 227bf59 | 2012-09-24 17:56:23 +0200 | [diff] [blame] | 201 | struct zip_extra_mtime extra; |
Junio C Hamano | bb52d22 | 2012-09-18 13:32:39 -0700 | [diff] [blame] | 202 | unsigned long attr2; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 203 | unsigned long compressed_size; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 204 | unsigned long crc; |
| 205 | unsigned long direntsize; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 206 | int method; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 207 | unsigned char *out; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 208 | void *deflated = NULL; |
Nguyễn Thái Ngọc Duy | 9cb513b | 2012-05-03 08:51:03 +0700 | [diff] [blame] | 209 | void *buffer; |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 210 | struct git_istream *stream = NULL; |
| 211 | unsigned long flags = 0; |
Nguyễn Thái Ngọc Duy | 9cb513b | 2012-05-03 08:51:03 +0700 | [diff] [blame] | 212 | unsigned long size; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 213 | |
Rene Scharfe | 38f4d13 | 2006-11-18 13:06:56 +0100 | [diff] [blame] | 214 | crc = crc32(0, NULL, 0); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 215 | |
René Scharfe | 88182ba | 2012-09-18 21:46:56 +0200 | [diff] [blame] | 216 | if (!has_only_ascii(path)) { |
| 217 | if (is_utf8(path)) |
| 218 | flags |= ZIP_UTF8; |
| 219 | else |
| 220 | warning("Path is not valid UTF-8: %s", path); |
| 221 | } |
| 222 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 223 | if (pathlen > 0xffff) { |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 224 | return error("path too long (%d chars, SHA1: %s): %s", |
| 225 | (int)pathlen, sha1_to_hex(sha1), path); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 228 | if (S_ISDIR(mode) || S_ISGITLINK(mode)) { |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 229 | method = 0; |
Rene Scharfe | 62cdce1 | 2006-10-07 01:47:35 +0200 | [diff] [blame] | 230 | attr2 = 16; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 231 | out = NULL; |
René Scharfe | 60df6bd | 2012-05-03 08:51:05 +0700 | [diff] [blame] | 232 | size = 0; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 233 | compressed_size = 0; |
Nguyễn Thái Ngọc Duy | 9cb513b | 2012-05-03 08:51:03 +0700 | [diff] [blame] | 234 | buffer = NULL; |
Rene Scharfe | 62cdce1 | 2006-10-07 01:47:35 +0200 | [diff] [blame] | 235 | } else if (S_ISREG(mode) || S_ISLNK(mode)) { |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 236 | enum object_type type = sha1_object_info(sha1, &size); |
Nguyễn Thái Ngọc Duy | 9cb513b | 2012-05-03 08:51:03 +0700 | [diff] [blame] | 237 | |
Rene Scharfe | 62cdce1 | 2006-10-07 01:47:35 +0200 | [diff] [blame] | 238 | method = 0; |
Junio C Hamano | bb52d22 | 2012-09-18 13:32:39 -0700 | [diff] [blame] | 239 | attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) : |
| 240 | (mode & 0111) ? ((mode) << 16) : 0; |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 241 | if (S_ISREG(mode) && args->compression_level != 0 && size > 0) |
Rene Scharfe | 62cdce1 | 2006-10-07 01:47:35 +0200 | [diff] [blame] | 242 | method = 8; |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 243 | |
| 244 | if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert && |
René Scharfe | c743c21 | 2012-05-03 08:51:08 +0700 | [diff] [blame] | 245 | size > big_file_threshold) { |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 246 | stream = open_istream(sha1, &type, &size, NULL); |
| 247 | if (!stream) |
| 248 | return error("cannot stream blob %s", |
| 249 | sha1_to_hex(sha1)); |
| 250 | flags |= ZIP_STREAM; |
| 251 | out = buffer = NULL; |
| 252 | } else { |
| 253 | buffer = sha1_file_to_archive(args, path, sha1, mode, |
| 254 | &type, &size); |
| 255 | if (!buffer) |
| 256 | return error("cannot read %s", |
| 257 | sha1_to_hex(sha1)); |
| 258 | crc = crc32(crc, buffer, size); |
| 259 | out = buffer; |
| 260 | } |
René Scharfe | d3c1472 | 2013-02-27 11:20:21 +0100 | [diff] [blame] | 261 | compressed_size = (method == 0) ? size : 0; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 262 | } else { |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 263 | return error("unsupported file mode: 0%o (SHA1: %s)", mode, |
| 264 | sha1_to_hex(sha1)); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 265 | } |
| 266 | |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 267 | if (buffer && method == 8) { |
René Scharfe | c3c2e1a | 2013-03-15 23:21:51 +0100 | [diff] [blame] | 268 | out = deflated = zlib_deflate_raw(buffer, size, |
| 269 | args->compression_level, |
| 270 | &compressed_size); |
| 271 | if (!out || compressed_size >= size) { |
| 272 | out = buffer; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 273 | method = 0; |
| 274 | compressed_size = size; |
| 275 | } |
| 276 | } |
| 277 | |
René Scharfe | 227bf59 | 2012-09-24 17:56:23 +0200 | [diff] [blame] | 278 | copy_le16(extra.magic, 0x5455); |
| 279 | copy_le16(extra.extra_size, ZIP_EXTRA_MTIME_PAYLOAD_SIZE); |
| 280 | extra.flags[0] = 1; /* just mtime */ |
| 281 | copy_le32(extra.mtime, args->time); |
| 282 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 283 | /* make sure we have enough free space in the dictionary */ |
René Scharfe | 227bf59 | 2012-09-24 17:56:23 +0200 | [diff] [blame] | 284 | direntsize = ZIP_DIR_HEADER_SIZE + pathlen + ZIP_EXTRA_MTIME_SIZE; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 285 | while (zip_dir_size < zip_dir_offset + direntsize) { |
| 286 | zip_dir_size += ZIP_DIRECTORY_MIN_SIZE; |
| 287 | zip_dir = xrealloc(zip_dir, zip_dir_size); |
| 288 | } |
| 289 | |
| 290 | copy_le32(dirent.magic, 0x02014b50); |
Junio C Hamano | bb52d22 | 2012-09-18 13:32:39 -0700 | [diff] [blame] | 291 | copy_le16(dirent.creator_version, |
| 292 | S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0); |
Rene Scharfe | cf72fb0 | 2006-10-07 01:47:24 +0200 | [diff] [blame] | 293 | copy_le16(dirent.version, 10); |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 294 | copy_le16(dirent.flags, flags); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 295 | copy_le16(dirent.compression_method, method); |
| 296 | copy_le16(dirent.mtime, zip_time); |
| 297 | copy_le16(dirent.mdate, zip_date); |
René Scharfe | ebf5374 | 2012-05-03 08:51:06 +0700 | [diff] [blame] | 298 | set_zip_dir_data_desc(&dirent, size, compressed_size, crc); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 299 | copy_le16(dirent.filename_length, pathlen); |
René Scharfe | 227bf59 | 2012-09-24 17:56:23 +0200 | [diff] [blame] | 300 | copy_le16(dirent.extra_length, ZIP_EXTRA_MTIME_SIZE); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 301 | copy_le16(dirent.comment_length, 0); |
| 302 | copy_le16(dirent.disk, 0); |
| 303 | copy_le16(dirent.attr1, 0); |
Rene Scharfe | 62cdce1 | 2006-10-07 01:47:35 +0200 | [diff] [blame] | 304 | copy_le32(dirent.attr2, attr2); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 305 | copy_le32(dirent.offset, zip_offset); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 306 | |
| 307 | copy_le32(header.magic, 0x04034b50); |
Rene Scharfe | cf72fb0 | 2006-10-07 01:47:24 +0200 | [diff] [blame] | 308 | copy_le16(header.version, 10); |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 309 | copy_le16(header.flags, flags); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 310 | copy_le16(header.compression_method, method); |
| 311 | copy_le16(header.mtime, zip_time); |
| 312 | copy_le16(header.mdate, zip_date); |
René Scharfe | 5ea2c84 | 2013-01-06 16:20:57 +0100 | [diff] [blame] | 313 | set_zip_header_data_desc(&header, size, compressed_size, crc); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 314 | copy_le16(header.filename_length, pathlen); |
René Scharfe | 227bf59 | 2012-09-24 17:56:23 +0200 | [diff] [blame] | 315 | copy_le16(header.extra_length, ZIP_EXTRA_MTIME_SIZE); |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 316 | write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE); |
| 317 | zip_offset += ZIP_LOCAL_HEADER_SIZE; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 318 | write_or_die(1, path, pathlen); |
| 319 | zip_offset += pathlen; |
René Scharfe | 227bf59 | 2012-09-24 17:56:23 +0200 | [diff] [blame] | 320 | write_or_die(1, &extra, ZIP_EXTRA_MTIME_SIZE); |
| 321 | zip_offset += ZIP_EXTRA_MTIME_SIZE; |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 322 | if (stream && method == 0) { |
| 323 | unsigned char buf[STREAM_BUFFER_SIZE]; |
| 324 | ssize_t readlen; |
| 325 | |
| 326 | for (;;) { |
| 327 | readlen = read_istream(stream, buf, sizeof(buf)); |
| 328 | if (readlen <= 0) |
| 329 | break; |
| 330 | crc = crc32(crc, buf, readlen); |
| 331 | write_or_die(1, buf, readlen); |
| 332 | } |
| 333 | close_istream(stream); |
| 334 | if (readlen) |
| 335 | return readlen; |
| 336 | |
| 337 | compressed_size = size; |
| 338 | zip_offset += compressed_size; |
| 339 | |
| 340 | write_zip_data_desc(size, compressed_size, crc); |
| 341 | zip_offset += ZIP_DATA_DESC_SIZE; |
| 342 | |
| 343 | set_zip_dir_data_desc(&dirent, size, compressed_size, crc); |
René Scharfe | c743c21 | 2012-05-03 08:51:08 +0700 | [diff] [blame] | 344 | } else if (stream && method == 8) { |
| 345 | unsigned char buf[STREAM_BUFFER_SIZE]; |
| 346 | ssize_t readlen; |
| 347 | git_zstream zstream; |
| 348 | int result; |
| 349 | size_t out_len; |
| 350 | unsigned char compressed[STREAM_BUFFER_SIZE * 2]; |
| 351 | |
| 352 | memset(&zstream, 0, sizeof(zstream)); |
René Scharfe | c3c2e1a | 2013-03-15 23:21:51 +0100 | [diff] [blame] | 353 | git_deflate_init_raw(&zstream, args->compression_level); |
René Scharfe | c743c21 | 2012-05-03 08:51:08 +0700 | [diff] [blame] | 354 | |
| 355 | compressed_size = 0; |
| 356 | zstream.next_out = compressed; |
| 357 | zstream.avail_out = sizeof(compressed); |
| 358 | |
| 359 | for (;;) { |
| 360 | readlen = read_istream(stream, buf, sizeof(buf)); |
| 361 | if (readlen <= 0) |
| 362 | break; |
| 363 | crc = crc32(crc, buf, readlen); |
| 364 | |
| 365 | zstream.next_in = buf; |
| 366 | zstream.avail_in = readlen; |
| 367 | result = git_deflate(&zstream, 0); |
| 368 | if (result != Z_OK) |
| 369 | die("deflate error (%d)", result); |
René Scharfe | c3c2e1a | 2013-03-15 23:21:51 +0100 | [diff] [blame] | 370 | out_len = zstream.next_out - compressed; |
René Scharfe | c743c21 | 2012-05-03 08:51:08 +0700 | [diff] [blame] | 371 | |
| 372 | if (out_len > 0) { |
René Scharfe | c3c2e1a | 2013-03-15 23:21:51 +0100 | [diff] [blame] | 373 | write_or_die(1, compressed, out_len); |
René Scharfe | c743c21 | 2012-05-03 08:51:08 +0700 | [diff] [blame] | 374 | compressed_size += out_len; |
| 375 | zstream.next_out = compressed; |
| 376 | zstream.avail_out = sizeof(compressed); |
| 377 | } |
| 378 | |
| 379 | } |
| 380 | close_istream(stream); |
| 381 | if (readlen) |
| 382 | return readlen; |
| 383 | |
| 384 | zstream.next_in = buf; |
| 385 | zstream.avail_in = 0; |
| 386 | result = git_deflate(&zstream, Z_FINISH); |
| 387 | if (result != Z_STREAM_END) |
| 388 | die("deflate error (%d)", result); |
| 389 | |
| 390 | git_deflate_end(&zstream); |
René Scharfe | c3c2e1a | 2013-03-15 23:21:51 +0100 | [diff] [blame] | 391 | out_len = zstream.next_out - compressed; |
| 392 | write_or_die(1, compressed, out_len); |
René Scharfe | c743c21 | 2012-05-03 08:51:08 +0700 | [diff] [blame] | 393 | compressed_size += out_len; |
| 394 | zip_offset += compressed_size; |
| 395 | |
| 396 | write_zip_data_desc(size, compressed_size, crc); |
| 397 | zip_offset += ZIP_DATA_DESC_SIZE; |
| 398 | |
| 399 | set_zip_dir_data_desc(&dirent, size, compressed_size, crc); |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 400 | } else if (compressed_size > 0) { |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 401 | write_or_die(1, out, compressed_size); |
| 402 | zip_offset += compressed_size; |
| 403 | } |
| 404 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 405 | free(deflated); |
Nguyễn Thái Ngọc Duy | 9cb513b | 2012-05-03 08:51:03 +0700 | [diff] [blame] | 406 | free(buffer); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 407 | |
René Scharfe | ebf5374 | 2012-05-03 08:51:06 +0700 | [diff] [blame] | 408 | memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE); |
| 409 | zip_dir_offset += ZIP_DIR_HEADER_SIZE; |
| 410 | memcpy(zip_dir + zip_dir_offset, path, pathlen); |
| 411 | zip_dir_offset += pathlen; |
René Scharfe | 227bf59 | 2012-09-24 17:56:23 +0200 | [diff] [blame] | 412 | memcpy(zip_dir + zip_dir_offset, &extra, ZIP_EXTRA_MTIME_SIZE); |
| 413 | zip_dir_offset += ZIP_EXTRA_MTIME_SIZE; |
René Scharfe | ebf5374 | 2012-05-03 08:51:06 +0700 | [diff] [blame] | 414 | zip_dir_entries++; |
| 415 | |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 416 | return 0; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static void write_zip_trailer(const unsigned char *sha1) |
| 420 | { |
| 421 | struct zip_dir_trailer trailer; |
| 422 | |
| 423 | copy_le32(trailer.magic, 0x06054b50); |
| 424 | copy_le16(trailer.disk, 0); |
| 425 | copy_le16(trailer.directory_start_disk, 0); |
| 426 | copy_le16(trailer.entries_on_this_disk, zip_dir_entries); |
| 427 | copy_le16(trailer.entries, zip_dir_entries); |
| 428 | copy_le32(trailer.size, zip_dir_offset); |
| 429 | copy_le32(trailer.offset, zip_offset); |
| 430 | copy_le16(trailer.comment_length, sha1 ? 40 : 0); |
| 431 | |
| 432 | write_or_die(1, zip_dir, zip_dir_offset); |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 433 | write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 434 | if (sha1) |
| 435 | write_or_die(1, sha1_to_hex(sha1), 40); |
| 436 | } |
| 437 | |
| 438 | static void dos_time(time_t *time, int *dos_date, int *dos_time) |
| 439 | { |
| 440 | struct tm *t = localtime(time); |
| 441 | |
| 442 | *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 + |
| 443 | (t->tm_year + 1900 - 1980) * 512; |
| 444 | *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048; |
| 445 | } |
| 446 | |
Jeff King | 4d7c989 | 2011-06-21 21:24:07 -0400 | [diff] [blame] | 447 | static int write_zip_archive(const struct archiver *ar, |
| 448 | struct archiver_args *args) |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 449 | { |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 450 | int err; |
| 451 | |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 452 | dos_time(&args->time, &zip_date, &zip_time); |
| 453 | |
| 454 | zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE); |
| 455 | zip_dir_size = ZIP_DIRECTORY_MIN_SIZE; |
| 456 | |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 457 | err = write_archive_entries(args, write_zip_entry); |
| 458 | if (!err) |
| 459 | write_zip_trailer(args->commit_sha1); |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 460 | |
| 461 | free(zip_dir); |
| 462 | |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 463 | return err; |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 464 | } |
Jeff King | 13e0f88 | 2011-06-21 21:23:33 -0400 | [diff] [blame] | 465 | |
| 466 | static struct archiver zip_archiver = { |
| 467 | "zip", |
| 468 | write_zip_archive, |
Jeff King | 7b97730 | 2011-06-21 23:17:35 -0400 | [diff] [blame] | 469 | ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE |
Jeff King | 13e0f88 | 2011-06-21 21:23:33 -0400 | [diff] [blame] | 470 | }; |
| 471 | |
| 472 | void init_zip_archiver(void) |
| 473 | { |
| 474 | register_archiver(&zip_archiver); |
| 475 | } |