2007-03-08 03:32:23 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2007-03-11 16:26:37 +05:30
|
|
|
* Common code for gunzip-like applets
|
2007-03-08 03:32:23 +05:30
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2007-03-08 03:32:23 +05:30
|
|
|
#include "unarchive.h"
|
|
|
|
|
|
|
|
enum {
|
2007-03-14 05:37:51 +05:30
|
|
|
OPT_STDOUT = 0x1,
|
|
|
|
OPT_FORCE = 0x2,
|
2007-10-22 00:29:58 +05:30
|
|
|
/* gunzip and bunzip2 only: */
|
2007-03-14 05:37:51 +05:30
|
|
|
OPT_VERBOSE = 0x4,
|
|
|
|
OPT_DECOMPRESS = 0x8,
|
|
|
|
OPT_TEST = 0x10,
|
2007-03-08 03:32:23 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
static
|
|
|
|
int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
|
|
|
|
{
|
2007-04-12 04:50:53 +05:30
|
|
|
int fd = open3_or_warn(filename, flags, mode);
|
2007-03-08 03:32:23 +05:30
|
|
|
if (fd < 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
2007-04-12 04:50:53 +05:30
|
|
|
xmove_fd(fd, to_fd);
|
2007-03-08 03:32:23 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC bbunpack(char **argv,
|
2007-03-08 03:32:23 +05:30
|
|
|
char* (*make_new_name)(char *filename),
|
|
|
|
USE_DESKTOP(long long) int (*unpacker)(void)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct stat stat_buf;
|
|
|
|
USE_DESKTOP(long long) int status;
|
2007-03-08 04:52:47 +05:30
|
|
|
char *filename, *new_name;
|
2007-03-08 03:32:23 +05:30
|
|
|
smallint exitcode = 0;
|
|
|
|
|
|
|
|
do {
|
2007-03-08 04:52:47 +05:30
|
|
|
/* NB: new_name is *maybe* malloc'ed! */
|
|
|
|
new_name = NULL;
|
2007-03-08 03:32:23 +05:30
|
|
|
filename = *argv; /* can be NULL - 'streaming' bunzip2 */
|
2007-03-08 04:52:47 +05:30
|
|
|
|
2007-03-08 03:32:23 +05:30
|
|
|
if (filename && LONE_DASH(filename))
|
|
|
|
filename = NULL;
|
|
|
|
|
|
|
|
/* Open src */
|
|
|
|
if (filename) {
|
|
|
|
if (stat(filename, &stat_buf) != 0) {
|
2007-10-01 17:28:38 +05:30
|
|
|
bb_simple_perror_msg(filename);
|
2007-03-08 03:32:23 +05:30
|
|
|
err:
|
|
|
|
exitcode = 1;
|
|
|
|
goto free_name;
|
|
|
|
}
|
|
|
|
if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Special cases: test, stdout */
|
|
|
|
if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
|
|
|
|
if (option_mask32 & OPT_TEST)
|
|
|
|
if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
|
|
|
|
goto err;
|
|
|
|
filename = NULL;
|
|
|
|
}
|
|
|
|
|
2007-03-08 04:52:47 +05:30
|
|
|
/* Open dst if we are going to unpack to file */
|
2007-03-08 03:32:23 +05:30
|
|
|
if (filename) {
|
|
|
|
new_name = make_new_name(filename);
|
|
|
|
if (!new_name) {
|
|
|
|
bb_error_msg("%s: unknown suffix - ignored", filename);
|
|
|
|
goto err;
|
|
|
|
}
|
2008-06-26 02:24:45 +05:30
|
|
|
|
|
|
|
/* -f: overwrite existing output files */
|
|
|
|
if (option_mask32 & OPT_FORCE) {
|
|
|
|
unlink(new_name);
|
|
|
|
}
|
|
|
|
|
2007-03-08 04:52:47 +05:30
|
|
|
/* O_EXCL: "real" bunzip2 doesn't overwrite files */
|
2007-10-05 20:56:08 +05:30
|
|
|
/* GNU gunzip does not bail out, but goes to next file */
|
2007-03-08 03:32:23 +05:30
|
|
|
if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
|
|
|
|
stat_buf.st_mode))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2007-03-08 04:52:47 +05:30
|
|
|
/* Check that the input is sane */
|
2007-03-08 03:32:23 +05:30
|
|
|
if (isatty(STDIN_FILENO) && (option_mask32 & OPT_FORCE) == 0) {
|
|
|
|
bb_error_msg_and_die("compressed data not read from terminal, "
|
|
|
|
"use -f to force it");
|
|
|
|
}
|
|
|
|
|
|
|
|
status = unpacker();
|
|
|
|
if (status < 0)
|
|
|
|
exitcode = 1;
|
|
|
|
|
|
|
|
if (filename) {
|
|
|
|
char *del = new_name;
|
|
|
|
if (status >= 0) {
|
|
|
|
/* TODO: restore user/group/times here? */
|
2007-03-08 04:52:47 +05:30
|
|
|
/* Delete _compressed_ file */
|
2007-03-08 03:32:23 +05:30
|
|
|
del = filename;
|
2007-03-08 04:52:47 +05:30
|
|
|
/* restore extension (unless tgz -> tar case) */
|
2007-03-08 03:32:23 +05:30
|
|
|
if (new_name == filename)
|
|
|
|
filename[strlen(filename)] = '.';
|
|
|
|
}
|
2007-04-06 02:55:15 +05:30
|
|
|
xunlink(del);
|
2007-03-08 04:52:47 +05:30
|
|
|
|
2007-03-08 03:32:23 +05:30
|
|
|
#if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
|
|
|
|
/* Extreme bloat for gunzip compat */
|
|
|
|
if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
|
|
|
|
fprintf(stderr, "%s: %u%% - replaced with %s\n",
|
|
|
|
filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
|
|
|
|
}
|
|
|
|
#endif
|
2007-03-08 04:52:47 +05:30
|
|
|
|
2007-03-08 03:32:23 +05:30
|
|
|
free_name:
|
|
|
|
if (new_name != filename)
|
|
|
|
free(new_name);
|
|
|
|
}
|
|
|
|
} while (*argv && *++argv);
|
|
|
|
|
|
|
|
return exitcode;
|
|
|
|
}
|
|
|
|
|
2007-03-11 16:26:37 +05:30
|
|
|
#if ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNCOMPRESS
|
|
|
|
|
2007-03-08 03:32:23 +05:30
|
|
|
static
|
2007-03-10 02:19:55 +05:30
|
|
|
char* make_new_name_generic(char *filename, const char *expected_ext)
|
2007-03-08 03:32:23 +05:30
|
|
|
{
|
|
|
|
char *extension = strrchr(filename, '.');
|
2007-03-10 02:19:55 +05:30
|
|
|
if (!extension || strcmp(extension + 1, expected_ext) != 0) {
|
2007-03-08 04:52:47 +05:30
|
|
|
/* Mimic GNU gunzip - "real" bunzip2 tries to */
|
|
|
|
/* unpack file anyway, to file.out */
|
2007-03-08 03:32:23 +05:30
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*extension = '\0';
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2007-03-11 16:26:37 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2007-09-21 18:46:32 +05:30
|
|
|
* Modified for busybox by Glenn McGrath
|
2007-03-11 16:26:37 +05:30
|
|
|
* Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
2007-03-10 02:19:55 +05:30
|
|
|
#if ENABLE_BUNZIP2
|
|
|
|
|
|
|
|
static
|
|
|
|
char* make_new_name_bunzip2(char *filename)
|
|
|
|
{
|
|
|
|
return make_new_name_generic(filename, "bz2");
|
|
|
|
}
|
|
|
|
|
2007-03-08 03:32:23 +05:30
|
|
|
static
|
|
|
|
USE_DESKTOP(long long) int unpack_bunzip2(void)
|
|
|
|
{
|
2008-08-05 18:40:34 +05:30
|
|
|
return unpack_bz2_stream_prime(STDIN_FILENO, STDOUT_FILENO);
|
2007-03-08 03:32:23 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int bunzip2_main(int argc UNUSED_PARAM, char **argv)
|
2007-03-08 03:32:23 +05:30
|
|
|
{
|
2007-10-22 00:29:58 +05:30
|
|
|
getopt32(argv, "cfvdt");
|
2007-03-08 03:32:23 +05:30
|
|
|
argv += optind;
|
|
|
|
if (applet_name[2] == 'c')
|
|
|
|
option_mask32 |= OPT_STDOUT;
|
|
|
|
|
2007-03-14 05:37:51 +05:30
|
|
|
return bbunpack(argv, make_new_name_bunzip2, unpack_bunzip2);
|
2007-03-08 03:32:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Gzip implementation for busybox
|
|
|
|
*
|
|
|
|
* Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
|
|
|
|
*
|
|
|
|
* Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
|
|
|
|
* based on gzip sources
|
|
|
|
*
|
|
|
|
* Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
|
|
|
|
* well as stdin/stdout, and to generally behave itself wrt command line
|
|
|
|
* handling.
|
|
|
|
*
|
|
|
|
* General cleanup to better adhere to the style guide and make use of standard
|
2007-09-21 18:46:32 +05:30
|
|
|
* busybox functions by Glenn McGrath
|
2007-03-08 03:32:23 +05:30
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
|
|
*
|
|
|
|
* gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
|
|
|
|
* Copyright (C) 1992-1993 Jean-loup Gailly
|
|
|
|
* The unzip code was written and put in the public domain by Mark Adler.
|
|
|
|
* Portions of the lzw code are derived from the public domain 'compress'
|
|
|
|
* written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
|
|
|
|
* Ken Turkowski, Dave Mack and Peter Jannesen.
|
|
|
|
*
|
|
|
|
* See the license_msg below and the file COPYING for the software license.
|
|
|
|
* See the file algorithm.doc for the compression algorithms and file formats.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if ENABLE_GUNZIP
|
|
|
|
|
|
|
|
static
|
|
|
|
char* make_new_name_gunzip(char *filename)
|
|
|
|
{
|
|
|
|
char *extension = strrchr(filename, '.');
|
|
|
|
|
|
|
|
if (!extension)
|
|
|
|
return NULL;
|
|
|
|
|
2007-03-10 02:19:55 +05:30
|
|
|
extension++;
|
|
|
|
if (strcmp(extension, "tgz" + 1) == 0
|
2008-08-05 18:40:34 +05:30
|
|
|
#if ENABLE_FEATURE_SEAMLESS_Z
|
|
|
|
|| (extension[0] == 'Z' && extension[1] == '\0')
|
2007-03-08 03:32:23 +05:30
|
|
|
#endif
|
|
|
|
) {
|
2007-03-10 02:19:55 +05:30
|
|
|
extension[-1] = '\0';
|
2007-04-12 06:02:05 +05:30
|
|
|
} else if (strcmp(extension, "tgz") == 0) {
|
2007-03-08 03:32:23 +05:30
|
|
|
filename = xstrdup(filename);
|
|
|
|
extension = strrchr(filename, '.');
|
|
|
|
extension[2] = 'a';
|
|
|
|
extension[3] = 'r';
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
USE_DESKTOP(long long) int unpack_gunzip(void)
|
|
|
|
{
|
|
|
|
USE_DESKTOP(long long) int status = -1;
|
|
|
|
|
|
|
|
/* do the decompression, and cleanup */
|
|
|
|
if (xread_char(STDIN_FILENO) == 0x1f) {
|
|
|
|
unsigned char magic2;
|
|
|
|
|
|
|
|
magic2 = xread_char(STDIN_FILENO);
|
2008-08-05 18:40:34 +05:30
|
|
|
if (ENABLE_FEATURE_SEAMLESS_Z && magic2 == 0x9d) {
|
|
|
|
status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
|
2007-03-08 03:32:23 +05:30
|
|
|
} else if (magic2 == 0x8b) {
|
2007-06-08 18:35:39 +05:30
|
|
|
status = unpack_gz_stream(STDIN_FILENO, STDOUT_FILENO);
|
2007-03-08 03:32:23 +05:30
|
|
|
} else {
|
|
|
|
goto bad_magic;
|
|
|
|
}
|
|
|
|
if (status < 0) {
|
|
|
|
bb_error_msg("error inflating");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bad_magic:
|
|
|
|
bb_error_msg("invalid magic");
|
|
|
|
/* status is still == -1 */
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2008-03-30 18:41:47 +05:30
|
|
|
/*
|
|
|
|
* Linux kernel build uses gzip -d -n. We accept and ignore it.
|
|
|
|
* Man page says:
|
|
|
|
* -n --no-name
|
|
|
|
* gzip: do not save the original file name and time stamp.
|
|
|
|
* (The original name is always saved if the name had to be truncated.)
|
|
|
|
* gunzip: do not restore the original file name/time even if present
|
|
|
|
* (remove only the gzip suffix from the compressed file name).
|
|
|
|
* This option is the default when decompressing.
|
|
|
|
* -N --name
|
|
|
|
* gzip: always save the original file name and time stamp (this is the default)
|
|
|
|
* gunzip: restore the original file name and time stamp if present.
|
|
|
|
*/
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int gunzip_main(int argc UNUSED_PARAM, char **argv)
|
2007-03-08 03:32:23 +05:30
|
|
|
{
|
2008-03-30 18:41:47 +05:30
|
|
|
getopt32(argv, "cfvdtn");
|
2007-03-08 03:32:23 +05:30
|
|
|
argv += optind;
|
|
|
|
/* if called as zcat */
|
|
|
|
if (applet_name[1] == 'c')
|
|
|
|
option_mask32 |= OPT_STDOUT;
|
|
|
|
|
2007-03-14 05:37:51 +05:30
|
|
|
return bbunpack(argv, make_new_name_gunzip, unpack_gunzip);
|
2007-03-08 03:32:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Small lzma deflate implementation.
|
|
|
|
* Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
|
|
|
|
*
|
|
|
|
* Based on bunzip.c from busybox
|
|
|
|
*
|
|
|
|
* Licensed under GPL v2, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if ENABLE_UNLZMA
|
|
|
|
|
|
|
|
static
|
|
|
|
char* make_new_name_unlzma(char *filename)
|
|
|
|
{
|
2007-03-10 02:19:55 +05:30
|
|
|
return make_new_name_generic(filename, "lzma");
|
2007-03-08 03:32:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
USE_DESKTOP(long long) int unpack_unlzma(void)
|
|
|
|
{
|
2007-06-08 18:35:39 +05:30
|
|
|
return unpack_lzma_stream(STDIN_FILENO, STDOUT_FILENO);
|
2007-03-08 03:32:23 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int unlzma_main(int argc UNUSED_PARAM, char **argv)
|
2007-03-08 03:32:23 +05:30
|
|
|
{
|
2007-09-05 17:18:32 +05:30
|
|
|
getopt32(argv, "cf");
|
2007-03-08 03:32:23 +05:30
|
|
|
argv += optind;
|
|
|
|
/* lzmacat? */
|
|
|
|
if (applet_name[4] == 'c')
|
|
|
|
option_mask32 |= OPT_STDOUT;
|
|
|
|
|
2007-03-14 05:37:51 +05:30
|
|
|
return bbunpack(argv, make_new_name_unlzma, unpack_unlzma);
|
2007-03-08 03:32:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Uncompress applet for busybox (c) 2002 Glenn McGrath
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if ENABLE_UNCOMPRESS
|
|
|
|
|
|
|
|
static
|
|
|
|
char* make_new_name_uncompress(char *filename)
|
|
|
|
{
|
2007-03-10 02:19:55 +05:30
|
|
|
return make_new_name_generic(filename, "Z");
|
2007-03-08 03:32:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
USE_DESKTOP(long long) int unpack_uncompress(void)
|
|
|
|
{
|
|
|
|
USE_DESKTOP(long long) int status = -1;
|
|
|
|
|
|
|
|
if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
|
|
|
|
bb_error_msg("invalid magic");
|
|
|
|
} else {
|
2008-08-05 18:40:34 +05:30
|
|
|
status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
|
2007-03-08 03:32:23 +05:30
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int uncompress_main(int argc UNUSED_PARAM, char **argv)
|
2007-03-08 03:32:23 +05:30
|
|
|
{
|
2007-08-18 21:02:12 +05:30
|
|
|
getopt32(argv, "cf");
|
2007-03-08 03:32:23 +05:30
|
|
|
argv += optind;
|
|
|
|
|
2007-03-14 05:37:51 +05:30
|
|
|
return bbunpack(argv, make_new_name_uncompress, unpack_uncompress);
|
2007-03-08 03:32:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|