Package: busybox / 1:1.22.0-19

zcat:-complain-if-input-is-not-compressed.diff Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
commit 640ce3de07807133796bccd0bdfa146bbfc788c7
Author: Denys Vlasenko <vda.linux@googlemail.com>
Date:   Sun Feb 2 02:06:38 2014 +0100

    zcat: complain if input is not compressed
    
    function                                             old     new   delta
    buffer_fill_and_print                                178     191     +13
    varvalue                                             735     743      +8
    bbunpack                                             747     755      +8
    open_zipped                                           85      89      +4
    xmalloc_open_zipped_read_close                        61      63      +2
    get_addr_1                                           240     242      +2
    fbsplash_main                                       1228    1230      +2
    pstree_main                                          322     321      -1
    builtin_type                                         121     119      -2
    do_load                                              954     926     -28
    ------------------------------------------------------------------------------
    (add/remove: 0/0 grow/shrink: 7/3 up/down: 39/-31)              Total: 8 bytes
    
    Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>

diff --git a/archival/bbunzip.c b/archival/bbunzip.c
index b3fb90f..fce5ab9 100644
--- a/archival/bbunzip.c
+++ b/archival/bbunzip.c
@@ -72,7 +72,8 @@ int FAST_FUNC bbunpack(char **argv,
 					goto err;
 			} else {
 				/* "clever zcat" with FILE */
-				int fd = open_zipped(filename);
+				/* fail_if_not_compressed because zcat refuses uncompressed input */
+				int fd = open_zipped(filename, /*fail_if_not_compressed:*/ 1);
 				if (fd < 0)
 					goto err_name;
 				xmove_fd(fd, STDIN_FILENO);
@@ -80,7 +81,7 @@ int FAST_FUNC bbunpack(char **argv,
 		} else
 		if (option_mask32 & SEAMLESS_MAGIC) {
 			/* "clever zcat" on stdin */
-			if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_detected*/ 0))
+			if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_compressed*/ 1))
 				goto err;
 		}
 
diff --git a/archival/libarchive/get_header_tar.c b/archival/libarchive/get_header_tar.c
index 54d9104..ba43bb0 100644
--- a/archival/libarchive/get_header_tar.c
+++ b/archival/libarchive/get_header_tar.c
@@ -243,7 +243,7 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
 		 * or not first block (false positive, it's not .gz/.bz2!) */
 		if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0)
 			goto err;
-		if (setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_detected:*/ 0) != 0)
+		if (setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 0) != 0)
  err:
 			bb_error_msg_and_die("invalid tar magic");
 		archive_handle->offset = 0;
diff --git a/archival/libarchive/open_transformer.c b/archival/libarchive/open_transformer.c
index 27854af..1986630 100644
--- a/archival/libarchive/open_transformer.c
+++ b/archival/libarchive/open_transformer.c
@@ -118,7 +118,7 @@ void FAST_FUNC open_transformer(int fd, const char *transform_prog)
 /* Used by e.g. rpm which gives us a fd without filename,
  * thus we can't guess the format from filename's extension.
  */
-int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_detected)
+int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_compressed)
 {
 	union {
 		uint8_t b[4];
@@ -159,7 +159,7 @@ int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_detected)
 	}
 
 	/* No known magic seen */
-	if (fail_if_not_detected)
+	if (fail_if_not_compressed)
 		bb_error_msg_and_die("no gzip"
 			IF_FEATURE_SEAMLESS_BZ2("/bzip2")
 			IF_FEATURE_SEAMLESS_XZ("/xz")
@@ -180,7 +180,7 @@ int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_detected)
 	return 0;
 }
 
-int FAST_FUNC open_zipped(const char *fname)
+int FAST_FUNC open_zipped(const char *fname, int fail_if_not_compressed)
 {
 	int fd;
 
@@ -200,16 +200,7 @@ int FAST_FUNC open_zipped(const char *fname)
 	 || (ENABLE_FEATURE_SEAMLESS_BZ2)
 	 || (ENABLE_FEATURE_SEAMLESS_XZ)
 	) {
-		/*
-		 * Do we want to fail_if_not_detected?
-		 * In most cases, no: think "insmod non_compressed_module".
-		 * A case which would like to fail is "zcat uncompressed_file":
-		 * otherwise, it happily outputs uncompressed_file as-is,
-		 * which is, strictly speaking, not what is expected.
-		 * If this ever becomes a problem, we can add
-		 * fail_if_not_detected bool argument to open_zipped().
-		 */
-		setup_unzip_on_fd(fd, /*fail_if_not_detected:*/ 0);
+		setup_unzip_on_fd(fd, fail_if_not_compressed);
 	}
 
 	return fd;
@@ -222,7 +213,7 @@ void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_
 	int fd;
 	char *image;
 
-	fd = open_zipped(fname);
+	fd = open_zipped(fname, /*fail_if_not_compressed:*/ 0);
 	if (fd < 0)
 		return NULL;
 
diff --git a/archival/rpm.c b/archival/rpm.c
index 885eddd..1053944 100644
--- a/archival/rpm.c
+++ b/archival/rpm.c
@@ -122,7 +122,7 @@ static void extract_cpio(int fd, const char *source_rpm)
 	archive_handle->src_fd = fd;
 	/*archive_handle->offset = 0; - init_handle() did it */
 
-	setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_detected:*/ 1);
+	setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 1);
 	while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
 		continue;
 }
diff --git a/archival/rpm2cpio.c b/archival/rpm2cpio.c
index 61adde7..7057570 100644
--- a/archival/rpm2cpio.c
+++ b/archival/rpm2cpio.c
@@ -80,7 +80,7 @@ int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
 	//	signal(SIGCHLD, check_errors_in_children);
 
 	/* This works, but doesn't report uncompress errors (they happen in child) */
-	setup_unzip_on_fd(rpm_fd, /*fail_if_not_detected:*/ 1);
+	setup_unzip_on_fd(rpm_fd, /*fail_if_not_compressed:*/ 1);
 	if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
 		bb_error_msg_and_die("error unpacking");
 
diff --git a/archival/tar.c b/archival/tar.c
index bd61abd..aa02d35 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -1137,7 +1137,7 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
 			 && flags == O_RDONLY
 			 && !(opt & OPT_ANY_COMPRESS)
 			) {
-				tar_handle->src_fd = open_zipped(tar_filename);
+				tar_handle->src_fd = open_zipped(tar_filename, /*fail_if_not_compressed:*/ 0);
 				if (tar_handle->src_fd < 0)
 					bb_perror_msg_and_die("can't open '%s'", tar_filename);
 			} else {
diff --git a/include/libbb.h b/include/libbb.h
index 64167bb..96f3334 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -736,12 +736,12 @@ extern void *xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) FAS
 
 #if SEAMLESS_COMPRESSION
 /* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */
-extern int setup_unzip_on_fd(int fd, int fail_if_not_detected) FAST_FUNC;
+extern int setup_unzip_on_fd(int fd, int fail_if_not_compressed) FAST_FUNC;
 /* Autodetects .gz etc */
-extern int open_zipped(const char *fname) FAST_FUNC;
+extern int open_zipped(const char *fname, int fail_if_not_compressed) FAST_FUNC;
 #else
 # define setup_unzip_on_fd(...) (0)
-# define open_zipped(fname)     open((fname), O_RDONLY);
+# define open_zipped(fname, fail_if_not_compressed)  open((fname), O_RDONLY);
 #endif
 extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
 
diff --git a/miscutils/fbsplash.c b/miscutils/fbsplash.c
index 12a77b7..7b695b2 100644
--- a/miscutils/fbsplash.c
+++ b/miscutils/fbsplash.c
@@ -353,7 +353,7 @@ static void fb_drawimage(void)
 	if (LONE_DASH(G.image_filename)) {
 		theme_file = stdin;
 	} else {
-		int fd = open_zipped(G.image_filename);
+		int fd = open_zipped(G.image_filename, /*fail_if_not_compressed:*/ 0);
 		if (fd < 0)
 			bb_simple_perror_msg_and_die(G.image_filename);
 		theme_file = xfdopen_for_read(fd);
diff --git a/miscutils/man.c b/miscutils/man.c
index 51baceb..f046e24 100644
--- a/miscutils/man.c
+++ b/miscutils/man.c
@@ -102,7 +102,7 @@ static int run_pipe(const char *pager, char *man_filename, int man, int level)
 
  ordinary_manpage:
 	close(STDIN_FILENO);
-	open_zipped(man_filename); /* guaranteed to use fd 0 (STDIN_FILENO) */
+	open_zipped(man_filename, /*fail_if_not_compressed:*/ 0); /* guaranteed to use fd 0 (STDIN_FILENO) */
 	/* "2>&1" is added so that nroff errors are shown in pager too.
 	 * Otherwise it may show just empty screen */
 	cmd = xasprintf(