mirror of
https://github.com/sheumann/hush.git
synced 2025-01-02 09:31:26 +00:00
open_transformer: do not return fd, it does not change
libbb: adopt zipped read from modprobe-small function old new delta getoptscmd 708 713 +5 qgravechar 106 109 +3 huft_build 1165 1168 +3 tr_main 474 472 -2 open_transformer 91 89 -2 evalvar 1376 1374 -2 rpm_main 1691 1688 -3 qrealloc 36 33 -3 get_header_tar_lzma 55 52 -3 get_header_tar_gz 100 97 -3 get_header_tar_bz2 55 52 -3 get_header_tar_Z 89 86 -3 find_main 418 406 -12 prepare 302 283 -19 xmalloc_open_zipped_read_close 161 135 -26 xmalloc_read 248 199 -49 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/13 up/down: 11/-130) Total: -119 bytes
This commit is contained in:
parent
0e2c93fc0b
commit
b6052724ff
@ -43,6 +43,8 @@ lib-$(CONFIG_FEATURE_DEB_TAR_GZ) += open_transformer.o
|
|||||||
lib-$(CONFIG_FEATURE_DEB_TAR_BZ2) += open_transformer.o
|
lib-$(CONFIG_FEATURE_DEB_TAR_BZ2) += open_transformer.o
|
||||||
lib-$(CONFIG_FEATURE_DEB_TAR_LZMA) += open_transformer.o
|
lib-$(CONFIG_FEATURE_DEB_TAR_LZMA) += open_transformer.o
|
||||||
|
|
||||||
|
lib-$(CONFIG_FEATURE_MODPROBE_SMALL_ZIPPED) += open_transformer.o decompress_unzip.o decompress_bunzip2.o
|
||||||
|
|
||||||
lib-$(CONFIG_AR) += get_header_ar.o unpack_ar_archive.o
|
lib-$(CONFIG_AR) += get_header_ar.o unpack_ar_archive.o
|
||||||
lib-$(CONFIG_BUNZIP2) += decompress_bunzip2.o
|
lib-$(CONFIG_BUNZIP2) += decompress_bunzip2.o
|
||||||
lib-$(CONFIG_UNLZMA) += decompress_unlzma.o
|
lib-$(CONFIG_UNLZMA) += decompress_unlzma.o
|
||||||
|
@ -11,7 +11,7 @@ char FAST_FUNC get_header_tar_bz2(archive_handle_t *archive_handle)
|
|||||||
/* Can't lseek over pipes */
|
/* Can't lseek over pipes */
|
||||||
archive_handle->seek = seek_by_read;
|
archive_handle->seek = seek_by_read;
|
||||||
|
|
||||||
archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_bz2_stream, "bunzip2");
|
open_transformer(archive_handle->src_fd, unpack_bz2_stream, "bunzip2");
|
||||||
archive_handle->offset = 0;
|
archive_handle->offset = 0;
|
||||||
while (get_header_tar(archive_handle) == EXIT_SUCCESS)
|
while (get_header_tar(archive_handle) == EXIT_SUCCESS)
|
||||||
continue;
|
continue;
|
||||||
|
@ -25,7 +25,7 @@ char FAST_FUNC get_header_tar_gz(archive_handle_t *archive_handle)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_gz_stream, "gunzip");
|
open_transformer(archive_handle->src_fd, unpack_gz_stream, "gunzip");
|
||||||
archive_handle->offset = 0;
|
archive_handle->offset = 0;
|
||||||
while (get_header_tar(archive_handle) == EXIT_SUCCESS)
|
while (get_header_tar(archive_handle) == EXIT_SUCCESS)
|
||||||
continue;
|
continue;
|
||||||
|
@ -14,7 +14,7 @@ char FAST_FUNC get_header_tar_lzma(archive_handle_t *archive_handle)
|
|||||||
/* Can't lseek over pipes */
|
/* Can't lseek over pipes */
|
||||||
archive_handle->seek = seek_by_read;
|
archive_handle->seek = seek_by_read;
|
||||||
|
|
||||||
archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_lzma_stream, "unlzma");
|
open_transformer(archive_handle->src_fd, unpack_lzma_stream, "unlzma");
|
||||||
archive_handle->offset = 0;
|
archive_handle->offset = 0;
|
||||||
while (get_header_tar(archive_handle) == EXIT_SUCCESS)
|
while (get_header_tar(archive_handle) == EXIT_SUCCESS)
|
||||||
continue;
|
continue;
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
* On MMU machine, the transform_prog is removed by macro magic
|
* On MMU machine, the transform_prog is removed by macro magic
|
||||||
* in include/unarchive.h. On NOMMU, transformer is removed.
|
* in include/unarchive.h. On NOMMU, transformer is removed.
|
||||||
*/
|
*/
|
||||||
int FAST_FUNC open_transformer(int src_fd,
|
void FAST_FUNC open_transformer(int fd,
|
||||||
USE_DESKTOP(long long) int FAST_FUNC (*transformer)(int src_fd, int dst_fd),
|
USE_DESKTOP(long long) int FAST_FUNC (*transformer)(int src_fd, int dst_fd),
|
||||||
const char *transform_prog)
|
const char *transform_prog)
|
||||||
{
|
{
|
||||||
@ -32,20 +32,20 @@ int FAST_FUNC open_transformer(int src_fd,
|
|||||||
|
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
/* child process */
|
/* child process */
|
||||||
close(fd_pipe.rd); /* We don't want to read from the parent */
|
close(fd_pipe.rd); /* we don't want to read from the parent */
|
||||||
// FIXME: error check?
|
// FIXME: error check?
|
||||||
#if BB_MMU
|
#if BB_MMU
|
||||||
transformer(src_fd, fd_pipe.wr);
|
transformer(fd, fd_pipe.wr);
|
||||||
if (ENABLE_FEATURE_CLEAN_UP) {
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
||||||
close(fd_pipe.wr); /* Send EOF */
|
close(fd_pipe.wr); /* send EOF */
|
||||||
close(src_fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
/* must be _exit! bug was actually seen here */
|
/* must be _exit! bug was actually seen here */
|
||||||
_exit(EXIT_SUCCESS);
|
_exit(EXIT_SUCCESS);
|
||||||
#else
|
#else
|
||||||
{
|
{
|
||||||
char *argv[4];
|
char *argv[4];
|
||||||
xmove_fd(src_fd, 0);
|
xmove_fd(fd, 0);
|
||||||
xmove_fd(fd_pipe.wr, 1);
|
xmove_fd(fd_pipe.wr, 1);
|
||||||
argv[0] = (char*)transform_prog;
|
argv[0] = (char*)transform_prog;
|
||||||
argv[1] = (char*)"-cf";
|
argv[1] = (char*)"-cf";
|
||||||
@ -59,9 +59,6 @@ int FAST_FUNC open_transformer(int src_fd,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* parent process */
|
/* parent process */
|
||||||
close(fd_pipe.wr); /* Don't want to write to the child */
|
close(fd_pipe.wr); /* don't want to write to the child */
|
||||||
|
xmove_fd(fd_pipe.rd, fd);
|
||||||
//TODO: get rid of return value (become void)?
|
|
||||||
xmove_fd(fd_pipe.rd, src_fd);
|
|
||||||
return src_fd;
|
|
||||||
}
|
}
|
||||||
|
@ -236,7 +236,7 @@ static void extract_cpio_gz(int fd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
xchdir("/"); /* Install RPM's to root */
|
xchdir("/"); /* Install RPM's to root */
|
||||||
archive_handle->src_fd = open_transformer(archive_handle->src_fd, xformer, xformer_prog);
|
open_transformer(archive_handle->src_fd, xformer, xformer_prog);
|
||||||
archive_handle->offset = 0;
|
archive_handle->offset = 0;
|
||||||
while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
|
while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
|
||||||
continue;
|
continue;
|
||||||
|
@ -692,7 +692,7 @@ static char FAST_FUNC get_header_tar_Z(archive_handle_t *archive_handle)
|
|||||||
bb_error_msg_and_die("invalid magic");
|
bb_error_msg_and_die("invalid magic");
|
||||||
}
|
}
|
||||||
|
|
||||||
archive_handle->src_fd = open_transformer(archive_handle->src_fd, uncompress, "uncompress");
|
open_transformer(archive_handle->src_fd, uncompress, "uncompress");
|
||||||
archive_handle->offset = 0;
|
archive_handle->offset = 0;
|
||||||
while (get_header_tar(archive_handle) == EXIT_SUCCESS)
|
while (get_header_tar(archive_handle) == EXIT_SUCCESS)
|
||||||
continue;
|
continue;
|
||||||
|
@ -583,15 +583,18 @@ extern unsigned char xread_char(int fd) FAST_FUNC;
|
|||||||
// Reads one line a-la fgets (but doesn't save terminating '\n').
|
// Reads one line a-la fgets (but doesn't save terminating '\n').
|
||||||
// Uses single full_read() call, works only on seekable streams.
|
// Uses single full_read() call, works only on seekable streams.
|
||||||
extern char *reads(int fd, char *buf, size_t count) FAST_FUNC;
|
extern char *reads(int fd, char *buf, size_t count) FAST_FUNC;
|
||||||
|
extern ssize_t read_close(int fd, void *buf, size_t maxsz) FAST_FUNC;
|
||||||
|
extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FAST_FUNC;
|
||||||
// Reads one line a-la fgets (but doesn't save terminating '\n').
|
// Reads one line a-la fgets (but doesn't save terminating '\n').
|
||||||
// Reads byte-by-byte. Useful when it is important to not read ahead.
|
// Reads byte-by-byte. Useful when it is important to not read ahead.
|
||||||
// Bytes are appended to pfx (which must be malloced, or NULL).
|
// Bytes are appended to pfx (which must be malloced, or NULL).
|
||||||
extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p) FAST_FUNC;
|
extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p) FAST_FUNC;
|
||||||
extern ssize_t read_close(int fd, void *buf, size_t maxsz) FAST_FUNC;
|
/* Reads block up to *maxsz_p (default: MAX_INT(ssize_t)) */
|
||||||
extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FAST_FUNC;
|
extern void *xmalloc_read(int fd, size_t *maxsz_p) FAST_FUNC;
|
||||||
extern void *xmalloc_read(int fd, size_t *sizep) FAST_FUNC;
|
|
||||||
/* Returns NULL if file can't be opened */
|
/* Returns NULL if file can't be opened */
|
||||||
extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC;
|
extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC;
|
||||||
|
/* Autodetects .gz etc */
|
||||||
|
extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC;
|
||||||
/* Never returns NULL */
|
/* Never returns NULL */
|
||||||
extern void *xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC;
|
extern void *xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC;
|
||||||
|
|
||||||
|
@ -126,12 +126,12 @@ extern USE_DESKTOP(long long) int unpack_gz_stream(int src_fd, int dst_fd) FAST_
|
|||||||
extern USE_DESKTOP(long long) int unpack_lzma_stream(int src_fd, int dst_fd) FAST_FUNC;
|
extern USE_DESKTOP(long long) int unpack_lzma_stream(int src_fd, int dst_fd) FAST_FUNC;
|
||||||
|
|
||||||
#if BB_MMU
|
#if BB_MMU
|
||||||
extern int open_transformer(int src_fd,
|
extern void open_transformer(int fd,
|
||||||
USE_DESKTOP(long long) int FAST_FUNC (*transformer)(int src_fd, int dst_fd)) FAST_FUNC;
|
USE_DESKTOP(long long) int FAST_FUNC (*transformer)(int src_fd, int dst_fd)) FAST_FUNC;
|
||||||
#define open_transformer(src_fd, transformer, transform_prog) open_transformer(src_fd, transformer)
|
#define open_transformer(fd, transformer, transform_prog) open_transformer(fd, transformer)
|
||||||
#else
|
#else
|
||||||
extern int open_transformer(int src_fd, const char *transform_prog) FAST_FUNC;
|
extern int open_transformer(int src_fd, const char *transform_prog) FAST_FUNC;
|
||||||
#define open_transformer(src_fd, transformer, transform_prog) open_transformer(src_fd, transform_prog)
|
#define open_transformer(fd, transformer, transform_prog) open_transformer(fd, transform_prog)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if __GNUC_PREREQ(4,1)
|
#if __GNUC_PREREQ(4,1)
|
||||||
|
62
libbb/read.c
62
libbb/read.c
@ -8,6 +8,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "libbb.h"
|
#include "libbb.h"
|
||||||
|
#if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED
|
||||||
|
#include "unarchive.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count)
|
ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count)
|
||||||
{
|
{
|
||||||
@ -206,14 +209,14 @@ ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size)
|
|||||||
|
|
||||||
// Read (potentially big) files in one go. File size is estimated
|
// Read (potentially big) files in one go. File size is estimated
|
||||||
// by stat. Extra '\0' byte is appended.
|
// by stat. Extra '\0' byte is appended.
|
||||||
void* FAST_FUNC xmalloc_read(int fd, size_t *sizep)
|
void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
|
||||||
{
|
{
|
||||||
char *buf;
|
char *buf;
|
||||||
size_t size, rd_size, total;
|
size_t size, rd_size, total;
|
||||||
off_t to_read;
|
size_t to_read;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
to_read = sizep ? *sizep : MAXINT(ssize_t); /* max to read */
|
to_read = maxsz_p ? *maxsz_p : MAXINT(ssize_t); /* max to read */
|
||||||
|
|
||||||
/* Estimate file size */
|
/* Estimate file size */
|
||||||
st.st_size = 0; /* in case fstat fails, assume 0 */
|
st.st_size = 0; /* in case fstat fails, assume 0 */
|
||||||
@ -229,16 +232,16 @@ void* FAST_FUNC xmalloc_read(int fd, size_t *sizep)
|
|||||||
size = to_read;
|
size = to_read;
|
||||||
buf = xrealloc(buf, total + size + 1);
|
buf = xrealloc(buf, total + size + 1);
|
||||||
rd_size = full_read(fd, buf + total, size);
|
rd_size = full_read(fd, buf + total, size);
|
||||||
if ((ssize_t)rd_size < 0) { /* error */
|
if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
|
||||||
free(buf);
|
free(buf);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
total += rd_size;
|
total += rd_size;
|
||||||
if (rd_size < size) /* EOF */
|
if (rd_size < size) /* EOF */
|
||||||
break;
|
break;
|
||||||
to_read -= rd_size;
|
if (to_read <= rd_size)
|
||||||
if (to_read <= 0)
|
|
||||||
break;
|
break;
|
||||||
|
to_read -= rd_size;
|
||||||
/* grow by 1/8, but in [1k..64k] bounds */
|
/* grow by 1/8, but in [1k..64k] bounds */
|
||||||
size = ((total / 8) | 0x3ff) + 1;
|
size = ((total / 8) | 0x3ff) + 1;
|
||||||
if (size > 64*1024)
|
if (size > 64*1024)
|
||||||
@ -247,8 +250,8 @@ void* FAST_FUNC xmalloc_read(int fd, size_t *sizep)
|
|||||||
xrealloc(buf, total + 1);
|
xrealloc(buf, total + 1);
|
||||||
buf[total] = '\0';
|
buf[total] = '\0';
|
||||||
|
|
||||||
if (sizep)
|
if (maxsz_p)
|
||||||
*sizep = total;
|
*maxsz_p = total;
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -260,7 +263,7 @@ void* FAST_FUNC xmalloc_read(int fd, size_t *sizep)
|
|||||||
|
|
||||||
// Read (potentially big) files in one go. File size is estimated by
|
// Read (potentially big) files in one go. File size is estimated by
|
||||||
// lseek to end.
|
// lseek to end.
|
||||||
void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
|
void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
|
||||||
{
|
{
|
||||||
char *buf;
|
char *buf;
|
||||||
size_t size;
|
size_t size;
|
||||||
@ -277,7 +280,7 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
|
|||||||
len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
|
len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
|
||||||
if (len != (off_t)-1) {
|
if (len != (off_t)-1) {
|
||||||
xlseek(fd, 0, SEEK_SET);
|
xlseek(fd, 0, SEEK_SET);
|
||||||
size = sizep ? *sizep : INT_MAX;
|
size = maxsz_p ? *maxsz_p : INT_MAX;
|
||||||
if (len < size)
|
if (len < size)
|
||||||
size = len;
|
size = len;
|
||||||
}
|
}
|
||||||
@ -291,15 +294,15 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
|
|||||||
xrealloc(buf, size + 1);
|
xrealloc(buf, size + 1);
|
||||||
buf[size] = '\0';
|
buf[size] = '\0';
|
||||||
|
|
||||||
if (sizep)
|
if (maxsz_p)
|
||||||
*sizep = size;
|
*maxsz_p = size;
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Read (potentially big) files in one go. File size is estimated
|
// Read (potentially big) files in one go. File size is estimated
|
||||||
// by stat.
|
// by stat.
|
||||||
void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
|
void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
|
||||||
{
|
{
|
||||||
char *buf;
|
char *buf;
|
||||||
int fd;
|
int fd;
|
||||||
@ -308,15 +311,42 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
|
|||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
buf = xmalloc_read(fd, sizep);
|
buf = xmalloc_read(fd, maxsz_p);
|
||||||
close(fd);
|
close(fd);
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *sizep)
|
void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
|
||||||
{
|
{
|
||||||
void *buf = xmalloc_open_read_close(filename, sizep);
|
void *buf = xmalloc_open_read_close(filename, maxsz_p);
|
||||||
if (!buf)
|
if (!buf)
|
||||||
bb_perror_msg_and_die("can't read '%s'", filename);
|
bb_perror_msg_and_die("can't read '%s'", filename);
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED
|
||||||
|
void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
|
||||||
|
{
|
||||||
|
char *image;
|
||||||
|
char *suffix;
|
||||||
|
|
||||||
|
int fd = open(fname, O_RDONLY);
|
||||||
|
if (fd < 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
suffix = strrchr(fname, '.');
|
||||||
|
if (suffix) {
|
||||||
|
if (strcmp(suffix, ".gz") == 0)
|
||||||
|
open_transformer(fd, unpack_gz_stream, "gunzip");
|
||||||
|
else if (strcmp(suffix, ".bz2") == 0)
|
||||||
|
open_transformer(fd, unpack_bz2_stream, "bunzip2");
|
||||||
|
}
|
||||||
|
|
||||||
|
image = xmalloc_read(fd, maxsz_p);
|
||||||
|
if (!image)
|
||||||
|
bb_perror_msg("read error from '%s'", fname);
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
@ -143,34 +143,6 @@ static char* str_2_list(const char *str)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED
|
#if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED
|
||||||
static char *xmalloc_open_zipped_read_close(const char *fname, size_t *sizep)
|
|
||||||
{
|
|
||||||
size_t len;
|
|
||||||
char *image;
|
|
||||||
char *suffix;
|
|
||||||
|
|
||||||
int fd = open_or_warn(fname, O_RDONLY);
|
|
||||||
if (fd < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
suffix = strrchr(fname, '.');
|
|
||||||
if (suffix) {
|
|
||||||
if (strcmp(suffix, ".gz") == 0)
|
|
||||||
fd = open_transformer(fd, unpack_gz_stream, "gunzip");
|
|
||||||
else if (strcmp(suffix, ".bz2") == 0)
|
|
||||||
fd = open_transformer(fd, unpack_bz2_stream, "bunzip2");
|
|
||||||
}
|
|
||||||
|
|
||||||
len = (sizep) ? *sizep : 64 * 1024 * 1024;
|
|
||||||
image = xmalloc_read(fd, &len);
|
|
||||||
if (!image)
|
|
||||||
bb_perror_msg("read error from '%s'", fname);
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
if (sizep)
|
|
||||||
*sizep = len;
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
# define read_module xmalloc_open_zipped_read_close
|
# define read_module xmalloc_open_zipped_read_close
|
||||||
#else
|
#else
|
||||||
# define read_module xmalloc_open_read_close
|
# define read_module xmalloc_open_read_close
|
||||||
|
Loading…
Reference in New Issue
Block a user