aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2025-04-20 23:43:19 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2025-04-20 23:49:33 +0200
commitc61fdadf974205104f6690ad360117bdf86f0ba6 (patch)
treee0dace986e6b3dd538f083795ac206c23645f757
parent636315ccb9b9e7e6cb040a9626282b7892b20df3 (diff)
downloadbusybox-w32-c61fdadf974205104f6690ad360117bdf86f0ba6.tar.gz
busybox-w32-c61fdadf974205104f6690ad360117bdf86f0ba6.tar.bz2
busybox-w32-c61fdadf974205104f6690ad360117bdf86f0ba6.zip
libbb/archival: make setup_unzip_on_fd() return bytes read if not compressed
setup_unzip_on_fd() does not return the transformer structure, so the user does not know how much to seek back (or alternatively what the signature was) when compressor signature is not detected. Currently not needed (the only user is tar which dies anyway). However, rpm2cpio may need this if we extend it to extract the internal .cpio even if cpio's compressions algo is not known. function old new delta setup_unzip_on_fd 53 59 +6 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--archival/bbunzip.c6
-rw-r--r--archival/libarchive/open_transformer.c26
-rw-r--r--archival/rpm.c4
-rw-r--r--archival/tar.c2
-rw-r--r--include/libbb.h6
-rw-r--r--miscutils/fbsplash.c2
-rw-r--r--miscutils/man.c2
7 files changed, 25 insertions, 23 deletions
diff --git a/archival/bbunzip.c b/archival/bbunzip.c
index b7944a62a..42b4baf88 100644
--- a/archival/bbunzip.c
+++ b/archival/bbunzip.c
@@ -71,8 +71,8 @@ int FAST_FUNC bbunpack(char **argv,
71 goto err; 71 goto err;
72 } else { 72 } else {
73 /* "clever zcat" with FILE */ 73 /* "clever zcat" with FILE */
74 /* fail_if_not_compressed because zcat refuses uncompressed input */ 74 /* die_if_not_compressed because zcat refuses uncompressed input */
75 int fd = open_zipped(filename, /*fail_if_not_compressed:*/ 1); 75 int fd = open_zipped(filename, /*die_if_not_compressed:*/ 1);
76 if (fd < 0) 76 if (fd < 0)
77 goto err_name; 77 goto err_name;
78 xmove_fd(fd, STDIN_FILENO); 78 xmove_fd(fd, STDIN_FILENO);
@@ -80,7 +80,7 @@ int FAST_FUNC bbunpack(char **argv,
80 } else 80 } else
81 if (option_mask32 & BBUNPK_SEAMLESS_MAGIC) { 81 if (option_mask32 & BBUNPK_SEAMLESS_MAGIC) {
82 /* "clever zcat" on stdin */ 82 /* "clever zcat" on stdin */
83 if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_compressed*/ 1)) 83 if (setup_unzip_on_fd(STDIN_FILENO, /*die_if_not_compressed*/ 1))
84 goto err; 84 goto err;
85 } 85 }
86 86
diff --git a/archival/libarchive/open_transformer.c b/archival/libarchive/open_transformer.c
index 44715ef25..353f68217 100644
--- a/archival/libarchive/open_transformer.c
+++ b/archival/libarchive/open_transformer.c
@@ -157,7 +157,7 @@ void FAST_FUNC fork_transformer(int fd, const char *transform_prog)
157/* Used by e.g. rpm which gives us a fd without filename, 157/* Used by e.g. rpm which gives us a fd without filename,
158 * thus we can't guess the format from filename's extension. 158 * thus we can't guess the format from filename's extension.
159 */ 159 */
160static transformer_state_t *setup_transformer_on_fd(int fd, int fail_if_not_compressed) 160static transformer_state_t *setup_transformer_on_fd(int fd, int die_if_not_compressed)
161{ 161{
162 transformer_state_t *xstate; 162 transformer_state_t *xstate;
163 163
@@ -204,7 +204,7 @@ static transformer_state_t *setup_transformer_on_fd(int fd, int fail_if_not_comp
204 } 204 }
205 205
206 /* No known magic seen */ 206 /* No known magic seen */
207 if (fail_if_not_compressed) 207 if (die_if_not_compressed)
208 bb_simple_error_msg_and_die("no gzip" 208 bb_simple_error_msg_and_die("no gzip"
209 IF_FEATURE_SEAMLESS_BZ2("/bzip2") 209 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
210 IF_FEATURE_SEAMLESS_XZ("/xz") 210 IF_FEATURE_SEAMLESS_XZ("/xz")
@@ -240,13 +240,15 @@ static void fork_transformer_and_free(transformer_state_t *xstate)
240/* Used by e.g. rpm which gives us a fd without filename, 240/* Used by e.g. rpm which gives us a fd without filename,
241 * thus we can't guess the format from filename's extension. 241 * thus we can't guess the format from filename's extension.
242 */ 242 */
243int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_compressed) 243int FAST_FUNC setup_unzip_on_fd(int fd, int die_if_not_compressed)
244{ 244{
245 transformer_state_t *xstate = setup_transformer_on_fd(fd, fail_if_not_compressed); 245 transformer_state_t *xstate = setup_transformer_on_fd(fd, die_if_not_compressed);
246 246
247 if (!xstate->xformer) { 247 if (!xstate->xformer) {
248 /* Not compressed */
249 int retval = xstate->signature_skipped; /* never zero */
248 free(xstate); 250 free(xstate);
249 return 1; 251 return retval;
250 } 252 }
251 253
252 fork_transformer_and_free(xstate); 254 fork_transformer_and_free(xstate);
@@ -264,7 +266,7 @@ void FAST_FUNC setup_lzma_on_fd(int fd)
264} 266}
265#endif 267#endif
266 268
267static transformer_state_t *open_transformer(const char *fname, int fail_if_not_compressed) 269static transformer_state_t *open_transformer(const char *fname, int die_if_not_compressed)
268{ 270{
269 transformer_state_t *xstate; 271 transformer_state_t *xstate;
270 int fd; 272 int fd;
@@ -284,18 +286,18 @@ static transformer_state_t *open_transformer(const char *fname, int fail_if_not_
284 } 286 }
285 } 287 }
286 288
287 xstate = setup_transformer_on_fd(fd, fail_if_not_compressed); 289 xstate = setup_transformer_on_fd(fd, die_if_not_compressed);
288 290
289 return xstate; 291 return xstate;
290} 292}
291 293
292int FAST_FUNC open_zipped(const char *fname, int fail_if_not_compressed) 294int FAST_FUNC open_zipped(const char *fname, int die_if_not_compressed)
293{ 295{
294 int fd; 296 int fd;
295 transformer_state_t *xstate; 297 transformer_state_t *xstate;
296 298
297 xstate = open_transformer(fname, fail_if_not_compressed); 299 xstate = open_transformer(fname, die_if_not_compressed);
298 if (!xstate) 300 if (!xstate) /* open error */
299 return -1; 301 return -1;
300 302
301 fd = xstate->src_fd; 303 fd = xstate->src_fd;
@@ -326,7 +328,7 @@ void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_
326 transformer_state_t *xstate; 328 transformer_state_t *xstate;
327 char *image; 329 char *image;
328 330
329 xstate = open_transformer(fname, /*fail_if_not_compressed:*/ 0); 331 xstate = open_transformer(fname, /*die_if_not_compressed:*/ 0);
330 if (!xstate) /* file open error */ 332 if (!xstate) /* file open error */
331 return NULL; 333 return NULL;
332 334
@@ -371,7 +373,7 @@ void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_
371 int fd; 373 int fd;
372 char *image; 374 char *image;
373 375
374 fd = open_zipped(fname, /*fail_if_not_compressed:*/ 0); 376 fd = open_zipped(fname, /*die_if_not_compressed:*/ 0);
375 if (fd < 0) 377 if (fd < 0)
376 return NULL; 378 return NULL;
377 379
diff --git a/archival/rpm.c b/archival/rpm.c
index af8db99a6..7fd2a2b46 100644
--- a/archival/rpm.c
+++ b/archival/rpm.c
@@ -316,7 +316,7 @@ static void extract_cpio(int fd, const char *source_rpm)
316 archive_handle->src_fd = fd; 316 archive_handle->src_fd = fd;
317 /*archive_handle->offset = 0; - init_handle() did it */ 317 /*archive_handle->offset = 0; - init_handle() did it */
318 318
319 setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 1); 319 setup_unzip_on_fd(archive_handle->src_fd, /*die_if_not_compressed:*/ 1);
320 while (get_header_cpio(archive_handle) == EXIT_SUCCESS) 320 while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
321 continue; 321 continue;
322} 322}
@@ -541,7 +541,7 @@ int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
541 // set up decompressor without detection 541 // set up decompressor without detection
542 setup_lzma_on_fd(rpm_fd); 542 setup_lzma_on_fd(rpm_fd);
543 } else { 543 } else {
544 setup_unzip_on_fd(rpm_fd, /*fail_if_not_compressed:*/ 1); 544 setup_unzip_on_fd(rpm_fd, /*die_if_not_compressed:*/ 1);
545 } 545 }
546 546
547 if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0) 547 if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
diff --git a/archival/tar.c b/archival/tar.c
index d6ca6c1e0..38906ba02 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -1164,7 +1164,7 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
1164 * on e.g. tarball with 1st file named "BZh5". 1164 * on e.g. tarball with 1st file named "BZh5".
1165 */ 1165 */
1166 ) { 1166 ) {
1167 tar_handle->src_fd = open_zipped(tar_filename, /*fail_if_not_compressed:*/ 0); 1167 tar_handle->src_fd = open_zipped(tar_filename, /*die_if_not_compressed:*/ 0);
1168 if (tar_handle->src_fd < 0) 1168 if (tar_handle->src_fd < 0)
1169 bb_perror_msg_and_die("can't open '%s'", tar_filename); 1169 bb_perror_msg_and_die("can't open '%s'", tar_filename);
1170 } else { 1170 } else {
diff --git a/include/libbb.h b/include/libbb.h
index 4d6193795..558f3c627 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1011,13 +1011,13 @@ unsigned bb_clk_tck(void) FAST_FUNC;
1011 1011
1012#if SEAMLESS_COMPRESSION 1012#if SEAMLESS_COMPRESSION
1013/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */ 1013/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */
1014int setup_unzip_on_fd(int fd, int fail_if_not_compressed) FAST_FUNC; 1014int setup_unzip_on_fd(int fd, int die_if_not_compressed) FAST_FUNC;
1015/* Autodetects .gz etc */ 1015/* Autodetects .gz etc */
1016extern int open_zipped(const char *fname, int fail_if_not_compressed) FAST_FUNC; 1016extern int open_zipped(const char *fname, int die_if_not_compressed) FAST_FUNC;
1017extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC; 1017extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
1018#else 1018#else
1019# define setup_unzip_on_fd(...) (0) 1019# define setup_unzip_on_fd(...) (0)
1020# define open_zipped(fname, fail_if_not_compressed) open((fname), O_RDONLY); 1020# define open_zipped(fname, die_if_not_compressed) open((fname), O_RDONLY);
1021# define xmalloc_open_zipped_read_close(fname, maxsz_p) xmalloc_open_read_close((fname), (maxsz_p)) 1021# define xmalloc_open_zipped_read_close(fname, maxsz_p) xmalloc_open_read_close((fname), (maxsz_p))
1022#endif 1022#endif
1023/* lzma has no signature, need a little helper. NB: exist only for ENABLE_FEATURE_SEAMLESS_LZMA=y */ 1023/* lzma has no signature, need a little helper. NB: exist only for ENABLE_FEATURE_SEAMLESS_LZMA=y */
diff --git a/miscutils/fbsplash.c b/miscutils/fbsplash.c
index 2934d8eb7..912a501a3 100644
--- a/miscutils/fbsplash.c
+++ b/miscutils/fbsplash.c
@@ -382,7 +382,7 @@ static void fb_drawimage(void)
382 if (LONE_DASH(G.image_filename)) { 382 if (LONE_DASH(G.image_filename)) {
383 theme_file = stdin; 383 theme_file = stdin;
384 } else { 384 } else {
385 int fd = open_zipped(G.image_filename, /*fail_if_not_compressed:*/ 0); 385 int fd = open_zipped(G.image_filename, /*die_if_not_compressed:*/ 0);
386 if (fd < 0) 386 if (fd < 0)
387 bb_simple_perror_msg_and_die(G.image_filename); 387 bb_simple_perror_msg_and_die(G.image_filename);
388 theme_file = xfdopen_for_read(fd); 388 theme_file = xfdopen_for_read(fd);
diff --git a/miscutils/man.c b/miscutils/man.c
index deaf9e5ab..6fa1fbfdc 100644
--- a/miscutils/man.c
+++ b/miscutils/man.c
@@ -143,7 +143,7 @@ static int run_pipe(char *man_filename, int man, int level)
143 143
144 ordinary_manpage: 144 ordinary_manpage:
145 close(STDIN_FILENO); 145 close(STDIN_FILENO);
146 open_zipped(man_filename, /*fail_if_not_compressed:*/ 0); /* guaranteed to use fd 0 (STDIN_FILENO) */ 146 open_zipped(man_filename, /*die_if_not_compressed:*/ 0); /* guaranteed to use fd 0 (STDIN_FILENO) */
147 if (man) { 147 if (man) {
148 int w = get_terminal_width(-1); 148 int w = get_terminal_width(-1);
149 if (w > 10) 149 if (w > 10)