archival: note implicit dependencies between lzop & bbunzip
[platform/upstream/busybox.git] / archival / bbunzip.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Common code for gunzip-like applets
4  *
5  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6  */
7 #include "libbb.h"
8 #include "bb_archive.h"
9
10 /* Note: must be kept in sync with archival/lzop.c */
11 enum {
12         OPT_STDOUT     = 1 << 0,
13         OPT_FORCE      = 1 << 1,
14         /* only some decompressors: */
15         OPT_VERBOSE    = 1 << 2,
16         OPT_QUIET      = 1 << 3,
17         OPT_DECOMPRESS = 1 << 4,
18         OPT_TEST       = 1 << 5,
19         SEAMLESS_MAGIC = (1 << 31) * SEAMLESS_COMPRESSION,
20 };
21
22 static
23 int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
24 {
25         int fd = open3_or_warn(filename, flags, mode);
26         if (fd < 0) {
27                 return 1;
28         }
29         xmove_fd(fd, to_fd);
30         return 0;
31 }
32
33 char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
34 {
35         return xasprintf("%s.%s", filename, expected_ext);
36 }
37
38 int FAST_FUNC bbunpack(char **argv,
39         IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_aux_data_t *aux),
40         char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
41         const char *expected_ext
42 )
43 {
44         struct stat stat_buf;
45         IF_DESKTOP(long long) int status = 0;
46         char *filename, *new_name;
47         smallint exitcode = 0;
48         transformer_aux_data_t aux;
49
50         do {
51                 /* NB: new_name is *maybe* malloc'ed! */
52                 new_name = NULL;
53                 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
54
55                 if (filename && LONE_DASH(filename))
56                         filename = NULL;
57
58                 /* Open src */
59                 if (filename) {
60                         if (!(option_mask32 & SEAMLESS_MAGIC)) {
61                                 if (stat(filename, &stat_buf) != 0) {
62  err_name:
63                                         bb_simple_perror_msg(filename);
64  err:
65                                         exitcode = 1;
66                                         goto free_name;
67                                 }
68                                 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
69                                         goto err;
70                         } else {
71                                 /* "clever zcat" with FILE */
72                                 int fd = open_zipped(filename);
73                                 if (fd < 0)
74                                         goto err_name;
75                                 xmove_fd(fd, STDIN_FILENO);
76                         }
77                 } else
78                 if (option_mask32 & SEAMLESS_MAGIC) {
79                         /* "clever zcat" on stdin */
80                         if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_detected*/ 0))
81                                 goto err;
82                 }
83
84                 /* Special cases: test, stdout */
85                 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
86                         if (option_mask32 & OPT_TEST)
87                                 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
88                                         xfunc_die();
89                         filename = NULL;
90                 }
91
92                 /* Open dst if we are going to unpack to file */
93                 if (filename) {
94                         new_name = make_new_name(filename, expected_ext);
95                         if (!new_name) {
96                                 bb_error_msg("%s: unknown suffix - ignored", filename);
97                                 goto err;
98                         }
99
100                         /* -f: overwrite existing output files */
101                         if (option_mask32 & OPT_FORCE) {
102                                 unlink(new_name);
103                         }
104
105                         /* O_EXCL: "real" bunzip2 doesn't overwrite files */
106                         /* GNU gunzip does not bail out, but goes to next file */
107                         if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
108                                         stat_buf.st_mode))
109                                 goto err;
110                 }
111
112                 /* Check that the input is sane */
113                 if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
114                         bb_error_msg_and_die("compressed data not read from terminal, "
115                                         "use -f to force it");
116                 }
117
118                 if (!(option_mask32 & SEAMLESS_MAGIC)) {
119                         init_transformer_aux_data(&aux);
120                         aux.check_signature = 1;
121                         status = unpacker(&aux);
122                         if (status < 0)
123                                 exitcode = 1;
124                 } else {
125                         if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
126                                 /* Disk full, tty closed, etc. No point in continuing */
127                                 xfunc_die();
128                 }
129
130                 if (!(option_mask32 & OPT_STDOUT))
131                         xclose(STDOUT_FILENO); /* with error check! */
132
133                 if (filename) {
134                         char *del = new_name;
135                         if (status >= 0) {
136                                 /* TODO: restore other things? */
137                                 if (aux.mtime != 0) {
138                                         struct timeval times[2];
139
140                                         times[1].tv_sec = times[0].tv_sec = aux.mtime;
141                                         times[1].tv_usec = times[0].tv_usec = 0;
142                                         /* Note: we closed it first.
143                                          * On some systems calling utimes
144                                          * then closing resets the mtime
145                                          * back to current time. */
146                                         utimes(new_name, times); /* ignoring errors */
147                                 }
148
149                                 /* Delete _compressed_ file */
150                                 del = filename;
151                                 /* restore extension (unless tgz -> tar case) */
152                                 if (new_name == filename)
153                                         filename[strlen(filename)] = '.';
154                         }
155                         xunlink(del);
156
157 #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
158                         /* Extreme bloat for gunzip compat */
159                         if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
160                                 fprintf(stderr, "%s: %u%% - replaced with %s\n",
161                                         filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
162                         }
163 #endif
164
165  free_name:
166                         if (new_name != filename)
167                                 free(new_name);
168                 }
169         } while (*argv && *++argv);
170
171         if (option_mask32 & OPT_STDOUT)
172                 xclose(STDOUT_FILENO); /* with error check! */
173
174         return exitcode;
175 }
176
177 #if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
178 static
179 char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
180 {
181         char *extension = strrchr(filename, '.');
182         if (!extension || strcmp(extension + 1, expected_ext) != 0) {
183                 /* Mimic GNU gunzip - "real" bunzip2 tries to */
184                 /* unpack file anyway, to file.out */
185                 return NULL;
186         }
187         *extension = '\0';
188         return filename;
189 }
190 #endif
191
192
193 /*
194  * Uncompress applet for busybox (c) 2002 Glenn McGrath
195  *
196  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
197  */
198
199 //usage:#define uncompress_trivial_usage
200 //usage:       "[-cf] [FILE]..."
201 //usage:#define uncompress_full_usage "\n\n"
202 //usage:       "Decompress .Z file[s]\n"
203 //usage:     "\n        -c      Write to stdout"
204 //usage:     "\n        -f      Overwrite"
205
206 #if ENABLE_UNCOMPRESS
207 static
208 IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux)
209 {
210         return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO);
211 }
212 int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
213 int uncompress_main(int argc UNUSED_PARAM, char **argv)
214 {
215         getopt32(argv, "cf");
216         argv += optind;
217
218         return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
219 }
220 #endif
221
222
223 /*
224  * Gzip implementation for busybox
225  *
226  * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
227  *
228  * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
229  * based on gzip sources
230  *
231  * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
232  * well as stdin/stdout, and to generally behave itself wrt command line
233  * handling.
234  *
235  * General cleanup to better adhere to the style guide and make use of standard
236  * busybox functions by Glenn McGrath
237  *
238  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
239  *
240  * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
241  * Copyright (C) 1992-1993 Jean-loup Gailly
242  * The unzip code was written and put in the public domain by Mark Adler.
243  * Portions of the lzw code are derived from the public domain 'compress'
244  * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
245  * Ken Turkowski, Dave Mack and Peter Jannesen.
246  *
247  * See the license_msg below and the file COPYING for the software license.
248  * See the file algorithm.doc for the compression algorithms and file formats.
249  */
250
251 //usage:#define gunzip_trivial_usage
252 //usage:       "[-cft] [FILE]..."
253 //usage:#define gunzip_full_usage "\n\n"
254 //usage:       "Decompress FILEs (or stdin)\n"
255 //usage:     "\n        -c      Write to stdout"
256 //usage:     "\n        -f      Force"
257 //usage:     "\n        -t      Test file integrity"
258 //usage:
259 //usage:#define gunzip_example_usage
260 //usage:       "$ ls -la /tmp/BusyBox*\n"
261 //usage:       "-rw-rw-r--    1 andersen andersen   557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
262 //usage:       "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
263 //usage:       "$ ls -la /tmp/BusyBox*\n"
264 //usage:       "-rw-rw-r--    1 andersen andersen  1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
265 //usage:
266 //usage:#define zcat_trivial_usage
267 //usage:       "[FILE]..."
268 //usage:#define zcat_full_usage "\n\n"
269 //usage:       "Decompress to stdout"
270
271 #if ENABLE_GUNZIP
272 static
273 char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
274 {
275         char *extension = strrchr(filename, '.');
276
277         if (!extension)
278                 return NULL;
279
280         extension++;
281         if (strcmp(extension, "tgz" + 1) == 0
282 #if ENABLE_FEATURE_SEAMLESS_Z
283          || (extension[0] == 'Z' && extension[1] == '\0')
284 #endif
285         ) {
286                 extension[-1] = '\0';
287         } else if (strcmp(extension, "tgz") == 0) {
288                 filename = xstrdup(filename);
289                 extension = strrchr(filename, '.');
290                 extension[2] = 'a';
291                 extension[3] = 'r';
292         } else {
293                 return NULL;
294         }
295         return filename;
296 }
297 static
298 IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
299 {
300         return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
301 }
302 /*
303  * Linux kernel build uses gzip -d -n. We accept and ignore it.
304  * Man page says:
305  * -n --no-name
306  * gzip: do not save the original file name and time stamp.
307  * (The original name is always saved if the name had to be truncated.)
308  * gunzip: do not restore the original file name/time even if present
309  * (remove only the gzip suffix from the compressed file name).
310  * This option is the default when decompressing.
311  * -N --name
312  * gzip: always save the original file name and time stamp (this is the default)
313  * gunzip: restore the original file name and time stamp if present.
314  */
315 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
316 int gunzip_main(int argc UNUSED_PARAM, char **argv)
317 {
318         getopt32(argv, "cfvqdtn");
319         argv += optind;
320
321         /* If called as zcat...
322          * Normally, "zcat" is just "gunzip -c".
323          * But if seamless magic is enabled, then we are much more clever.
324          */
325         if (applet_name[1] == 'c')
326                 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
327
328         return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
329 }
330 #endif
331
332
333 /*
334  * Modified for busybox by Glenn McGrath
335  * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
336  *
337  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
338  */
339 //usage:#define bunzip2_trivial_usage
340 //usage:       "[-cf] [FILE]..."
341 //usage:#define bunzip2_full_usage "\n\n"
342 //usage:       "Decompress FILEs (or stdin)\n"
343 //usage:     "\n        -c      Write to stdout"
344 //usage:     "\n        -f      Force"
345 //usage:#define bzcat_trivial_usage
346 //usage:       "[FILE]..."
347 //usage:#define bzcat_full_usage "\n\n"
348 //usage:       "Decompress to stdout"
349 //applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
350 //applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
351 #if ENABLE_BUNZIP2
352 static
353 IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux)
354 {
355         return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO);
356 }
357 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
358 int bunzip2_main(int argc UNUSED_PARAM, char **argv)
359 {
360         getopt32(argv, "cfvqdt");
361         argv += optind;
362         if (applet_name[2] == 'c') /* bzcat */
363                 option_mask32 |= OPT_STDOUT;
364
365         return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
366 }
367 #endif
368
369
370 /*
371  * Small lzma deflate implementation.
372  * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
373  *
374  * Based on bunzip.c from busybox
375  *
376  * Licensed under GPLv2, see file LICENSE in this source tree.
377  */
378
379 //usage:#define unlzma_trivial_usage
380 //usage:       "[-cf] [FILE]..."
381 //usage:#define unlzma_full_usage "\n\n"
382 //usage:       "Decompress FILE (or stdin)\n"
383 //usage:     "\n        -c      Write to stdout"
384 //usage:     "\n        -f      Force"
385 //usage:
386 //usage:#define lzma_trivial_usage
387 //usage:       "-d [-cf] [FILE]..."
388 //usage:#define lzma_full_usage "\n\n"
389 //usage:       "Decompress FILE (or stdin)\n"
390 //usage:     "\n        -d      Decompress"
391 //usage:     "\n        -c      Write to stdout"
392 //usage:     "\n        -f      Force"
393 //usage:
394 //usage:#define lzcat_trivial_usage
395 //usage:       "[FILE]..."
396 //usage:#define lzcat_full_usage "\n\n"
397 //usage:       "Decompress to stdout"
398 //usage:
399 //usage:#define unxz_trivial_usage
400 //usage:       "[-cf] [FILE]..."
401 //usage:#define unxz_full_usage "\n\n"
402 //usage:       "Decompress FILE (or stdin)\n"
403 //usage:     "\n        -c      Write to stdout"
404 //usage:     "\n        -f      Force"
405 //usage:
406 //usage:#define xz_trivial_usage
407 //usage:       "-d [-cf] [FILE]..."
408 //usage:#define xz_full_usage "\n\n"
409 //usage:       "Decompress FILE (or stdin)\n"
410 //usage:     "\n        -d      Decompress"
411 //usage:     "\n        -c      Write to stdout"
412 //usage:     "\n        -f      Force"
413 //usage:
414 //usage:#define xzcat_trivial_usage
415 //usage:       "[FILE]..."
416 //usage:#define xzcat_full_usage "\n\n"
417 //usage:       "Decompress to stdout"
418
419 #if ENABLE_UNLZMA
420 static
421 IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux)
422 {
423         return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO);
424 }
425 int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
426 int unlzma_main(int argc UNUSED_PARAM, char **argv)
427 {
428         IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
429 # if ENABLE_LZMA
430         /* lzma without -d or -t? */
431         if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
432                 bb_show_usage();
433 # endif
434         /* lzcat? */
435         if (applet_name[2] == 'c')
436                 option_mask32 |= OPT_STDOUT;
437
438         argv += optind;
439         return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
440 }
441 #endif
442
443
444 #if ENABLE_UNXZ
445 static
446 IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux)
447 {
448         return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
449 }
450 int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
451 int unxz_main(int argc UNUSED_PARAM, char **argv)
452 {
453         IF_XZ(int opts =) getopt32(argv, "cfvqdt");
454 # if ENABLE_XZ
455         /* xz without -d or -t? */
456         if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
457                 bb_show_usage();
458 # endif
459         /* xzcat? */
460         if (applet_name[2] == 'c')
461                 option_mask32 |= OPT_STDOUT;
462
463         argv += optind;
464         return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");
465 }
466 #endif