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