source: MondoRescue/branches/stable/mindi-busybox/archival/unzip.c

Last change on this file was 1770, checked in by Bruno Cornec, 16 years ago
  • Better output for mindi-busybox revision
  • Remove dummy file created on NFS - report from Arnaud Tiger <arnaud.tiger_at_hp.com>
  • strace useful for debug
  • fix new versions for pb (2.0.0 for mindi and 1.7.2 for mindi-busybox)
  • fix build process for mindi-busybox + options used in that version (dd for label-partitions-as-necessary)
  • fix typo in label-partitions-as-necessary which doesn't seem to work
  • Update to busybox 1.7.2
  • perl is now required at restore time to support uuid swap partitions (and will be used for many other thigs

in the future for sure)

  • next mindi version will be 2.0.0 due to all the changes made in it (udev may break working distros)
  • small optimization in mindi on keyboard handling (one single find instead of multiple)
  • better interaction for USB device when launching mindi manually
  • attempt to automatically guess block disk size for ramdisk
  • fix typos in bkphw
  • Fix the remaining problem with UUID support for swap partitions
  • Updates mondoarchive man page for USB support
  • Adds preliminary Hardware support to mindi (Proliant SSSTK)
  • Tries to add udev support also for rhel4
  • Fix UUID support which was still broken.
  • Be conservative in test for the start-nfs script
  • Update config file for mindi-busybox for 1.7.2 migration
  • Try to run around a busybox bug (1.2.2 pb on inexistant links)
  • Add build content for mindi-busybox in pb
  • Remove distributions content for mindi-busybox
  • Fix a warning on inexistant raidtab
  • Solve problem on tmpfs in restore init (Problem of inexistant symlink and busybox)
  • Create MONDO_CACHE and use it everywhere + creation at start
  • Really never try to eject a USB device
  • Fix a issue with &> usage (replaced with 1> and 2>)
  • Adds magic file to depllist in order to have file working + ldd which helps for debugging issues
  • tty modes correct to avoid sh error messages
  • Use ext3 normally and not ext2 instead
  • USB device should be corrected after reading (take 1st part)
  • Adds a mount_USB_here function derived from mount_CDROM_here
  • usb detection place before /dev detection in device name at restore time
  • Fix when restoring from USB: media is asked in interactive mode
  • Adds USB support for mondorestore
  • mount_cdrom => mount_media
  • elilo.efi is now searched throughout /boot/efi and not in a fixed place as there is no standard
  • untar-and-softlink => untar (+ interface change)
  • suppress useless softlinks creation/removal in boot process
  • avoids udevd messages on groups
  • Increase # of disks to 99 as in mindi at restore time (should be a conf file parameter)
  • skip existing big file creation
  • seems to work correctly for USB mindi boot
  • Adds group and tty link to udev conf
  • Always load usb-torage (even 2.6) to initiate USB bus discovery
  • Better printing of messages
  • Attempt to fix a bug in supporting OpenSusE 10.3 kernel for initramfs (mindi may now use multiple regex for kernel initrd detection)
  • Links were not correctly done as non relative for modules in mindi
  • exclusion of modules denied now works
  • Also create modules in their ordinary place, so that classical modprobe works + copy modules.dep
  • Fix bugs for DENY_MODS handling
  • Add device /dev/console for udev
  • ide-generic should now really be excluded
  • Fix a bug in major number for tty
  • If udev then adds modprobe/insmod to rootfs
  • tty0 is also cretaed with udev
  • ide-generic put rather in DENY_MODS
  • udevd remove from deplist s handled in mindi directly
  • better default for mindi when using --usb
  • Handles dynamically linked busybox (in case we want to use it soon ;-)
  • Adds fixed devices to create for udev
  • ide-generic should not be part of the initrd when using libata v2
  • support a dynamically linked udev (case on Ubuntu 7.10 and Mandriva 2008.0 so should be quite generic) This will give incitation to move to dyn. linked binaries in the initrd which will help for other tasks (ia6 4)
  • Improvement in udev support (do not use cl options not available in busybox)
  • Udev in mindi
    • auto creation of the right links at boot time with udev-links.conf(from Mandriva 2008.0)
    • rework startup of udev as current makes kernel crash (from Mandriva 2008.0)
    • add support for 64 bits udev
  • Try to render MyInsmod silent at boot time
  • Adds udev support (mandatory for newest distributions to avoid remapping of devices in a different way as on the original system)
  • We also need vaft format support for USB boot
  • Adds libusual support (Ubuntu 7.10 needs it for USB)
  • Improve Ubuntu/Debian keyboard detection and support
  • pbinit adapted to new pb (0.8.10). Filtering of docs done in it
  • Suppress some mondo warnings and errors on USB again
  • Tries to fix lack of files in deb mindi package
  • Verify should now work for USB devices
  • More log/mesages improvement for USB support
  • - Supress g_erase_tmpdir_and_scratchdir
  • Improve some log messages for USB support
  • Try to improve install in mindi to avoid issues with isolinux.cfg not installed vene if in the pkg :-(
  • Improve mindi-busybox build
  • In conformity with pb 0.8.9
  • Add support for Ubuntu 7.10 in build process
  • Add USB Key button to Menu UI (CD streamer removed)
  • Attempt to fix error messages on tmp/scratch files at the end by removing those dir at the latest possible.
  • Fix a bug linked to the size of the -E param which could be used (Arnaud Tiger/René Ribaud).
  • Integrate ~/.pbrc content into mondorescue.pb (required project-builder >= 0.8.7)
  • Put mondorescue in conformity with new pb filtering rules
  • Add USB support at restore time (no test done yet). New start-usb script PB varibale added where useful
  • Unmounting USB device before removal of temporary scratchdir
  • Stil refining USB copy back to mondo (one command was not executed)
  • No need to have the image subdor in the csratchdir when USB.
  • umount the USB partition before attempting to use it
  • Remove useless copy from mindi to mondo at end of USB handling

(risky merge, we are raising the limits of 2 diverging branches. The status of stable is not completely sure as such. Will need lots of tests, but it's not yet done :-()
(merge -r1692:1769 $SVN_M/branches/2.2.5)

File size: 10.2 KB
RevLine 
[821]1/* vi: set sw=4 ts=4: */
2/*
3 * Mini unzip implementation for busybox
4 *
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.
9 *
10 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
11 */
12
13/* For reference see
14 * http://www.pkware.com/company/standards/appnote/
15 * http://www.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip
16 */
17
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 */
26
[1770]27#include "libbb.h"
[821]28#include "unarchive.h"
29
[1770]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)
[821]34
35typedef 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 */
43 unsigned int crc32 ATTRIBUTE_PACKED; /* 10-13 */
44 unsigned int cmpsize ATTRIBUTE_PACKED; /* 14-17 */
45 unsigned int ucmpsize ATTRIBUTE_PACKED; /* 18-21 */
46 unsigned short filename_len; /* 22-23 */
47 unsigned short extra_len; /* 24-25 */
[1770]48 } formatted ATTRIBUTE_PACKED;
[821]49} zip_header_t;
50
51static void unzip_skip(int fd, off_t skip)
52{
53 if (lseek(fd, skip, SEEK_CUR) == (off_t)-1) {
[1770]54 if (errno != ESPIPE)
55 bb_error_msg_and_die("seek failure");
56 bb_copyfd_exact_size(fd, -1, skip);
[821]57 }
58}
59
60static void unzip_create_leading_dirs(char *fn)
61{
62 /* Create all leading directories */
[1770]63 char *name = xstrdup(fn);
[821]64 if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) {
[1770]65 bb_error_msg_and_die("exiting"); /* bb_make_directory is noisy */
[821]66 }
67 free(name);
68}
69
70static int unzip_extract(zip_header_t *zip_header, int src_fd, int dst_fd)
71{
[1770]72 if (zip_header->formatted.method == 0) {
[821]73 /* Method 0 - stored (not compressed) */
[1770]74 off_t size = zip_header->formatted.ucmpsize;
75 if (size)
76 bb_copyfd_exact_size(src_fd, dst_fd, size);
[821]77 } else {
78 /* Method 8 - inflate */
[1770]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
[821]82 /* Validate decompression - crc */
[1770]83 if (zip_header->formatted.crc32 != (res.crc ^ 0xffffffffL)) {
84 bb_error_msg("invalid compressed data--%s error", "crc");
[821]85 return 1;
86 }
87 /* Validate decompression - size */
[1770]88 if (zip_header->formatted.ucmpsize != res.bytes_out) {
89 bb_error_msg("invalid compressed data--%s error", "length");
[821]90 return 1;
91 }
92 }
93 return 0;
94}
95
[1770]96int unzip_main(int argc, char **argv);
[821]97int unzip_main(int argc, char **argv)
98{
99 zip_header_t zip_header;
100 enum {v_silent, v_normal, v_list} verbosity = v_normal;
101 enum {o_prompt, o_never, o_always} overwrite = o_prompt;
102 unsigned int total_size = 0;
103 unsigned int total_entries = 0;
104 int src_fd = -1, dst_fd = -1;
105 char *src_fn = NULL, *dst_fn = NULL;
106 llist_t *zaccept = NULL;
107 llist_t *zreject = NULL;
108 char *base_dir = NULL;
109 int failed, i, opt, opt_range = 0, list_header_done = 0;
110 char key_buf[512];
111 struct stat stat_buf;
112
[1770]113 while ((opt = getopt(argc, argv, "-d:lnopqx")) != -1) {
114 switch (opt_range) {
[821]115 case 0: /* Options */
[1770]116 switch (opt) {
[821]117 case 'l': /* List */
118 verbosity = v_list;
119 break;
120
121 case 'n': /* Never overwrite existing files */
122 overwrite = o_never;
123 break;
124
125 case 'o': /* Always overwrite existing files */
126 overwrite = o_always;
127 break;
128
129 case 'p': /* Extract files to stdout and fall through to set verbosity */
130 dst_fd = STDOUT_FILENO;
131
132 case 'q': /* Be quiet */
133 verbosity = (verbosity == v_normal) ? v_silent : verbosity;
134 break;
135
136 case 1 : /* The zip file */
[1770]137 src_fn = xmalloc(strlen(optarg)+4);
138 strcpy(src_fn, optarg);
[821]139 opt_range++;
140 break;
141
142 default:
143 bb_show_usage();
144
145 }
146 break;
147
148 case 1: /* Include files */
149 if (opt == 1) {
150 llist_add_to(&zaccept, optarg);
151
152 } else if (opt == 'd') {
153 base_dir = optarg;
154 opt_range += 2;
155
156 } else if (opt == 'x') {
157 opt_range++;
158
159 } else {
160 bb_show_usage();
161 }
162 break;
163
164 case 2 : /* Exclude files */
165 if (opt == 1) {
166 llist_add_to(&zreject, optarg);
167
168 } else if (opt == 'd') { /* Extract to base directory */
169 base_dir = optarg;
170 opt_range++;
171
172 } else {
173 bb_show_usage();
174 }
175 break;
176
177 default:
178 bb_show_usage();
179 }
180 }
181
182 if (src_fn == NULL) {
183 bb_show_usage();
184 }
185
186 /* Open input file */
[1770]187 if (LONE_DASH(src_fn)) {
[821]188 src_fd = STDIN_FILENO;
189 /* Cannot use prompt mode since zip data is arriving on STDIN */
190 overwrite = (overwrite == o_prompt) ? o_never : overwrite;
191 } else {
192 static const char *const extn[] = {"", ".zip", ".ZIP"};
193 int orig_src_fn_len = strlen(src_fn);
[1770]194 for (i = 0; (i < 3) && (src_fd == -1); i++) {
[821]195 strcpy(src_fn + orig_src_fn_len, extn[i]);
196 src_fd = open(src_fn, O_RDONLY);
197 }
198 if (src_fd == -1) {
[1770]199 src_fn[orig_src_fn_len] = '\0';
200 bb_error_msg_and_die("cannot open %s, %s.zip, %s.ZIP", src_fn, src_fn, src_fn);
[821]201 }
202 }
203
204 /* Change dir if necessary */
205 if (base_dir)
[1770]206 xchdir(base_dir);
[821]207
208 if (verbosity != v_silent)
209 printf("Archive: %s\n", src_fn);
210
211 failed = 0;
212
213 while (1) {
214 unsigned int magic;
215
216 /* Check magic number */
[1770]217 xread(src_fd, &magic, 4);
[821]218 if (magic == ZIP_CDS_MAGIC) {
219 break;
220 } else if (magic != ZIP_FILEHEADER_MAGIC) {
[1770]221 bb_error_msg_and_die("invalid zip magic %08X", magic);
[821]222 }
223
224 /* Read the file header */
[1770]225 xread(src_fd, zip_header.raw, 26);
226 zip_header.formatted.version = SWAP_LE32(zip_header.formatted.version);
227 zip_header.formatted.flags = SWAP_LE32(zip_header.formatted.flags);
228 zip_header.formatted.method = SWAP_LE32(zip_header.formatted.method);
229 zip_header.formatted.modtime = SWAP_LE32(zip_header.formatted.modtime);
230 zip_header.formatted.moddate = SWAP_LE32(zip_header.formatted.moddate);
231 zip_header.formatted.crc32 = SWAP_LE32(zip_header.formatted.crc32);
232 zip_header.formatted.cmpsize = SWAP_LE32(zip_header.formatted.cmpsize);
233 zip_header.formatted.ucmpsize = SWAP_LE32(zip_header.formatted.ucmpsize);
234 zip_header.formatted.filename_len = SWAP_LE32(zip_header.formatted.filename_len);
235 zip_header.formatted.extra_len = SWAP_LE32(zip_header.formatted.extra_len);
236 if ((zip_header.formatted.method != 0) && (zip_header.formatted.method != 8)) {
237 bb_error_msg_and_die("unsupported compression method %d", zip_header.formatted.method);
[821]238 }
239
240 /* Read filename */
241 free(dst_fn);
[1770]242 dst_fn = xzalloc(zip_header.formatted.filename_len + 1);
243 xread(src_fd, dst_fn, zip_header.formatted.filename_len);
[821]244
245 /* Skip extra header bytes */
[1770]246 unzip_skip(src_fd, zip_header.formatted.extra_len);
[821]247
248 if ((verbosity == v_list) && !list_header_done){
[1770]249 puts(" Length Date Time Name\n"
250 " -------- ---- ---- ----");
[821]251 list_header_done = 1;
252 }
253
254 /* Filter zip entries */
255 if (find_list_entry(zreject, dst_fn) ||
256 (zaccept && !find_list_entry(zaccept, dst_fn))) { /* Skip entry */
257 i = 'n';
258
259 } else { /* Extract entry */
[1770]260 total_size += zip_header.formatted.ucmpsize;
[821]261
262 if (verbosity == v_list) { /* List entry */
[1770]263 unsigned int dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16);
[821]264 printf("%9u %02u-%02u-%02u %02u:%02u %s\n",
[1770]265 zip_header.formatted.ucmpsize,
[821]266 (dostime & 0x01e00000) >> 21,
267 (dostime & 0x001f0000) >> 16,
268 (((dostime & 0xfe000000) >> 25) + 1980) % 100,
269 (dostime & 0x0000f800) >> 11,
270 (dostime & 0x000007e0) >> 5,
271 dst_fn);
272 total_entries++;
273 i = 'n';
274 } else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */
275 i = -1;
276 } else if (last_char_is(dst_fn, '/')) { /* Extract directory */
277 if (stat(dst_fn, &stat_buf) == -1) {
278 if (errno != ENOENT) {
[1770]279 bb_perror_msg_and_die("cannot stat '%s'",dst_fn);
[821]280 }
281 if (verbosity == v_normal) {
282 printf(" creating: %s\n", dst_fn);
283 }
284 unzip_create_leading_dirs(dst_fn);
285 if (bb_make_directory(dst_fn, 0777, 0)) {
[1770]286 bb_error_msg_and_die("exiting");
[821]287 }
288 } else {
289 if (!S_ISDIR(stat_buf.st_mode)) {
290 bb_error_msg_and_die("'%s' exists but is not directory", dst_fn);
291 }
292 }
293 i = 'n';
294
295 } else { /* Extract file */
[1770]296 _check_file:
[821]297 if (stat(dst_fn, &stat_buf) == -1) { /* File does not exist */
298 if (errno != ENOENT) {
[1770]299 bb_perror_msg_and_die("cannot stat '%s'",dst_fn);
[821]300 }
301 i = 'y';
302 } else { /* File already exists */
303 if (overwrite == o_never) {
304 i = 'n';
305 } else if (S_ISREG(stat_buf.st_mode)) { /* File is regular file */
306 if (overwrite == o_always) {
307 i = 'y';
308 } else {
309 printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
310 if (!fgets(key_buf, 512, stdin)) {
[1770]311 bb_perror_msg_and_die("cannot read input");
[821]312 }
313 i = key_buf[0];
314 }
315 } else { /* File is not regular file */
316 bb_error_msg_and_die("'%s' exists but is not regular file",dst_fn);
317 }
318 }
319 }
320 }
321
322 switch (i) {
323 case 'A':
324 overwrite = o_always;
325 case 'y': /* Open file and fall into unzip */
326 unzip_create_leading_dirs(dst_fn);
[1770]327 dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC);
[821]328 case -1: /* Unzip */
329 if (verbosity == v_normal) {
330 printf(" inflating: %s\n", dst_fn);
331 }
332 if (unzip_extract(&zip_header, src_fd, dst_fd)) {
[1770]333 failed = 1;
[821]334 }
335 if (dst_fd != STDOUT_FILENO) {
336 /* closing STDOUT is potentially bad for future business */
337 close(dst_fd);
338 }
339 break;
340
341 case 'N':
342 overwrite = o_never;
343 case 'n':
344 /* Skip entry data */
[1770]345 unzip_skip(src_fd, zip_header.formatted.cmpsize);
[821]346 break;
347
348 case 'r':
349 /* Prompt for new name */
350 printf("new name: ");
351 if (!fgets(key_buf, 512, stdin)) {
[1770]352 bb_perror_msg_and_die("cannot read input");
[821]353 }
354 free(dst_fn);
[1770]355 dst_fn = xstrdup(key_buf);
[821]356 chomp(dst_fn);
357 goto _check_file;
358
359 default:
360 printf("error: invalid response [%c]\n",(char)i);
361 goto _check_file;
362 }
363
364 /* Data descriptor section */
[1770]365 if (zip_header.formatted.flags & 4) {
[821]366 /* skip over duplicate crc, compressed size and uncompressed size */
367 unzip_skip(src_fd, 12);
368 }
369 }
370
371 if (verbosity == v_list) {
372 printf(" -------- -------\n"
373 "%9d %d files\n", total_size, total_entries);
374 }
375
376 return failed;
377}
Note: See TracBrowser for help on using the repository browser.