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