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