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