Commit | Line | Data |
---|---|---|
e4fbbfe9 RS |
1 | /* |
2 | * Copyright (c) 2006 Rene Scharfe | |
3 | */ | |
e4fbbfe9 | 4 | #include "cache.h" |
b2141fc1 | 5 | #include "config.h" |
ec06bff5 | 6 | #include "archive.h" |
2158f883 | 7 | #include "streaming.h" |
88182bab | 8 | #include "utf8.h" |
cbd53a21 | 9 | #include "object-store.h" |
4aff646d RS |
10 | #include "userdiff.h" |
11 | #include "xdiff-interface.h" | |
e4fbbfe9 | 12 | |
e4fbbfe9 RS |
13 | static int zip_date; |
14 | static int zip_time; | |
15 | ||
c061a149 RS |
16 | /* We only care about the "buf" part here. */ |
17 | static struct strbuf zip_dir; | |
e4fbbfe9 | 18 | |
af95749f | 19 | static uintmax_t zip_offset; |
88329ca8 RS |
20 | static uint64_t zip_dir_entries; |
21 | ||
22 | static unsigned int max_creator_version; | |
e4fbbfe9 | 23 | |
88182bab RS |
24 | #define ZIP_STREAM (1 << 3) |
25 | #define ZIP_UTF8 (1 << 11) | |
e4fbbfe9 | 26 | |
e05e8cf0 RS |
27 | enum zip_method { |
28 | ZIP_METHOD_STORE = 0, | |
29 | ZIP_METHOD_DEFLATE = 8 | |
30 | }; | |
31 | ||
e4fbbfe9 RS |
32 | struct zip_local_header { |
33 | unsigned char magic[4]; | |
34 | unsigned char version[2]; | |
35 | unsigned char flags[2]; | |
36 | unsigned char compression_method[2]; | |
37 | unsigned char mtime[2]; | |
38 | unsigned char mdate[2]; | |
39 | unsigned char crc32[4]; | |
40 | unsigned char compressed_size[4]; | |
41 | unsigned char size[4]; | |
42 | unsigned char filename_length[2]; | |
43 | unsigned char extra_length[2]; | |
0ea865ce | 44 | unsigned char _end[1]; |
e4fbbfe9 RS |
45 | }; |
46 | ||
2158f883 RS |
47 | struct zip_data_desc { |
48 | unsigned char magic[4]; | |
49 | unsigned char crc32[4]; | |
50 | unsigned char compressed_size[4]; | |
51 | unsigned char size[4]; | |
52 | unsigned char _end[1]; | |
53 | }; | |
54 | ||
4cdf3f9d RS |
55 | struct zip64_data_desc { |
56 | unsigned char magic[4]; | |
57 | unsigned char crc32[4]; | |
58 | unsigned char compressed_size[8]; | |
59 | unsigned char size[8]; | |
60 | unsigned char _end[1]; | |
61 | }; | |
62 | ||
e4fbbfe9 RS |
63 | struct zip_dir_trailer { |
64 | unsigned char magic[4]; | |
65 | unsigned char disk[2]; | |
66 | unsigned char directory_start_disk[2]; | |
67 | unsigned char entries_on_this_disk[2]; | |
68 | unsigned char entries[2]; | |
69 | unsigned char size[4]; | |
70 | unsigned char offset[4]; | |
71 | unsigned char comment_length[2]; | |
0ea865ce | 72 | unsigned char _end[1]; |
e4fbbfe9 RS |
73 | }; |
74 | ||
227bf598 RS |
75 | struct zip_extra_mtime { |
76 | unsigned char magic[2]; | |
77 | unsigned char extra_size[2]; | |
78 | unsigned char flags[1]; | |
79 | unsigned char mtime[4]; | |
80 | unsigned char _end[1]; | |
81 | }; | |
82 | ||
4cdf3f9d RS |
83 | struct zip64_extra { |
84 | unsigned char magic[2]; | |
85 | unsigned char extra_size[2]; | |
86 | unsigned char size[8]; | |
87 | unsigned char compressed_size[8]; | |
88 | unsigned char _end[1]; | |
89 | }; | |
90 | ||
88329ca8 RS |
91 | struct zip64_dir_trailer { |
92 | unsigned char magic[4]; | |
93 | unsigned char record_size[8]; | |
94 | unsigned char creator_version[2]; | |
95 | unsigned char version[2]; | |
96 | unsigned char disk[4]; | |
97 | unsigned char directory_start_disk[4]; | |
98 | unsigned char entries_on_this_disk[8]; | |
99 | unsigned char entries[8]; | |
100 | unsigned char size[8]; | |
101 | unsigned char offset[8]; | |
102 | unsigned char _end[1]; | |
103 | }; | |
104 | ||
105 | struct zip64_dir_trailer_locator { | |
106 | unsigned char magic[4]; | |
107 | unsigned char disk[4]; | |
108 | unsigned char offset[8]; | |
109 | unsigned char number_of_disks[4]; | |
110 | unsigned char _end[1]; | |
111 | }; | |
112 | ||
0ea865ce RS |
113 | /* |
114 | * On ARM, padding is added at the end of the struct, so a simple | |
115 | * sizeof(struct ...) reports two bytes more than the payload size | |
116 | * we're interested in. | |
117 | */ | |
118 | #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end) | |
2158f883 | 119 | #define ZIP_DATA_DESC_SIZE offsetof(struct zip_data_desc, _end) |
4cdf3f9d | 120 | #define ZIP64_DATA_DESC_SIZE offsetof(struct zip64_data_desc, _end) |
0ea865ce RS |
121 | #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end) |
122 | #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end) | |
227bf598 RS |
123 | #define ZIP_EXTRA_MTIME_SIZE offsetof(struct zip_extra_mtime, _end) |
124 | #define ZIP_EXTRA_MTIME_PAYLOAD_SIZE \ | |
125 | (ZIP_EXTRA_MTIME_SIZE - offsetof(struct zip_extra_mtime, flags)) | |
4cdf3f9d RS |
126 | #define ZIP64_EXTRA_SIZE offsetof(struct zip64_extra, _end) |
127 | #define ZIP64_EXTRA_PAYLOAD_SIZE \ | |
128 | (ZIP64_EXTRA_SIZE - offsetof(struct zip64_extra, size)) | |
88329ca8 RS |
129 | #define ZIP64_DIR_TRAILER_SIZE offsetof(struct zip64_dir_trailer, _end) |
130 | #define ZIP64_DIR_TRAILER_RECORD_SIZE \ | |
131 | (ZIP64_DIR_TRAILER_SIZE - \ | |
132 | offsetof(struct zip64_dir_trailer, creator_version)) | |
133 | #define ZIP64_DIR_TRAILER_LOCATOR_SIZE \ | |
134 | offsetof(struct zip64_dir_trailer_locator, _end) | |
0ea865ce | 135 | |
e4fbbfe9 RS |
136 | static void copy_le16(unsigned char *dest, unsigned int n) |
137 | { | |
138 | dest[0] = 0xff & n; | |
139 | dest[1] = 0xff & (n >> 010); | |
140 | } | |
141 | ||
142 | static void copy_le32(unsigned char *dest, unsigned int n) | |
143 | { | |
144 | dest[0] = 0xff & n; | |
145 | dest[1] = 0xff & (n >> 010); | |
146 | dest[2] = 0xff & (n >> 020); | |
147 | dest[3] = 0xff & (n >> 030); | |
148 | } | |
149 | ||
88329ca8 RS |
150 | static void copy_le64(unsigned char *dest, uint64_t n) |
151 | { | |
152 | dest[0] = 0xff & n; | |
153 | dest[1] = 0xff & (n >> 010); | |
154 | dest[2] = 0xff & (n >> 020); | |
155 | dest[3] = 0xff & (n >> 030); | |
156 | dest[4] = 0xff & (n >> 040); | |
157 | dest[5] = 0xff & (n >> 050); | |
158 | dest[6] = 0xff & (n >> 060); | |
159 | dest[7] = 0xff & (n >> 070); | |
160 | } | |
161 | ||
162 | static uint64_t clamp_max(uint64_t n, uint64_t max, int *clamped) | |
163 | { | |
164 | if (n <= max) | |
165 | return n; | |
166 | *clamped = 1; | |
167 | return max; | |
168 | } | |
169 | ||
170 | static void copy_le16_clamp(unsigned char *dest, uint64_t n, int *clamped) | |
171 | { | |
172 | copy_le16(dest, clamp_max(n, 0xffff, clamped)); | |
173 | } | |
174 | ||
af95749f RS |
175 | static void copy_le32_clamp(unsigned char *dest, uint64_t n, int *clamped) |
176 | { | |
177 | copy_le32(dest, clamp_max(n, 0xffffffff, clamped)); | |
178 | } | |
179 | ||
3c78fd80 RS |
180 | static int strbuf_add_le(struct strbuf *sb, size_t size, uintmax_t n) |
181 | { | |
182 | while (size-- > 0) { | |
183 | strbuf_addch(sb, n & 0xff); | |
184 | n >>= 8; | |
185 | } | |
186 | return -!!n; | |
187 | } | |
188 | ||
af95749f RS |
189 | static uint32_t clamp32(uintmax_t n) |
190 | { | |
191 | const uintmax_t max = 0xffffffff; | |
192 | return (n < max) ? n : max; | |
193 | } | |
194 | ||
c3c2e1a0 RS |
195 | static void *zlib_deflate_raw(void *data, unsigned long size, |
196 | int compression_level, | |
197 | unsigned long *compressed_size) | |
e4fbbfe9 | 198 | { |
ef49a7a0 | 199 | git_zstream stream; |
e4fbbfe9 RS |
200 | unsigned long maxsize; |
201 | void *buffer; | |
202 | int result; | |
203 | ||
c3c2e1a0 | 204 | git_deflate_init_raw(&stream, compression_level); |
225a6f10 | 205 | maxsize = git_deflate_bound(&stream, size); |
e4fbbfe9 RS |
206 | buffer = xmalloc(maxsize); |
207 | ||
208 | stream.next_in = data; | |
209 | stream.avail_in = size; | |
210 | stream.next_out = buffer; | |
211 | stream.avail_out = maxsize; | |
212 | ||
213 | do { | |
55bb5c91 | 214 | result = git_deflate(&stream, Z_FINISH); |
e4fbbfe9 RS |
215 | } while (result == Z_OK); |
216 | ||
217 | if (result != Z_STREAM_END) { | |
218 | free(buffer); | |
219 | return NULL; | |
220 | } | |
221 | ||
55bb5c91 | 222 | git_deflate_end(&stream); |
e4fbbfe9 RS |
223 | *compressed_size = stream.total_out; |
224 | ||
225 | return buffer; | |
226 | } | |
227 | ||
2158f883 RS |
228 | static void write_zip_data_desc(unsigned long size, |
229 | unsigned long compressed_size, | |
230 | unsigned long crc) | |
231 | { | |
4cdf3f9d RS |
232 | if (size >= 0xffffffff || compressed_size >= 0xffffffff) { |
233 | struct zip64_data_desc trailer; | |
234 | copy_le32(trailer.magic, 0x08074b50); | |
235 | copy_le32(trailer.crc32, crc); | |
236 | copy_le64(trailer.compressed_size, compressed_size); | |
237 | copy_le64(trailer.size, size); | |
238 | write_or_die(1, &trailer, ZIP64_DATA_DESC_SIZE); | |
239 | zip_offset += ZIP64_DATA_DESC_SIZE; | |
240 | } else { | |
241 | struct zip_data_desc trailer; | |
242 | copy_le32(trailer.magic, 0x08074b50); | |
243 | copy_le32(trailer.crc32, crc); | |
244 | copy_le32(trailer.compressed_size, compressed_size); | |
245 | copy_le32(trailer.size, size); | |
246 | write_or_die(1, &trailer, ZIP_DATA_DESC_SIZE); | |
247 | zip_offset += ZIP_DATA_DESC_SIZE; | |
248 | } | |
2158f883 RS |
249 | } |
250 | ||
ebf5374a RS |
251 | static void set_zip_header_data_desc(struct zip_local_header *header, |
252 | unsigned long size, | |
253 | unsigned long compressed_size, | |
254 | unsigned long crc) | |
255 | { | |
256 | copy_le32(header->crc32, crc); | |
257 | copy_le32(header->compressed_size, compressed_size); | |
258 | copy_le32(header->size, size); | |
259 | } | |
260 | ||
88182bab RS |
261 | static int has_only_ascii(const char *s) |
262 | { | |
263 | for (;;) { | |
264 | int c = *s++; | |
265 | if (c == '\0') | |
266 | return 1; | |
267 | if (!isascii(c)) | |
268 | return 0; | |
269 | } | |
270 | } | |
271 | ||
acd00ea0 NTND |
272 | static int entry_is_binary(struct index_state *istate, const char *path, |
273 | const void *buffer, size_t size) | |
4aff646d | 274 | { |
acd00ea0 | 275 | struct userdiff_driver *driver = userdiff_find_by_path(istate, path); |
4aff646d RS |
276 | if (!driver) |
277 | driver = userdiff_find_by_name("default"); | |
278 | if (driver->binary != -1) | |
279 | return driver->binary; | |
280 | return buffer_is_binary(buffer, size); | |
281 | } | |
282 | ||
2158f883 RS |
283 | #define STREAM_BUFFER_SIZE (1024 * 16) |
284 | ||
562e25ab | 285 | static int write_zip_entry(struct archiver_args *args, |
015ff4f8 | 286 | const struct object_id *oid, |
9cb513b7 NTND |
287 | const char *path, size_t pathlen, |
288 | unsigned int mode) | |
e4fbbfe9 RS |
289 | { |
290 | struct zip_local_header header; | |
3c78fd80 | 291 | uintmax_t offset = zip_offset; |
227bf598 | 292 | struct zip_extra_mtime extra; |
4cdf3f9d RS |
293 | struct zip64_extra extra64; |
294 | size_t header_extra_size = ZIP_EXTRA_MTIME_SIZE; | |
295 | int need_zip64_extra = 0; | |
bb52d22e | 296 | unsigned long attr2; |
e4fbbfe9 | 297 | unsigned long compressed_size; |
e4fbbfe9 | 298 | unsigned long crc; |
e05e8cf0 | 299 | enum zip_method method; |
e4fbbfe9 | 300 | unsigned char *out; |
e4fbbfe9 | 301 | void *deflated = NULL; |
9cb513b7 | 302 | void *buffer; |
2158f883 RS |
303 | struct git_istream *stream = NULL; |
304 | unsigned long flags = 0; | |
9cb513b7 | 305 | unsigned long size; |
4aff646d RS |
306 | int is_binary = -1; |
307 | const char *path_without_prefix = path + args->baselen; | |
0f747f9d | 308 | unsigned int creator_version = 0; |
ebdfa294 | 309 | unsigned int version_needed = 10; |
af95749f RS |
310 | size_t zip_dir_extra_size = ZIP_EXTRA_MTIME_SIZE; |
311 | size_t zip64_dir_extra_payload_size = 0; | |
e4fbbfe9 | 312 | |
38f4d138 | 313 | crc = crc32(0, NULL, 0); |
e4fbbfe9 | 314 | |
88182bab RS |
315 | if (!has_only_ascii(path)) { |
316 | if (is_utf8(path)) | |
317 | flags |= ZIP_UTF8; | |
318 | else | |
02f3fe5a | 319 | warning(_("path is not valid UTF-8: %s"), path); |
88182bab RS |
320 | } |
321 | ||
e4fbbfe9 | 322 | if (pathlen > 0xffff) { |
02f3fe5a | 323 | return error(_("path too long (%d chars, SHA1: %s): %s"), |
015ff4f8 | 324 | (int)pathlen, oid_to_hex(oid), path); |
e4fbbfe9 RS |
325 | } |
326 | ||
302b9282 | 327 | if (S_ISDIR(mode) || S_ISGITLINK(mode)) { |
e05e8cf0 | 328 | method = ZIP_METHOD_STORE; |
62cdce17 | 329 | attr2 = 16; |
e4fbbfe9 | 330 | out = NULL; |
60df6bd1 | 331 | size = 0; |
e4fbbfe9 | 332 | compressed_size = 0; |
9cb513b7 | 333 | buffer = NULL; |
62cdce17 | 334 | } else if (S_ISREG(mode) || S_ISLNK(mode)) { |
b67b5512 | 335 | enum object_type type = oid_object_info(args->repo, oid, |
0df8e965 | 336 | &size); |
9cb513b7 | 337 | |
e05e8cf0 | 338 | method = ZIP_METHOD_STORE; |
bb52d22e JH |
339 | attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) : |
340 | (mode & 0111) ? ((mode) << 16) : 0; | |
0f747f9d RS |
341 | if (S_ISLNK(mode) || (mode & 0111)) |
342 | creator_version = 0x0317; | |
2158f883 | 343 | if (S_ISREG(mode) && args->compression_level != 0 && size > 0) |
e05e8cf0 | 344 | method = ZIP_METHOD_DEFLATE; |
2158f883 RS |
345 | |
346 | if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert && | |
c743c215 | 347 | size > big_file_threshold) { |
ef7b5195 | 348 | stream = open_istream(oid, &type, &size, NULL); |
2158f883 | 349 | if (!stream) |
02f3fe5a | 350 | return error(_("cannot stream blob %s"), |
015ff4f8 | 351 | oid_to_hex(oid)); |
2158f883 RS |
352 | flags |= ZIP_STREAM; |
353 | out = buffer = NULL; | |
354 | } else { | |
e5ec981a | 355 | buffer = object_file_to_archive(args, path, oid, mode, |
356 | &type, &size); | |
2158f883 | 357 | if (!buffer) |
02f3fe5a | 358 | return error(_("cannot read %s"), |
015ff4f8 | 359 | oid_to_hex(oid)); |
2158f883 | 360 | crc = crc32(crc, buffer, size); |
acd00ea0 NTND |
361 | is_binary = entry_is_binary(args->repo->index, |
362 | path_without_prefix, | |
4aff646d | 363 | buffer, size); |
2158f883 RS |
364 | out = buffer; |
365 | } | |
e05e8cf0 | 366 | compressed_size = (method == ZIP_METHOD_STORE) ? size : 0; |
e4fbbfe9 | 367 | } else { |
02f3fe5a | 368 | return error(_("unsupported file mode: 0%o (SHA1: %s)"), mode, |
015ff4f8 | 369 | oid_to_hex(oid)); |
e4fbbfe9 RS |
370 | } |
371 | ||
88329ca8 RS |
372 | if (creator_version > max_creator_version) |
373 | max_creator_version = creator_version; | |
374 | ||
e05e8cf0 | 375 | if (buffer && method == ZIP_METHOD_DEFLATE) { |
c3c2e1a0 RS |
376 | out = deflated = zlib_deflate_raw(buffer, size, |
377 | args->compression_level, | |
378 | &compressed_size); | |
379 | if (!out || compressed_size >= size) { | |
380 | out = buffer; | |
e05e8cf0 | 381 | method = ZIP_METHOD_STORE; |
e4fbbfe9 RS |
382 | compressed_size = size; |
383 | } | |
384 | } | |
385 | ||
227bf598 RS |
386 | copy_le16(extra.magic, 0x5455); |
387 | copy_le16(extra.extra_size, ZIP_EXTRA_MTIME_PAYLOAD_SIZE); | |
388 | extra.flags[0] = 1; /* just mtime */ | |
389 | copy_le32(extra.mtime, args->time); | |
390 | ||
4cdf3f9d RS |
391 | if (size > 0xffffffff || compressed_size > 0xffffffff) |
392 | need_zip64_extra = 1; | |
393 | if (stream && size > 0x7fffffff) | |
394 | need_zip64_extra = 1; | |
395 | ||
ebdfa294 RS |
396 | if (need_zip64_extra) |
397 | version_needed = 45; | |
398 | ||
e4fbbfe9 | 399 | copy_le32(header.magic, 0x04034b50); |
ebdfa294 | 400 | copy_le16(header.version, version_needed); |
2158f883 | 401 | copy_le16(header.flags, flags); |
e4fbbfe9 RS |
402 | copy_le16(header.compression_method, method); |
403 | copy_le16(header.mtime, zip_time); | |
404 | copy_le16(header.mdate, zip_date); | |
4cdf3f9d RS |
405 | if (need_zip64_extra) { |
406 | set_zip_header_data_desc(&header, 0xffffffff, 0xffffffff, crc); | |
407 | header_extra_size += ZIP64_EXTRA_SIZE; | |
408 | } else { | |
409 | set_zip_header_data_desc(&header, size, compressed_size, crc); | |
410 | } | |
e4fbbfe9 | 411 | copy_le16(header.filename_length, pathlen); |
4cdf3f9d | 412 | copy_le16(header.extra_length, header_extra_size); |
0ea865ce RS |
413 | write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE); |
414 | zip_offset += ZIP_LOCAL_HEADER_SIZE; | |
e4fbbfe9 RS |
415 | write_or_die(1, path, pathlen); |
416 | zip_offset += pathlen; | |
227bf598 RS |
417 | write_or_die(1, &extra, ZIP_EXTRA_MTIME_SIZE); |
418 | zip_offset += ZIP_EXTRA_MTIME_SIZE; | |
4cdf3f9d RS |
419 | if (need_zip64_extra) { |
420 | copy_le16(extra64.magic, 0x0001); | |
421 | copy_le16(extra64.extra_size, ZIP64_EXTRA_PAYLOAD_SIZE); | |
422 | copy_le64(extra64.size, size); | |
423 | copy_le64(extra64.compressed_size, compressed_size); | |
424 | write_or_die(1, &extra64, ZIP64_EXTRA_SIZE); | |
425 | zip_offset += ZIP64_EXTRA_SIZE; | |
426 | } | |
427 | ||
e05e8cf0 | 428 | if (stream && method == ZIP_METHOD_STORE) { |
2158f883 RS |
429 | unsigned char buf[STREAM_BUFFER_SIZE]; |
430 | ssize_t readlen; | |
431 | ||
432 | for (;;) { | |
433 | readlen = read_istream(stream, buf, sizeof(buf)); | |
434 | if (readlen <= 0) | |
435 | break; | |
436 | crc = crc32(crc, buf, readlen); | |
4aff646d | 437 | if (is_binary == -1) |
acd00ea0 NTND |
438 | is_binary = entry_is_binary(args->repo->index, |
439 | path_without_prefix, | |
4aff646d | 440 | buf, readlen); |
2158f883 RS |
441 | write_or_die(1, buf, readlen); |
442 | } | |
443 | close_istream(stream); | |
444 | if (readlen) | |
445 | return readlen; | |
446 | ||
447 | compressed_size = size; | |
448 | zip_offset += compressed_size; | |
449 | ||
450 | write_zip_data_desc(size, compressed_size, crc); | |
e05e8cf0 | 451 | } else if (stream && method == ZIP_METHOD_DEFLATE) { |
c743c215 RS |
452 | unsigned char buf[STREAM_BUFFER_SIZE]; |
453 | ssize_t readlen; | |
454 | git_zstream zstream; | |
455 | int result; | |
456 | size_t out_len; | |
457 | unsigned char compressed[STREAM_BUFFER_SIZE * 2]; | |
458 | ||
c3c2e1a0 | 459 | git_deflate_init_raw(&zstream, args->compression_level); |
c743c215 RS |
460 | |
461 | compressed_size = 0; | |
462 | zstream.next_out = compressed; | |
463 | zstream.avail_out = sizeof(compressed); | |
464 | ||
465 | for (;;) { | |
466 | readlen = read_istream(stream, buf, sizeof(buf)); | |
467 | if (readlen <= 0) | |
468 | break; | |
469 | crc = crc32(crc, buf, readlen); | |
4aff646d | 470 | if (is_binary == -1) |
acd00ea0 NTND |
471 | is_binary = entry_is_binary(args->repo->index, |
472 | path_without_prefix, | |
4aff646d | 473 | buf, readlen); |
c743c215 RS |
474 | |
475 | zstream.next_in = buf; | |
476 | zstream.avail_in = readlen; | |
477 | result = git_deflate(&zstream, 0); | |
478 | if (result != Z_OK) | |
02f3fe5a | 479 | die(_("deflate error (%d)"), result); |
c3c2e1a0 | 480 | out_len = zstream.next_out - compressed; |
c743c215 RS |
481 | |
482 | if (out_len > 0) { | |
c3c2e1a0 | 483 | write_or_die(1, compressed, out_len); |
c743c215 RS |
484 | compressed_size += out_len; |
485 | zstream.next_out = compressed; | |
486 | zstream.avail_out = sizeof(compressed); | |
487 | } | |
488 | ||
489 | } | |
490 | close_istream(stream); | |
491 | if (readlen) | |
492 | return readlen; | |
493 | ||
494 | zstream.next_in = buf; | |
495 | zstream.avail_in = 0; | |
496 | result = git_deflate(&zstream, Z_FINISH); | |
497 | if (result != Z_STREAM_END) | |
498 | die("deflate error (%d)", result); | |
499 | ||
500 | git_deflate_end(&zstream); | |
c3c2e1a0 RS |
501 | out_len = zstream.next_out - compressed; |
502 | write_or_die(1, compressed, out_len); | |
c743c215 RS |
503 | compressed_size += out_len; |
504 | zip_offset += compressed_size; | |
505 | ||
506 | write_zip_data_desc(size, compressed_size, crc); | |
2158f883 | 507 | } else if (compressed_size > 0) { |
e4fbbfe9 RS |
508 | write_or_die(1, out, compressed_size); |
509 | zip_offset += compressed_size; | |
510 | } | |
511 | ||
e4fbbfe9 | 512 | free(deflated); |
9cb513b7 | 513 | free(buffer); |
e4fbbfe9 | 514 | |
4cdf3f9d RS |
515 | if (compressed_size > 0xffffffff || size > 0xffffffff || |
516 | offset > 0xffffffff) { | |
517 | if (compressed_size >= 0xffffffff) | |
518 | zip64_dir_extra_payload_size += 8; | |
519 | if (size >= 0xffffffff) | |
520 | zip64_dir_extra_payload_size += 8; | |
521 | if (offset >= 0xffffffff) | |
522 | zip64_dir_extra_payload_size += 8; | |
af95749f RS |
523 | zip_dir_extra_size += 2 + 2 + zip64_dir_extra_payload_size; |
524 | } | |
525 | ||
3c78fd80 RS |
526 | strbuf_add_le(&zip_dir, 4, 0x02014b50); /* magic */ |
527 | strbuf_add_le(&zip_dir, 2, creator_version); | |
ebdfa294 | 528 | strbuf_add_le(&zip_dir, 2, version_needed); |
3c78fd80 RS |
529 | strbuf_add_le(&zip_dir, 2, flags); |
530 | strbuf_add_le(&zip_dir, 2, method); | |
531 | strbuf_add_le(&zip_dir, 2, zip_time); | |
532 | strbuf_add_le(&zip_dir, 2, zip_date); | |
533 | strbuf_add_le(&zip_dir, 4, crc); | |
4cdf3f9d RS |
534 | strbuf_add_le(&zip_dir, 4, clamp32(compressed_size)); |
535 | strbuf_add_le(&zip_dir, 4, clamp32(size)); | |
3c78fd80 | 536 | strbuf_add_le(&zip_dir, 2, pathlen); |
af95749f | 537 | strbuf_add_le(&zip_dir, 2, zip_dir_extra_size); |
3c78fd80 RS |
538 | strbuf_add_le(&zip_dir, 2, 0); /* comment length */ |
539 | strbuf_add_le(&zip_dir, 2, 0); /* disk */ | |
540 | strbuf_add_le(&zip_dir, 2, !is_binary); | |
541 | strbuf_add_le(&zip_dir, 4, attr2); | |
af95749f | 542 | strbuf_add_le(&zip_dir, 4, clamp32(offset)); |
c061a149 RS |
543 | strbuf_add(&zip_dir, path, pathlen); |
544 | strbuf_add(&zip_dir, &extra, ZIP_EXTRA_MTIME_SIZE); | |
af95749f RS |
545 | if (zip64_dir_extra_payload_size) { |
546 | strbuf_add_le(&zip_dir, 2, 0x0001); /* magic */ | |
547 | strbuf_add_le(&zip_dir, 2, zip64_dir_extra_payload_size); | |
4cdf3f9d RS |
548 | if (size >= 0xffffffff) |
549 | strbuf_add_le(&zip_dir, 8, size); | |
550 | if (compressed_size >= 0xffffffff) | |
551 | strbuf_add_le(&zip_dir, 8, compressed_size); | |
af95749f RS |
552 | if (offset >= 0xffffffff) |
553 | strbuf_add_le(&zip_dir, 8, offset); | |
554 | } | |
ebf5374a RS |
555 | zip_dir_entries++; |
556 | ||
562e25ab | 557 | return 0; |
e4fbbfe9 RS |
558 | } |
559 | ||
88329ca8 RS |
560 | static void write_zip64_trailer(void) |
561 | { | |
562 | struct zip64_dir_trailer trailer64; | |
563 | struct zip64_dir_trailer_locator locator64; | |
564 | ||
565 | copy_le32(trailer64.magic, 0x06064b50); | |
566 | copy_le64(trailer64.record_size, ZIP64_DIR_TRAILER_RECORD_SIZE); | |
567 | copy_le16(trailer64.creator_version, max_creator_version); | |
568 | copy_le16(trailer64.version, 45); | |
569 | copy_le32(trailer64.disk, 0); | |
570 | copy_le32(trailer64.directory_start_disk, 0); | |
571 | copy_le64(trailer64.entries_on_this_disk, zip_dir_entries); | |
572 | copy_le64(trailer64.entries, zip_dir_entries); | |
c061a149 | 573 | copy_le64(trailer64.size, zip_dir.len); |
88329ca8 RS |
574 | copy_le64(trailer64.offset, zip_offset); |
575 | ||
576 | copy_le32(locator64.magic, 0x07064b50); | |
577 | copy_le32(locator64.disk, 0); | |
c061a149 | 578 | copy_le64(locator64.offset, zip_offset + zip_dir.len); |
88329ca8 RS |
579 | copy_le32(locator64.number_of_disks, 1); |
580 | ||
581 | write_or_die(1, &trailer64, ZIP64_DIR_TRAILER_SIZE); | |
582 | write_or_die(1, &locator64, ZIP64_DIR_TRAILER_LOCATOR_SIZE); | |
583 | } | |
584 | ||
bbf05cf7 | 585 | static void write_zip_trailer(const struct object_id *oid) |
e4fbbfe9 RS |
586 | { |
587 | struct zip_dir_trailer trailer; | |
88329ca8 | 588 | int clamped = 0; |
e4fbbfe9 RS |
589 | |
590 | copy_le32(trailer.magic, 0x06054b50); | |
591 | copy_le16(trailer.disk, 0); | |
592 | copy_le16(trailer.directory_start_disk, 0); | |
88329ca8 RS |
593 | copy_le16_clamp(trailer.entries_on_this_disk, zip_dir_entries, |
594 | &clamped); | |
595 | copy_le16_clamp(trailer.entries, zip_dir_entries, &clamped); | |
c061a149 | 596 | copy_le32(trailer.size, zip_dir.len); |
af95749f | 597 | copy_le32_clamp(trailer.offset, zip_offset, &clamped); |
bbf05cf7 | 598 | copy_le16(trailer.comment_length, oid ? the_hash_algo->hexsz : 0); |
e4fbbfe9 | 599 | |
c061a149 | 600 | write_or_die(1, zip_dir.buf, zip_dir.len); |
88329ca8 RS |
601 | if (clamped) |
602 | write_zip64_trailer(); | |
0ea865ce | 603 | write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE); |
bbf05cf7 | 604 | if (oid) |
605 | write_or_die(1, oid_to_hex(oid), the_hash_algo->hexsz); | |
e4fbbfe9 RS |
606 | } |
607 | ||
dddbad72 | 608 | static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time) |
e4fbbfe9 | 609 | { |
dddbad72 | 610 | time_t time; |
b5ab03bc | 611 | struct tm tm; |
dddbad72 JS |
612 | |
613 | if (date_overflows(*timestamp)) | |
02f3fe5a | 614 | die(_("timestamp too large for this system: %"PRItime), |
dddbad72 JS |
615 | *timestamp); |
616 | time = (time_t)*timestamp; | |
b5ab03bc | 617 | localtime_r(&time, &tm); |
dddbad72 | 618 | *timestamp = time; |
e4fbbfe9 | 619 | |
b5ab03bc DTCD |
620 | *dos_date = tm.tm_mday + (tm.tm_mon + 1) * 32 + |
621 | (tm.tm_year + 1900 - 1980) * 512; | |
622 | *dos_time = tm.tm_sec / 2 + tm.tm_min * 32 + tm.tm_hour * 2048; | |
e4fbbfe9 RS |
623 | } |
624 | ||
965cba2e JK |
625 | static int archive_zip_config(const char *var, const char *value, void *data) |
626 | { | |
627 | return userdiff_config(var, value); | |
628 | } | |
629 | ||
4d7c9898 JK |
630 | static int write_zip_archive(const struct archiver *ar, |
631 | struct archiver_args *args) | |
ec06bff5 | 632 | { |
562e25ab RS |
633 | int err; |
634 | ||
965cba2e JK |
635 | git_config(archive_zip_config, NULL); |
636 | ||
ec06bff5 FBH |
637 | dos_time(&args->time, &zip_date, &zip_time); |
638 | ||
c061a149 | 639 | strbuf_init(&zip_dir, 0); |
562e25ab RS |
640 | |
641 | err = write_archive_entries(args, write_zip_entry); | |
642 | if (!err) | |
bbf05cf7 | 643 | write_zip_trailer(args->commit_oid); |
ec06bff5 | 644 | |
c061a149 | 645 | strbuf_release(&zip_dir); |
ec06bff5 | 646 | |
562e25ab | 647 | return err; |
ec06bff5 | 648 | } |
13e0f88d JK |
649 | |
650 | static struct archiver zip_archiver = { | |
651 | "zip", | |
652 | write_zip_archive, | |
7b97730b | 653 | ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE |
13e0f88d JK |
654 | }; |
655 | ||
656 | void init_zip_archiver(void) | |
657 | { | |
658 | register_archiver(&zip_archiver); | |
659 | } |