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