blob: 1c9bc246c71accea6652db50fcc794c80c7df53e [file] [log] [blame]
Glenn L McGrath87ac7022002-01-02 13:52:26 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini unzip implementation for busybox
4 *
Paul Fox0840b762005-07-20 20:26:49 +00005 * 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.
Glenn L McGrath87ac7022002-01-02 13:52:26 +00009 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000010 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath87ac7022002-01-02 13:52:26 +000011 */
12
Glenn L McGrathf34b0e92004-06-06 10:22:43 +000013/* For reference see
Paul Fox0840b762005-07-20 20:26:49 +000014 * http://www.pkware.com/company/standards/appnote/
Glenn L McGrathf34b0e92004-06-06 10:22:43 +000015 * http://www.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip
16 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000017
Paul Fox0840b762005-07-20 20:26:49 +000018/* TODO
19 * Endian issues
20 * Zip64 + other methods
21 * Improve handling of zip format, ie.
22 * - deferred CRC, comp. & uncomp. lengths (zip header flags bit 3)
23 * - unix file permissions, etc.
24 * - central directory
25 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000026
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000027#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000028#include "unarchive.h"
Glenn L McGrath87ac7022002-01-02 13:52:26 +000029
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000030enum {
31#if BB_BIG_ENDIAN
32 ZIP_FILEHEADER_MAGIC = 0x504b0304,
33 ZIP_CDS_MAGIC = 0x504b0102,
34 ZIP_CDS_END_MAGIC = 0x504b0506,
35 ZIP_DD_MAGIC = 0x504b0708,
36#else
37 ZIP_FILEHEADER_MAGIC = 0x04034b50,
38 ZIP_CDS_MAGIC = 0x02014b50,
39 ZIP_CDS_END_MAGIC = 0x06054b50,
40 ZIP_DD_MAGIC = 0x08074b50,
41#endif
42};
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000043
Paul Foxcb981632007-11-05 23:09:03 +000044#define ZIP_HEADER_LEN 26
45
Paul Fox0840b762005-07-20 20:26:49 +000046typedef union {
Paul Foxcb981632007-11-05 23:09:03 +000047 uint8_t raw[ZIP_HEADER_LEN];
Paul Fox0840b762005-07-20 20:26:49 +000048 struct {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000049 uint16_t version; /* 0-1 */
50 uint16_t flags; /* 2-3 */
51 uint16_t method; /* 4-5 */
52 uint16_t modtime; /* 6-7 */
53 uint16_t moddate; /* 8-9 */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000054 uint32_t crc32 PACKED; /* 10-13 */
55 uint32_t cmpsize PACKED; /* 14-17 */
56 uint32_t ucmpsize PACKED; /* 18-21 */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000057 uint16_t filename_len; /* 22-23 */
58 uint16_t extra_len; /* 24-25 */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000059 } formatted PACKED;
60} zip_header_t; /* PACKED - gcc 4.2.1 doesn't like it (spews warning) */
Paul Fox0840b762005-07-20 20:26:49 +000061
Paul Foxcb981632007-11-05 23:09:03 +000062/* Check the offset of the last element, not the length. This leniency
63 * allows for poor packing, whereby the overall struct may be too long,
64 * even though the elements are all in the right place.
65 */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000066struct BUG_zip_header_must_be_26_bytes {
Paul Foxcb981632007-11-05 23:09:03 +000067 char BUG_zip_header_must_be_26_bytes[
68 offsetof(zip_header_t, formatted.extra_len) + 2 ==
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000069 ZIP_HEADER_LEN ? 1 : -1];
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000070};
71
72#define FIX_ENDIANNESS(zip_header) do { \
73 (zip_header).formatted.version = SWAP_LE16((zip_header).formatted.version ); \
74 (zip_header).formatted.flags = SWAP_LE16((zip_header).formatted.flags ); \
75 (zip_header).formatted.method = SWAP_LE16((zip_header).formatted.method ); \
76 (zip_header).formatted.modtime = SWAP_LE16((zip_header).formatted.modtime ); \
77 (zip_header).formatted.moddate = SWAP_LE16((zip_header).formatted.moddate ); \
78 (zip_header).formatted.crc32 = SWAP_LE32((zip_header).formatted.crc32 ); \
79 (zip_header).formatted.cmpsize = SWAP_LE32((zip_header).formatted.cmpsize ); \
80 (zip_header).formatted.ucmpsize = SWAP_LE32((zip_header).formatted.ucmpsize ); \
81 (zip_header).formatted.filename_len = SWAP_LE16((zip_header).formatted.filename_len); \
82 (zip_header).formatted.extra_len = SWAP_LE16((zip_header).formatted.extra_len ); \
83} while (0)
84
Paul Fox0840b762005-07-20 20:26:49 +000085static void unzip_skip(int fd, off_t skip)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000086{
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000087 bb_copyfd_exact_size(fd, -1, skip);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000088}
89
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000090static void unzip_create_leading_dirs(const char *fn)
Paul Fox0840b762005-07-20 20:26:49 +000091{
92 /* Create all leading directories */
Rob Landleyd921b2e2006-08-03 15:41:12 +000093 char *name = xstrdup(fn);
Paul Fox0840b762005-07-20 20:26:49 +000094 if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) {
Denis Vlasenko13858992006-10-08 12:49:22 +000095 bb_error_msg_and_die("exiting"); /* bb_make_directory is noisy */
Paul Fox0840b762005-07-20 20:26:49 +000096 }
97 free(name);
98}
99
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000100static void unzip_extract(zip_header_t *zip_header, int src_fd, int dst_fd)
Paul Fox0840b762005-07-20 20:26:49 +0000101{
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000102 if (zip_header->formatted.method == 0) {
Paul Fox0840b762005-07-20 20:26:49 +0000103 /* Method 0 - stored (not compressed) */
Denis Vlasenko7039a662006-10-08 17:54:47 +0000104 off_t size = zip_header->formatted.ucmpsize;
Denis Vlasenko714701c2006-12-22 00:21:07 +0000105 if (size)
106 bb_copyfd_exact_size(src_fd, dst_fd, size);
Paul Fox0840b762005-07-20 20:26:49 +0000107 } else {
108 /* Method 8 - inflate */
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000109 inflate_unzip_result res;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000110 if (inflate_unzip(&res, zip_header->formatted.cmpsize, src_fd, dst_fd) < 0)
111 bb_error_msg_and_die("inflate error");
Paul Fox0840b762005-07-20 20:26:49 +0000112 /* Validate decompression - crc */
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000113 if (zip_header->formatted.crc32 != (res.crc ^ 0xffffffffL)) {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000114 bb_error_msg_and_die("crc error");
Paul Fox0840b762005-07-20 20:26:49 +0000115 }
116 /* Validate decompression - size */
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000117 if (zip_header->formatted.ucmpsize != res.bytes_out) {
Denis Vlasenkofcc56962007-10-19 21:03:09 +0000118 /* Don't die. Who knows, maybe len calculation
119 * was botched somewhere. After all, crc matched! */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000120 bb_error_msg("bad length");
Paul Fox0840b762005-07-20 20:26:49 +0000121 }
122 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000123}
124
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000125int unzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000126int unzip_main(int argc, char **argv)
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000127{
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000128 enum { O_PROMPT, O_NEVER, O_ALWAYS };
129
Paul Fox0840b762005-07-20 20:26:49 +0000130 zip_header_t zip_header;
Paul Fox9382b382007-09-07 20:28:25 +0000131 smallint verbose = 1;
132 smallint listing = 0;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000133 smallint overwrite = O_PROMPT;
134 unsigned total_size;
135 unsigned total_entries;
136 int src_fd = -1;
137 int dst_fd = -1;
138 char *src_fn = NULL;
139 char *dst_fn = NULL;
Mike Frysinger69024552005-07-30 07:30:26 +0000140 llist_t *zaccept = NULL;
141 llist_t *zreject = NULL;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000142 char *base_dir = NULL;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000143 int i, opt;
144 int opt_range = 0;
145 char key_buf[80];
Paul Fox0840b762005-07-20 20:26:49 +0000146 struct stat stat_buf;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000147
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000148 /* '-' makes getopt return 1 for non-options */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000149 while ((opt = getopt(argc, argv, "-d:lnopqx")) != -1) {
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000150 switch (opt_range) {
Paul Fox0840b762005-07-20 20:26:49 +0000151 case 0: /* Options */
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000152 switch (opt) {
Paul Fox0840b762005-07-20 20:26:49 +0000153 case 'l': /* List */
Paul Fox9382b382007-09-07 20:28:25 +0000154 listing = 1;
Paul Fox0840b762005-07-20 20:26:49 +0000155 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000156
Paul Fox0840b762005-07-20 20:26:49 +0000157 case 'n': /* Never overwrite existing files */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000158 overwrite = O_NEVER;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000159 break;
Paul Fox0840b762005-07-20 20:26:49 +0000160
161 case 'o': /* Always overwrite existing files */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000162 overwrite = O_ALWAYS;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000163 break;
Paul Fox0840b762005-07-20 20:26:49 +0000164
165 case 'p': /* Extract files to stdout and fall through to set verbosity */
166 dst_fd = STDOUT_FILENO;
167
168 case 'q': /* Be quiet */
Paul Fox9382b382007-09-07 20:28:25 +0000169 verbose = 0;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000170 break;
Paul Fox0840b762005-07-20 20:26:49 +0000171
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000172 case 1: /* The zip file */
173 /* +5: space for ".zip" and NUL */
174 src_fn = xmalloc(strlen(optarg) + 5);
Denis Vlasenko666c40c2007-03-31 10:17:24 +0000175 strcpy(src_fn, optarg);
Paul Fox0840b762005-07-20 20:26:49 +0000176 opt_range++;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000177 break;
Paul Fox0840b762005-07-20 20:26:49 +0000178
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000179 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180 bb_show_usage();
Paul Fox0840b762005-07-20 20:26:49 +0000181
182 }
183 break;
184
185 case 1: /* Include files */
186 if (opt == 1) {
Rob Landley8bb50782006-05-26 23:44:51 +0000187 llist_add_to(&zaccept, optarg);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000188 break;
189 }
190 if (opt == 'd') {
Paul Fox0840b762005-07-20 20:26:49 +0000191 base_dir = optarg;
192 opt_range += 2;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000193 break;
Paul Fox0840b762005-07-20 20:26:49 +0000194 }
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000195 if (opt == 'x') {
196 opt_range++;
197 break;
198 }
199 bb_show_usage();
Paul Fox0840b762005-07-20 20:26:49 +0000200
201 case 2 : /* Exclude files */
202 if (opt == 1) {
Rob Landley8bb50782006-05-26 23:44:51 +0000203 llist_add_to(&zreject, optarg);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000204 break;
205 }
206 if (opt == 'd') { /* Extract to base directory */
Paul Fox0840b762005-07-20 20:26:49 +0000207 base_dir = optarg;
208 opt_range++;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000209 break;
Paul Fox0840b762005-07-20 20:26:49 +0000210 }
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000211 /* fall through */
Paul Fox0840b762005-07-20 20:26:49 +0000212
213 default:
214 bb_show_usage();
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000215 }
216 }
217
Paul Fox0840b762005-07-20 20:26:49 +0000218 if (src_fn == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000219 bb_show_usage();
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000220 }
221
Paul Fox0840b762005-07-20 20:26:49 +0000222 /* Open input file */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000223 if (LONE_DASH(src_fn)) {
Paul Fox0840b762005-07-20 20:26:49 +0000224 src_fd = STDIN_FILENO;
225 /* Cannot use prompt mode since zip data is arriving on STDIN */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000226 if (overwrite == O_PROMPT)
227 overwrite = O_NEVER;
Glenn L McGrath237ae422002-11-03 14:05:15 +0000228 } else {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000229 static const char extn[][5] = {"", ".zip", ".ZIP"};
Paul Fox0840b762005-07-20 20:26:49 +0000230 int orig_src_fn_len = strlen(src_fn);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000231
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000232 for (i = 0; (i < 3) && (src_fd == -1); i++) {
Paul Fox0840b762005-07-20 20:26:49 +0000233 strcpy(src_fn + orig_src_fn_len, extn[i]);
234 src_fd = open(src_fn, O_RDONLY);
235 }
236 if (src_fd == -1) {
Denis Vlasenko666c40c2007-03-31 10:17:24 +0000237 src_fn[orig_src_fn_len] = '\0';
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000238 bb_error_msg_and_die("can't open %s, %s.zip, %s.ZIP", src_fn, src_fn, src_fn);
Paul Fox0840b762005-07-20 20:26:49 +0000239 }
Glenn L McGrath237ae422002-11-03 14:05:15 +0000240 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000241
Paul Fox0840b762005-07-20 20:26:49 +0000242 /* Change dir if necessary */
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +0000243 if (base_dir)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000244 xchdir(base_dir);
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000245
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000246 if (verbose) {
Paul Fox0840b762005-07-20 20:26:49 +0000247 printf("Archive: %s\n", src_fn);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000248 if (listing){
249 puts(" Length Date Time Name\n"
250 " -------- ---- ---- ----");
251 }
252 }
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000253
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000254 total_size = 0;
255 total_entries = 0;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000256 while (1) {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000257 uint32_t magic;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000258
Paul Fox0840b762005-07-20 20:26:49 +0000259 /* Check magic number */
Rob Landley53437472006-07-16 08:14:35 +0000260 xread(src_fd, &magic, 4);
Denis Vlasenko48a99712008-07-26 17:32:41 +0000261 /* Central directory? It's at the end, so exit */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000262 if (magic == ZIP_CDS_MAGIC)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000263 break;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000264 if (magic != ZIP_FILEHEADER_MAGIC)
Denis Vlasenko48a99712008-07-26 17:32:41 +0000265 bb_error_msg_and_die("invalid zip magic %08X", (int)magic);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000266
267 /* Read the file header */
Paul Foxcb981632007-11-05 23:09:03 +0000268 xread(src_fd, zip_header.raw, ZIP_HEADER_LEN);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000269 FIX_ENDIANNESS(zip_header);
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000270 if ((zip_header.formatted.method != 0) && (zip_header.formatted.method != 8)) {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000271 bb_error_msg_and_die("unsupported method %d", zip_header.formatted.method);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000272 }
Denis Vlasenko48a99712008-07-26 17:32:41 +0000273 if (zip_header.formatted.flags & (0x0008|0x0001)) {
274 /* 0x0001 - encrypted */
275 /* 0x0008 - streaming. [u]cmpsize can be reliably gotten
276 * only from Central Directory. See unzip_doc.txt */
277 bb_error_msg_and_die("zip flags 8 and 1 are not supported");
278 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000279
280 /* Read filename */
Paul Fox0840b762005-07-20 20:26:49 +0000281 free(dst_fn);
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000282 dst_fn = xzalloc(zip_header.formatted.filename_len + 1);
283 xread(src_fd, dst_fn, zip_header.formatted.filename_len);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000284
Paul Fox0840b762005-07-20 20:26:49 +0000285 /* Skip extra header bytes */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000286 unzip_skip(src_fd, zip_header.formatted.extra_len);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000287
Paul Fox0840b762005-07-20 20:26:49 +0000288 /* Filter zip entries */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000289 if (find_list_entry(zreject, dst_fn)
290 || (zaccept && !find_list_entry(zaccept, dst_fn))
291 ) { /* Skip entry */
Paul Fox0840b762005-07-20 20:26:49 +0000292 i = 'n';
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000293
Paul Fox0840b762005-07-20 20:26:49 +0000294 } else { /* Extract entry */
Paul Fox9382b382007-09-07 20:28:25 +0000295 if (listing) { /* List entry */
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000296 if (verbose) {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000297 unsigned dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16);
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000298 printf("%9u %02u-%02u-%02u %02u:%02u %s\n",
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000299 zip_header.formatted.ucmpsize,
Paul Fox0840b762005-07-20 20:26:49 +0000300 (dostime & 0x01e00000) >> 21,
301 (dostime & 0x001f0000) >> 16,
302 (((dostime & 0xfe000000) >> 25) + 1980) % 100,
303 (dostime & 0x0000f800) >> 11,
304 (dostime & 0x000007e0) >> 5,
305 dst_fn);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000306 total_size += zip_header.formatted.ucmpsize;
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000307 total_entries++;
308 } else {
309 /* short listing -- filenames only */
310 puts(dst_fn);
311 }
312 i = 'n';
Paul Fox0840b762005-07-20 20:26:49 +0000313 } else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */
314 i = -1;
Paul Fox0840b762005-07-20 20:26:49 +0000315 } else if (last_char_is(dst_fn, '/')) { /* Extract directory */
316 if (stat(dst_fn, &stat_buf) == -1) {
317 if (errno != ENOENT) {
Denis Vlasenkof5d8c902008-06-26 14:32:57 +0000318 bb_perror_msg_and_die("cannot stat '%s'", dst_fn);
Paul Fox0840b762005-07-20 20:26:49 +0000319 }
Paul Fox9382b382007-09-07 20:28:25 +0000320 if (verbose) {
Paul Fox0840b762005-07-20 20:26:49 +0000321 printf(" creating: %s\n", dst_fn);
322 }
323 unzip_create_leading_dirs(dst_fn);
324 if (bb_make_directory(dst_fn, 0777, 0)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000325 bb_error_msg_and_die("exiting");
Paul Fox0840b762005-07-20 20:26:49 +0000326 }
327 } else {
328 if (!S_ISDIR(stat_buf.st_mode)) {
329 bb_error_msg_and_die("'%s' exists but is not directory", dst_fn);
330 }
331 }
332 i = 'n';
333
334 } else { /* Extract file */
Denis Vlasenko48a99712008-07-26 17:32:41 +0000335 check_file:
Paul Fox0840b762005-07-20 20:26:49 +0000336 if (stat(dst_fn, &stat_buf) == -1) { /* File does not exist */
337 if (errno != ENOENT) {
Denis Vlasenkof5d8c902008-06-26 14:32:57 +0000338 bb_perror_msg_and_die("cannot stat '%s'", dst_fn);
Paul Fox0840b762005-07-20 20:26:49 +0000339 }
340 i = 'y';
Paul Fox0840b762005-07-20 20:26:49 +0000341 } else { /* File already exists */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000342 if (overwrite == O_NEVER) {
Paul Fox0840b762005-07-20 20:26:49 +0000343 i = 'n';
Paul Fox0840b762005-07-20 20:26:49 +0000344 } else if (S_ISREG(stat_buf.st_mode)) { /* File is regular file */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000345 if (overwrite == O_ALWAYS) {
Paul Fox0840b762005-07-20 20:26:49 +0000346 i = 'y';
347 } else {
348 printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000349 if (!fgets(key_buf, sizeof(key_buf), stdin)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000350 bb_perror_msg_and_die("cannot read input");
Paul Fox0840b762005-07-20 20:26:49 +0000351 }
352 i = key_buf[0];
353 }
Paul Fox0840b762005-07-20 20:26:49 +0000354 } else { /* File is not regular file */
Denis Vlasenkof5d8c902008-06-26 14:32:57 +0000355 bb_error_msg_and_die("'%s' exists but is not regular file", dst_fn);
Paul Fox0840b762005-07-20 20:26:49 +0000356 }
357 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000358 }
359 }
360
Paul Fox0840b762005-07-20 20:26:49 +0000361 switch (i) {
362 case 'A':
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000363 overwrite = O_ALWAYS;
Paul Fox0840b762005-07-20 20:26:49 +0000364 case 'y': /* Open file and fall into unzip */
365 unzip_create_leading_dirs(dst_fn);
Bernhard Reutner-Fischer64d7e932006-09-11 16:01:40 +0000366 dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC);
Paul Fox0840b762005-07-20 20:26:49 +0000367 case -1: /* Unzip */
Paul Fox9382b382007-09-07 20:28:25 +0000368 if (verbose) {
Paul Fox0840b762005-07-20 20:26:49 +0000369 printf(" inflating: %s\n", dst_fn);
370 }
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000371 unzip_extract(&zip_header, src_fd, dst_fd);
Paul Fox0840b762005-07-20 20:26:49 +0000372 if (dst_fd != STDOUT_FILENO) {
373 /* closing STDOUT is potentially bad for future business */
374 close(dst_fd);
375 }
376 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000377
Paul Fox0840b762005-07-20 20:26:49 +0000378 case 'N':
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000379 overwrite = O_NEVER;
Paul Fox0840b762005-07-20 20:26:49 +0000380 case 'n':
381 /* Skip entry data */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000382 unzip_skip(src_fd, zip_header.formatted.cmpsize);
Paul Fox0840b762005-07-20 20:26:49 +0000383 break;
384
385 case 'r':
386 /* Prompt for new name */
387 printf("new name: ");
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000388 if (!fgets(key_buf, sizeof(key_buf), stdin)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000389 bb_perror_msg_and_die("cannot read input");
Paul Fox0840b762005-07-20 20:26:49 +0000390 }
391 free(dst_fn);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000392 dst_fn = xstrdup(key_buf);
Paul Fox0840b762005-07-20 20:26:49 +0000393 chomp(dst_fn);
Denis Vlasenko48a99712008-07-26 17:32:41 +0000394 goto check_file;
Paul Fox0840b762005-07-20 20:26:49 +0000395
396 default:
Rob Landleye7c43b62006-03-01 16:39:45 +0000397 printf("error: invalid response [%c]\n",(char)i);
Denis Vlasenko48a99712008-07-26 17:32:41 +0000398 goto check_file;
Paul Fox0840b762005-07-20 20:26:49 +0000399 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000400
Denis Vlasenko48a99712008-07-26 17:32:41 +0000401// Looks like bug (data descriptor cannot be identified this way)
402// /* Data descriptor section */
403// if (zip_header.formatted.flags & 4) {
404// /* skip over duplicate crc, compressed size and uncompressed size */
405// unzip_skip(src_fd, 12);
406// }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000407 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000408
Paul Fox9382b382007-09-07 20:28:25 +0000409 if (listing && verbose) {
Rob Landleye7c43b62006-03-01 16:39:45 +0000410 printf(" -------- -------\n"
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000411 "%9d %d files\n",
412 total_size, total_entries);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000413 }
414
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000415 return 0;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000416}