archive-zip.c 17 KB

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