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