aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-07-10 17:43:01 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-07-10 17:43:01 +0000
commitb6052724ffc9d45894147b70e7aff226938bd2d3 (patch)
treee39d062e16d85da315cdb8cc0a96a03f15c10a23
parent0e2c93fc0b5f335d731ff712ce8f42d8616f05b9 (diff)
downloadbusybox-w32-b6052724ffc9d45894147b70e7aff226938bd2d3.tar.gz
busybox-w32-b6052724ffc9d45894147b70e7aff226938bd2d3.tar.bz2
busybox-w32-b6052724ffc9d45894147b70e7aff226938bd2d3.zip
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
-rw-r--r--archival/libunarchive/Kbuild2
-rw-r--r--archival/libunarchive/get_header_tar_bz2.c2
-rw-r--r--archival/libunarchive/get_header_tar_gz.c2
-rw-r--r--archival/libunarchive/get_header_tar_lzma.c2
-rw-r--r--archival/libunarchive/open_transformer.c19
-rw-r--r--archival/rpm.c2
-rw-r--r--archival/tar.c2
-rw-r--r--include/libbb.h9
-rw-r--r--include/unarchive.h6
-rw-r--r--libbb/read.c62
-rw-r--r--modutils/modprobe-small.c28
11 files changed, 70 insertions, 66 deletions
diff --git a/archival/libunarchive/Kbuild b/archival/libunarchive/Kbuild
index 1bc054a96..609bc50cb 100644
--- a/archival/libunarchive/Kbuild
+++ b/archival/libunarchive/Kbuild
@@ -43,6 +43,8 @@ lib-$(CONFIG_FEATURE_DEB_TAR_GZ) += open_transformer.o
43lib-$(CONFIG_FEATURE_DEB_TAR_BZ2) += open_transformer.o 43lib-$(CONFIG_FEATURE_DEB_TAR_BZ2) += open_transformer.o
44lib-$(CONFIG_FEATURE_DEB_TAR_LZMA) += open_transformer.o 44lib-$(CONFIG_FEATURE_DEB_TAR_LZMA) += open_transformer.o
45 45
46lib-$(CONFIG_FEATURE_MODPROBE_SMALL_ZIPPED) += open_transformer.o decompress_unzip.o decompress_bunzip2.o
47
46lib-$(CONFIG_AR) += get_header_ar.o unpack_ar_archive.o 48lib-$(CONFIG_AR) += get_header_ar.o unpack_ar_archive.o
47lib-$(CONFIG_BUNZIP2) += decompress_bunzip2.o 49lib-$(CONFIG_BUNZIP2) += decompress_bunzip2.o
48lib-$(CONFIG_UNLZMA) += decompress_unlzma.o 50lib-$(CONFIG_UNLZMA) += decompress_unlzma.o
diff --git a/archival/libunarchive/get_header_tar_bz2.c b/archival/libunarchive/get_header_tar_bz2.c
index cfdc016d6..035c10bfb 100644
--- a/archival/libunarchive/get_header_tar_bz2.c
+++ b/archival/libunarchive/get_header_tar_bz2.c
@@ -11,7 +11,7 @@ char FAST_FUNC get_header_tar_bz2(archive_handle_t *archive_handle)
11 /* Can't lseek over pipes */ 11 /* Can't lseek over pipes */
12 archive_handle->seek = seek_by_read; 12 archive_handle->seek = seek_by_read;
13 13
14 archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_bz2_stream, "bunzip2"); 14 open_transformer(archive_handle->src_fd, unpack_bz2_stream, "bunzip2");
15 archive_handle->offset = 0; 15 archive_handle->offset = 0;
16 while (get_header_tar(archive_handle) == EXIT_SUCCESS) 16 while (get_header_tar(archive_handle) == EXIT_SUCCESS)
17 continue; 17 continue;
diff --git a/archival/libunarchive/get_header_tar_gz.c b/archival/libunarchive/get_header_tar_gz.c
index 33c62729e..086c6df42 100644
--- a/archival/libunarchive/get_header_tar_gz.c
+++ b/archival/libunarchive/get_header_tar_gz.c
@@ -25,7 +25,7 @@ char FAST_FUNC get_header_tar_gz(archive_handle_t *archive_handle)
25 } 25 }
26#endif 26#endif
27 27
28 archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_gz_stream, "gunzip"); 28 open_transformer(archive_handle->src_fd, unpack_gz_stream, "gunzip");
29 archive_handle->offset = 0; 29 archive_handle->offset = 0;
30 while (get_header_tar(archive_handle) == EXIT_SUCCESS) 30 while (get_header_tar(archive_handle) == EXIT_SUCCESS)
31 continue; 31 continue;
diff --git a/archival/libunarchive/get_header_tar_lzma.c b/archival/libunarchive/get_header_tar_lzma.c
index 730c1b1bb..03b1b792d 100644
--- a/archival/libunarchive/get_header_tar_lzma.c
+++ b/archival/libunarchive/get_header_tar_lzma.c
@@ -14,7 +14,7 @@ char FAST_FUNC get_header_tar_lzma(archive_handle_t *archive_handle)
14 /* Can't lseek over pipes */ 14 /* Can't lseek over pipes */
15 archive_handle->seek = seek_by_read; 15 archive_handle->seek = seek_by_read;
16 16
17 archive_handle->src_fd = open_transformer(archive_handle->src_fd, unpack_lzma_stream, "unlzma"); 17 open_transformer(archive_handle->src_fd, unpack_lzma_stream, "unlzma");
18 archive_handle->offset = 0; 18 archive_handle->offset = 0;
19 while (get_header_tar(archive_handle) == EXIT_SUCCESS) 19 while (get_header_tar(archive_handle) == EXIT_SUCCESS)
20 continue; 20 continue;
diff --git a/archival/libunarchive/open_transformer.c b/archival/libunarchive/open_transformer.c
index a5ee97167..42fdd96a6 100644
--- a/archival/libunarchive/open_transformer.c
+++ b/archival/libunarchive/open_transformer.c
@@ -11,7 +11,7 @@
11 * On MMU machine, the transform_prog is removed by macro magic 11 * On MMU machine, the transform_prog is removed by macro magic
12 * in include/unarchive.h. On NOMMU, transformer is removed. 12 * in include/unarchive.h. On NOMMU, transformer is removed.
13 */ 13 */
14int FAST_FUNC open_transformer(int src_fd, 14void FAST_FUNC open_transformer(int fd,
15 USE_DESKTOP(long long) int FAST_FUNC (*transformer)(int src_fd, int dst_fd), 15 USE_DESKTOP(long long) int FAST_FUNC (*transformer)(int src_fd, int dst_fd),
16 const char *transform_prog) 16 const char *transform_prog)
17{ 17{
@@ -32,20 +32,20 @@ int FAST_FUNC open_transformer(int src_fd,
32 32
33 if (pid == 0) { 33 if (pid == 0) {
34 /* child process */ 34 /* child process */
35 close(fd_pipe.rd); /* We don't want to read from the parent */ 35 close(fd_pipe.rd); /* we don't want to read from the parent */
36 // FIXME: error check? 36 // FIXME: error check?
37#if BB_MMU 37#if BB_MMU
38 transformer(src_fd, fd_pipe.wr); 38 transformer(fd, fd_pipe.wr);
39 if (ENABLE_FEATURE_CLEAN_UP) { 39 if (ENABLE_FEATURE_CLEAN_UP) {
40 close(fd_pipe.wr); /* Send EOF */ 40 close(fd_pipe.wr); /* send EOF */
41 close(src_fd); 41 close(fd);
42 } 42 }
43 /* must be _exit! bug was actually seen here */ 43 /* must be _exit! bug was actually seen here */
44 _exit(EXIT_SUCCESS); 44 _exit(EXIT_SUCCESS);
45#else 45#else
46 { 46 {
47 char *argv[4]; 47 char *argv[4];
48 xmove_fd(src_fd, 0); 48 xmove_fd(fd, 0);
49 xmove_fd(fd_pipe.wr, 1); 49 xmove_fd(fd_pipe.wr, 1);
50 argv[0] = (char*)transform_prog; 50 argv[0] = (char*)transform_prog;
51 argv[1] = (char*)"-cf"; 51 argv[1] = (char*)"-cf";
@@ -59,9 +59,6 @@ int FAST_FUNC open_transformer(int src_fd,
59 } 59 }
60 60
61 /* parent process */ 61 /* parent process */
62 close(fd_pipe.wr); /* Don't want to write to the child */ 62 close(fd_pipe.wr); /* don't want to write to the child */
63 63 xmove_fd(fd_pipe.rd, fd);
64//TODO: get rid of return value (become void)?
65 xmove_fd(fd_pipe.rd, src_fd);
66 return src_fd;
67} 64}
diff --git a/archival/rpm.c b/archival/rpm.c
index b3d7cd5f7..e6dd3d853 100644
--- a/archival/rpm.c
+++ b/archival/rpm.c
@@ -236,7 +236,7 @@ static void extract_cpio_gz(int fd)
236 } 236 }
237 237
238 xchdir("/"); /* Install RPM's to root */ 238 xchdir("/"); /* Install RPM's to root */
239 archive_handle->src_fd = open_transformer(archive_handle->src_fd, xformer, xformer_prog); 239 open_transformer(archive_handle->src_fd, xformer, xformer_prog);
240 archive_handle->offset = 0; 240 archive_handle->offset = 0;
241 while (get_header_cpio(archive_handle) == EXIT_SUCCESS) 241 while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
242 continue; 242 continue;
diff --git a/archival/tar.c b/archival/tar.c
index 13cdbd930..a46442f8e 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -692,7 +692,7 @@ static char FAST_FUNC get_header_tar_Z(archive_handle_t *archive_handle)
692 bb_error_msg_and_die("invalid magic"); 692 bb_error_msg_and_die("invalid magic");
693 } 693 }
694 694
695 archive_handle->src_fd = open_transformer(archive_handle->src_fd, uncompress, "uncompress"); 695 open_transformer(archive_handle->src_fd, uncompress, "uncompress");
696 archive_handle->offset = 0; 696 archive_handle->offset = 0;
697 while (get_header_tar(archive_handle) == EXIT_SUCCESS) 697 while (get_header_tar(archive_handle) == EXIT_SUCCESS)
698 continue; 698 continue;
diff --git a/include/libbb.h b/include/libbb.h
index 2dfdded5b..671584b43 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -583,15 +583,18 @@ extern unsigned char xread_char(int fd) FAST_FUNC;
583// Reads one line a-la fgets (but doesn't save terminating '\n'). 583// Reads one line a-la fgets (but doesn't save terminating '\n').
584// Uses single full_read() call, works only on seekable streams. 584// Uses single full_read() call, works only on seekable streams.
585extern char *reads(int fd, char *buf, size_t count) FAST_FUNC; 585extern char *reads(int fd, char *buf, size_t count) FAST_FUNC;
586extern ssize_t read_close(int fd, void *buf, size_t maxsz) FAST_FUNC;
587extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FAST_FUNC;
586// Reads one line a-la fgets (but doesn't save terminating '\n'). 588// Reads one line a-la fgets (but doesn't save terminating '\n').
587// Reads byte-by-byte. Useful when it is important to not read ahead. 589// Reads byte-by-byte. Useful when it is important to not read ahead.
588// Bytes are appended to pfx (which must be malloced, or NULL). 590// Bytes are appended to pfx (which must be malloced, or NULL).
589extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p) FAST_FUNC; 591extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p) FAST_FUNC;
590extern ssize_t read_close(int fd, void *buf, size_t maxsz) FAST_FUNC; 592/* Reads block up to *maxsz_p (default: MAX_INT(ssize_t)) */
591extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FAST_FUNC; 593extern void *xmalloc_read(int fd, size_t *maxsz_p) FAST_FUNC;
592extern void *xmalloc_read(int fd, size_t *sizep) FAST_FUNC;
593/* Returns NULL if file can't be opened */ 594/* Returns NULL if file can't be opened */
594extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC; 595extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC;
596/* Autodetects .gz etc */
597extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC;
595/* Never returns NULL */ 598/* Never returns NULL */
596extern void *xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC; 599extern void *xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC;
597 600
diff --git a/include/unarchive.h b/include/unarchive.h
index 1fab570c3..0e380db08 100644
--- a/include/unarchive.h
+++ b/include/unarchive.h
@@ -126,12 +126,12 @@ extern USE_DESKTOP(long long) int unpack_gz_stream(int src_fd, int dst_fd) FAST_
126extern USE_DESKTOP(long long) int unpack_lzma_stream(int src_fd, int dst_fd) FAST_FUNC; 126extern USE_DESKTOP(long long) int unpack_lzma_stream(int src_fd, int dst_fd) FAST_FUNC;
127 127
128#if BB_MMU 128#if BB_MMU
129extern int open_transformer(int src_fd, 129extern void open_transformer(int fd,
130 USE_DESKTOP(long long) int FAST_FUNC (*transformer)(int src_fd, int dst_fd)) FAST_FUNC; 130 USE_DESKTOP(long long) int FAST_FUNC (*transformer)(int src_fd, int dst_fd)) FAST_FUNC;
131#define open_transformer(src_fd, transformer, transform_prog) open_transformer(src_fd, transformer) 131#define open_transformer(fd, transformer, transform_prog) open_transformer(fd, transformer)
132#else 132#else
133extern int open_transformer(int src_fd, const char *transform_prog) FAST_FUNC; 133extern int open_transformer(int src_fd, const char *transform_prog) FAST_FUNC;
134#define open_transformer(src_fd, transformer, transform_prog) open_transformer(src_fd, transform_prog) 134#define open_transformer(fd, transformer, transform_prog) open_transformer(fd, transform_prog)
135#endif 135#endif
136 136
137#if __GNUC_PREREQ(4,1) 137#if __GNUC_PREREQ(4,1)
diff --git a/libbb/read.c b/libbb/read.c
index e67bbfb7e..7af895207 100644
--- a/libbb/read.c
+++ b/libbb/read.c
@@ -8,6 +8,9 @@
8 */ 8 */
9 9
10#include "libbb.h" 10#include "libbb.h"
11#if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED
12#include "unarchive.h"
13#endif
11 14
12ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count) 15ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count)
13{ 16{
@@ -206,14 +209,14 @@ ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size)
206 209
207// Read (potentially big) files in one go. File size is estimated 210// Read (potentially big) files in one go. File size is estimated
208// by stat. Extra '\0' byte is appended. 211// by stat. Extra '\0' byte is appended.
209void* FAST_FUNC xmalloc_read(int fd, size_t *sizep) 212void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
210{ 213{
211 char *buf; 214 char *buf;
212 size_t size, rd_size, total; 215 size_t size, rd_size, total;
213 off_t to_read; 216 size_t to_read;
214 struct stat st; 217 struct stat st;
215 218
216 to_read = sizep ? *sizep : MAXINT(ssize_t); /* max to read */ 219 to_read = maxsz_p ? *maxsz_p : MAXINT(ssize_t); /* max to read */
217 220
218 /* Estimate file size */ 221 /* Estimate file size */
219 st.st_size = 0; /* in case fstat fails, assume 0 */ 222 st.st_size = 0; /* in case fstat fails, assume 0 */
@@ -229,16 +232,16 @@ void* FAST_FUNC xmalloc_read(int fd, size_t *sizep)
229 size = to_read; 232 size = to_read;
230 buf = xrealloc(buf, total + size + 1); 233 buf = xrealloc(buf, total + size + 1);
231 rd_size = full_read(fd, buf + total, size); 234 rd_size = full_read(fd, buf + total, size);
232 if ((ssize_t)rd_size < 0) { /* error */ 235 if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
233 free(buf); 236 free(buf);
234 return NULL; 237 return NULL;
235 } 238 }
236 total += rd_size; 239 total += rd_size;
237 if (rd_size < size) /* EOF */ 240 if (rd_size < size) /* EOF */
238 break; 241 break;
239 to_read -= rd_size; 242 if (to_read <= rd_size)
240 if (to_read <= 0)
241 break; 243 break;
244 to_read -= rd_size;
242 /* grow by 1/8, but in [1k..64k] bounds */ 245 /* grow by 1/8, but in [1k..64k] bounds */
243 size = ((total / 8) | 0x3ff) + 1; 246 size = ((total / 8) | 0x3ff) + 1;
244 if (size > 64*1024) 247 if (size > 64*1024)
@@ -247,8 +250,8 @@ void* FAST_FUNC xmalloc_read(int fd, size_t *sizep)
247 xrealloc(buf, total + 1); 250 xrealloc(buf, total + 1);
248 buf[total] = '\0'; 251 buf[total] = '\0';
249 252
250 if (sizep) 253 if (maxsz_p)
251 *sizep = total; 254 *maxsz_p = total;
252 return buf; 255 return buf;
253} 256}
254 257
@@ -260,7 +263,7 @@ void* FAST_FUNC xmalloc_read(int fd, size_t *sizep)
260 263
261// Read (potentially big) files in one go. File size is estimated by 264// Read (potentially big) files in one go. File size is estimated by
262// lseek to end. 265// lseek to end.
263void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep) 266void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
264{ 267{
265 char *buf; 268 char *buf;
266 size_t size; 269 size_t size;
@@ -277,7 +280,7 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
277 len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ 280 len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
278 if (len != (off_t)-1) { 281 if (len != (off_t)-1) {
279 xlseek(fd, 0, SEEK_SET); 282 xlseek(fd, 0, SEEK_SET);
280 size = sizep ? *sizep : INT_MAX; 283 size = maxsz_p ? *maxsz_p : INT_MAX;
281 if (len < size) 284 if (len < size)
282 size = len; 285 size = len;
283 } 286 }
@@ -291,15 +294,15 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
291 xrealloc(buf, size + 1); 294 xrealloc(buf, size + 1);
292 buf[size] = '\0'; 295 buf[size] = '\0';
293 296
294 if (sizep) 297 if (maxsz_p)
295 *sizep = size; 298 *maxsz_p = size;
296 return buf; 299 return buf;
297} 300}
298#endif 301#endif
299 302
300// Read (potentially big) files in one go. File size is estimated 303// Read (potentially big) files in one go. File size is estimated
301// by stat. 304// by stat.
302void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep) 305void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
303{ 306{
304 char *buf; 307 char *buf;
305 int fd; 308 int fd;
@@ -308,15 +311,42 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
308 if (fd < 0) 311 if (fd < 0)
309 return NULL; 312 return NULL;
310 313
311 buf = xmalloc_read(fd, sizep); 314 buf = xmalloc_read(fd, maxsz_p);
312 close(fd); 315 close(fd);
313 return buf; 316 return buf;
314} 317}
315 318
316void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *sizep) 319void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
317{ 320{
318 void *buf = xmalloc_open_read_close(filename, sizep); 321 void *buf = xmalloc_open_read_close(filename, maxsz_p);
319 if (!buf) 322 if (!buf)
320 bb_perror_msg_and_die("can't read '%s'", filename); 323 bb_perror_msg_and_die("can't read '%s'", filename);
321 return buf; 324 return buf;
322} 325}
326
327#if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED
328void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
329{
330 char *image;
331 char *suffix;
332
333 int fd = open(fname, O_RDONLY);
334 if (fd < 0)
335 return NULL;
336
337 suffix = strrchr(fname, '.');
338 if (suffix) {
339 if (strcmp(suffix, ".gz") == 0)
340 open_transformer(fd, unpack_gz_stream, "gunzip");
341 else if (strcmp(suffix, ".bz2") == 0)
342 open_transformer(fd, unpack_bz2_stream, "bunzip2");
343 }
344
345 image = xmalloc_read(fd, maxsz_p);
346 if (!image)
347 bb_perror_msg("read error from '%s'", fname);
348 close(fd);
349
350 return image;
351}
352#endif
diff --git a/modutils/modprobe-small.c b/modutils/modprobe-small.c
index f28c42558..517c103d5 100644
--- a/modutils/modprobe-small.c
+++ b/modutils/modprobe-small.c
@@ -143,34 +143,6 @@ static char* str_2_list(const char *str)
143} 143}
144 144
145#if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED 145#if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED
146static char *xmalloc_open_zipped_read_close(const char *fname, size_t *sizep)
147{
148 size_t len;
149 char *image;
150 char *suffix;
151
152 int fd = open_or_warn(fname, O_RDONLY);
153 if (fd < 0)
154 return NULL;
155
156 suffix = strrchr(fname, '.');
157 if (suffix) {
158 if (strcmp(suffix, ".gz") == 0)
159 fd = open_transformer(fd, unpack_gz_stream, "gunzip");
160 else if (strcmp(suffix, ".bz2") == 0)
161 fd = open_transformer(fd, unpack_bz2_stream, "bunzip2");
162 }
163
164 len = (sizep) ? *sizep : 64 * 1024 * 1024;
165 image = xmalloc_read(fd, &len);
166 if (!image)
167 bb_perror_msg("read error from '%s'", fname);
168 close(fd);
169
170 if (sizep)
171 *sizep = len;
172 return image;
173}
174# define read_module xmalloc_open_zipped_read_close 146# define read_module xmalloc_open_zipped_read_close
175#else 147#else
176# define read_module xmalloc_open_read_close 148# define read_module xmalloc_open_read_close