unzip.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini unzip implementation for busybox
  4. *
  5. * Copyright (C) 2004 by Ed Clark
  6. *
  7. * Loosely based on original busybox unzip applet by Laurence Anderson.
  8. * All options and features should work in this version.
  9. *
  10. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  11. */
  12. /* For reference see
  13. * http://www.pkware.com/company/standards/appnote/
  14. * http://www.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip
  15. */
  16. /* TODO
  17. * Zip64 + other methods
  18. */
  19. #include "libbb.h"
  20. #include "unarchive.h"
  21. enum {
  22. #if BB_BIG_ENDIAN
  23. ZIP_FILEHEADER_MAGIC = 0x504b0304,
  24. ZIP_CDS_MAGIC = 0x504b0102,
  25. ZIP_CDE_MAGIC = 0x504b0506,
  26. ZIP_DD_MAGIC = 0x504b0708,
  27. #else
  28. ZIP_FILEHEADER_MAGIC = 0x04034b50,
  29. ZIP_CDS_MAGIC = 0x02014b50,
  30. ZIP_CDE_MAGIC = 0x06054b50,
  31. ZIP_DD_MAGIC = 0x08074b50,
  32. #endif
  33. };
  34. #define ZIP_HEADER_LEN 26
  35. typedef union {
  36. uint8_t raw[ZIP_HEADER_LEN];
  37. struct {
  38. uint16_t version; /* 0-1 */
  39. uint16_t flags; /* 2-3 */
  40. uint16_t method; /* 4-5 */
  41. uint16_t modtime; /* 6-7 */
  42. uint16_t moddate; /* 8-9 */
  43. uint32_t crc32 PACKED; /* 10-13 */
  44. uint32_t cmpsize PACKED; /* 14-17 */
  45. uint32_t ucmpsize PACKED; /* 18-21 */
  46. uint16_t filename_len; /* 22-23 */
  47. uint16_t extra_len; /* 24-25 */
  48. } formatted PACKED;
  49. } zip_header_t; /* PACKED - gcc 4.2.1 doesn't like it (spews warning) */
  50. /* Check the offset of the last element, not the length. This leniency
  51. * allows for poor packing, whereby the overall struct may be too long,
  52. * even though the elements are all in the right place.
  53. */
  54. struct BUG_zip_header_must_be_26_bytes {
  55. char BUG_zip_header_must_be_26_bytes[
  56. offsetof(zip_header_t, formatted.extra_len) + 2
  57. == ZIP_HEADER_LEN ? 1 : -1];
  58. };
  59. #define FIX_ENDIANNESS_ZIP(zip_header) do { \
  60. (zip_header).formatted.version = SWAP_LE16((zip_header).formatted.version ); \
  61. (zip_header).formatted.flags = SWAP_LE16((zip_header).formatted.flags ); \
  62. (zip_header).formatted.method = SWAP_LE16((zip_header).formatted.method ); \
  63. (zip_header).formatted.modtime = SWAP_LE16((zip_header).formatted.modtime ); \
  64. (zip_header).formatted.moddate = SWAP_LE16((zip_header).formatted.moddate ); \
  65. (zip_header).formatted.crc32 = SWAP_LE32((zip_header).formatted.crc32 ); \
  66. (zip_header).formatted.cmpsize = SWAP_LE32((zip_header).formatted.cmpsize ); \
  67. (zip_header).formatted.ucmpsize = SWAP_LE32((zip_header).formatted.ucmpsize ); \
  68. (zip_header).formatted.filename_len = SWAP_LE16((zip_header).formatted.filename_len); \
  69. (zip_header).formatted.extra_len = SWAP_LE16((zip_header).formatted.extra_len ); \
  70. } while (0)
  71. #define CDS_HEADER_LEN 42
  72. typedef union {
  73. uint8_t raw[CDS_HEADER_LEN];
  74. struct {
  75. /* uint32_t signature; 50 4b 01 02 */
  76. uint16_t version_made_by; /* 0-1 */
  77. uint16_t version_needed; /* 2-3 */
  78. uint16_t cds_flags; /* 4-5 */
  79. uint16_t method; /* 6-7 */
  80. uint16_t mtime; /* 8-9 */
  81. uint16_t mdate; /* 10-11 */
  82. uint32_t crc32; /* 12-15 */
  83. uint32_t cmpsize; /* 16-19 */
  84. uint32_t ucmpsize; /* 20-23 */
  85. uint16_t file_name_length; /* 24-25 */
  86. uint16_t extra_field_length; /* 26-27 */
  87. uint16_t file_comment_length; /* 28-29 */
  88. uint16_t disk_number_start; /* 30-31 */
  89. uint16_t internal_file_attributes; /* 32-33 */
  90. uint32_t external_file_attributes PACKED; /* 34-37 */
  91. uint32_t relative_offset_of_local_header PACKED; /* 38-41 */
  92. } formatted PACKED;
  93. } cds_header_t;
  94. struct BUG_cds_header_must_be_42_bytes {
  95. char BUG_cds_header_must_be_42_bytes[
  96. offsetof(cds_header_t, formatted.relative_offset_of_local_header) + 4
  97. == CDS_HEADER_LEN ? 1 : -1];
  98. };
  99. #define FIX_ENDIANNESS_CDS(cds_header) do { \
  100. (cds_header).formatted.crc32 = SWAP_LE32((cds_header).formatted.crc32 ); \
  101. (cds_header).formatted.cmpsize = SWAP_LE32((cds_header).formatted.cmpsize ); \
  102. (cds_header).formatted.ucmpsize = SWAP_LE32((cds_header).formatted.ucmpsize ); \
  103. (cds_header).formatted.file_name_length = SWAP_LE16((cds_header).formatted.file_name_length); \
  104. (cds_header).formatted.extra_field_length = SWAP_LE16((cds_header).formatted.extra_field_length); \
  105. (cds_header).formatted.file_comment_length = SWAP_LE16((cds_header).formatted.file_comment_length); \
  106. } while (0)
  107. #define CDE_HEADER_LEN 16
  108. typedef union {
  109. uint8_t raw[CDE_HEADER_LEN];
  110. struct {
  111. /* uint32_t signature; 50 4b 05 06 */
  112. uint16_t this_disk_no;
  113. uint16_t disk_with_cds_no;
  114. uint16_t cds_entries_on_this_disk;
  115. uint16_t cds_entries_total;
  116. uint32_t cds_size;
  117. uint32_t cds_offset;
  118. /* uint16_t file_comment_length; */
  119. /* .ZIP file comment (variable size) */
  120. } formatted PACKED;
  121. } cde_header_t;
  122. struct BUG_cde_header_must_be_16_bytes {
  123. char BUG_cde_header_must_be_16_bytes[
  124. sizeof(cde_header_t) == CDE_HEADER_LEN ? 1 : -1];
  125. };
  126. #define FIX_ENDIANNESS_CDE(cde_header) do { \
  127. (cde_header).formatted.cds_offset = SWAP_LE32((cde_header).formatted.cds_offset); \
  128. } while (0)
  129. enum { zip_fd = 3 };
  130. #if ENABLE_DESKTOP
  131. /* NB: does not preserve file position! */
  132. static uint32_t find_cds_offset(void)
  133. {
  134. unsigned char buf[1024];
  135. cde_header_t cde_header;
  136. unsigned char *p;
  137. off_t end;
  138. end = xlseek(zip_fd, 0, SEEK_END);
  139. if (end < 1024)
  140. end = 1024;
  141. end -= 1024;
  142. xlseek(zip_fd, end, SEEK_SET);
  143. full_read(zip_fd, buf, 1024);
  144. p = buf;
  145. while (p <= buf + 1024 - CDE_HEADER_LEN - 4) {
  146. if (*p != 'P') {
  147. p++;
  148. continue;
  149. }
  150. if (*++p != 'K')
  151. continue;
  152. if (*++p != 5)
  153. continue;
  154. if (*++p != 6)
  155. continue;
  156. /* we found CDE! */
  157. memcpy(cde_header.raw, p + 1, CDE_HEADER_LEN);
  158. FIX_ENDIANNESS_CDE(cde_header);
  159. return cde_header.formatted.cds_offset;
  160. }
  161. bb_error_msg_and_die("can't find file table");
  162. };
  163. static uint32_t read_next_cds(int count_m1, uint32_t cds_offset, cds_header_t *cds_ptr)
  164. {
  165. off_t org;
  166. org = xlseek(zip_fd, 0, SEEK_CUR);
  167. if (!cds_offset)
  168. cds_offset = find_cds_offset();
  169. while (count_m1-- >= 0) {
  170. xlseek(zip_fd, cds_offset + 4, SEEK_SET);
  171. xread(zip_fd, cds_ptr->raw, CDS_HEADER_LEN);
  172. FIX_ENDIANNESS_CDS(*cds_ptr);
  173. cds_offset += 4 + CDS_HEADER_LEN
  174. + cds_ptr->formatted.file_name_length
  175. + cds_ptr->formatted.extra_field_length
  176. + cds_ptr->formatted.file_comment_length;
  177. }
  178. xlseek(zip_fd, org, SEEK_SET);
  179. return cds_offset;
  180. };
  181. #endif
  182. static void unzip_skip(off_t skip)
  183. {
  184. if (lseek(zip_fd, skip, SEEK_CUR) == (off_t)-1)
  185. bb_copyfd_exact_size(zip_fd, -1, skip);
  186. }
  187. static void unzip_create_leading_dirs(const char *fn)
  188. {
  189. /* Create all leading directories */
  190. char *name = xstrdup(fn);
  191. if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) {
  192. bb_error_msg_and_die("exiting"); /* bb_make_directory is noisy */
  193. }
  194. free(name);
  195. }
  196. static void unzip_extract(zip_header_t *zip_header, int dst_fd)
  197. {
  198. if (zip_header->formatted.method == 0) {
  199. /* Method 0 - stored (not compressed) */
  200. off_t size = zip_header->formatted.ucmpsize;
  201. if (size)
  202. bb_copyfd_exact_size(zip_fd, dst_fd, size);
  203. } else {
  204. /* Method 8 - inflate */
  205. inflate_unzip_result res;
  206. if (inflate_unzip(&res, zip_header->formatted.cmpsize, zip_fd, dst_fd) < 0)
  207. bb_error_msg_and_die("inflate error");
  208. /* Validate decompression - crc */
  209. if (zip_header->formatted.crc32 != (res.crc ^ 0xffffffffL)) {
  210. bb_error_msg_and_die("crc error");
  211. }
  212. /* Validate decompression - size */
  213. if (zip_header->formatted.ucmpsize != res.bytes_out) {
  214. /* Don't die. Who knows, maybe len calculation
  215. * was botched somewhere. After all, crc matched! */
  216. bb_error_msg("bad length");
  217. }
  218. }
  219. }
  220. int unzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  221. int unzip_main(int argc, char **argv)
  222. {
  223. enum { O_PROMPT, O_NEVER, O_ALWAYS };
  224. zip_header_t zip_header;
  225. smallint quiet = 0;
  226. IF_NOT_DESKTOP(const) smallint verbose = 0;
  227. smallint listing = 0;
  228. smallint overwrite = O_PROMPT;
  229. #if ENABLE_DESKTOP
  230. uint32_t cds_offset;
  231. unsigned cds_entries;
  232. #endif
  233. unsigned long total_usize;
  234. unsigned long total_size;
  235. unsigned total_entries;
  236. int dst_fd = -1;
  237. char *src_fn = NULL;
  238. char *dst_fn = NULL;
  239. llist_t *zaccept = NULL;
  240. llist_t *zreject = NULL;
  241. char *base_dir = NULL;
  242. int i, opt;
  243. int opt_range = 0;
  244. char key_buf[80];
  245. struct stat stat_buf;
  246. /* -q, -l and -v: UnZip 5.52 of 28 February 2005, by Info-ZIP:
  247. *
  248. * # /usr/bin/unzip -qq -v decompress_unlzma.i.zip
  249. * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i
  250. * # /usr/bin/unzip -q -v decompress_unlzma.i.zip
  251. * Length Method Size Ratio Date Time CRC-32 Name
  252. * -------- ------ ------- ----- ---- ---- ------ ----
  253. * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i
  254. * -------- ------- --- -------
  255. * 204372 35278 83% 1 file
  256. * # /usr/bin/unzip -v decompress_unlzma.i.zip
  257. * Archive: decompress_unlzma.i.zip
  258. * Length Method Size Ratio Date Time CRC-32 Name
  259. * -------- ------ ------- ----- ---- ---- ------ ----
  260. * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i
  261. * -------- ------- --- -------
  262. * 204372 35278 83% 1 file
  263. * # unzip -v decompress_unlzma.i.zip
  264. * Archive: decompress_unlzma.i.zip
  265. * Length Date Time Name
  266. * -------- ---- ---- ----
  267. * 204372 09-06-09 14:23 decompress_unlzma.i
  268. * -------- -------
  269. * 204372 1 files
  270. * # /usr/bin/unzip -l -qq decompress_unlzma.i.zip
  271. * 204372 09-06-09 14:23 decompress_unlzma.i
  272. * # /usr/bin/unzip -l -q decompress_unlzma.i.zip
  273. * Length Date Time Name
  274. * -------- ---- ---- ----
  275. * 204372 09-06-09 14:23 decompress_unlzma.i
  276. * -------- -------
  277. * 204372 1 file
  278. * # /usr/bin/unzip -l decompress_unlzma.i.zip
  279. * Archive: decompress_unlzma.i.zip
  280. * Length Date Time Name
  281. * -------- ---- ---- ----
  282. * 204372 09-06-09 14:23 decompress_unlzma.i
  283. * -------- -------
  284. * 204372 1 file
  285. */
  286. /* '-' makes getopt return 1 for non-options */
  287. while ((opt = getopt(argc, argv, "-d:lnopqxv")) != -1) {
  288. switch (opt_range) {
  289. case 0: /* Options */
  290. switch (opt) {
  291. case 'l': /* List */
  292. listing = 1;
  293. break;
  294. case 'n': /* Never overwrite existing files */
  295. overwrite = O_NEVER;
  296. break;
  297. case 'o': /* Always overwrite existing files */
  298. overwrite = O_ALWAYS;
  299. break;
  300. case 'p': /* Extract files to stdout and fall through to set verbosity */
  301. dst_fd = STDOUT_FILENO;
  302. case 'q': /* Be quiet */
  303. quiet++;
  304. break;
  305. case 'v': /* Verbose list */
  306. IF_DESKTOP(verbose++;)
  307. listing = 1;
  308. break;
  309. case 1: /* The zip file */
  310. /* +5: space for ".zip" and NUL */
  311. src_fn = xmalloc(strlen(optarg) + 5);
  312. strcpy(src_fn, optarg);
  313. opt_range++;
  314. break;
  315. default:
  316. bb_show_usage();
  317. }
  318. break;
  319. case 1: /* Include files */
  320. if (opt == 1) {
  321. llist_add_to(&zaccept, optarg);
  322. break;
  323. }
  324. if (opt == 'd') {
  325. base_dir = optarg;
  326. opt_range += 2;
  327. break;
  328. }
  329. if (opt == 'x') {
  330. opt_range++;
  331. break;
  332. }
  333. bb_show_usage();
  334. case 2 : /* Exclude files */
  335. if (opt == 1) {
  336. llist_add_to(&zreject, optarg);
  337. break;
  338. }
  339. if (opt == 'd') { /* Extract to base directory */
  340. base_dir = optarg;
  341. opt_range++;
  342. break;
  343. }
  344. /* fall through */
  345. default:
  346. bb_show_usage();
  347. }
  348. }
  349. if (src_fn == NULL) {
  350. bb_show_usage();
  351. }
  352. /* Open input file */
  353. if (LONE_DASH(src_fn)) {
  354. xdup2(STDIN_FILENO, zip_fd);
  355. /* Cannot use prompt mode since zip data is arriving on STDIN */
  356. if (overwrite == O_PROMPT)
  357. overwrite = O_NEVER;
  358. } else {
  359. static const char extn[][5] = {"", ".zip", ".ZIP"};
  360. int orig_src_fn_len = strlen(src_fn);
  361. int src_fd = -1;
  362. for (i = 0; (i < 3) && (src_fd == -1); i++) {
  363. strcpy(src_fn + orig_src_fn_len, extn[i]);
  364. src_fd = open(src_fn, O_RDONLY);
  365. }
  366. if (src_fd == -1) {
  367. src_fn[orig_src_fn_len] = '\0';
  368. bb_error_msg_and_die("can't open %s, %s.zip, %s.ZIP", src_fn, src_fn, src_fn);
  369. }
  370. xmove_fd(src_fd, zip_fd);
  371. }
  372. /* Change dir if necessary */
  373. if (base_dir)
  374. xchdir(base_dir);
  375. if (quiet <= 1) { /* not -qq */
  376. if (quiet == 0)
  377. printf("Archive: %s\n", src_fn);
  378. if (listing) {
  379. puts(verbose ?
  380. " Length Method Size Ratio Date Time CRC-32 Name\n"
  381. "-------- ------ ------- ----- ---- ---- ------ ----"
  382. :
  383. " Length Date Time Name\n"
  384. " -------- ---- ---- ----"
  385. );
  386. }
  387. }
  388. total_usize = 0;
  389. total_size = 0;
  390. total_entries = 0;
  391. #if ENABLE_DESKTOP
  392. cds_entries = 0;
  393. cds_offset = 0;
  394. #endif
  395. while (1) {
  396. uint32_t magic;
  397. /* Check magic number */
  398. xread(zip_fd, &magic, 4);
  399. /* Central directory? It's at the end, so exit */
  400. if (magic == ZIP_CDS_MAGIC)
  401. break;
  402. #if ENABLE_DESKTOP
  403. /* Data descriptor? It was a streaming file, go on */
  404. if (magic == ZIP_DD_MAGIC) {
  405. /* skip over duplicate crc32, cmpsize and ucmpsize */
  406. unzip_skip(3 * 4);
  407. continue;
  408. }
  409. #endif
  410. if (magic != ZIP_FILEHEADER_MAGIC)
  411. bb_error_msg_and_die("invalid zip magic %08X", (int)magic);
  412. /* Read the file header */
  413. xread(zip_fd, zip_header.raw, ZIP_HEADER_LEN);
  414. FIX_ENDIANNESS_ZIP(zip_header);
  415. if ((zip_header.formatted.method != 0) && (zip_header.formatted.method != 8)) {
  416. bb_error_msg_and_die("unsupported method %d", zip_header.formatted.method);
  417. }
  418. #if !ENABLE_DESKTOP
  419. if (zip_header.formatted.flags & 0x0009) {
  420. bb_error_msg_and_die("zip flags 1 and 8 are not supported");
  421. }
  422. #else
  423. if (zip_header.formatted.flags & 0x0001) {
  424. /* 0x0001 - encrypted */
  425. bb_error_msg_and_die("zip flag 1 (encryption) is not supported");
  426. }
  427. if (zip_header.formatted.flags & 0x0008) {
  428. cds_header_t cds_header;
  429. /* 0x0008 - streaming. [u]cmpsize can be reliably gotten
  430. * only from Central Directory. See unzip_doc.txt */
  431. cds_offset = read_next_cds(total_entries - cds_entries, cds_offset, &cds_header);
  432. cds_entries = total_entries + 1;
  433. zip_header.formatted.crc32 = cds_header.formatted.crc32;
  434. zip_header.formatted.cmpsize = cds_header.formatted.cmpsize;
  435. zip_header.formatted.ucmpsize = cds_header.formatted.ucmpsize;
  436. }
  437. #endif
  438. /* Read filename */
  439. free(dst_fn);
  440. dst_fn = xzalloc(zip_header.formatted.filename_len + 1);
  441. xread(zip_fd, dst_fn, zip_header.formatted.filename_len);
  442. /* Skip extra header bytes */
  443. unzip_skip(zip_header.formatted.extra_len);
  444. /* Filter zip entries */
  445. if (find_list_entry(zreject, dst_fn)
  446. || (zaccept && !find_list_entry(zaccept, dst_fn))
  447. ) { /* Skip entry */
  448. i = 'n';
  449. } else { /* Extract entry */
  450. if (listing) { /* List entry */
  451. unsigned dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16);
  452. if (!verbose) {
  453. // " Length Date Time Name\n"
  454. // " -------- ---- ---- ----"
  455. printf( "%9u %02u-%02u-%02u %02u:%02u %s\n",
  456. (unsigned)zip_header.formatted.ucmpsize,
  457. (dostime & 0x01e00000) >> 21,
  458. (dostime & 0x001f0000) >> 16,
  459. (((dostime & 0xfe000000) >> 25) + 1980) % 100,
  460. (dostime & 0x0000f800) >> 11,
  461. (dostime & 0x000007e0) >> 5,
  462. dst_fn);
  463. total_usize += zip_header.formatted.ucmpsize;
  464. } else {
  465. unsigned long percents = zip_header.formatted.ucmpsize - zip_header.formatted.cmpsize;
  466. percents = percents * 100;
  467. if (zip_header.formatted.ucmpsize)
  468. percents /= zip_header.formatted.ucmpsize;
  469. // " Length Method Size Ratio Date Time CRC-32 Name\n"
  470. // "-------- ------ ------- ----- ---- ---- ------ ----"
  471. printf( "%8u Defl:N" "%9u%4u%% %02u-%02u-%02u %02u:%02u %08x %s\n",
  472. (unsigned)zip_header.formatted.ucmpsize,
  473. (unsigned)zip_header.formatted.cmpsize,
  474. (unsigned)percents,
  475. (dostime & 0x01e00000) >> 21,
  476. (dostime & 0x001f0000) >> 16,
  477. (((dostime & 0xfe000000) >> 25) + 1980) % 100,
  478. (dostime & 0x0000f800) >> 11,
  479. (dostime & 0x000007e0) >> 5,
  480. zip_header.formatted.crc32,
  481. dst_fn);
  482. total_usize += zip_header.formatted.ucmpsize;
  483. total_size += zip_header.formatted.cmpsize;
  484. }
  485. i = 'n';
  486. } else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */
  487. i = -1;
  488. } else if (last_char_is(dst_fn, '/')) { /* Extract directory */
  489. if (stat(dst_fn, &stat_buf) == -1) {
  490. if (errno != ENOENT) {
  491. bb_perror_msg_and_die("can't stat '%s'", dst_fn);
  492. }
  493. if (!quiet) {
  494. printf(" creating: %s\n", dst_fn);
  495. }
  496. unzip_create_leading_dirs(dst_fn);
  497. if (bb_make_directory(dst_fn, 0777, 0)) {
  498. bb_error_msg_and_die("exiting");
  499. }
  500. } else {
  501. if (!S_ISDIR(stat_buf.st_mode)) {
  502. bb_error_msg_and_die("'%s' exists but is not directory", dst_fn);
  503. }
  504. }
  505. i = 'n';
  506. } else { /* Extract file */
  507. check_file:
  508. if (stat(dst_fn, &stat_buf) == -1) { /* File does not exist */
  509. if (errno != ENOENT) {
  510. bb_perror_msg_and_die("can't stat '%s'", dst_fn);
  511. }
  512. i = 'y';
  513. } else { /* File already exists */
  514. if (overwrite == O_NEVER) {
  515. i = 'n';
  516. } else if (S_ISREG(stat_buf.st_mode)) { /* File is regular file */
  517. if (overwrite == O_ALWAYS) {
  518. i = 'y';
  519. } else {
  520. printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
  521. if (!fgets(key_buf, sizeof(key_buf), stdin)) {
  522. bb_perror_msg_and_die("can't read input");
  523. }
  524. i = key_buf[0];
  525. }
  526. } else { /* File is not regular file */
  527. bb_error_msg_and_die("'%s' exists but is not regular file", dst_fn);
  528. }
  529. }
  530. }
  531. }
  532. switch (i) {
  533. case 'A':
  534. overwrite = O_ALWAYS;
  535. case 'y': /* Open file and fall into unzip */
  536. unzip_create_leading_dirs(dst_fn);
  537. dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC);
  538. case -1: /* Unzip */
  539. if (!quiet) {
  540. printf(" inflating: %s\n", dst_fn);
  541. }
  542. unzip_extract(&zip_header, dst_fd);
  543. if (dst_fd != STDOUT_FILENO) {
  544. /* closing STDOUT is potentially bad for future business */
  545. close(dst_fd);
  546. }
  547. break;
  548. case 'N':
  549. overwrite = O_NEVER;
  550. case 'n':
  551. /* Skip entry data */
  552. unzip_skip(zip_header.formatted.cmpsize);
  553. break;
  554. case 'r':
  555. /* Prompt for new name */
  556. printf("new name: ");
  557. if (!fgets(key_buf, sizeof(key_buf), stdin)) {
  558. bb_perror_msg_and_die("can't read input");
  559. }
  560. free(dst_fn);
  561. dst_fn = xstrdup(key_buf);
  562. chomp(dst_fn);
  563. goto check_file;
  564. default:
  565. printf("error: invalid response [%c]\n", (char)i);
  566. goto check_file;
  567. }
  568. total_entries++;
  569. }
  570. if (listing && quiet <= 1) {
  571. if (!verbose) {
  572. // " Length Date Time Name\n"
  573. // " -------- ---- ---- ----"
  574. printf( " -------- -------\n"
  575. "%9lu" " %u files\n",
  576. total_usize, total_entries);
  577. } else {
  578. unsigned long percents = total_usize - total_size;
  579. percents = percents * 100;
  580. if (total_usize)
  581. percents /= total_usize;
  582. // " Length Method Size Ratio Date Time CRC-32 Name\n"
  583. // "-------- ------ ------- ----- ---- ---- ------ ----"
  584. printf( "-------- ------- --- -------\n"
  585. "%8lu" "%17lu%4u%% %u files\n",
  586. total_usize, total_size, (unsigned)percents,
  587. total_entries);
  588. }
  589. }
  590. return 0;
  591. }