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