| 1 |
# Copyright 1999-2012 Gentoo Foundation
|
| 2 |
# Distributed under the terms of the GNU General Public License v2
|
| 3 |
# $Header: /var/cvsroot/gentoo-x86/eclass/unpacker.eclass,v 1.8 2012/04/29 00:15:10 vapier Exp $
|
| 4 |
|
| 5 |
# @ECLASS: unpacker.eclass
|
| 6 |
# @MAINTAINER:
|
| 7 |
# base-system@gentoo.org
|
| 8 |
# @BLURB: helpers for extraneous file formats and consistent behavior across EAPIs
|
| 9 |
# @DESCRIPTION:
|
| 10 |
# Some extraneous file formats are not part of PMS, or are only in certain
|
| 11 |
# EAPIs. Rather than worrying about that, support the crazy cruft here
|
| 12 |
# and for all EAPI versions.
|
| 13 |
|
| 14 |
# Possible todos:
|
| 15 |
# - merge rpm unpacking
|
| 16 |
# - support partial unpacks?
|
| 17 |
|
| 18 |
if [[ ${___ECLASS_ONCE_UNPACKER} != "recur -_+^+_- spank" ]] ; then
|
| 19 |
___ECLASS_ONCE_UNPACKER="recur -_+^+_- spank"
|
| 20 |
|
| 21 |
# @ECLASS-VARIABLE: UNPACKER_BZ2
|
| 22 |
# @DEFAULT_UNSET
|
| 23 |
# @DESCRIPTION:
|
| 24 |
# Utility to use to decompress bzip2 files. Will dynamically pick between
|
| 25 |
# `pbzip2` and `bzip2`. Make sure your choice accepts the "-c" option.
|
| 26 |
# Note: this is meant for users to set, not ebuilds.
|
| 27 |
|
| 28 |
# for internal use only (unpack_pdv and unpack_makeself)
|
| 29 |
find_unpackable_file() {
|
| 30 |
local src=$1
|
| 31 |
if [[ -z ${src} ]] ; then
|
| 32 |
src=${DISTDIR}/${A}
|
| 33 |
else
|
| 34 |
if [[ ${src} == ./* ]] ; then
|
| 35 |
: # already what we want
|
| 36 |
elif [[ -e ${DISTDIR}/${src} ]] ; then
|
| 37 |
src=${DISTDIR}/${src}
|
| 38 |
elif [[ -e ${PWD}/${src} ]] ; then
|
| 39 |
src=${PWD}/${src}
|
| 40 |
elif [[ -e ${src} ]] ; then
|
| 41 |
src=${src}
|
| 42 |
fi
|
| 43 |
fi
|
| 44 |
[[ ! -e ${src} ]] && return 1
|
| 45 |
echo "${src}"
|
| 46 |
}
|
| 47 |
|
| 48 |
unpack_banner() {
|
| 49 |
echo ">>> Unpacking ${1##*/} to ${PWD}"
|
| 50 |
}
|
| 51 |
|
| 52 |
# @FUNCTION: unpack_pdv
|
| 53 |
# @USAGE: <file to unpack> <size of off_t>
|
| 54 |
# @DESCRIPTION:
|
| 55 |
# Unpack those pesky pdv generated files ...
|
| 56 |
# They're self-unpacking programs with the binary package stuffed in
|
| 57 |
# the middle of the archive. Valve seems to use it a lot ... too bad
|
| 58 |
# it seems to like to segfault a lot :(. So lets take it apart ourselves.
|
| 59 |
#
|
| 60 |
# You have to specify the off_t size ... I have no idea how to extract that
|
| 61 |
# information out of the binary executable myself. Basically you pass in
|
| 62 |
# the size of the off_t type (in bytes) on the machine that built the pdv
|
| 63 |
# archive.
|
| 64 |
#
|
| 65 |
# One way to determine this is by running the following commands:
|
| 66 |
#
|
| 67 |
# @CODE
|
| 68 |
# strings <pdv archive> | grep lseek
|
| 69 |
# strace -elseek <pdv archive>
|
| 70 |
# @CODE
|
| 71 |
#
|
| 72 |
# Basically look for the first lseek command (we do the strings/grep because
|
| 73 |
# sometimes the function call is _llseek or something) and steal the 2nd
|
| 74 |
# parameter. Here is an example:
|
| 75 |
#
|
| 76 |
# @CODE
|
| 77 |
# vapier@vapier 0 pdv_unpack # strings hldsupdatetool.bin | grep lseek
|
| 78 |
# lseek
|
| 79 |
# vapier@vapier 0 pdv_unpack # strace -elseek ./hldsupdatetool.bin
|
| 80 |
# lseek(3, -4, SEEK_END) = 2981250
|
| 81 |
# @CODE
|
| 82 |
#
|
| 83 |
# Thus we would pass in the value of '4' as the second parameter.
|
| 84 |
unpack_pdv() {
|
| 85 |
local src=$(find_unpackable_file "$1")
|
| 86 |
local sizeoff_t=$2
|
| 87 |
|
| 88 |
[[ -z ${src} ]] && die "Could not locate source for '$1'"
|
| 89 |
[[ -z ${sizeoff_t} ]] && die "No idea what off_t size was used for this pdv :("
|
| 90 |
|
| 91 |
unpack_banner "${src}"
|
| 92 |
|
| 93 |
local metaskip=$(tail -c ${sizeoff_t} "${src}" | hexdump -e \"%i\")
|
| 94 |
local tailskip=$(tail -c $((${sizeoff_t}*2)) "${src}" | head -c ${sizeoff_t} | hexdump -e \"%i\")
|
| 95 |
|
| 96 |
# grab metadata for debug reasons
|
| 97 |
local metafile="${T}/${FUNCNAME}.meta"
|
| 98 |
tail -c +$((${metaskip}+1)) "${src}" > "${metafile}"
|
| 99 |
|
| 100 |
# rip out the final file name from the metadata
|
| 101 |
local datafile=$(tail -c +$((${metaskip}+1)) "${src}" | strings | head -n 1)
|
| 102 |
datafile=$(basename "${datafile}")
|
| 103 |
|
| 104 |
# now lets uncompress/untar the file if need be
|
| 105 |
local tmpfile="${T}/${FUNCNAME}"
|
| 106 |
tail -c +$((${tailskip}+1)) ${src} 2>/dev/null | head -c 512 > "${tmpfile}"
|
| 107 |
|
| 108 |
local iscompressed=$(file -b "${tmpfile}")
|
| 109 |
if [[ ${iscompressed:0:8} == "compress" ]] ; then
|
| 110 |
iscompressed=1
|
| 111 |
mv "${tmpfile}"{,.Z}
|
| 112 |
gunzip "${tmpfile}"
|
| 113 |
else
|
| 114 |
iscompressed=0
|
| 115 |
fi
|
| 116 |
local istar=$(file -b "${tmpfile}")
|
| 117 |
if [[ ${istar:0:9} == "POSIX tar" ]] ; then
|
| 118 |
istar=1
|
| 119 |
else
|
| 120 |
istar=0
|
| 121 |
fi
|
| 122 |
|
| 123 |
#for some reason gzip dies with this ... dd cant provide buffer fast enough ?
|
| 124 |
#dd if=${src} ibs=${metaskip} count=1 \
|
| 125 |
# | dd ibs=${tailskip} skip=1 \
|
| 126 |
# | gzip -dc \
|
| 127 |
# > ${datafile}
|
| 128 |
if [ ${iscompressed} -eq 1 ] ; then
|
| 129 |
if [ ${istar} -eq 1 ] ; then
|
| 130 |
tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \
|
| 131 |
| head -c $((${metaskip}-${tailskip})) \
|
| 132 |
| tar -xzf -
|
| 133 |
else
|
| 134 |
tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \
|
| 135 |
| head -c $((${metaskip}-${tailskip})) \
|
| 136 |
| gzip -dc \
|
| 137 |
> ${datafile}
|
| 138 |
fi
|
| 139 |
else
|
| 140 |
if [ ${istar} -eq 1 ] ; then
|
| 141 |
tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \
|
| 142 |
| head -c $((${metaskip}-${tailskip})) \
|
| 143 |
| tar --no-same-owner -xf -
|
| 144 |
else
|
| 145 |
tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \
|
| 146 |
| head -c $((${metaskip}-${tailskip})) \
|
| 147 |
> ${datafile}
|
| 148 |
fi
|
| 149 |
fi
|
| 150 |
true
|
| 151 |
#[ -s "${datafile}" ] || die "failure unpacking pdv ('${metaskip}' '${tailskip}' '${datafile}')"
|
| 152 |
#assert "failure unpacking pdv ('${metaskip}' '${tailskip}' '${datafile}')"
|
| 153 |
}
|
| 154 |
|
| 155 |
# @FUNCTION: unpack_makeself
|
| 156 |
# @USAGE: [file to unpack] [offset] [tail|dd]
|
| 157 |
# @DESCRIPTION:
|
| 158 |
# Unpack those pesky makeself generated files ...
|
| 159 |
# They're shell scripts with the binary package tagged onto
|
| 160 |
# the end of the archive. Loki utilized the format as does
|
| 161 |
# many other game companies.
|
| 162 |
#
|
| 163 |
# If the file is not specified, then ${A} is used. If the
|
| 164 |
# offset is not specified then we will attempt to extract
|
| 165 |
# the proper offset from the script itself.
|
| 166 |
unpack_makeself() {
|
| 167 |
local src_input=${1:-${A}}
|
| 168 |
local src=$(find_unpackable_file "${src_input}")
|
| 169 |
local skip=$2
|
| 170 |
local exe=$3
|
| 171 |
|
| 172 |
[[ -z ${src} ]] && die "Could not locate source for '${src_input}'"
|
| 173 |
|
| 174 |
unpack_banner "${src}"
|
| 175 |
|
| 176 |
if [[ -z ${skip} ]] ; then
|
| 177 |
local ver=$(grep -m1 -a '#.*Makeself' "${src}" | awk '{print $NF}')
|
| 178 |
local skip=0
|
| 179 |
exe=tail
|
| 180 |
case ${ver} in
|
| 181 |
1.5.*|1.6.0-nv) # tested 1.5.{3,4,5} ... guessing 1.5.x series is same
|
| 182 |
skip=$(grep -a ^skip= "${src}" | cut -d= -f2)
|
| 183 |
;;
|
| 184 |
2.0|2.0.1)
|
| 185 |
skip=$(grep -a ^$'\t'tail "${src}" | awk '{print $2}' | cut -b2-)
|
| 186 |
;;
|
| 187 |
2.1.1)
|
| 188 |
skip=$(grep -a ^offset= "${src}" | awk '{print $2}' | cut -b2-)
|
| 189 |
(( skip++ ))
|
| 190 |
;;
|
| 191 |
2.1.2)
|
| 192 |
skip=$(grep -a ^offset= "${src}" | awk '{print $3}' | head -n 1)
|
| 193 |
(( skip++ ))
|
| 194 |
;;
|
| 195 |
2.1.3)
|
| 196 |
skip=`grep -a ^offset= "${src}" | awk '{print $3}'`
|
| 197 |
(( skip++ ))
|
| 198 |
;;
|
| 199 |
2.1.4|2.1.5|2.1.6)
|
| 200 |
skip=$(grep -a offset=.*head.*wc "${src}" | awk '{print $3}' | head -n 1)
|
| 201 |
skip=$(head -n ${skip} "${src}" | wc -c)
|
| 202 |
exe="dd"
|
| 203 |
;;
|
| 204 |
*)
|
| 205 |
eerror "I'm sorry, but I was unable to support the Makeself file."
|
| 206 |
eerror "The version I detected was '${ver}'."
|
| 207 |
eerror "Please file a bug about the file ${src##*/} at"
|
| 208 |
eerror "http://bugs.gentoo.org/ so that support can be added."
|
| 209 |
die "makeself version '${ver}' not supported"
|
| 210 |
;;
|
| 211 |
esac
|
| 212 |
debug-print "Detected Makeself version ${ver} ... using ${skip} as offset"
|
| 213 |
fi
|
| 214 |
case ${exe} in
|
| 215 |
tail) exe="tail -n +${skip} '${src}'";;
|
| 216 |
dd) exe="dd ibs=${skip} skip=1 if='${src}'";;
|
| 217 |
*) die "makeself cant handle exe '${exe}'"
|
| 218 |
esac
|
| 219 |
|
| 220 |
# lets grab the first few bytes of the file to figure out what kind of archive it is
|
| 221 |
local filetype tmpfile="${T}/${FUNCNAME}"
|
| 222 |
eval ${exe} 2>/dev/null | head -c 512 > "${tmpfile}"
|
| 223 |
filetype=$(file -b "${tmpfile}") || die
|
| 224 |
case ${filetype} in
|
| 225 |
*tar\ archive*)
|
| 226 |
eval ${exe} | tar --no-same-owner -xf -
|
| 227 |
;;
|
| 228 |
bzip2*)
|
| 229 |
eval ${exe} | bzip2 -dc | tar --no-same-owner -xf -
|
| 230 |
;;
|
| 231 |
gzip*)
|
| 232 |
eval ${exe} | tar --no-same-owner -xzf -
|
| 233 |
;;
|
| 234 |
compress*)
|
| 235 |
eval ${exe} | gunzip | tar --no-same-owner -xf -
|
| 236 |
;;
|
| 237 |
*)
|
| 238 |
eerror "Unknown filetype \"${filetype}\" ?"
|
| 239 |
false
|
| 240 |
;;
|
| 241 |
esac
|
| 242 |
assert "failure unpacking (${filetype}) makeself ${src##*/} ('${ver}' +${skip})"
|
| 243 |
}
|
| 244 |
|
| 245 |
# @FUNCTION: unpack_deb
|
| 246 |
# @USAGE: <one deb to unpack>
|
| 247 |
# @DESCRIPTION:
|
| 248 |
# Unpack a Debian .deb archive in style.
|
| 249 |
unpack_deb() {
|
| 250 |
[[ $# -eq 1 ]] || die "Usage: ${FUNCNAME} <file>"
|
| 251 |
|
| 252 |
local deb=$(find_unpackable_file "$1")
|
| 253 |
|
| 254 |
unpack_banner "${deb}"
|
| 255 |
|
| 256 |
# on AIX ar doesn't work out as their ar used a different format
|
| 257 |
# from what GNU ar (and thus what .deb files) produce
|
| 258 |
if [[ -n ${EPREFIX} ]] ; then
|
| 259 |
{
|
| 260 |
read # global header
|
| 261 |
[[ ${REPLY} = "!<arch>" ]] || die "${deb} does not seem to be a deb archive"
|
| 262 |
local f timestamp uid gid mode size magic
|
| 263 |
while read f timestamp uid gid mode size magic ; do
|
| 264 |
[[ -n ${f} && -n ${size} ]] || continue # ignore empty lines
|
| 265 |
if [[ ${f} = "data.tar"* ]] ; then
|
| 266 |
head -c "${size}" > "${f}"
|
| 267 |
else
|
| 268 |
head -c "${size}" > /dev/null # trash it
|
| 269 |
fi
|
| 270 |
done
|
| 271 |
} < "${deb}"
|
| 272 |
else
|
| 273 |
ar x "${deb}"
|
| 274 |
fi
|
| 275 |
|
| 276 |
unpacker ./data.tar*
|
| 277 |
}
|
| 278 |
|
| 279 |
# @FUNCTION: _unpacker
|
| 280 |
# @USAGE: <one archive to unpack>
|
| 281 |
# @INTERNAL
|
| 282 |
# @DESCRIPTION:
|
| 283 |
# Unpack the specified archive. We only operate on one archive here
|
| 284 |
# to keep down on the looping logic (that is handled by `unpacker`).
|
| 285 |
_unpacker() {
|
| 286 |
[[ $# -eq 1 ]] || die "Usage: ${FUNCNAME} <file>"
|
| 287 |
|
| 288 |
local a=$1
|
| 289 |
local m=$(echo "${a}" | tr '[:upper:]' '[:lower:]')
|
| 290 |
a=$(find_unpackable_file "${a}")
|
| 291 |
|
| 292 |
# first figure out the decompression method
|
| 293 |
case ${m} in
|
| 294 |
*.bz2|*.tbz|*.tbz2)
|
| 295 |
local bzcmd=${PORTAGE_BZIP2_COMMAND:-$(type -P pbzip2 || type -P bzip2)}
|
| 296 |
local bzuncmd=${PORTAGE_BUNZIP2_COMMAND:-${bzcmd} -d}
|
| 297 |
: ${UNPACKER_BZ2:=${bzuncmd}}
|
| 298 |
comp="${UNPACKER_BZ2} -c"
|
| 299 |
;;
|
| 300 |
*.z|*.gz|*.tgz)
|
| 301 |
comp="gzip -dc" ;;
|
| 302 |
*.lzma|*.xz|*.txz)
|
| 303 |
comp="xz -dc" ;;
|
| 304 |
*) comp="" ;;
|
| 305 |
esac
|
| 306 |
|
| 307 |
# then figure out if there are any archiving aspects
|
| 308 |
arch=""
|
| 309 |
case ${m} in
|
| 310 |
*.tgz|*.tbz|*.tbz2|*.txz|*.tar.*|*.tar)
|
| 311 |
arch="tar --no-same-owner -xof" ;;
|
| 312 |
*.deb)
|
| 313 |
arch="unpack_deb" ;;
|
| 314 |
*.run)
|
| 315 |
arch="unpack_makeself" ;;
|
| 316 |
*.sh)
|
| 317 |
# Not all shell scripts are makeself
|
| 318 |
if head -n 30 "${a}" | grep -qs '#.*Makeself' ; then
|
| 319 |
arch="unpack_makeself"
|
| 320 |
fi
|
| 321 |
;;
|
| 322 |
*.bin)
|
| 323 |
# Makeself archives can be annoyingly named
|
| 324 |
if head -c 100 "${a}" | grep -qs '#.*Makeself' ; then
|
| 325 |
arch="unpack_makeself"
|
| 326 |
fi
|
| 327 |
;;
|
| 328 |
esac
|
| 329 |
|
| 330 |
# finally do the unpack
|
| 331 |
if [[ -z ${arch}${comp} ]] ; then
|
| 332 |
unpack "$1"
|
| 333 |
return $?
|
| 334 |
fi
|
| 335 |
|
| 336 |
[[ ${arch} != unpack_* ]] && unpack_banner "${a}"
|
| 337 |
|
| 338 |
if [[ -z ${arch} ]] ; then
|
| 339 |
# Need to decompress the file into $PWD #408801
|
| 340 |
local _a=${a%.*}
|
| 341 |
${comp} "${a}" > "${_a##*/}"
|
| 342 |
elif [[ -z ${comp} ]] ; then
|
| 343 |
${arch} "${a}"
|
| 344 |
else
|
| 345 |
${comp} "${a}" | ${arch} -
|
| 346 |
fi
|
| 347 |
|
| 348 |
assert "unpacking ${a} failed (comp=${comp} arch=${arch})"
|
| 349 |
}
|
| 350 |
|
| 351 |
# @FUNCTION: unpacker
|
| 352 |
# @USAGE: [archives to unpack]
|
| 353 |
# @DESCRIPTION:
|
| 354 |
# This works in the same way that `unpack` does. If you don't specify
|
| 355 |
# any files, it will default to ${A}.
|
| 356 |
unpacker() {
|
| 357 |
local a
|
| 358 |
[[ $# -eq 0 ]] && set -- ${A}
|
| 359 |
for a ; do _unpacker "${a}" ; done
|
| 360 |
}
|
| 361 |
|
| 362 |
# @FUNCTION: unpacker_src_unpack
|
| 363 |
# @DESCRIPTION:
|
| 364 |
# Run `unpacker` to unpack all our stuff.
|
| 365 |
unpacker_src_unpack() {
|
| 366 |
unpacker
|
| 367 |
}
|
| 368 |
|
| 369 |
# @FUNCTION: unpacker_src_uri_depends
|
| 370 |
# @USAGE: [archives that we will unpack]
|
| 371 |
# @RETURN: Dependencies needed to unpack all the archives
|
| 372 |
# @DESCRIPTION:
|
| 373 |
# Walk all the specified files (defaults to $SRC_URI) and figure out the
|
| 374 |
# dependencies that are needed to unpack things.
|
| 375 |
#
|
| 376 |
# Note: USE flags are not yet handled.
|
| 377 |
unpacker_src_uri_depends() {
|
| 378 |
local uri deps d
|
| 379 |
|
| 380 |
[[ $# -eq 0 ]] && set -- ${SRC_URI}
|
| 381 |
|
| 382 |
for uri in "$@" ; do
|
| 383 |
case ${uri} in
|
| 384 |
*.rar|*.RAR)
|
| 385 |
d="app-arch/unrar" ;;
|
| 386 |
*.7z)
|
| 387 |
d="app-arch/p7zip" ;;
|
| 388 |
*.xz)
|
| 389 |
d="app-arch/xz-utils" ;;
|
| 390 |
esac
|
| 391 |
deps+=" ${d}"
|
| 392 |
done
|
| 393 |
|
| 394 |
echo "${deps}"
|
| 395 |
}
|
| 396 |
|
| 397 |
EXPORT_FUNCTIONS src_unpack
|
| 398 |
|
| 399 |
fi
|