[git commit master 1/1] tar: fix a bug where autodetection messes up -z on extract

Denys Vlasenko vda.linux at googlemail.com
Sun Feb 6 19:01:11 UTC 2011


commit: http://git.busybox.net/busybox/commit/?id=aef441cb4d567da5575c498141b21eb38dc3fdaf
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 archival/libarchive/get_header_tar_gz.c |    4 ++--
 archival/tar.c                          |    6 ++++--
 include/archive.h                       |   20 ++++++++++----------
 testsuite/tar.tests                     |   14 ++++++++++++++
 4 files changed, 30 insertions(+), 14 deletions(-)

diff --git a/archival/libarchive/get_header_tar_gz.c b/archival/libarchive/get_header_tar_gz.c
index b09f869..889fed0 100644
--- a/archival/libarchive/get_header_tar_gz.c
+++ b/archival/libarchive/get_header_tar_gz.c
@@ -9,7 +9,7 @@
 char FAST_FUNC get_header_tar_gz(archive_handle_t *archive_handle)
 {
 #if BB_MMU
-	unsigned char magic[2];
+	uint16_t magic;
 #endif
 
 	/* Can't lseek over pipes */
@@ -21,7 +21,7 @@ char FAST_FUNC get_header_tar_gz(archive_handle_t *archive_handle)
 #if BB_MMU
 	xread(archive_handle->src_fd, &magic, 2);
 	/* Can skip this check, but error message will be less clear */
-	if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
+	if (magic != GZIP_MAGIC) {
 		bb_error_msg_and_die("invalid gzip magic");
 	}
 #endif
diff --git a/archival/tar.c b/archival/tar.c
index e9dc41f..1e3cecf 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -1060,8 +1060,10 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
 			tar_handle->src_fd = tar_fd;
 			tar_handle->seek = seek_by_read;
 		} else {
-			if (ENABLE_FEATURE_TAR_AUTODETECT && flags == O_RDONLY) {
-				get_header_ptr = get_header_tar;
+			if (ENABLE_FEATURE_TAR_AUTODETECT
+			 && flags == O_RDONLY
+			 && get_header_ptr == get_header_tar
+			) {
 				tar_handle->src_fd = open_zipped(tar_filename);
 				if (tar_handle->src_fd < 0)
 					bb_perror_msg_and_die("can't open '%s'", tar_filename);
diff --git a/include/archive.h b/include/archive.h
index ba6d323..49c4787 100644
--- a/include/archive.h
+++ b/include/archive.h
@@ -8,22 +8,22 @@ enum {
 #if BB_BIG_ENDIAN
 	COMPRESS_MAGIC = 0x1f9d,
 	GZIP_MAGIC  = 0x1f8b,
-	BZIP2_MAGIC = 'B' * 256 + 'Z',
+	BZIP2_MAGIC = 256 * 'B' + 'Z',
 	/* .xz signature: 0xfd, '7', 'z', 'X', 'Z', 0x00 */
 	/* More info at: http://tukaani.org/xz/xz-file-format.txt */
-	XZ_MAGIC1   = 0xfd * 256 + '7',
-	XZ_MAGIC2   = (('z' * 256 + 'X') * 256 + 'Z') * 256 + 0,
+	XZ_MAGIC1   = 256 * 0xfd + '7',
+	XZ_MAGIC2   = 256 * (256 * (256 * 'z' + 'X') + 'Z') + 0,
 	/* Different form: 32 bits, then 16 bits: */
-	XZ_MAGIC1a  = ((0xfd * 256 + '7') * 256 + 'z') * 256 + 'X',
-	XZ_MAGIC2a  = 'Z' * 256 + 0,
+	XZ_MAGIC1a  = 256 * (256 * (256 * 0xfd + '7') + 'z') + 'X',
+	XZ_MAGIC2a  = 256 * 'Z' + 0,
 #else
 	COMPRESS_MAGIC = 0x9d1f,
 	GZIP_MAGIC  = 0x8b1f,
-	BZIP2_MAGIC = 'Z' * 256 + 'B',
-	XZ_MAGIC1   = '7' * 256 + 0xfd,
-	XZ_MAGIC2   = ((0 * 256 + 'Z') * 256 + 'X') * 256 + 'z',
-	XZ_MAGIC1a  = (('X' * 256 + 'z') * 256 + '7') * 256 + 0xfd,
-	XZ_MAGIC2a  = 0 * 256 + 'Z',
+	BZIP2_MAGIC = 'B' + 'Z' * 256,
+	XZ_MAGIC1   = 0xfd + '7' * 256,
+	XZ_MAGIC2   = 'z' + ('X' + ('Z' + 0 * 256) * 256) * 256,
+	XZ_MAGIC1a  = 0xfd + ('7' + ('z' + 'X' * 256) * 256) * 256,
+	XZ_MAGIC2a  = 'Z' + 0 * 256,
 #endif
 };
 
diff --git a/testsuite/tar.tests b/testsuite/tar.tests
index f2f4e93..824d6d5 100755
--- a/testsuite/tar.tests
+++ b/testsuite/tar.tests
@@ -154,6 +154,20 @@ dr-xr-x--- input_dir
 SKIP=
 }
 
+# Had a bug where on extrace autodetect first "switched off" -z
+# and then failed to recognize .tgz extension
+testing "tar extract tgz" "\
+dd count=1 bs=1M if=/dev/zero of=F0 2>/dev/null
+tar -czf F0.tgz F0
+rm F0
+tar -xzvf F0.tgz && echo Ok
+rm F0 || echo BAD
+" "\
+F0
+Ok
+" \
+"" ""
+
 
 cd .. && rm -rf tar.tempdir || exit 1
 
-- 
1.7.3.4



More information about the busybox-cvs mailing list