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