use utimes() rather than obsolescent utime()
[oweals/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 tarball for details.
6  */
7
8 #include "libbb.h"
9 #include "unarchive.h"
10
11 enum {
12         OPT_STDOUT = 0x1,
13         OPT_FORCE = 0x2,
14 /* gunzip and bunzip2 only: */
15         OPT_VERBOSE = 0x4,
16         OPT_DECOMPRESS = 0x8,
17         OPT_TEST = 0x10,
18 };
19
20 static
21 int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
22 {
23         int fd = open3_or_warn(filename, flags, mode);
24         if (fd < 0) {
25                 return 1;
26         }
27         xmove_fd(fd, to_fd);
28         return 0;
29 }
30
31 int FAST_FUNC bbunpack(char **argv,
32         char* (*make_new_name)(char *filename),
33         IF_DESKTOP(long long) int (*unpacker)(unpack_info_t *info)
34 )
35 {
36         struct stat stat_buf;
37         IF_DESKTOP(long long) int status;
38         char *filename, *new_name;
39         smallint exitcode = 0;
40         unpack_info_t info;
41
42         do {
43                 /* NB: new_name is *maybe* malloc'ed! */
44                 new_name = NULL;
45                 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
46
47                 if (filename && LONE_DASH(filename))
48                         filename = NULL;
49
50                 /* Open src */
51                 if (filename) {
52                         if (stat(filename, &stat_buf) != 0) {
53                                 bb_simple_perror_msg(filename);
54  err:
55                                 exitcode = 1;
56                                 goto free_name;
57                         }
58                         if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
59                                 goto err;
60                 }
61
62                 /* Special cases: test, stdout */
63                 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
64                         if (option_mask32 & OPT_TEST)
65                                 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
66                                         goto err;
67                         filename = NULL;
68                 }
69
70                 /* Open dst if we are going to unpack to file */
71                 if (filename) {
72                         new_name = make_new_name(filename);
73                         if (!new_name) {
74                                 bb_error_msg("%s: unknown suffix - ignored", filename);
75                                 goto err;
76                         }
77
78                         /* -f: overwrite existing output files */
79                         if (option_mask32 & OPT_FORCE) {
80                                 unlink(new_name);
81                         }
82
83                         /* O_EXCL: "real" bunzip2 doesn't overwrite files */
84                         /* GNU gunzip does not bail out, but goes to next file */
85                         if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
86                                         stat_buf.st_mode))
87                                 goto err;
88                 }
89
90                 /* Check that the input is sane */
91                 if (isatty(STDIN_FILENO) && (option_mask32 & OPT_FORCE) == 0) {
92                         bb_error_msg_and_die("compressed data not read from terminal, "
93                                         "use -f to force it");
94                 }
95
96                 /* memset(&info, 0, sizeof(info)); */
97                 info.mtime = 0; /* so far it has one member only */
98                 status = unpacker(&info);
99                 if (status < 0)
100                         exitcode = 1;
101                 xclose(STDOUT_FILENO); /* with error check! */
102
103                 if (filename) {
104                         char *del = new_name;
105                         if (status >= 0) {
106                                 /* TODO: restore other things? */
107                                 if (info.mtime) {
108                                         struct timeval times = {.tv_sec = info.mtime,
109                                                                                         .tv_usec = 0};
110                                         /* Note: we closed it first.
111                                          * On some systems calling utime
112                                          * then closing resets the mtime
113                                          * back to current time. */
114                                         utimes(new_name, &times); /* ignoring errors */
115                                 }
116
117                                 /* Delete _compressed_ file */
118                                 del = filename;
119                                 /* restore extension (unless tgz -> tar case) */
120                                 if (new_name == filename)
121                                         filename[strlen(filename)] = '.';
122                         }
123                         xunlink(del);
124
125 #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
126                         /* Extreme bloat for gunzip compat */
127                         if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
128                                 fprintf(stderr, "%s: %u%% - replaced with %s\n",
129                                         filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
130                         }
131 #endif
132
133  free_name:
134                         if (new_name != filename)
135                                 free(new_name);
136                 }
137         } while (*argv && *++argv);
138
139         return exitcode;
140 }
141
142 #if ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNCOMPRESS
143
144 static
145 char* make_new_name_generic(char *filename, const char *expected_ext)
146 {
147         char *extension = strrchr(filename, '.');
148         if (!extension || strcmp(extension + 1, expected_ext) != 0) {
149                 /* Mimic GNU gunzip - "real" bunzip2 tries to */
150                 /* unpack file anyway, to file.out */
151                 return NULL;
152         }
153         *extension = '\0';
154         return filename;
155 }
156
157 #endif
158
159
160 /*
161  *  Modified for busybox by Glenn McGrath
162  *  Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
163  *
164  *  Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
165  */
166
167 #if ENABLE_BUNZIP2
168
169 static
170 char* make_new_name_bunzip2(char *filename)
171 {
172         return make_new_name_generic(filename, "bz2");
173 }
174
175 static
176 IF_DESKTOP(long long) int unpack_bunzip2(unpack_info_t *info UNUSED_PARAM)
177 {
178         return unpack_bz2_stream_prime(STDIN_FILENO, STDOUT_FILENO);
179 }
180
181 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
182 int bunzip2_main(int argc UNUSED_PARAM, char **argv)
183 {
184         getopt32(argv, "cfvdt");
185         argv += optind;
186         if (applet_name[2] == 'c')
187                 option_mask32 |= OPT_STDOUT;
188
189         return bbunpack(argv, make_new_name_bunzip2, unpack_bunzip2);
190 }
191
192 #endif
193
194
195 /*
196  * Gzip implementation for busybox
197  *
198  * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
199  *
200  * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
201  * based on gzip sources
202  *
203  * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
204  * well as stdin/stdout, and to generally behave itself wrt command line
205  * handling.
206  *
207  * General cleanup to better adhere to the style guide and make use of standard
208  * busybox functions by Glenn McGrath
209  *
210  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
211  *
212  * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
213  * Copyright (C) 1992-1993 Jean-loup Gailly
214  * The unzip code was written and put in the public domain by Mark Adler.
215  * Portions of the lzw code are derived from the public domain 'compress'
216  * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
217  * Ken Turkowski, Dave Mack and Peter Jannesen.
218  *
219  * See the license_msg below and the file COPYING for the software license.
220  * See the file algorithm.doc for the compression algorithms and file formats.
221  */
222
223 #if ENABLE_GUNZIP
224
225 static
226 char* make_new_name_gunzip(char *filename)
227 {
228         char *extension = strrchr(filename, '.');
229
230         if (!extension)
231                 return NULL;
232
233         extension++;
234         if (strcmp(extension, "tgz" + 1) == 0
235 #if ENABLE_FEATURE_SEAMLESS_Z
236          || (extension[0] == 'Z' && extension[1] == '\0')
237 #endif
238         ) {
239                 extension[-1] = '\0';
240         } else if (strcmp(extension, "tgz") == 0) {
241                 filename = xstrdup(filename);
242                 extension = strrchr(filename, '.');
243                 extension[2] = 'a';
244                 extension[3] = 'r';
245         } else {
246                 return NULL;
247         }
248         return filename;
249 }
250
251 static
252 IF_DESKTOP(long long) int unpack_gunzip(unpack_info_t *info)
253 {
254         IF_DESKTOP(long long) int status = -1;
255
256         /* do the decompression, and cleanup */
257         if (xread_char(STDIN_FILENO) == 0x1f) {
258                 unsigned char magic2;
259
260                 magic2 = xread_char(STDIN_FILENO);
261                 if (ENABLE_FEATURE_SEAMLESS_Z && magic2 == 0x9d) {
262                         status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
263                 } else if (magic2 == 0x8b) {
264                         status = unpack_gz_stream_with_info(STDIN_FILENO, STDOUT_FILENO, info);
265                 } else {
266                         goto bad_magic;
267                 }
268                 if (status < 0) {
269                         bb_error_msg("error inflating");
270                 }
271         } else {
272  bad_magic:
273                 bb_error_msg("invalid magic");
274                 /* status is still == -1 */
275         }
276         return status;
277 }
278
279 /*
280  * Linux kernel build uses gzip -d -n. We accept and ignore it.
281  * Man page says:
282  * -n --no-name
283  * gzip: do not save the original file name and time stamp.
284  * (The original name is always saved if the name had to be truncated.)
285  * gunzip: do not restore the original file name/time even if present
286  * (remove only the gzip suffix from the compressed file name).
287  * This option is the default when decompressing.
288  * -N --name
289  * gzip: always save the original file name and time stamp (this is the default)
290  * gunzip: restore the original file name and time stamp if present.
291  */
292
293 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
294 int gunzip_main(int argc UNUSED_PARAM, char **argv)
295 {
296         getopt32(argv, "cfvdtn");
297         argv += optind;
298         /* if called as zcat */
299         if (applet_name[1] == 'c')
300                 option_mask32 |= OPT_STDOUT;
301
302         return bbunpack(argv, make_new_name_gunzip, unpack_gunzip);
303 }
304
305 #endif
306
307
308 /*
309  * Small lzma deflate implementation.
310  * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
311  *
312  * Based on bunzip.c from busybox
313  *
314  * Licensed under GPL v2, see file LICENSE in this tarball for details.
315  */
316
317 #if ENABLE_UNLZMA
318
319 static
320 char* make_new_name_unlzma(char *filename)
321 {
322         return make_new_name_generic(filename, "lzma");
323 }
324
325 static
326 IF_DESKTOP(long long) int unpack_unlzma(unpack_info_t *info UNUSED_PARAM)
327 {
328         return unpack_lzma_stream(STDIN_FILENO, STDOUT_FILENO);
329 }
330
331 int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
332 int unlzma_main(int argc UNUSED_PARAM, char **argv)
333 {
334         getopt32(argv, "cf");
335         argv += optind;
336         /* lzmacat? */
337         if (applet_name[4] == 'c')
338                 option_mask32 |= OPT_STDOUT;
339
340         return bbunpack(argv, make_new_name_unlzma, unpack_unlzma);
341 }
342
343 #endif
344
345
346 /*
347  *      Uncompress applet for busybox (c) 2002 Glenn McGrath
348  *
349  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
350  */
351
352 #if ENABLE_UNCOMPRESS
353
354 static
355 char* make_new_name_uncompress(char *filename)
356 {
357         return make_new_name_generic(filename, "Z");
358 }
359
360 static
361 IF_DESKTOP(long long) int unpack_uncompress(unpack_info_t *info UNUSED_PARAM)
362 {
363         IF_DESKTOP(long long) int status = -1;
364
365         if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
366                 bb_error_msg("invalid magic");
367         } else {
368                 status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
369         }
370         return status;
371 }
372
373 int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
374 int uncompress_main(int argc UNUSED_PARAM, char **argv)
375 {
376         getopt32(argv, "cf");
377         argv += optind;
378
379         return bbunpack(argv, make_new_name_uncompress, unpack_uncompress);
380 }
381
382 #endif