unzip.c revision 1.3 1 1.3 joerg /* $NetBSD: unzip.c,v 1.3 2009/08/22 17:19:11 joerg Exp $ */
2 1.1 joerg
3 1.1 joerg /*-
4 1.1 joerg * Copyright (c) 2009 Joerg Sonnenberger <joerg (at) NetBSD.org>
5 1.1 joerg * Copyright (c) 2007-2008 Dag-Erling Codan Smrgrav
6 1.1 joerg * All rights reserved.
7 1.1 joerg *
8 1.1 joerg * Redistribution and use in source and binary forms, with or without
9 1.1 joerg * modification, are permitted provided that the following conditions
10 1.1 joerg * are met:
11 1.1 joerg * 1. Redistributions of source code must retain the above copyright
12 1.1 joerg * notice, this list of conditions and the following disclaimer
13 1.1 joerg * in this position and unchanged.
14 1.1 joerg * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 joerg * notice, this list of conditions and the following disclaimer in the
16 1.1 joerg * documentation and/or other materials provided with the distribution.
17 1.1 joerg *
18 1.1 joerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 1.1 joerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 joerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 joerg * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 joerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 joerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 joerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 joerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 joerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 joerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 joerg * SUCH DAMAGE.
29 1.1 joerg *
30 1.1 joerg * $FreeBSD: revision 180124$
31 1.1 joerg *
32 1.1 joerg * This file would be much shorter if we didn't care about command-line
33 1.1 joerg * compatibility with Info-ZIP's UnZip, which requires us to duplicate
34 1.1 joerg * parts of libarchive in order to gain more detailed control of its
35 1.1 joerg * behaviour for the purpose of implementing the -n, -o, -L and -a
36 1.1 joerg * options.
37 1.1 joerg */
38 1.1 joerg
39 1.1 joerg #include <sys/cdefs.h>
40 1.3 joerg __RCSID("$NetBSD: unzip.c,v 1.3 2009/08/22 17:19:11 joerg Exp $");
41 1.1 joerg
42 1.1 joerg #include <sys/queue.h>
43 1.1 joerg #include <sys/stat.h>
44 1.1 joerg
45 1.1 joerg #include <ctype.h>
46 1.1 joerg #include <errno.h>
47 1.1 joerg #include <fcntl.h>
48 1.1 joerg #include <fnmatch.h>
49 1.1 joerg #include <stdarg.h>
50 1.1 joerg #include <stdio.h>
51 1.1 joerg #include <stdlib.h>
52 1.1 joerg #include <string.h>
53 1.1 joerg #include <unistd.h>
54 1.1 joerg
55 1.1 joerg #include <archive.h>
56 1.1 joerg #include <archive_entry.h>
57 1.1 joerg
58 1.1 joerg /* command-line options */
59 1.1 joerg static int a_opt; /* convert EOL */
60 1.3 joerg static int c_opt; /* extract to stoud */
61 1.1 joerg static const char *d_arg; /* directory */
62 1.1 joerg static int f_opt; /* update existing files only */
63 1.1 joerg static int j_opt; /* junk directories */
64 1.1 joerg static int L_opt; /* lowercase names */
65 1.1 joerg static int n_opt; /* never overwrite */
66 1.1 joerg static int o_opt; /* always overwrite */
67 1.3 joerg static int p_opt; /* extract to stdout, quiet */
68 1.1 joerg static int q_opt; /* quiet */
69 1.1 joerg static int t_opt; /* test */
70 1.1 joerg static int u_opt; /* update */
71 1.3 joerg static int v_opt; /* verbose/list */
72 1.1 joerg
73 1.1 joerg /* time when unzip started */
74 1.1 joerg static time_t now;
75 1.1 joerg
76 1.1 joerg /* debug flag */
77 1.1 joerg static int unzip_debug;
78 1.1 joerg
79 1.1 joerg /* running on tty? */
80 1.1 joerg static int tty;
81 1.1 joerg
82 1.1 joerg /* error flag for -t */
83 1.1 joerg static int test_failed;
84 1.1 joerg
85 1.1 joerg /* convenience macro */
86 1.1 joerg /* XXX should differentiate between ARCHIVE_{WARN,FAIL,RETRY} */
87 1.1 joerg #define ac(call) \
88 1.1 joerg do { \
89 1.1 joerg int acret = (call); \
90 1.1 joerg if (acret != ARCHIVE_OK) \
91 1.1 joerg errorx("%s", archive_error_string(a)); \
92 1.1 joerg } while (0)
93 1.1 joerg
94 1.1 joerg /*
95 1.1 joerg * Indicates that last info() did not end with EOL. This helps error() et
96 1.1 joerg * al. avoid printing an error message on the same line as an incomplete
97 1.1 joerg * informational message.
98 1.1 joerg */
99 1.1 joerg static int noeol;
100 1.1 joerg
101 1.1 joerg /* fatal error message + errno */
102 1.1 joerg static void
103 1.1 joerg error(const char *fmt, ...)
104 1.1 joerg {
105 1.1 joerg va_list ap;
106 1.1 joerg
107 1.1 joerg if (noeol)
108 1.1 joerg fprintf(stdout, "\n");
109 1.1 joerg fflush(stdout);
110 1.1 joerg fprintf(stderr, "unzip: ");
111 1.1 joerg va_start(ap, fmt);
112 1.1 joerg vfprintf(stderr, fmt, ap);
113 1.1 joerg va_end(ap);
114 1.1 joerg fprintf(stderr, ": %s\n", strerror(errno));
115 1.1 joerg exit(1);
116 1.1 joerg }
117 1.1 joerg
118 1.1 joerg /* fatal error message, no errno */
119 1.1 joerg static void
120 1.1 joerg errorx(const char *fmt, ...)
121 1.1 joerg {
122 1.1 joerg va_list ap;
123 1.1 joerg
124 1.1 joerg if (noeol)
125 1.1 joerg fprintf(stdout, "\n");
126 1.1 joerg fflush(stdout);
127 1.1 joerg fprintf(stderr, "unzip: ");
128 1.1 joerg va_start(ap, fmt);
129 1.1 joerg vfprintf(stderr, fmt, ap);
130 1.1 joerg va_end(ap);
131 1.1 joerg fprintf(stderr, "\n");
132 1.1 joerg exit(1);
133 1.1 joerg }
134 1.1 joerg
135 1.1 joerg #if 0
136 1.1 joerg /* non-fatal error message + errno */
137 1.1 joerg static void
138 1.1 joerg warning(const char *fmt, ...)
139 1.1 joerg {
140 1.1 joerg va_list ap;
141 1.1 joerg
142 1.1 joerg if (noeol)
143 1.1 joerg fprintf(stdout, "\n");
144 1.1 joerg fflush(stdout);
145 1.1 joerg fprintf(stderr, "unzip: ");
146 1.1 joerg va_start(ap, fmt);
147 1.1 joerg vfprintf(stderr, fmt, ap);
148 1.1 joerg va_end(ap);
149 1.1 joerg fprintf(stderr, ": %s\n", strerror(errno));
150 1.1 joerg }
151 1.1 joerg #endif
152 1.1 joerg
153 1.1 joerg /* non-fatal error message, no errno */
154 1.1 joerg static void
155 1.1 joerg warningx(const char *fmt, ...)
156 1.1 joerg {
157 1.1 joerg va_list ap;
158 1.1 joerg
159 1.1 joerg if (noeol)
160 1.1 joerg fprintf(stdout, "\n");
161 1.1 joerg fflush(stdout);
162 1.1 joerg fprintf(stderr, "unzip: ");
163 1.1 joerg va_start(ap, fmt);
164 1.1 joerg vfprintf(stderr, fmt, ap);
165 1.1 joerg va_end(ap);
166 1.1 joerg fprintf(stderr, "\n");
167 1.1 joerg }
168 1.1 joerg
169 1.1 joerg /* informational message (if not -q) */
170 1.1 joerg static void
171 1.1 joerg info(const char *fmt, ...)
172 1.1 joerg {
173 1.1 joerg va_list ap;
174 1.1 joerg
175 1.1 joerg if (q_opt && !unzip_debug)
176 1.1 joerg return;
177 1.1 joerg va_start(ap, fmt);
178 1.1 joerg vfprintf(stdout, fmt, ap);
179 1.1 joerg va_end(ap);
180 1.1 joerg fflush(stdout);
181 1.1 joerg
182 1.1 joerg if (*fmt == '\0')
183 1.1 joerg noeol = 1;
184 1.1 joerg else
185 1.1 joerg noeol = fmt[strlen(fmt) - 1] != '\n';
186 1.1 joerg }
187 1.1 joerg
188 1.1 joerg /* debug message (if unzip_debug) */
189 1.1 joerg static void
190 1.1 joerg debug(const char *fmt, ...)
191 1.1 joerg {
192 1.1 joerg va_list ap;
193 1.1 joerg
194 1.1 joerg if (!unzip_debug)
195 1.1 joerg return;
196 1.1 joerg va_start(ap, fmt);
197 1.1 joerg vfprintf(stderr, fmt, ap);
198 1.1 joerg va_end(ap);
199 1.1 joerg fflush(stderr);
200 1.1 joerg
201 1.1 joerg if (*fmt == '\0')
202 1.1 joerg noeol = 1;
203 1.1 joerg else
204 1.1 joerg noeol = fmt[strlen(fmt) - 1] != '\n';
205 1.1 joerg }
206 1.1 joerg
207 1.1 joerg /* duplicate a path name, possibly converting to lower case */
208 1.1 joerg static char *
209 1.1 joerg pathdup(const char *path)
210 1.1 joerg {
211 1.1 joerg char *str;
212 1.1 joerg size_t i, len;
213 1.1 joerg
214 1.1 joerg len = strlen(path);
215 1.1 joerg while (len && path[len - 1] == '/')
216 1.1 joerg len--;
217 1.1 joerg if ((str = malloc(len + 1)) == NULL) {
218 1.1 joerg errno = ENOMEM;
219 1.1 joerg error("malloc()");
220 1.1 joerg }
221 1.1 joerg if (L_opt) {
222 1.1 joerg for (i = 0; i < len; ++i)
223 1.1 joerg str[i] = tolower((unsigned char)path[i]);
224 1.1 joerg } else {
225 1.1 joerg memcpy(str, path, len);
226 1.1 joerg }
227 1.1 joerg str[len] = '\0';
228 1.1 joerg
229 1.1 joerg return (str);
230 1.1 joerg }
231 1.1 joerg
232 1.1 joerg /* concatenate two path names */
233 1.1 joerg static char *
234 1.1 joerg pathcat(const char *prefix, const char *path)
235 1.1 joerg {
236 1.1 joerg char *str;
237 1.1 joerg size_t prelen, len;
238 1.1 joerg
239 1.1 joerg prelen = prefix ? strlen(prefix) + 1 : 0;
240 1.1 joerg len = strlen(path) + 1;
241 1.1 joerg if ((str = malloc(prelen + len)) == NULL) {
242 1.1 joerg errno = ENOMEM;
243 1.1 joerg error("malloc()");
244 1.1 joerg }
245 1.1 joerg if (prefix) {
246 1.1 joerg memcpy(str, prefix, prelen); /* includes zero */
247 1.1 joerg str[prelen - 1] = '/'; /* splat zero */
248 1.1 joerg }
249 1.1 joerg memcpy(str + prelen, path, len); /* includes zero */
250 1.1 joerg
251 1.1 joerg return (str);
252 1.1 joerg }
253 1.1 joerg
254 1.1 joerg /*
255 1.1 joerg * Pattern lists for include / exclude processing
256 1.1 joerg */
257 1.1 joerg struct pattern {
258 1.1 joerg STAILQ_ENTRY(pattern) link;
259 1.1 joerg char pattern[];
260 1.1 joerg };
261 1.1 joerg
262 1.1 joerg STAILQ_HEAD(pattern_list, pattern);
263 1.1 joerg static struct pattern_list include = STAILQ_HEAD_INITIALIZER(include);
264 1.1 joerg static struct pattern_list exclude = STAILQ_HEAD_INITIALIZER(exclude);
265 1.1 joerg
266 1.1 joerg /*
267 1.1 joerg * Add an entry to a pattern list
268 1.1 joerg */
269 1.1 joerg static void
270 1.1 joerg add_pattern(struct pattern_list *list, const char *pattern)
271 1.1 joerg {
272 1.1 joerg struct pattern *entry;
273 1.1 joerg size_t len;
274 1.1 joerg
275 1.1 joerg debug("adding pattern '%s'\n", pattern);
276 1.1 joerg len = strlen(pattern);
277 1.1 joerg if ((entry = malloc(sizeof *entry + len + 1)) == NULL) {
278 1.1 joerg errno = ENOMEM;
279 1.1 joerg error("malloc()");
280 1.1 joerg }
281 1.1 joerg memcpy(entry->pattern, pattern, len + 1);
282 1.1 joerg STAILQ_INSERT_TAIL(list, entry, link);
283 1.1 joerg }
284 1.1 joerg
285 1.1 joerg /*
286 1.1 joerg * Match a string against a list of patterns
287 1.1 joerg */
288 1.1 joerg static int
289 1.1 joerg match_pattern(struct pattern_list *list, const char *str)
290 1.1 joerg {
291 1.1 joerg struct pattern *entry;
292 1.1 joerg
293 1.1 joerg STAILQ_FOREACH(entry, list, link) {
294 1.1 joerg if (fnmatch(entry->pattern, str, 0) == 0)
295 1.1 joerg return (1);
296 1.1 joerg }
297 1.1 joerg return (0);
298 1.1 joerg }
299 1.1 joerg
300 1.1 joerg /*
301 1.1 joerg * Verify that a given pathname is in the include list and not in the
302 1.1 joerg * exclude list.
303 1.1 joerg */
304 1.1 joerg static int
305 1.1 joerg accept_pathname(const char *pathname)
306 1.1 joerg {
307 1.1 joerg
308 1.1 joerg if (!STAILQ_EMPTY(&include) && !match_pattern(&include, pathname))
309 1.1 joerg return (0);
310 1.1 joerg if (!STAILQ_EMPTY(&exclude) && match_pattern(&exclude, pathname))
311 1.1 joerg return (0);
312 1.1 joerg return (1);
313 1.1 joerg }
314 1.1 joerg
315 1.1 joerg /*
316 1.1 joerg * Create the specified directory with the specified mode, taking certain
317 1.1 joerg * precautions on they way.
318 1.1 joerg */
319 1.1 joerg static void
320 1.1 joerg make_dir(const char *path, int mode)
321 1.1 joerg {
322 1.1 joerg struct stat sb;
323 1.1 joerg
324 1.1 joerg if (lstat(path, &sb) == 0) {
325 1.1 joerg if (S_ISDIR(sb.st_mode))
326 1.1 joerg return;
327 1.1 joerg /*
328 1.1 joerg * Normally, we should either ask the user about removing
329 1.1 joerg * the non-directory of the same name as a directory we
330 1.1 joerg * wish to create, or respect the -n or -o command-line
331 1.1 joerg * options. However, this may lead to a later failure or
332 1.1 joerg * even compromise (if this non-directory happens to be a
333 1.1 joerg * symlink to somewhere unsafe), so we don't.
334 1.1 joerg */
335 1.1 joerg
336 1.1 joerg /*
337 1.1 joerg * Don't check unlink() result; failure will cause mkdir()
338 1.1 joerg * to fail later, which we will catch.
339 1.1 joerg */
340 1.1 joerg (void)unlink(path);
341 1.1 joerg }
342 1.1 joerg if (mkdir(path, mode) != 0 && errno != EEXIST)
343 1.1 joerg error("mkdir('%s')", path);
344 1.1 joerg }
345 1.1 joerg
346 1.1 joerg /*
347 1.1 joerg * Ensure that all directories leading up to (but not including) the
348 1.1 joerg * specified path exist.
349 1.1 joerg *
350 1.1 joerg * XXX inefficient + modifies the file in-place
351 1.1 joerg */
352 1.1 joerg static void
353 1.1 joerg make_parent(char *path)
354 1.1 joerg {
355 1.1 joerg struct stat sb;
356 1.1 joerg char *sep;
357 1.1 joerg
358 1.1 joerg sep = strrchr(path, '/');
359 1.1 joerg if (sep == NULL || sep == path)
360 1.1 joerg return;
361 1.1 joerg *sep = '\0';
362 1.1 joerg if (lstat(path, &sb) == 0) {
363 1.1 joerg if (S_ISDIR(sb.st_mode)) {
364 1.1 joerg *sep = '/';
365 1.1 joerg return;
366 1.1 joerg }
367 1.1 joerg unlink(path);
368 1.1 joerg }
369 1.1 joerg make_parent(path);
370 1.1 joerg mkdir(path, 0755);
371 1.1 joerg *sep = '/';
372 1.1 joerg
373 1.1 joerg #if 0
374 1.1 joerg for (sep = path; (sep = strchr(sep, '/')) != NULL; sep++) {
375 1.1 joerg /* root in case of absolute d_arg */
376 1.1 joerg if (sep == path)
377 1.1 joerg continue;
378 1.1 joerg *sep = '\0';
379 1.1 joerg make_dir(path, 0755);
380 1.1 joerg *sep = '/';
381 1.1 joerg }
382 1.1 joerg #endif
383 1.1 joerg }
384 1.1 joerg
385 1.1 joerg /*
386 1.1 joerg * Extract a directory.
387 1.1 joerg */
388 1.1 joerg static void
389 1.1 joerg extract_dir(struct archive *a, struct archive_entry *e, const char *path)
390 1.1 joerg {
391 1.1 joerg int mode;
392 1.1 joerg
393 1.1 joerg mode = archive_entry_filetype(e) & 0777;
394 1.1 joerg if (mode == 0)
395 1.1 joerg mode = 0755;
396 1.1 joerg
397 1.1 joerg /*
398 1.1 joerg * Some zipfiles contain directories with weird permissions such
399 1.1 joerg * as 0644 or 0444. This can cause strange issues such as being
400 1.1 joerg * unable to extract files into the directory we just created, or
401 1.1 joerg * the user being unable to remove the directory later without
402 1.1 joerg * first manually changing its permissions. Therefore, we whack
403 1.1 joerg * the permissions into shape, assuming that the user wants full
404 1.1 joerg * access and that anyone who gets read access also gets execute
405 1.1 joerg * access.
406 1.1 joerg */
407 1.1 joerg mode |= 0700;
408 1.1 joerg if (mode & 0040)
409 1.1 joerg mode |= 0010;
410 1.1 joerg if (mode & 0004)
411 1.1 joerg mode |= 0001;
412 1.1 joerg
413 1.1 joerg info("d %s\n", path);
414 1.1 joerg make_dir(path, mode);
415 1.1 joerg ac(archive_read_data_skip(a));
416 1.1 joerg }
417 1.1 joerg
418 1.1 joerg static unsigned char buffer[8192];
419 1.1 joerg static char spinner[] = { '|', '/', '-', '\\' };
420 1.1 joerg
421 1.1 joerg /*
422 1.1 joerg * Extract a regular file.
423 1.1 joerg */
424 1.1 joerg static void
425 1.1 joerg extract_file(struct archive *a, struct archive_entry *e, const char *path)
426 1.1 joerg {
427 1.1 joerg int mode;
428 1.1 joerg time_t mtime;
429 1.1 joerg struct stat sb;
430 1.1 joerg struct timeval tv[2];
431 1.1 joerg int cr, fd, text, warn;
432 1.1 joerg ssize_t len;
433 1.1 joerg unsigned char *p, *q, *end;
434 1.1 joerg
435 1.1 joerg mode = archive_entry_filetype(e) & 0777;
436 1.1 joerg if (mode == 0)
437 1.1 joerg mode = 0644;
438 1.1 joerg mtime = archive_entry_mtime(e);
439 1.1 joerg
440 1.1 joerg /* look for existing file of same name */
441 1.1 joerg if (lstat(path, &sb) == 0) {
442 1.1 joerg if (u_opt || f_opt) {
443 1.1 joerg /* check if up-to-date */
444 1.1 joerg if (S_ISREG(sb.st_mode) && sb.st_mtime >= mtime)
445 1.1 joerg return;
446 1.1 joerg (void)unlink(path);
447 1.1 joerg } else if (o_opt) {
448 1.1 joerg /* overwrite */
449 1.1 joerg (void)unlink(path);
450 1.1 joerg } else if (n_opt) {
451 1.1 joerg /* do not overwrite */
452 1.1 joerg return;
453 1.1 joerg } else {
454 1.1 joerg /* XXX ask user */
455 1.1 joerg errorx("not implemented");
456 1.1 joerg }
457 1.1 joerg } else {
458 1.1 joerg if (f_opt)
459 1.1 joerg return;
460 1.1 joerg }
461 1.1 joerg
462 1.1 joerg if ((fd = open(path, O_RDWR|O_CREAT|O_TRUNC, mode)) < 0)
463 1.1 joerg error("open('%s')", path);
464 1.1 joerg
465 1.1 joerg /* loop over file contents and write to disk */
466 1.1 joerg info("x %s", path);
467 1.1 joerg text = a_opt;
468 1.1 joerg warn = 0;
469 1.1 joerg cr = 0;
470 1.1 joerg for (int n = 0; ; n++) {
471 1.1 joerg if (tty && (n % 4) == 0)
472 1.1 joerg info(" %c\b\b", spinner[(n / 4) % sizeof spinner]);
473 1.1 joerg
474 1.1 joerg len = archive_read_data(a, buffer, sizeof buffer);
475 1.1 joerg
476 1.1 joerg if (len < 0)
477 1.1 joerg ac(len);
478 1.1 joerg
479 1.1 joerg /* left over CR from previous buffer */
480 1.1 joerg if (a_opt && cr) {
481 1.1 joerg if (len == 0 || buffer[0] != '\n')
482 1.1 joerg if (write(fd, "\r", 1) != 1)
483 1.1 joerg error("write('%s')", path);
484 1.1 joerg cr = 0;
485 1.1 joerg }
486 1.1 joerg
487 1.1 joerg /* EOF */
488 1.1 joerg if (len == 0)
489 1.1 joerg break;
490 1.1 joerg end = buffer + len;
491 1.1 joerg
492 1.1 joerg /*
493 1.1 joerg * Detect whether this is a text file. The correct way to
494 1.1 joerg * do this is to check the least significant bit of the
495 1.1 joerg * "internal file attributes" field of the corresponding
496 1.1 joerg * file header in the central directory, but libarchive
497 1.1 joerg * does not read the central directory, so we have to
498 1.1 joerg * guess by looking for non-ASCII characters in the
499 1.1 joerg * buffer. Hopefully we won't guess wrong. If we do
500 1.1 joerg * guess wrong, we print a warning message later.
501 1.1 joerg */
502 1.1 joerg if (a_opt && n == 0) {
503 1.1 joerg for (p = buffer; p < end; ++p) {
504 1.1 joerg if (!isascii((unsigned char)*p)) {
505 1.1 joerg text = 0;
506 1.1 joerg break;
507 1.1 joerg }
508 1.1 joerg }
509 1.1 joerg }
510 1.1 joerg
511 1.1 joerg /* simple case */
512 1.1 joerg if (!a_opt || !text) {
513 1.1 joerg if (write(fd, buffer, len) != len)
514 1.1 joerg error("write('%s')", path);
515 1.1 joerg continue;
516 1.1 joerg }
517 1.1 joerg
518 1.1 joerg /* hard case: convert \r\n to \n (sigh...) */
519 1.1 joerg for (p = buffer; p < end; p = q + 1) {
520 1.1 joerg for (q = p; q < end; q++) {
521 1.1 joerg if (!warn && !isascii(*q)) {
522 1.1 joerg warningx("%s may be corrupted due"
523 1.1 joerg " to weak text file detection"
524 1.1 joerg " heuristic", path);
525 1.1 joerg warn = 1;
526 1.1 joerg }
527 1.1 joerg if (q[0] != '\r')
528 1.1 joerg continue;
529 1.1 joerg if (&q[1] == end) {
530 1.1 joerg cr = 1;
531 1.1 joerg break;
532 1.1 joerg }
533 1.1 joerg if (q[1] == '\n')
534 1.1 joerg break;
535 1.1 joerg }
536 1.1 joerg if (write(fd, p, q - p) != q - p)
537 1.1 joerg error("write('%s')", path);
538 1.1 joerg }
539 1.1 joerg }
540 1.1 joerg if (tty)
541 1.1 joerg info(" \b\b");
542 1.1 joerg if (text)
543 1.1 joerg info(" (text)");
544 1.1 joerg info("\n");
545 1.1 joerg
546 1.1 joerg /* set access and modification time */
547 1.1 joerg tv[0].tv_sec = now;
548 1.1 joerg tv[0].tv_usec = 0;
549 1.1 joerg tv[1].tv_sec = mtime;
550 1.1 joerg tv[1].tv_usec = 0;
551 1.1 joerg if (futimes(fd, tv) != 0)
552 1.1 joerg error("utimes('%s')", path);
553 1.1 joerg if (close(fd) != 0)
554 1.1 joerg error("close('%s')", path);
555 1.1 joerg }
556 1.1 joerg
557 1.1 joerg /*
558 1.1 joerg * Extract a zipfile entry: first perform some sanity checks to ensure
559 1.1 joerg * that it is either a directory or a regular file and that the path is
560 1.1 joerg * not absolute and does not try to break out of the current directory;
561 1.1 joerg * then call either extract_dir() or extract_file() as appropriate.
562 1.1 joerg *
563 1.1 joerg * This is complicated a bit by the various ways in which we need to
564 1.1 joerg * manipulate the path name. Case conversion (if requested by the -L
565 1.1 joerg * option) happens first, but the include / exclude patterns are applied
566 1.1 joerg * to the full converted path name, before the directory part of the path
567 1.1 joerg * is removed in accordance with the -j option. Sanity checks are
568 1.1 joerg * intentionally done earlier than they need to be, so the user will get a
569 1.1 joerg * warning about insecure paths even for files or directories which
570 1.1 joerg * wouldn't be extracted anyway.
571 1.1 joerg */
572 1.1 joerg static void
573 1.1 joerg extract(struct archive *a, struct archive_entry *e)
574 1.1 joerg {
575 1.1 joerg char *pathname, *realpathname;
576 1.1 joerg mode_t filetype;
577 1.1 joerg char *p, *q;
578 1.1 joerg
579 1.1 joerg pathname = pathdup(archive_entry_pathname(e));
580 1.1 joerg filetype = archive_entry_filetype(e);
581 1.1 joerg
582 1.1 joerg /* sanity checks */
583 1.1 joerg if (pathname[0] == '/' ||
584 1.1 joerg strncmp(pathname, "../", 3) == 0 ||
585 1.1 joerg strstr(pathname, "/../") != NULL) {
586 1.1 joerg warningx("skipping insecure entry '%s'", pathname);
587 1.1 joerg ac(archive_read_data_skip(a));
588 1.1 joerg free(pathname);
589 1.1 joerg return;
590 1.1 joerg }
591 1.1 joerg
592 1.1 joerg /* I don't think this can happen in a zipfile.. */
593 1.1 joerg if (!S_ISDIR(filetype) && !S_ISREG(filetype)) {
594 1.1 joerg warningx("skipping non-regular entry '%s'", pathname);
595 1.1 joerg ac(archive_read_data_skip(a));
596 1.1 joerg free(pathname);
597 1.1 joerg return;
598 1.1 joerg }
599 1.1 joerg
600 1.1 joerg /* skip directories in -j case */
601 1.1 joerg if (S_ISDIR(filetype) && j_opt) {
602 1.1 joerg ac(archive_read_data_skip(a));
603 1.1 joerg free(pathname);
604 1.1 joerg return;
605 1.1 joerg }
606 1.1 joerg
607 1.1 joerg /* apply include / exclude patterns */
608 1.1 joerg if (!accept_pathname(pathname)) {
609 1.1 joerg ac(archive_read_data_skip(a));
610 1.1 joerg free(pathname);
611 1.1 joerg return;
612 1.1 joerg }
613 1.1 joerg
614 1.1 joerg /* apply -j and -d */
615 1.1 joerg if (j_opt) {
616 1.1 joerg for (p = q = pathname; *p; ++p)
617 1.1 joerg if (*p == '/')
618 1.1 joerg q = p + 1;
619 1.1 joerg realpathname = pathcat(d_arg, q);
620 1.1 joerg } else {
621 1.1 joerg realpathname = pathcat(d_arg, pathname);
622 1.1 joerg }
623 1.1 joerg
624 1.1 joerg /* ensure that parent directory exists */
625 1.1 joerg make_parent(realpathname);
626 1.1 joerg
627 1.1 joerg if (S_ISDIR(filetype))
628 1.1 joerg extract_dir(a, e, realpathname);
629 1.1 joerg else
630 1.1 joerg extract_file(a, e, realpathname);
631 1.1 joerg
632 1.1 joerg free(realpathname);
633 1.1 joerg free(pathname);
634 1.1 joerg }
635 1.1 joerg
636 1.2 joerg static void
637 1.2 joerg extract_stdout(struct archive *a, struct archive_entry *e)
638 1.2 joerg {
639 1.2 joerg char *pathname;
640 1.2 joerg mode_t filetype;
641 1.2 joerg int cr, text, warn;
642 1.2 joerg ssize_t len;
643 1.2 joerg unsigned char *p, *q, *end;
644 1.2 joerg
645 1.2 joerg pathname = pathdup(archive_entry_pathname(e));
646 1.2 joerg filetype = archive_entry_filetype(e);
647 1.2 joerg
648 1.2 joerg /* I don't think this can happen in a zipfile.. */
649 1.2 joerg if (!S_ISDIR(filetype) && !S_ISREG(filetype)) {
650 1.2 joerg warningx("skipping non-regular entry '%s'", pathname);
651 1.2 joerg ac(archive_read_data_skip(a));
652 1.2 joerg free(pathname);
653 1.2 joerg return;
654 1.2 joerg }
655 1.2 joerg
656 1.2 joerg /* skip directories in -j case */
657 1.2 joerg if (S_ISDIR(filetype)) {
658 1.2 joerg ac(archive_read_data_skip(a));
659 1.2 joerg free(pathname);
660 1.2 joerg return;
661 1.2 joerg }
662 1.2 joerg
663 1.2 joerg /* apply include / exclude patterns */
664 1.2 joerg if (!accept_pathname(pathname)) {
665 1.2 joerg ac(archive_read_data_skip(a));
666 1.2 joerg free(pathname);
667 1.2 joerg return;
668 1.2 joerg }
669 1.2 joerg
670 1.3 joerg if (c_opt)
671 1.3 joerg info("x %s\n", pathname);
672 1.3 joerg
673 1.2 joerg text = a_opt;
674 1.2 joerg warn = 0;
675 1.2 joerg cr = 0;
676 1.2 joerg for (int n = 0; ; n++) {
677 1.2 joerg len = archive_read_data(a, buffer, sizeof buffer);
678 1.2 joerg
679 1.2 joerg if (len < 0)
680 1.2 joerg ac(len);
681 1.2 joerg
682 1.2 joerg /* left over CR from previous buffer */
683 1.2 joerg if (a_opt && cr) {
684 1.3 joerg if (len == 0 || buffer[0] != '\n') {
685 1.3 joerg if (fwrite("\r", 1, 1, stderr) != 1)
686 1.2 joerg error("write('%s')", pathname);
687 1.3 joerg }
688 1.2 joerg cr = 0;
689 1.2 joerg }
690 1.2 joerg
691 1.2 joerg /* EOF */
692 1.2 joerg if (len == 0)
693 1.2 joerg break;
694 1.2 joerg end = buffer + len;
695 1.2 joerg
696 1.2 joerg /*
697 1.2 joerg * Detect whether this is a text file. The correct way to
698 1.2 joerg * do this is to check the least significant bit of the
699 1.2 joerg * "internal file attributes" field of the corresponding
700 1.2 joerg * file header in the central directory, but libarchive
701 1.2 joerg * does not read the central directory, so we have to
702 1.2 joerg * guess by looking for non-ASCII characters in the
703 1.2 joerg * buffer. Hopefully we won't guess wrong. If we do
704 1.2 joerg * guess wrong, we print a warning message later.
705 1.2 joerg */
706 1.2 joerg if (a_opt && n == 0) {
707 1.2 joerg for (p = buffer; p < end; ++p) {
708 1.2 joerg if (!isascii((unsigned char)*p)) {
709 1.2 joerg text = 0;
710 1.2 joerg break;
711 1.2 joerg }
712 1.2 joerg }
713 1.2 joerg }
714 1.2 joerg
715 1.2 joerg /* simple case */
716 1.2 joerg if (!a_opt || !text) {
717 1.3 joerg if (fwrite(buffer, 1, len, stdout) != (size_t)len)
718 1.2 joerg error("write('%s')", pathname);
719 1.2 joerg continue;
720 1.2 joerg }
721 1.2 joerg
722 1.2 joerg /* hard case: convert \r\n to \n (sigh...) */
723 1.2 joerg for (p = buffer; p < end; p = q + 1) {
724 1.2 joerg for (q = p; q < end; q++) {
725 1.2 joerg if (!warn && !isascii(*q)) {
726 1.2 joerg warningx("%s may be corrupted due"
727 1.2 joerg " to weak text file detection"
728 1.2 joerg " heuristic", pathname);
729 1.2 joerg warn = 1;
730 1.2 joerg }
731 1.2 joerg if (q[0] != '\r')
732 1.2 joerg continue;
733 1.2 joerg if (&q[1] == end) {
734 1.2 joerg cr = 1;
735 1.2 joerg break;
736 1.2 joerg }
737 1.2 joerg if (q[1] == '\n')
738 1.2 joerg break;
739 1.2 joerg }
740 1.3 joerg if (fwrite(p, 1, q - p, stdout) != (size_t)(q - p))
741 1.2 joerg error("write('%s')", pathname);
742 1.2 joerg }
743 1.2 joerg }
744 1.2 joerg
745 1.2 joerg free(pathname);
746 1.2 joerg }
747 1.2 joerg
748 1.1 joerg /*
749 1.1 joerg * Print the name of an entry to stdout.
750 1.1 joerg */
751 1.1 joerg static void
752 1.1 joerg list(struct archive *a, struct archive_entry *e)
753 1.1 joerg {
754 1.2 joerg char buf[20];
755 1.2 joerg time_t mtime;
756 1.1 joerg
757 1.3 joerg mtime = archive_entry_mtime(e);
758 1.3 joerg strftime(buf, sizeof(buf), "%m-%d-%g %R", localtime(&mtime));
759 1.2 joerg
760 1.3 joerg if (v_opt == 1) {
761 1.3 joerg printf(" %8ju %s %s\n",
762 1.3 joerg (uintmax_t)archive_entry_size(e),
763 1.3 joerg buf, archive_entry_pathname(e));
764 1.3 joerg } else if (v_opt == 2) {
765 1.2 joerg printf("%8ju Stored %7ju 0%% %s %08x %s\n",
766 1.2 joerg (uintmax_t)archive_entry_size(e),
767 1.2 joerg (uintmax_t)archive_entry_size(e),
768 1.2 joerg buf,
769 1.2 joerg 0U,
770 1.2 joerg archive_entry_pathname(e));
771 1.2 joerg }
772 1.1 joerg ac(archive_read_data_skip(a));
773 1.1 joerg }
774 1.1 joerg
775 1.1 joerg /*
776 1.1 joerg * Extract to memory to check CRC
777 1.1 joerg */
778 1.1 joerg static void
779 1.1 joerg test(struct archive *a, struct archive_entry *e)
780 1.1 joerg {
781 1.1 joerg ssize_t len;
782 1.1 joerg
783 1.1 joerg if (S_ISDIR(archive_entry_filetype(e)))
784 1.1 joerg return;
785 1.1 joerg
786 1.1 joerg info("%s ", archive_entry_pathname(e));
787 1.1 joerg while ((len = archive_read_data(a, buffer, sizeof buffer)) > 0)
788 1.1 joerg /* nothing */;
789 1.1 joerg if (len < 0) {
790 1.1 joerg info("%s\n", archive_error_string(a));
791 1.1 joerg ++test_failed;
792 1.1 joerg } else {
793 1.1 joerg info("OK\n");
794 1.1 joerg }
795 1.1 joerg
796 1.1 joerg /* shouldn't be necessary, but it doesn't hurt */
797 1.1 joerg ac(archive_read_data_skip(a));
798 1.1 joerg }
799 1.1 joerg
800 1.1 joerg
801 1.1 joerg /*
802 1.1 joerg * Main loop: open the zipfile, iterate over its contents and decide what
803 1.1 joerg * to do with each entry.
804 1.1 joerg */
805 1.1 joerg static void
806 1.1 joerg unzip(const char *fn)
807 1.1 joerg {
808 1.1 joerg struct archive *a;
809 1.1 joerg struct archive_entry *e;
810 1.1 joerg int fd, ret;
811 1.3 joerg uintmax_t total_size, file_count;
812 1.1 joerg
813 1.1 joerg if ((fd = open(fn, O_RDONLY)) < 0)
814 1.1 joerg error("%s", fn);
815 1.1 joerg
816 1.1 joerg a = archive_read_new();
817 1.1 joerg ac(archive_read_support_format_zip(a));
818 1.1 joerg ac(archive_read_open_fd(a, fd, 8192));
819 1.1 joerg
820 1.3 joerg if (v_opt == 1) {
821 1.3 joerg printf(" Length Date Time Name\n");
822 1.3 joerg printf(" -------- ---- ---- ----\n");
823 1.3 joerg } else if (v_opt == 2) {
824 1.3 joerg printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
825 1.3 joerg printf("-------- ------ ------- ----- ---- ---- ------ ----\n");
826 1.3 joerg }
827 1.3 joerg
828 1.3 joerg total_size = 0;
829 1.3 joerg file_count = 0;
830 1.1 joerg for (;;) {
831 1.1 joerg ret = archive_read_next_header(a, &e);
832 1.1 joerg if (ret == ARCHIVE_EOF)
833 1.1 joerg break;
834 1.1 joerg ac(ret);
835 1.1 joerg if (t_opt)
836 1.1 joerg test(a, e);
837 1.2 joerg else if (v_opt)
838 1.1 joerg list(a, e);
839 1.3 joerg else if (p_opt || c_opt)
840 1.2 joerg extract_stdout(a, e);
841 1.1 joerg else
842 1.1 joerg extract(a, e);
843 1.3 joerg
844 1.3 joerg total_size += archive_entry_size(e);
845 1.3 joerg ++file_count;
846 1.3 joerg }
847 1.3 joerg
848 1.3 joerg if (v_opt == 1) {
849 1.3 joerg printf(" -------- -------\n");
850 1.3 joerg printf(" %8ju %ju file%s\n",
851 1.3 joerg total_size, file_count, file_count != 1 ? "s" : "");
852 1.3 joerg } else if (v_opt == 2) {
853 1.3 joerg printf("-------- ------- --- -------\n");
854 1.3 joerg printf("%8ju %7ju 0%% %8ju file%s\n",
855 1.3 joerg total_size, total_size, file_count,
856 1.3 joerg file_count != 1 ? "s" : "");
857 1.1 joerg }
858 1.1 joerg
859 1.1 joerg ac(archive_read_close(a));
860 1.1 joerg (void)archive_read_finish(a);
861 1.1 joerg if (close(fd) != 0)
862 1.1 joerg error("%s", fn);
863 1.1 joerg
864 1.1 joerg if (t_opt && test_failed)
865 1.1 joerg errorx("%d checksum error(s) found.", test_failed);
866 1.1 joerg }
867 1.1 joerg
868 1.1 joerg static void
869 1.1 joerg usage(void)
870 1.1 joerg {
871 1.1 joerg
872 1.1 joerg fprintf(stderr, "usage: unzip [-ajLlnoqtu] [-d dir] zipfile\n");
873 1.1 joerg exit(1);
874 1.1 joerg }
875 1.1 joerg
876 1.1 joerg static int
877 1.1 joerg getopts(int argc, char *argv[])
878 1.1 joerg {
879 1.1 joerg int opt;
880 1.1 joerg
881 1.1 joerg optreset = optind = 1;
882 1.3 joerg while ((opt = getopt(argc, argv, "acd:fjLlnopqtuvx:")) != -1)
883 1.1 joerg switch (opt) {
884 1.1 joerg case 'a':
885 1.1 joerg a_opt = 1;
886 1.1 joerg break;
887 1.3 joerg case 'c':
888 1.3 joerg c_opt = 1;
889 1.3 joerg break;
890 1.1 joerg case 'd':
891 1.1 joerg d_arg = optarg;
892 1.1 joerg break;
893 1.1 joerg case 'f':
894 1.1 joerg f_opt = 1;
895 1.1 joerg break;
896 1.1 joerg case 'j':
897 1.1 joerg j_opt = 1;
898 1.1 joerg break;
899 1.1 joerg case 'L':
900 1.1 joerg L_opt = 1;
901 1.1 joerg break;
902 1.1 joerg case 'l':
903 1.2 joerg if (v_opt == 0)
904 1.2 joerg v_opt = 1;
905 1.1 joerg break;
906 1.1 joerg case 'n':
907 1.1 joerg n_opt = 1;
908 1.1 joerg break;
909 1.1 joerg case 'o':
910 1.1 joerg o_opt = 1;
911 1.2 joerg q_opt = 1;
912 1.2 joerg break;
913 1.2 joerg case 'p':
914 1.2 joerg p_opt = 1;
915 1.1 joerg break;
916 1.1 joerg case 'q':
917 1.1 joerg q_opt = 1;
918 1.1 joerg break;
919 1.1 joerg case 't':
920 1.1 joerg t_opt = 1;
921 1.1 joerg break;
922 1.1 joerg case 'u':
923 1.1 joerg u_opt = 1;
924 1.1 joerg break;
925 1.2 joerg case 'v':
926 1.2 joerg v_opt = 2;
927 1.2 joerg break;
928 1.1 joerg case 'x':
929 1.1 joerg add_pattern(&exclude, optarg);
930 1.1 joerg break;
931 1.1 joerg default:
932 1.1 joerg usage();
933 1.1 joerg }
934 1.1 joerg
935 1.1 joerg return (optind);
936 1.1 joerg }
937 1.1 joerg
938 1.1 joerg int
939 1.1 joerg main(int argc, char *argv[])
940 1.1 joerg {
941 1.1 joerg const char *zipfile;
942 1.1 joerg int nopts;
943 1.1 joerg
944 1.1 joerg if (isatty(STDOUT_FILENO))
945 1.1 joerg tty = 1;
946 1.1 joerg
947 1.1 joerg if (getenv("UNZIP_DEBUG") != NULL)
948 1.1 joerg unzip_debug = 1;
949 1.1 joerg for (int i = 0; i < argc; ++i)
950 1.1 joerg debug("%s%c", argv[i], (i < argc - 1) ? ' ' : '\n');
951 1.1 joerg
952 1.1 joerg /*
953 1.1 joerg * Info-ZIP's unzip(1) expects certain options to come before the
954 1.1 joerg * zipfile name, and others to come after - though it does not
955 1.1 joerg * enforce this. For simplicity, we accept *all* options both
956 1.1 joerg * before and after the zipfile name.
957 1.1 joerg */
958 1.1 joerg nopts = getopts(argc, argv);
959 1.1 joerg
960 1.1 joerg if (argc <= nopts)
961 1.1 joerg usage();
962 1.1 joerg zipfile = argv[nopts++];
963 1.1 joerg
964 1.1 joerg while (nopts < argc && *argv[nopts] != '-')
965 1.1 joerg add_pattern(&include, argv[nopts++]);
966 1.1 joerg
967 1.1 joerg nopts--; /* fake argv[0] */
968 1.1 joerg nopts += getopts(argc - nopts, argv + nopts);
969 1.1 joerg
970 1.1 joerg if (n_opt + o_opt + u_opt > 1)
971 1.1 joerg errorx("-n, -o and -u are contradictory");
972 1.1 joerg
973 1.1 joerg time(&now);
974 1.1 joerg
975 1.1 joerg unzip(zipfile);
976 1.1 joerg
977 1.1 joerg exit(0);
978 1.1 joerg }
979