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 |
| 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 McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 26 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 27 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 28 | #include "unarchive.h" |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 29 | |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 30 | enum { |
| 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 McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 43 | |
Paul Fox | cb98163 | 2007-11-05 23:09:03 +0000 | [diff] [blame] | 44 | #define ZIP_HEADER_LEN 26 |
| 45 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 46 | typedef union { |
Paul Fox | cb98163 | 2007-11-05 23:09:03 +0000 | [diff] [blame] | 47 | uint8_t raw[ZIP_HEADER_LEN]; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 48 | struct { |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 49 | 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 */ |
| 54 | uint32_t crc32 ATTRIBUTE_PACKED; /* 10-13 */ |
| 55 | uint32_t cmpsize ATTRIBUTE_PACKED; /* 14-17 */ |
| 56 | uint32_t ucmpsize ATTRIBUTE_PACKED; /* 18-21 */ |
| 57 | uint16_t filename_len; /* 22-23 */ |
| 58 | uint16_t extra_len; /* 24-25 */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 59 | } formatted ATTRIBUTE_PACKED; |
Denis Vlasenko | cd0fbb5 | 2007-11-06 02:16:01 +0000 | [diff] [blame] | 60 | } zip_header_t; /* ATTRIBUTE_PACKED - gcc 4.2.1 doesn't like it (spews warning) */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 61 | |
Paul Fox | cb98163 | 2007-11-05 23:09:03 +0000 | [diff] [blame] | 62 | /* 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 Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 66 | struct BUG_zip_header_must_be_26_bytes { |
Paul Fox | cb98163 | 2007-11-05 23:09:03 +0000 | [diff] [blame] | 67 | char BUG_zip_header_must_be_26_bytes[ |
| 68 | offsetof(zip_header_t, formatted.extra_len) + 2 == |
| 69 | ZIP_HEADER_LEN ? 1 : -1]; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 70 | }; |
| 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 Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 85 | static void unzip_skip(int fd, off_t skip) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 86 | { |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 87 | bb_copyfd_exact_size(fd, -1, skip); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 90 | static void unzip_create_leading_dirs(const char *fn) |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 91 | { |
| 92 | /* Create all leading directories */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 93 | char *name = xstrdup(fn); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 94 | if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 95 | bb_error_msg_and_die("exiting"); /* bb_make_directory is noisy */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 96 | } |
| 97 | free(name); |
| 98 | } |
| 99 | |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 100 | static void unzip_extract(zip_header_t *zip_header, int src_fd, int dst_fd) |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 101 | { |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 102 | if (zip_header->formatted.method == 0) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 103 | /* Method 0 - stored (not compressed) */ |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 104 | off_t size = zip_header->formatted.ucmpsize; |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 105 | if (size) |
| 106 | bb_copyfd_exact_size(src_fd, dst_fd, size); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 107 | } else { |
| 108 | /* Method 8 - inflate */ |
Denis Vlasenko | cd42cb8 | 2007-01-05 23:56:53 +0000 | [diff] [blame] | 109 | inflate_unzip_result res; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 110 | if (inflate_unzip(&res, zip_header->formatted.cmpsize, src_fd, dst_fd) < 0) |
| 111 | bb_error_msg_and_die("inflate error"); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 112 | /* Validate decompression - crc */ |
Denis Vlasenko | cd42cb8 | 2007-01-05 23:56:53 +0000 | [diff] [blame] | 113 | if (zip_header->formatted.crc32 != (res.crc ^ 0xffffffffL)) { |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 114 | bb_error_msg_and_die("crc error"); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 115 | } |
| 116 | /* Validate decompression - size */ |
Denis Vlasenko | cd42cb8 | 2007-01-05 23:56:53 +0000 | [diff] [blame] | 117 | if (zip_header->formatted.ucmpsize != res.bytes_out) { |
Denis Vlasenko | fcc5696 | 2007-10-19 21:03:09 +0000 | [diff] [blame] | 118 | /* Don't die. Who knows, maybe len calculation |
| 119 | * was botched somewhere. After all, crc matched! */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 120 | bb_error_msg("bad length"); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 125 | int unzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 126 | int unzip_main(int argc, char **argv) |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 127 | { |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 128 | enum { O_PROMPT, O_NEVER, O_ALWAYS }; |
| 129 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 130 | zip_header_t zip_header; |
Paul Fox | 9382b38 | 2007-09-07 20:28:25 +0000 | [diff] [blame] | 131 | smallint verbose = 1; |
| 132 | smallint listing = 0; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 133 | 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 Frysinger | 6902455 | 2005-07-30 07:30:26 +0000 | [diff] [blame] | 140 | llist_t *zaccept = NULL; |
| 141 | llist_t *zreject = NULL; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 142 | char *base_dir = NULL; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 143 | int i, opt; |
| 144 | int opt_range = 0; |
| 145 | char key_buf[80]; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 146 | struct stat stat_buf; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 147 | |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 148 | /* '-' makes getopt return 1 for non-options */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 149 | while ((opt = getopt(argc, argv, "-d:lnopqx")) != -1) { |
Denis Vlasenko | c16bd21 | 2006-09-27 19:51:06 +0000 | [diff] [blame] | 150 | switch (opt_range) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 151 | case 0: /* Options */ |
Denis Vlasenko | c16bd21 | 2006-09-27 19:51:06 +0000 | [diff] [blame] | 152 | switch (opt) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 153 | case 'l': /* List */ |
Paul Fox | 9382b38 | 2007-09-07 20:28:25 +0000 | [diff] [blame] | 154 | listing = 1; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 155 | break; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 156 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 157 | case 'n': /* Never overwrite existing files */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 158 | overwrite = O_NEVER; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 159 | break; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 160 | |
| 161 | case 'o': /* Always overwrite existing files */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 162 | overwrite = O_ALWAYS; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 163 | break; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 164 | |
| 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 Fox | 9382b38 | 2007-09-07 20:28:25 +0000 | [diff] [blame] | 169 | verbose = 0; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 170 | break; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 171 | |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 172 | case 1: /* The zip file */ |
| 173 | /* +5: space for ".zip" and NUL */ |
| 174 | src_fn = xmalloc(strlen(optarg) + 5); |
Denis Vlasenko | 666c40c | 2007-03-31 10:17:24 +0000 | [diff] [blame] | 175 | strcpy(src_fn, optarg); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 176 | opt_range++; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 177 | break; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 178 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 179 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 180 | bb_show_usage(); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 181 | |
| 182 | } |
| 183 | break; |
| 184 | |
| 185 | case 1: /* Include files */ |
| 186 | if (opt == 1) { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 187 | llist_add_to(&zaccept, optarg); |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | if (opt == 'd') { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 191 | base_dir = optarg; |
| 192 | opt_range += 2; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 193 | break; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 194 | } |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 195 | if (opt == 'x') { |
| 196 | opt_range++; |
| 197 | break; |
| 198 | } |
| 199 | bb_show_usage(); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 200 | |
| 201 | case 2 : /* Exclude files */ |
| 202 | if (opt == 1) { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 203 | llist_add_to(&zreject, optarg); |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | if (opt == 'd') { /* Extract to base directory */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 207 | base_dir = optarg; |
| 208 | opt_range++; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 209 | break; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 210 | } |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 211 | /* fall through */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 212 | |
| 213 | default: |
| 214 | bb_show_usage(); |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 218 | if (src_fn == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 219 | bb_show_usage(); |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 222 | /* Open input file */ |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 223 | if (LONE_DASH(src_fn)) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 224 | src_fd = STDIN_FILENO; |
| 225 | /* Cannot use prompt mode since zip data is arriving on STDIN */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 226 | if (overwrite == O_PROMPT) |
| 227 | overwrite = O_NEVER; |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 228 | } else { |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 229 | static const char extn[][5] = {"", ".zip", ".ZIP"}; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 230 | int orig_src_fn_len = strlen(src_fn); |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 231 | |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 232 | for (i = 0; (i < 3) && (src_fd == -1); i++) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 233 | strcpy(src_fn + orig_src_fn_len, extn[i]); |
| 234 | src_fd = open(src_fn, O_RDONLY); |
| 235 | } |
| 236 | if (src_fd == -1) { |
Denis Vlasenko | 666c40c | 2007-03-31 10:17:24 +0000 | [diff] [blame] | 237 | src_fn[orig_src_fn_len] = '\0'; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 238 | 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] | 239 | } |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 240 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 241 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 242 | /* Change dir if necessary */ |
Bernhard Reutner-Fischer | d9cf7ac | 2006-04-12 18:39:58 +0000 | [diff] [blame] | 243 | if (base_dir) |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 244 | xchdir(base_dir); |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 245 | |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 246 | if (verbose) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 247 | printf("Archive: %s\n", src_fn); |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 248 | if (listing){ |
| 249 | puts(" Length Date Time Name\n" |
| 250 | " -------- ---- ---- ----"); |
| 251 | } |
| 252 | } |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 253 | |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 254 | total_size = 0; |
| 255 | total_entries = 0; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 256 | while (1) { |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 257 | uint32_t magic; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 258 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 259 | /* Check magic number */ |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 260 | xread(src_fd, &magic, 4); |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 261 | if (magic == ZIP_CDS_MAGIC) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 262 | break; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 263 | if (magic != ZIP_FILEHEADER_MAGIC) |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 264 | bb_error_msg_and_die("invalid zip magic %08X", magic); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 265 | |
| 266 | /* Read the file header */ |
Paul Fox | cb98163 | 2007-11-05 23:09:03 +0000 | [diff] [blame] | 267 | xread(src_fd, zip_header.raw, ZIP_HEADER_LEN); |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 268 | FIX_ENDIANNESS(zip_header); |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 269 | if ((zip_header.formatted.method != 0) && (zip_header.formatted.method != 8)) { |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 270 | 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] | 271 | } |
| 272 | |
| 273 | /* Read filename */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 274 | free(dst_fn); |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 275 | dst_fn = xzalloc(zip_header.formatted.filename_len + 1); |
| 276 | xread(src_fd, dst_fn, zip_header.formatted.filename_len); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 277 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 278 | /* Skip extra header bytes */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 279 | unzip_skip(src_fd, zip_header.formatted.extra_len); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 280 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 281 | /* Filter zip entries */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 282 | if (find_list_entry(zreject, dst_fn) |
| 283 | || (zaccept && !find_list_entry(zaccept, dst_fn)) |
| 284 | ) { /* Skip entry */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 285 | i = 'n'; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 286 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 287 | } else { /* Extract entry */ |
Paul Fox | 9382b38 | 2007-09-07 20:28:25 +0000 | [diff] [blame] | 288 | if (listing) { /* List entry */ |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 289 | if (verbose) { |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 290 | unsigned dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16); |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 291 | printf("%9u %02u-%02u-%02u %02u:%02u %s\n", |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 292 | zip_header.formatted.ucmpsize, |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 293 | (dostime & 0x01e00000) >> 21, |
| 294 | (dostime & 0x001f0000) >> 16, |
| 295 | (((dostime & 0xfe000000) >> 25) + 1980) % 100, |
| 296 | (dostime & 0x0000f800) >> 11, |
| 297 | (dostime & 0x000007e0) >> 5, |
| 298 | dst_fn); |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 299 | total_size += zip_header.formatted.ucmpsize; |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 300 | total_entries++; |
| 301 | } else { |
| 302 | /* short listing -- filenames only */ |
| 303 | puts(dst_fn); |
| 304 | } |
| 305 | i = 'n'; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 306 | } else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */ |
| 307 | i = -1; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 308 | } else if (last_char_is(dst_fn, '/')) { /* Extract directory */ |
| 309 | if (stat(dst_fn, &stat_buf) == -1) { |
| 310 | if (errno != ENOENT) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 311 | bb_perror_msg_and_die("cannot stat '%s'",dst_fn); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 312 | } |
Paul Fox | 9382b38 | 2007-09-07 20:28:25 +0000 | [diff] [blame] | 313 | if (verbose) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 314 | printf(" creating: %s\n", dst_fn); |
| 315 | } |
| 316 | unzip_create_leading_dirs(dst_fn); |
| 317 | if (bb_make_directory(dst_fn, 0777, 0)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 318 | bb_error_msg_and_die("exiting"); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 319 | } |
| 320 | } else { |
| 321 | if (!S_ISDIR(stat_buf.st_mode)) { |
| 322 | bb_error_msg_and_die("'%s' exists but is not directory", dst_fn); |
| 323 | } |
| 324 | } |
| 325 | i = 'n'; |
| 326 | |
| 327 | } else { /* Extract file */ |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 328 | _check_file: |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 329 | if (stat(dst_fn, &stat_buf) == -1) { /* File does not exist */ |
| 330 | if (errno != ENOENT) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 331 | bb_perror_msg_and_die("cannot stat '%s'",dst_fn); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 332 | } |
| 333 | i = 'y'; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 334 | } else { /* File already exists */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 335 | if (overwrite == O_NEVER) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 336 | i = 'n'; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 337 | } else if (S_ISREG(stat_buf.st_mode)) { /* File is regular file */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 338 | if (overwrite == O_ALWAYS) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 339 | i = 'y'; |
| 340 | } else { |
| 341 | 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] | 342 | if (!fgets(key_buf, sizeof(key_buf), stdin)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 343 | bb_perror_msg_and_die("cannot read input"); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 344 | } |
| 345 | i = key_buf[0]; |
| 346 | } |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 347 | } else { /* File is not regular file */ |
| 348 | bb_error_msg_and_die("'%s' exists but is not regular file",dst_fn); |
| 349 | } |
| 350 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 354 | switch (i) { |
| 355 | case 'A': |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 356 | overwrite = O_ALWAYS; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 357 | case 'y': /* Open file and fall into unzip */ |
| 358 | unzip_create_leading_dirs(dst_fn); |
Bernhard Reutner-Fischer | 64d7e93 | 2006-09-11 16:01:40 +0000 | [diff] [blame] | 359 | dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 360 | case -1: /* Unzip */ |
Paul Fox | 9382b38 | 2007-09-07 20:28:25 +0000 | [diff] [blame] | 361 | if (verbose) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 362 | printf(" inflating: %s\n", dst_fn); |
| 363 | } |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 364 | unzip_extract(&zip_header, src_fd, dst_fd); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 365 | if (dst_fd != STDOUT_FILENO) { |
| 366 | /* closing STDOUT is potentially bad for future business */ |
| 367 | close(dst_fd); |
| 368 | } |
| 369 | break; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 370 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 371 | case 'N': |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 372 | overwrite = O_NEVER; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 373 | case 'n': |
| 374 | /* Skip entry data */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 375 | unzip_skip(src_fd, zip_header.formatted.cmpsize); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 376 | break; |
| 377 | |
| 378 | case 'r': |
| 379 | /* Prompt for new name */ |
| 380 | printf("new name: "); |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 381 | if (!fgets(key_buf, sizeof(key_buf), stdin)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 382 | bb_perror_msg_and_die("cannot read input"); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 383 | } |
| 384 | free(dst_fn); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 385 | dst_fn = xstrdup(key_buf); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 386 | chomp(dst_fn); |
| 387 | goto _check_file; |
| 388 | |
| 389 | default: |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 390 | printf("error: invalid response [%c]\n",(char)i); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 391 | goto _check_file; |
| 392 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 393 | |
| 394 | /* Data descriptor section */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 395 | if (zip_header.formatted.flags & 4) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 396 | /* skip over duplicate crc, compressed size and uncompressed size */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 397 | unzip_skip(src_fd, 12); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 400 | |
Paul Fox | 9382b38 | 2007-09-07 20:28:25 +0000 | [diff] [blame] | 401 | if (listing && verbose) { |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 402 | printf(" -------- -------\n" |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 403 | "%9d %d files\n", |
| 404 | total_size, total_entries); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 407 | return 0; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 408 | } |