[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gentoo-dev
Subject:    [gentoo-dev] unpacker.eclass: add decompress probe helper
From:       Mike Frysinger <vapier () gentoo ! org>
Date:       2013-06-22 17:55:24
Message-ID: 201306221355.25904.vapier () gentoo ! org
[Download RAW message or body]


On Monday 17 June 2013 01:55:09 Mike Frysinger wrote:
> i wish we could merge with the file detection in unpack_makeself somehow

this patch should unify that aspect
-mike

--- unpacker.eclass	10 Apr 2013 14:47:49 -0000	1.13
+++ unpacker.eclass	22 Jun 2013 17:54:09 -0000
@@ -22,7 +22,14 @@ ___ECLASS_ONCE_UNPACKER="recur -_+^+_- s
 # @DEFAULT_UNSET
 # @DESCRIPTION:
 # Utility to use to decompress bzip2 files.  Will dynamically pick between
-# `pbzip2` and `bzip2`.  Make sure your choice accepts the "-c" option.
+# `pbzip2` and `bzip2`.  Make sure your choice accepts the "-dc" options.
+# Note: this is meant for users to set, not ebuilds.
+
+# @ECLASS-VARIABLE: UNPACKER_GZIP
+# @DEFAULT_UNSET
+# @DESCRIPTION:
+# Utility to use to decompress gzip files.  Will dynamically pick between
+# `gzip` and `pigz`.  Make sure your choice accepts the "-dc" options.
 # Note: this is meant for users to set, not ebuilds.
 
 # for internal use only (unpack_pdv and unpack_makeself)
@@ -74,9 +81,9 @@ unpack_banner() {
 # parameter.  Here is an example:
 #
 # @CODE
-# 	vapier@vapier 0 pdv_unpack # strings hldsupdatetool.bin | grep lseek
+# 	$ strings hldsupdatetool.bin | grep lseek
 # 	lseek
-# 	vapier@vapier 0 pdv_unpack # strace -elseek ./hldsupdatetool.bin
+# 	$ strace -elseek ./hldsupdatetool.bin
 # 	lseek(3, -4, SEEK_END)					= 2981250
 # @CODE
 #
@@ -105,19 +112,17 @@ unpack_pdv() {
 	local tmpfile="${T}/${FUNCNAME}"
 	tail -c +$((${tailskip}+1)) ${src} 2>/dev/null | head -c 512 > "${tmpfile}"
 
-	local iscompressed=$(file -b "${tmpfile}")
-	if [[ ${iscompressed:0:8} == "compress" ]] ; then
-		iscompressed=1
+	local comp=$(decompress_prog "${tmpfile}")
+	if [[ -n ${comp} ]] ; then
+		comp="| ${comp}"
 		mv "${tmpfile}"{,.Z}
 		gunzip "${tmpfile}"
-	else
-		iscompressed=0
 	fi
-	local istar=$(file -b "${tmpfile}")
-	if [[ ${istar:0:9} == "POSIX tar" ]] ; then
-		istar=1
+	local istar filetype=$(file -b "${tmpfile}")
+	if [[ ${filetype} == "POSIX tar"* ]] ; then
+		istar=true
 	else
-		istar=0
+		istar=false
 	fi
 
 	#for some reason gzip dies with this ... dd cant provide buffer fast enough ?
@@ -125,29 +130,18 @@ unpack_pdv() {
 	#	| dd ibs=${tailskip} skip=1 \
 	#	| gzip -dc \
 	#	> ${datafile}
-	if [ ${iscompressed} -eq 1 ] ; then
-		if [ ${istar} -eq 1 ] ; then
-			tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \
-				| head -c $((${metaskip}-${tailskip})) \
-				| tar -xzf -
-		else
-			tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \
-				| head -c $((${metaskip}-${tailskip})) \
-				| gzip -dc \
-				> ${datafile}
-		fi
+	(
+	th() {
+		tail -c +$(( tailskip + 1 )) "${src}" 2>/dev/null | \
+			head -c $(( metaskip - tailskip ))
+	}
+	if ${istar} ; then
+		eval th ${comp} | tar --no-same-owner -xf -
 	else
-		if [ ${istar} -eq 1 ] ; then
-			tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \
-				| head -c $((${metaskip}-${tailskip})) \
-				| tar --no-same-owner -xf -
-		else
-			tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \
-				| head -c $((${metaskip}-${tailskip})) \
-				> ${datafile}
-		fi
+		eval th ${comp} > "${datafile}"
 	fi
-	true
+	exit 0
+	)
 	#[ -s "${datafile}" ] || die "failure unpacking pdv ('${metaskip}' '${tailskip}' '${datafile}')"
 	#assert "failure unpacking pdv ('${metaskip}' '${tailskip}' '${datafile}')"
 }
@@ -218,31 +212,11 @@ unpack_makeself() {
 	esac
 
 	# lets grab the first few bytes of the file to figure out what kind of archive it is
-	local filetype tmpfile="${T}/${FUNCNAME}"
+	local tmpfile="${T}/${FUNCNAME}"
 	eval ${exe} 2>/dev/null | head -c 512 > "${tmpfile}"
-	filetype=$(file -b "${tmpfile}") || die
-	case ${filetype} in
-		*tar\ archive*)
-			eval ${exe} | tar --no-same-owner -xf -
-			;;
-		bzip2*)
-			eval ${exe} | bzip2 -dc | tar --no-same-owner -xf -
-			;;
-		gzip*)
-			eval ${exe} | tar --no-same-owner -xzf -
-			;;
-		compress*)
-			eval ${exe} | gunzip | tar --no-same-owner -xf -
-			;;
-		XZ*)
-			eval ${exe} | unxz | tar --no-same-owner -xf -
-			;;
-		*)
-			eerror "Unknown filetype \"${filetype}\" ?"
-			false
-			;;
-	esac
-	assert "failure unpacking (${filetype}) makeself ${src##*/} ('${ver}' +${skip})"
+	local comp=$(decompress_prog "${tmpfile}")
+	eval ${exe} | eval "${comp:+${comp} |}" tar --no-same-owner -xf -
+	assert "failure unpacking makeself ${src##*/} ('${ver}' +${skip})"
 }
 
 # @FUNCTION: unpack_deb
@@ -319,6 +293,54 @@ unpack_zip() {
 	[[ $? -le 1 ]] || die "unpacking ${zip} failed (arch=unpack_zip)"
 }
 
+# @FUNCTION: decompress_prog
+# @USAGE: <mime|format|filename>
+# @DESCRIPTION:
+# Get the program name (and args) needed to decompress things.
+# You can give this a mime type, or a format name (bz2/gz/xz/etc...),
+# or you can give it a filename.  If the type can be detected based on
+# the suffix alone, we'll use that, otherwise we'll rely on `file` to
+# probe the type.
+decompress_prog() {
+	[[ $# -eq 1 ]] || die "Usage: ${FUNCNAME} <mime|format|filename>"
+
+	local comp
+
+	case $1 in
+	application/x-bzip2\;*|\
+	bzip2|bz2|\
+	*.bz2|*.tbz|*.tbz2)
+		local bzcmd=${PORTAGE_BZIP2_COMMAND:-$(type -P pbzip2 || type -P bzip2)}
+		local bzuncmd=${PORTAGE_BUNZIP2_COMMAND:-${bzcmd} -d}
+		: ${UNPACKER_BZ2:=${bzuncmd}}
+		comp="${UNPACKER_BZ2} -dc"
+		;;
+
+	application/x-gzip\;*|\
+	application/x-compress\;*|\
+	gzip|gz|\
+	*.z|*.gz|*.tgz)
+		: ${UNPACKER_GZIP:=$(type -P pigz || type -P gzip)}
+		comp="${UNPACKER_GZIP} -dc"
+		;;
+
+	application/x-xz\;*|\
+	lzma|xz|\
+	*.lzma|*.xz|*.txz)
+		comp="xz -dc"
+		;;
+	esac
+
+	if [[ -n ${comp} ]] ; then
+		echo "${comp}"
+		return 0
+	fi
+
+	if [[ -s $1 ]] ; then
+		decompress_prog "$(file -ib "$1")"
+	fi
+}
+
 # @FUNCTION: _unpacker
 # @USAGE: <one archive to unpack>
 # @INTERNAL
@@ -333,19 +355,7 @@ _unpacker() {
 	a=$(find_unpackable_file "${a}")
 
 	# first figure out the decompression method
-	case ${m} in
-	*.bz2|*.tbz|*.tbz2)
-		local bzcmd=${PORTAGE_BZIP2_COMMAND:-$(type -P pbzip2 || type -P bzip2)}
-		local bzuncmd=${PORTAGE_BUNZIP2_COMMAND:-${bzcmd} -d}
-		: ${UNPACKER_BZ2:=${bzuncmd}}
-		comp="${UNPACKER_BZ2} -c"
-		;;
-	*.z|*.gz|*.tgz)
-		comp="gzip -dc" ;;
-	*.lzma|*.xz|*.txz)
-		comp="xz -dc" ;;
-	*)	comp="" ;;
-	esac
+	comp=$(decompress_prog "${n}")
 
 	# then figure out if there are any archiving aspects
 	arch=""

["signature.asc" (application/pgp-signature)]

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic