Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006 Rene Scharfe |
| 3 | */ |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 4 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 5 | #include "config.h" |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 6 | #include "archive.h" |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 7 | #include "streaming.h" |
René Scharfe | 88182ba | 2012-09-18 21:46:56 +0200 | [diff] [blame] | 8 | #include "utf8.h" |
René Scharfe | 4aff646 | 2015-03-05 20:06:02 +0100 | [diff] [blame] | 9 | #include "userdiff.h" |
| 10 | #include "xdiff-interface.h" |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 11 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 12 | static int zip_date; |
| 13 | static int zip_time; |
| 14 | |
René Scharfe | c061a14 | 2017-04-24 19:30:49 +0200 | [diff] [blame] | 15 | /* We only care about the "buf" part here. */ |
| 16 | static struct strbuf zip_dir; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 17 | |
René Scharfe | af95749 | 2017-04-24 19:32:36 +0200 | [diff] [blame] | 18 | static uintmax_t zip_offset; |
René Scharfe | 88329ca | 2015-08-22 21:06:45 +0200 | [diff] [blame] | 19 | static uint64_t zip_dir_entries; |
| 20 | |
| 21 | static unsigned int max_creator_version; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 22 | |
René Scharfe | 88182ba | 2012-09-18 21:46:56 +0200 | [diff] [blame] | 23 | #define ZIP_STREAM (1 << 3) |
| 24 | #define ZIP_UTF8 (1 << 11) |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 25 | |
| 26 | struct zip_local_header { |
| 27 | unsigned char magic[4]; |
| 28 | unsigned char version[2]; |
| 29 | unsigned char flags[2]; |
| 30 | unsigned char compression_method[2]; |
| 31 | unsigned char mtime[2]; |
| 32 | unsigned char mdate[2]; |
| 33 | unsigned char crc32[4]; |
| 34 | unsigned char compressed_size[4]; |
| 35 | unsigned char size[4]; |
| 36 | unsigned char filename_length[2]; |
| 37 | unsigned char extra_length[2]; |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 38 | unsigned char _end[1]; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 41 | struct zip_data_desc { |
| 42 | unsigned char magic[4]; |
| 43 | unsigned char crc32[4]; |
| 44 | unsigned char compressed_size[4]; |
| 45 | unsigned char size[4]; |
| 46 | unsigned char _end[1]; |
| 47 | }; |
| 48 | |
René Scharfe | 4cdf3f9 | 2017-04-24 19:33:34 +0200 | [diff] [blame] | 49 | struct zip64_data_desc { |
| 50 | unsigned char magic[4]; |
| 51 | unsigned char crc32[4]; |
| 52 | unsigned char compressed_size[8]; |
| 53 | unsigned char size[8]; |
| 54 | unsigned char _end[1]; |
| 55 | }; |
| 56 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 57 | struct zip_dir_trailer { |
| 58 | unsigned char magic[4]; |
| 59 | unsigned char disk[2]; |
| 60 | unsigned char directory_start_disk[2]; |
| 61 | unsigned char entries_on_this_disk[2]; |
| 62 | unsigned char entries[2]; |
| 63 | unsigned char size[4]; |
| 64 | unsigned char offset[4]; |
| 65 | unsigned char comment_length[2]; |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 66 | unsigned char _end[1]; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
René Scharfe | 227bf59 | 2012-09-24 17:56:23 +0200 | [diff] [blame] | 69 | struct zip_extra_mtime { |
| 70 | unsigned char magic[2]; |
| 71 | unsigned char extra_size[2]; |
| 72 | unsigned char flags[1]; |
| 73 | unsigned char mtime[4]; |
| 74 | unsigned char _end[1]; |
| 75 | }; |
| 76 | |
René Scharfe | 4cdf3f9 | 2017-04-24 19:33:34 +0200 | [diff] [blame] | 77 | struct zip64_extra { |
| 78 | unsigned char magic[2]; |
| 79 | unsigned char extra_size[2]; |
| 80 | unsigned char size[8]; |
| 81 | unsigned char compressed_size[8]; |
| 82 | unsigned char _end[1]; |
| 83 | }; |
| 84 | |
René Scharfe | 88329ca | 2015-08-22 21:06:45 +0200 | [diff] [blame] | 85 | struct zip64_dir_trailer { |
| 86 | unsigned char magic[4]; |
| 87 | unsigned char record_size[8]; |
| 88 | unsigned char creator_version[2]; |
| 89 | unsigned char version[2]; |
| 90 | unsigned char disk[4]; |
| 91 | unsigned char directory_start_disk[4]; |
| 92 | unsigned char entries_on_this_disk[8]; |
| 93 | unsigned char entries[8]; |
| 94 | unsigned char size[8]; |
| 95 | unsigned char offset[8]; |
| 96 | unsigned char _end[1]; |
| 97 | }; |
| 98 | |
| 99 | struct zip64_dir_trailer_locator { |
| 100 | unsigned char magic[4]; |
| 101 | unsigned char disk[4]; |
| 102 | unsigned char offset[8]; |
| 103 | unsigned char number_of_disks[4]; |
| 104 | unsigned char _end[1]; |
| 105 | }; |
| 106 | |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 107 | /* |
| 108 | * On ARM, padding is added at the end of the struct, so a simple |
| 109 | * sizeof(struct ...) reports two bytes more than the payload size |
| 110 | * we're interested in. |
| 111 | */ |
| 112 | #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end) |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 113 | #define ZIP_DATA_DESC_SIZE offsetof(struct zip_data_desc, _end) |
René Scharfe | 4cdf3f9 | 2017-04-24 19:33:34 +0200 | [diff] [blame] | 114 | #define ZIP64_DATA_DESC_SIZE offsetof(struct zip64_data_desc, _end) |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 115 | #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end) |
| 116 | #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end) |
René Scharfe | 227bf59 | 2012-09-24 17:56:23 +0200 | [diff] [blame] | 117 | #define ZIP_EXTRA_MTIME_SIZE offsetof(struct zip_extra_mtime, _end) |
| 118 | #define ZIP_EXTRA_MTIME_PAYLOAD_SIZE \ |
| 119 | (ZIP_EXTRA_MTIME_SIZE - offsetof(struct zip_extra_mtime, flags)) |
René Scharfe | 4cdf3f9 | 2017-04-24 19:33:34 +0200 | [diff] [blame] | 120 | #define ZIP64_EXTRA_SIZE offsetof(struct zip64_extra, _end) |
| 121 | #define ZIP64_EXTRA_PAYLOAD_SIZE \ |
| 122 | (ZIP64_EXTRA_SIZE - offsetof(struct zip64_extra, size)) |
René Scharfe | 88329ca | 2015-08-22 21:06:45 +0200 | [diff] [blame] | 123 | #define ZIP64_DIR_TRAILER_SIZE offsetof(struct zip64_dir_trailer, _end) |
| 124 | #define ZIP64_DIR_TRAILER_RECORD_SIZE \ |
| 125 | (ZIP64_DIR_TRAILER_SIZE - \ |
| 126 | offsetof(struct zip64_dir_trailer, creator_version)) |
| 127 | #define ZIP64_DIR_TRAILER_LOCATOR_SIZE \ |
| 128 | offsetof(struct zip64_dir_trailer_locator, _end) |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 129 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 130 | static void copy_le16(unsigned char *dest, unsigned int n) |
| 131 | { |
| 132 | dest[0] = 0xff & n; |
| 133 | dest[1] = 0xff & (n >> 010); |
| 134 | } |
| 135 | |
| 136 | static void copy_le32(unsigned char *dest, unsigned int n) |
| 137 | { |
| 138 | dest[0] = 0xff & n; |
| 139 | dest[1] = 0xff & (n >> 010); |
| 140 | dest[2] = 0xff & (n >> 020); |
| 141 | dest[3] = 0xff & (n >> 030); |
| 142 | } |
| 143 | |
René Scharfe | 88329ca | 2015-08-22 21:06:45 +0200 | [diff] [blame] | 144 | static void copy_le64(unsigned char *dest, uint64_t n) |
| 145 | { |
| 146 | dest[0] = 0xff & n; |
| 147 | dest[1] = 0xff & (n >> 010); |
| 148 | dest[2] = 0xff & (n >> 020); |
| 149 | dest[3] = 0xff & (n >> 030); |
| 150 | dest[4] = 0xff & (n >> 040); |
| 151 | dest[5] = 0xff & (n >> 050); |
| 152 | dest[6] = 0xff & (n >> 060); |
| 153 | dest[7] = 0xff & (n >> 070); |
| 154 | } |
| 155 | |
| 156 | static uint64_t clamp_max(uint64_t n, uint64_t max, int *clamped) |
| 157 | { |
| 158 | if (n <= max) |
| 159 | return n; |
| 160 | *clamped = 1; |
| 161 | return max; |
| 162 | } |
| 163 | |
| 164 | static void copy_le16_clamp(unsigned char *dest, uint64_t n, int *clamped) |
| 165 | { |
| 166 | copy_le16(dest, clamp_max(n, 0xffff, clamped)); |
| 167 | } |
| 168 | |
René Scharfe | af95749 | 2017-04-24 19:32:36 +0200 | [diff] [blame] | 169 | static void copy_le32_clamp(unsigned char *dest, uint64_t n, int *clamped) |
| 170 | { |
| 171 | copy_le32(dest, clamp_max(n, 0xffffffff, clamped)); |
| 172 | } |
| 173 | |
René Scharfe | 3c78fd8 | 2017-04-24 19:31:44 +0200 | [diff] [blame] | 174 | static int strbuf_add_le(struct strbuf *sb, size_t size, uintmax_t n) |
| 175 | { |
| 176 | while (size-- > 0) { |
| 177 | strbuf_addch(sb, n & 0xff); |
| 178 | n >>= 8; |
| 179 | } |
| 180 | return -!!n; |
| 181 | } |
| 182 | |
René Scharfe | af95749 | 2017-04-24 19:32:36 +0200 | [diff] [blame] | 183 | static uint32_t clamp32(uintmax_t n) |
| 184 | { |
| 185 | const uintmax_t max = 0xffffffff; |
| 186 | return (n < max) ? n : max; |
| 187 | } |
| 188 | |
René Scharfe | c3c2e1a | 2013-03-15 23:21:51 +0100 | [diff] [blame] | 189 | static void *zlib_deflate_raw(void *data, unsigned long size, |
| 190 | int compression_level, |
| 191 | unsigned long *compressed_size) |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 192 | { |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 193 | git_zstream stream; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 194 | unsigned long maxsize; |
| 195 | void *buffer; |
| 196 | int result; |
| 197 | |
René Scharfe | c3c2e1a | 2013-03-15 23:21:51 +0100 | [diff] [blame] | 198 | git_deflate_init_raw(&stream, compression_level); |
Junio C Hamano | 225a6f1 | 2011-06-10 11:18:17 -0700 | [diff] [blame] | 199 | maxsize = git_deflate_bound(&stream, size); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 200 | buffer = xmalloc(maxsize); |
| 201 | |
| 202 | stream.next_in = data; |
| 203 | stream.avail_in = size; |
| 204 | stream.next_out = buffer; |
| 205 | stream.avail_out = maxsize; |
| 206 | |
| 207 | do { |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 208 | result = git_deflate(&stream, Z_FINISH); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 209 | } while (result == Z_OK); |
| 210 | |
| 211 | if (result != Z_STREAM_END) { |
| 212 | free(buffer); |
| 213 | return NULL; |
| 214 | } |
| 215 | |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 216 | git_deflate_end(&stream); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 217 | *compressed_size = stream.total_out; |
| 218 | |
| 219 | return buffer; |
| 220 | } |
| 221 | |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 222 | static void write_zip_data_desc(unsigned long size, |
| 223 | unsigned long compressed_size, |
| 224 | unsigned long crc) |
| 225 | { |
René Scharfe | 4cdf3f9 | 2017-04-24 19:33:34 +0200 | [diff] [blame] | 226 | if (size >= 0xffffffff || compressed_size >= 0xffffffff) { |
| 227 | struct zip64_data_desc trailer; |
| 228 | copy_le32(trailer.magic, 0x08074b50); |
| 229 | copy_le32(trailer.crc32, crc); |
| 230 | copy_le64(trailer.compressed_size, compressed_size); |
| 231 | copy_le64(trailer.size, size); |
| 232 | write_or_die(1, &trailer, ZIP64_DATA_DESC_SIZE); |
| 233 | zip_offset += ZIP64_DATA_DESC_SIZE; |
| 234 | } else { |
| 235 | struct zip_data_desc trailer; |
| 236 | copy_le32(trailer.magic, 0x08074b50); |
| 237 | copy_le32(trailer.crc32, crc); |
| 238 | copy_le32(trailer.compressed_size, compressed_size); |
| 239 | copy_le32(trailer.size, size); |
| 240 | write_or_die(1, &trailer, ZIP_DATA_DESC_SIZE); |
| 241 | zip_offset += ZIP_DATA_DESC_SIZE; |
| 242 | } |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 243 | } |
| 244 | |
René Scharfe | ebf5374 | 2012-05-03 08:51:06 +0700 | [diff] [blame] | 245 | static void set_zip_header_data_desc(struct zip_local_header *header, |
| 246 | unsigned long size, |
| 247 | unsigned long compressed_size, |
| 248 | unsigned long crc) |
| 249 | { |
| 250 | copy_le32(header->crc32, crc); |
| 251 | copy_le32(header->compressed_size, compressed_size); |
| 252 | copy_le32(header->size, size); |
| 253 | } |
| 254 | |
René Scharfe | 88182ba | 2012-09-18 21:46:56 +0200 | [diff] [blame] | 255 | static int has_only_ascii(const char *s) |
| 256 | { |
| 257 | for (;;) { |
| 258 | int c = *s++; |
| 259 | if (c == '\0') |
| 260 | return 1; |
| 261 | if (!isascii(c)) |
| 262 | return 0; |
| 263 | } |
| 264 | } |
| 265 | |
René Scharfe | 4aff646 | 2015-03-05 20:06:02 +0100 | [diff] [blame] | 266 | static int entry_is_binary(const char *path, const void *buffer, size_t size) |
| 267 | { |
| 268 | struct userdiff_driver *driver = userdiff_find_by_path(path); |
| 269 | if (!driver) |
| 270 | driver = userdiff_find_by_name("default"); |
| 271 | if (driver->binary != -1) |
| 272 | return driver->binary; |
| 273 | return buffer_is_binary(buffer, size); |
| 274 | } |
| 275 | |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 276 | #define STREAM_BUFFER_SIZE (1024 * 16) |
| 277 | |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 278 | static int write_zip_entry(struct archiver_args *args, |
brian m. carlson | 015ff4f | 2018-03-12 02:27:35 +0000 | [diff] [blame] | 279 | const struct object_id *oid, |
Nguyễn Thái Ngọc Duy | 9cb513b | 2012-05-03 08:51:03 +0700 | [diff] [blame] | 280 | const char *path, size_t pathlen, |
| 281 | unsigned int mode) |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 282 | { |
| 283 | struct zip_local_header header; |
René Scharfe | 3c78fd8 | 2017-04-24 19:31:44 +0200 | [diff] [blame] | 284 | uintmax_t offset = zip_offset; |
René Scharfe | 227bf59 | 2012-09-24 17:56:23 +0200 | [diff] [blame] | 285 | struct zip_extra_mtime extra; |
René Scharfe | 4cdf3f9 | 2017-04-24 19:33:34 +0200 | [diff] [blame] | 286 | struct zip64_extra extra64; |
| 287 | size_t header_extra_size = ZIP_EXTRA_MTIME_SIZE; |
| 288 | int need_zip64_extra = 0; |
Junio C Hamano | bb52d22 | 2012-09-18 13:32:39 -0700 | [diff] [blame] | 289 | unsigned long attr2; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 290 | unsigned long compressed_size; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 291 | unsigned long crc; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 292 | int method; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 293 | unsigned char *out; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 294 | void *deflated = NULL; |
Nguyễn Thái Ngọc Duy | 9cb513b | 2012-05-03 08:51:03 +0700 | [diff] [blame] | 295 | void *buffer; |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 296 | struct git_istream *stream = NULL; |
| 297 | unsigned long flags = 0; |
Nguyễn Thái Ngọc Duy | 9cb513b | 2012-05-03 08:51:03 +0700 | [diff] [blame] | 298 | unsigned long size; |
René Scharfe | 4aff646 | 2015-03-05 20:06:02 +0100 | [diff] [blame] | 299 | int is_binary = -1; |
| 300 | const char *path_without_prefix = path + args->baselen; |
René Scharfe | 0f747f9 | 2015-08-22 21:06:31 +0200 | [diff] [blame] | 301 | unsigned int creator_version = 0; |
René Scharfe | ebdfa29 | 2017-04-27 22:25:45 +0200 | [diff] [blame] | 302 | unsigned int version_needed = 10; |
René Scharfe | af95749 | 2017-04-24 19:32:36 +0200 | [diff] [blame] | 303 | size_t zip_dir_extra_size = ZIP_EXTRA_MTIME_SIZE; |
| 304 | size_t zip64_dir_extra_payload_size = 0; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 305 | |
Rene Scharfe | 38f4d13 | 2006-11-18 13:06:56 +0100 | [diff] [blame] | 306 | crc = crc32(0, NULL, 0); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 307 | |
René Scharfe | 88182ba | 2012-09-18 21:46:56 +0200 | [diff] [blame] | 308 | if (!has_only_ascii(path)) { |
| 309 | if (is_utf8(path)) |
| 310 | flags |= ZIP_UTF8; |
| 311 | else |
Nguyễn Thái Ngọc Duy | 02f3fe5 | 2018-07-21 09:49:21 +0200 | [diff] [blame] | 312 | warning(_("path is not valid UTF-8: %s"), path); |
René Scharfe | 88182ba | 2012-09-18 21:46:56 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 315 | if (pathlen > 0xffff) { |
Nguyễn Thái Ngọc Duy | 02f3fe5 | 2018-07-21 09:49:21 +0200 | [diff] [blame] | 316 | return error(_("path too long (%d chars, SHA1: %s): %s"), |
brian m. carlson | 015ff4f | 2018-03-12 02:27:35 +0000 | [diff] [blame] | 317 | (int)pathlen, oid_to_hex(oid), path); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 320 | if (S_ISDIR(mode) || S_ISGITLINK(mode)) { |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 321 | method = 0; |
Rene Scharfe | 62cdce1 | 2006-10-07 01:47:35 +0200 | [diff] [blame] | 322 | attr2 = 16; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 323 | out = NULL; |
René Scharfe | 60df6bd | 2012-05-03 08:51:05 +0700 | [diff] [blame] | 324 | size = 0; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 325 | compressed_size = 0; |
Nguyễn Thái Ngọc Duy | 9cb513b | 2012-05-03 08:51:03 +0700 | [diff] [blame] | 326 | buffer = NULL; |
Rene Scharfe | 62cdce1 | 2006-10-07 01:47:35 +0200 | [diff] [blame] | 327 | } else if (S_ISREG(mode) || S_ISLNK(mode)) { |
Stefan Beller | 0df8e96 | 2018-04-25 11:20:59 -0700 | [diff] [blame] | 328 | enum object_type type = oid_object_info(the_repository, oid, |
| 329 | &size); |
Nguyễn Thái Ngọc Duy | 9cb513b | 2012-05-03 08:51:03 +0700 | [diff] [blame] | 330 | |
Rene Scharfe | 62cdce1 | 2006-10-07 01:47:35 +0200 | [diff] [blame] | 331 | method = 0; |
Junio C Hamano | bb52d22 | 2012-09-18 13:32:39 -0700 | [diff] [blame] | 332 | attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) : |
| 333 | (mode & 0111) ? ((mode) << 16) : 0; |
René Scharfe | 0f747f9 | 2015-08-22 21:06:31 +0200 | [diff] [blame] | 334 | if (S_ISLNK(mode) || (mode & 0111)) |
| 335 | creator_version = 0x0317; |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 336 | if (S_ISREG(mode) && args->compression_level != 0 && size > 0) |
Rene Scharfe | 62cdce1 | 2006-10-07 01:47:35 +0200 | [diff] [blame] | 337 | method = 8; |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 338 | |
| 339 | if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert && |
René Scharfe | c743c21 | 2012-05-03 08:51:08 +0700 | [diff] [blame] | 340 | size > big_file_threshold) { |
brian m. carlson | ef7b519 | 2018-03-12 02:27:40 +0000 | [diff] [blame] | 341 | stream = open_istream(oid, &type, &size, NULL); |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 342 | if (!stream) |
Nguyễn Thái Ngọc Duy | 02f3fe5 | 2018-07-21 09:49:21 +0200 | [diff] [blame] | 343 | return error(_("cannot stream blob %s"), |
brian m. carlson | 015ff4f | 2018-03-12 02:27:35 +0000 | [diff] [blame] | 344 | oid_to_hex(oid)); |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 345 | flags |= ZIP_STREAM; |
| 346 | out = buffer = NULL; |
| 347 | } else { |
brian m. carlson | e5ec981 | 2018-03-12 02:27:36 +0000 | [diff] [blame] | 348 | buffer = object_file_to_archive(args, path, oid, mode, |
| 349 | &type, &size); |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 350 | if (!buffer) |
Nguyễn Thái Ngọc Duy | 02f3fe5 | 2018-07-21 09:49:21 +0200 | [diff] [blame] | 351 | return error(_("cannot read %s"), |
brian m. carlson | 015ff4f | 2018-03-12 02:27:35 +0000 | [diff] [blame] | 352 | oid_to_hex(oid)); |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 353 | crc = crc32(crc, buffer, size); |
René Scharfe | 4aff646 | 2015-03-05 20:06:02 +0100 | [diff] [blame] | 354 | is_binary = entry_is_binary(path_without_prefix, |
| 355 | buffer, size); |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 356 | out = buffer; |
| 357 | } |
René Scharfe | d3c1472 | 2013-02-27 11:20:21 +0100 | [diff] [blame] | 358 | compressed_size = (method == 0) ? size : 0; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 359 | } else { |
Nguyễn Thái Ngọc Duy | 02f3fe5 | 2018-07-21 09:49:21 +0200 | [diff] [blame] | 360 | return error(_("unsupported file mode: 0%o (SHA1: %s)"), mode, |
brian m. carlson | 015ff4f | 2018-03-12 02:27:35 +0000 | [diff] [blame] | 361 | oid_to_hex(oid)); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 362 | } |
| 363 | |
René Scharfe | 88329ca | 2015-08-22 21:06:45 +0200 | [diff] [blame] | 364 | if (creator_version > max_creator_version) |
| 365 | max_creator_version = creator_version; |
| 366 | |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 367 | if (buffer && method == 8) { |
René Scharfe | c3c2e1a | 2013-03-15 23:21:51 +0100 | [diff] [blame] | 368 | out = deflated = zlib_deflate_raw(buffer, size, |
| 369 | args->compression_level, |
| 370 | &compressed_size); |
| 371 | if (!out || compressed_size >= size) { |
| 372 | out = buffer; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 373 | method = 0; |
| 374 | compressed_size = size; |
| 375 | } |
| 376 | } |
| 377 | |
René Scharfe | 227bf59 | 2012-09-24 17:56:23 +0200 | [diff] [blame] | 378 | copy_le16(extra.magic, 0x5455); |
| 379 | copy_le16(extra.extra_size, ZIP_EXTRA_MTIME_PAYLOAD_SIZE); |
| 380 | extra.flags[0] = 1; /* just mtime */ |
| 381 | copy_le32(extra.mtime, args->time); |
| 382 | |
René Scharfe | 4cdf3f9 | 2017-04-24 19:33:34 +0200 | [diff] [blame] | 383 | if (size > 0xffffffff || compressed_size > 0xffffffff) |
| 384 | need_zip64_extra = 1; |
| 385 | if (stream && size > 0x7fffffff) |
| 386 | need_zip64_extra = 1; |
| 387 | |
René Scharfe | ebdfa29 | 2017-04-27 22:25:45 +0200 | [diff] [blame] | 388 | if (need_zip64_extra) |
| 389 | version_needed = 45; |
| 390 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 391 | copy_le32(header.magic, 0x04034b50); |
René Scharfe | ebdfa29 | 2017-04-27 22:25:45 +0200 | [diff] [blame] | 392 | copy_le16(header.version, version_needed); |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 393 | copy_le16(header.flags, flags); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 394 | copy_le16(header.compression_method, method); |
| 395 | copy_le16(header.mtime, zip_time); |
| 396 | copy_le16(header.mdate, zip_date); |
René Scharfe | 4cdf3f9 | 2017-04-24 19:33:34 +0200 | [diff] [blame] | 397 | if (need_zip64_extra) { |
| 398 | set_zip_header_data_desc(&header, 0xffffffff, 0xffffffff, crc); |
| 399 | header_extra_size += ZIP64_EXTRA_SIZE; |
| 400 | } else { |
| 401 | set_zip_header_data_desc(&header, size, compressed_size, crc); |
| 402 | } |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 403 | copy_le16(header.filename_length, pathlen); |
René Scharfe | 4cdf3f9 | 2017-04-24 19:33:34 +0200 | [diff] [blame] | 404 | copy_le16(header.extra_length, header_extra_size); |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 405 | write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE); |
| 406 | zip_offset += ZIP_LOCAL_HEADER_SIZE; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 407 | write_or_die(1, path, pathlen); |
| 408 | zip_offset += pathlen; |
René Scharfe | 227bf59 | 2012-09-24 17:56:23 +0200 | [diff] [blame] | 409 | write_or_die(1, &extra, ZIP_EXTRA_MTIME_SIZE); |
| 410 | zip_offset += ZIP_EXTRA_MTIME_SIZE; |
René Scharfe | 4cdf3f9 | 2017-04-24 19:33:34 +0200 | [diff] [blame] | 411 | if (need_zip64_extra) { |
| 412 | copy_le16(extra64.magic, 0x0001); |
| 413 | copy_le16(extra64.extra_size, ZIP64_EXTRA_PAYLOAD_SIZE); |
| 414 | copy_le64(extra64.size, size); |
| 415 | copy_le64(extra64.compressed_size, compressed_size); |
| 416 | write_or_die(1, &extra64, ZIP64_EXTRA_SIZE); |
| 417 | zip_offset += ZIP64_EXTRA_SIZE; |
| 418 | } |
| 419 | |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 420 | if (stream && method == 0) { |
| 421 | unsigned char buf[STREAM_BUFFER_SIZE]; |
| 422 | ssize_t readlen; |
| 423 | |
| 424 | for (;;) { |
| 425 | readlen = read_istream(stream, buf, sizeof(buf)); |
| 426 | if (readlen <= 0) |
| 427 | break; |
| 428 | crc = crc32(crc, buf, readlen); |
René Scharfe | 4aff646 | 2015-03-05 20:06:02 +0100 | [diff] [blame] | 429 | if (is_binary == -1) |
| 430 | is_binary = entry_is_binary(path_without_prefix, |
| 431 | buf, readlen); |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 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); |
René Scharfe | c743c21 | 2012-05-03 08:51:08 +0700 | [diff] [blame] | 442 | } else if (stream && method == 8) { |
| 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 | |
René Scharfe | c3c2e1a | 2013-03-15 23:21:51 +0100 | [diff] [blame] | 450 | git_deflate_init_raw(&zstream, args->compression_level); |
René Scharfe | c743c21 | 2012-05-03 08:51:08 +0700 | [diff] [blame] | 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); |
René Scharfe | 4aff646 | 2015-03-05 20:06:02 +0100 | [diff] [blame] | 461 | if (is_binary == -1) |
| 462 | is_binary = entry_is_binary(path_without_prefix, |
| 463 | buf, readlen); |
René Scharfe | c743c21 | 2012-05-03 08:51:08 +0700 | [diff] [blame] | 464 | |
| 465 | zstream.next_in = buf; |
| 466 | zstream.avail_in = readlen; |
| 467 | result = git_deflate(&zstream, 0); |
| 468 | if (result != Z_OK) |
Nguyễn Thái Ngọc Duy | 02f3fe5 | 2018-07-21 09:49:21 +0200 | [diff] [blame] | 469 | die(_("deflate error (%d)"), result); |
René Scharfe | c3c2e1a | 2013-03-15 23:21:51 +0100 | [diff] [blame] | 470 | out_len = zstream.next_out - compressed; |
René Scharfe | c743c21 | 2012-05-03 08:51:08 +0700 | [diff] [blame] | 471 | |
| 472 | if (out_len > 0) { |
René Scharfe | c3c2e1a | 2013-03-15 23:21:51 +0100 | [diff] [blame] | 473 | write_or_die(1, compressed, out_len); |
René Scharfe | c743c21 | 2012-05-03 08:51:08 +0700 | [diff] [blame] | 474 | compressed_size += out_len; |
| 475 | zstream.next_out = compressed; |
| 476 | zstream.avail_out = sizeof(compressed); |
| 477 | } |
| 478 | |
| 479 | } |
| 480 | close_istream(stream); |
| 481 | if (readlen) |
| 482 | return readlen; |
| 483 | |
| 484 | zstream.next_in = buf; |
| 485 | zstream.avail_in = 0; |
| 486 | result = git_deflate(&zstream, Z_FINISH); |
| 487 | if (result != Z_STREAM_END) |
| 488 | die("deflate error (%d)", result); |
| 489 | |
| 490 | git_deflate_end(&zstream); |
René Scharfe | c3c2e1a | 2013-03-15 23:21:51 +0100 | [diff] [blame] | 491 | out_len = zstream.next_out - compressed; |
| 492 | write_or_die(1, compressed, out_len); |
René Scharfe | c743c21 | 2012-05-03 08:51:08 +0700 | [diff] [blame] | 493 | compressed_size += out_len; |
| 494 | zip_offset += compressed_size; |
| 495 | |
| 496 | write_zip_data_desc(size, compressed_size, crc); |
René Scharfe | 2158f88 | 2012-05-03 08:51:07 +0700 | [diff] [blame] | 497 | } else if (compressed_size > 0) { |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 498 | write_or_die(1, out, compressed_size); |
| 499 | zip_offset += compressed_size; |
| 500 | } |
| 501 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 502 | free(deflated); |
Nguyễn Thái Ngọc Duy | 9cb513b | 2012-05-03 08:51:03 +0700 | [diff] [blame] | 503 | free(buffer); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 504 | |
René Scharfe | 4cdf3f9 | 2017-04-24 19:33:34 +0200 | [diff] [blame] | 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; |
René Scharfe | af95749 | 2017-04-24 19:32:36 +0200 | [diff] [blame] | 513 | zip_dir_extra_size += 2 + 2 + zip64_dir_extra_payload_size; |
| 514 | } |
| 515 | |
René Scharfe | 3c78fd8 | 2017-04-24 19:31:44 +0200 | [diff] [blame] | 516 | strbuf_add_le(&zip_dir, 4, 0x02014b50); /* magic */ |
| 517 | strbuf_add_le(&zip_dir, 2, creator_version); |
René Scharfe | ebdfa29 | 2017-04-27 22:25:45 +0200 | [diff] [blame] | 518 | strbuf_add_le(&zip_dir, 2, version_needed); |
René Scharfe | 3c78fd8 | 2017-04-24 19:31:44 +0200 | [diff] [blame] | 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); |
René Scharfe | 4cdf3f9 | 2017-04-24 19:33:34 +0200 | [diff] [blame] | 524 | strbuf_add_le(&zip_dir, 4, clamp32(compressed_size)); |
| 525 | strbuf_add_le(&zip_dir, 4, clamp32(size)); |
René Scharfe | 3c78fd8 | 2017-04-24 19:31:44 +0200 | [diff] [blame] | 526 | strbuf_add_le(&zip_dir, 2, pathlen); |
René Scharfe | af95749 | 2017-04-24 19:32:36 +0200 | [diff] [blame] | 527 | strbuf_add_le(&zip_dir, 2, zip_dir_extra_size); |
René Scharfe | 3c78fd8 | 2017-04-24 19:31:44 +0200 | [diff] [blame] | 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); |
René Scharfe | af95749 | 2017-04-24 19:32:36 +0200 | [diff] [blame] | 532 | strbuf_add_le(&zip_dir, 4, clamp32(offset)); |
René Scharfe | c061a14 | 2017-04-24 19:30:49 +0200 | [diff] [blame] | 533 | strbuf_add(&zip_dir, path, pathlen); |
| 534 | strbuf_add(&zip_dir, &extra, ZIP_EXTRA_MTIME_SIZE); |
René Scharfe | af95749 | 2017-04-24 19:32:36 +0200 | [diff] [blame] | 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); |
René Scharfe | 4cdf3f9 | 2017-04-24 19:33:34 +0200 | [diff] [blame] | 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); |
René Scharfe | af95749 | 2017-04-24 19:32:36 +0200 | [diff] [blame] | 542 | if (offset >= 0xffffffff) |
| 543 | strbuf_add_le(&zip_dir, 8, offset); |
| 544 | } |
René Scharfe | ebf5374 | 2012-05-03 08:51:06 +0700 | [diff] [blame] | 545 | zip_dir_entries++; |
| 546 | |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 547 | return 0; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 548 | } |
| 549 | |
René Scharfe | 88329ca | 2015-08-22 21:06:45 +0200 | [diff] [blame] | 550 | static 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); |
René Scharfe | c061a14 | 2017-04-24 19:30:49 +0200 | [diff] [blame] | 563 | copy_le64(trailer64.size, zip_dir.len); |
René Scharfe | 88329ca | 2015-08-22 21:06:45 +0200 | [diff] [blame] | 564 | copy_le64(trailer64.offset, zip_offset); |
| 565 | |
| 566 | copy_le32(locator64.magic, 0x07064b50); |
| 567 | copy_le32(locator64.disk, 0); |
René Scharfe | c061a14 | 2017-04-24 19:30:49 +0200 | [diff] [blame] | 568 | copy_le64(locator64.offset, zip_offset + zip_dir.len); |
René Scharfe | 88329ca | 2015-08-22 21:06:45 +0200 | [diff] [blame] | 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 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 575 | static void write_zip_trailer(const unsigned char *sha1) |
| 576 | { |
| 577 | struct zip_dir_trailer trailer; |
René Scharfe | 88329ca | 2015-08-22 21:06:45 +0200 | [diff] [blame] | 578 | int clamped = 0; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 579 | |
| 580 | copy_le32(trailer.magic, 0x06054b50); |
| 581 | copy_le16(trailer.disk, 0); |
| 582 | copy_le16(trailer.directory_start_disk, 0); |
René Scharfe | 88329ca | 2015-08-22 21:06:45 +0200 | [diff] [blame] | 583 | copy_le16_clamp(trailer.entries_on_this_disk, zip_dir_entries, |
| 584 | &clamped); |
| 585 | copy_le16_clamp(trailer.entries, zip_dir_entries, &clamped); |
René Scharfe | c061a14 | 2017-04-24 19:30:49 +0200 | [diff] [blame] | 586 | copy_le32(trailer.size, zip_dir.len); |
René Scharfe | af95749 | 2017-04-24 19:32:36 +0200 | [diff] [blame] | 587 | copy_le32_clamp(trailer.offset, zip_offset, &clamped); |
brian m. carlson | aeecdcd | 2015-03-13 23:39:31 +0000 | [diff] [blame] | 588 | copy_le16(trailer.comment_length, sha1 ? GIT_SHA1_HEXSZ : 0); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 589 | |
René Scharfe | c061a14 | 2017-04-24 19:30:49 +0200 | [diff] [blame] | 590 | write_or_die(1, zip_dir.buf, zip_dir.len); |
René Scharfe | 88329ca | 2015-08-22 21:06:45 +0200 | [diff] [blame] | 591 | if (clamped) |
| 592 | write_zip64_trailer(); |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 593 | write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 594 | if (sha1) |
brian m. carlson | aeecdcd | 2015-03-13 23:39:31 +0000 | [diff] [blame] | 595 | write_or_die(1, sha1_to_hex(sha1), GIT_SHA1_HEXSZ); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 596 | } |
| 597 | |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 598 | static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time) |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 599 | { |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 600 | time_t time; |
| 601 | struct tm *t; |
| 602 | |
| 603 | if (date_overflows(*timestamp)) |
Nguyễn Thái Ngọc Duy | 02f3fe5 | 2018-07-21 09:49:21 +0200 | [diff] [blame] | 604 | die(_("timestamp too large for this system: %"PRItime), |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 605 | *timestamp); |
| 606 | time = (time_t)*timestamp; |
| 607 | t = localtime(&time); |
| 608 | *timestamp = time; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 609 | |
| 610 | *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 + |
| 611 | (t->tm_year + 1900 - 1980) * 512; |
| 612 | *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048; |
| 613 | } |
| 614 | |
Jeff King | 965cba2 | 2017-01-02 17:25:09 -0500 | [diff] [blame] | 615 | static int archive_zip_config(const char *var, const char *value, void *data) |
| 616 | { |
| 617 | return userdiff_config(var, value); |
| 618 | } |
| 619 | |
Jeff King | 4d7c989 | 2011-06-21 21:24:07 -0400 | [diff] [blame] | 620 | static int write_zip_archive(const struct archiver *ar, |
| 621 | struct archiver_args *args) |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 622 | { |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 623 | int err; |
| 624 | |
Jeff King | 965cba2 | 2017-01-02 17:25:09 -0500 | [diff] [blame] | 625 | git_config(archive_zip_config, NULL); |
| 626 | |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 627 | dos_time(&args->time, &zip_date, &zip_time); |
| 628 | |
René Scharfe | c061a14 | 2017-04-24 19:30:49 +0200 | [diff] [blame] | 629 | strbuf_init(&zip_dir, 0); |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 630 | |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 631 | err = write_archive_entries(args, write_zip_entry); |
| 632 | if (!err) |
| 633 | write_zip_trailer(args->commit_sha1); |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 634 | |
René Scharfe | c061a14 | 2017-04-24 19:30:49 +0200 | [diff] [blame] | 635 | strbuf_release(&zip_dir); |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 636 | |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 637 | return err; |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 638 | } |
Jeff King | 13e0f88 | 2011-06-21 21:23:33 -0400 | [diff] [blame] | 639 | |
| 640 | static struct archiver zip_archiver = { |
| 641 | "zip", |
| 642 | write_zip_archive, |
Jeff King | 7b97730 | 2011-06-21 23:17:35 -0400 | [diff] [blame] | 643 | ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE |
Jeff King | 13e0f88 | 2011-06-21 21:23:33 -0400 | [diff] [blame] | 644 | }; |
| 645 | |
| 646 | void init_zip_archiver(void) |
| 647 | { |
| 648 | register_archiver(&zip_archiver); |
| 649 | } |