1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: unpacker.eclass
7 # base-system@gentoo.org
8 # @BLURB: helpers for extraneous file formats and consistent behavior across EAPIs
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.
15 # - merge rpm unpacking
16 # - support partial unpacks?
18 if [[ -z ${_UNPACKER_ECLASS} ]]; then
21 # @ECLASS-VARIABLE: UNPACKER_BZ2
24 # Utility to use to decompress bzip2 files. Will dynamically pick between
25 # `pbzip2` and `bzip2`. Make sure your choice accepts the "-dc" options.
26 # Note: this is meant for users to set, not ebuilds.
28 # @ECLASS-VARIABLE: UNPACKER_LZIP
31 # Utility to use to decompress lzip files. Will dynamically pick between
32 # `plzip`, `pdlzip` and `lzip`. Make sure your choice accepts the "-dc" options.
33 # Note: this is meant for users to set, not ebuilds.
35 # for internal use only (unpack_pdv and unpack_makeself)
36 find_unpackable_file() {
38 if [[ -z ${src} ]] ; then
41 if [[ ${src} == ./* ]] ; then
42 : # already what we want
43 elif [[ -e ${DISTDIR}/${src} ]] ; then
45 elif [[ -e ${PWD}/${src} ]] ; then
47 elif [[ -e ${src} ]] ; then
51 [[ ! -e ${src} ]] && return 1
56 echo ">>> Unpacking ${1##*/} to ${PWD}"
59 # @FUNCTION: unpack_pdv
60 # @USAGE: <file to unpack> <size of off_t>
62 # Unpack those pesky pdv generated files ...
63 # They're self-unpacking programs with the binary package stuffed in
64 # the middle of the archive. Valve seems to use it a lot ... too bad
65 # it seems to like to segfault a lot :(. So lets take it apart ourselves.
67 # You have to specify the off_t size ... I have no idea how to extract that
68 # information out of the binary executable myself. Basically you pass in
69 # the size of the off_t type (in bytes) on the machine that built the pdv
72 # One way to determine this is by running the following commands:
75 # strings <pdv archive> | grep lseek
76 # strace -elseek <pdv archive>
79 # Basically look for the first lseek command (we do the strings/grep because
80 # sometimes the function call is _llseek or something) and steal the 2nd
81 # parameter. Here is an example:
84 # $ strings hldsupdatetool.bin | grep lseek
86 # $ strace -elseek ./hldsupdatetool.bin
87 # lseek(3, -4, SEEK_END) = 2981250
90 # Thus we would pass in the value of '4' as the second parameter.
92 local src=$(find_unpackable_file "$1")
95 [[ -z ${src} ]] && die "Could not locate source for '$1'"
96 [[ -z ${sizeoff_t} ]] && die "No idea what off_t size was used for this pdv :("
98 unpack_banner "${src}"
100 local metaskip=$(tail -c ${sizeoff_t} "${src}" | hexdump -e \"%i\")
101 local tailskip=$(tail -c $((${sizeoff_t}*2)) "${src}" | head -c ${sizeoff_t} | hexdump -e \"%i\")
103 # grab metadata for debug reasons
104 local metafile="${T}/${FUNCNAME}.meta"
105 tail -c +$((${metaskip}+1)) "${src}" > "${metafile}"
107 # rip out the final file name from the metadata
108 local datafile=$(tail -c +$((${metaskip}+1)) "${src}" | strings | head -n 1)
109 datafile=$(basename "${datafile}")
111 # now lets uncompress/untar the file if need be
112 local tmpfile="${T}/${FUNCNAME}"
113 tail -c +$((${tailskip}+1)) ${src} 2>/dev/null | head -c 512 > "${tmpfile}"
115 local iscompressed=$(file -b "${tmpfile}")
116 if [[ ${iscompressed:0:8} == "compress" ]] ; then
123 local istar=$(file -b "${tmpfile}")
124 if [[ ${istar:0:9} == "POSIX tar" ]] ; then
130 #for some reason gzip dies with this ... dd cant provide buffer fast enough ?
131 #dd if=${src} ibs=${metaskip} count=1 \
132 # | dd ibs=${tailskip} skip=1 \
135 if [ ${iscompressed} -eq 1 ] ; then
136 if [ ${istar} -eq 1 ] ; then
137 tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \
138 | head -c $((${metaskip}-${tailskip})) \
141 tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \
142 | head -c $((${metaskip}-${tailskip})) \
147 if [ ${istar} -eq 1 ] ; then
148 tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \
149 | head -c $((${metaskip}-${tailskip})) \
150 | tar --no-same-owner -xf -
152 tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \
153 | head -c $((${metaskip}-${tailskip})) \
158 #[ -s "${datafile}" ] || die "failure unpacking pdv ('${metaskip}' '${tailskip}' '${datafile}')"
159 #assert "failure unpacking pdv ('${metaskip}' '${tailskip}' '${datafile}')"
162 # @FUNCTION: unpack_makeself
163 # @USAGE: [file to unpack] [offset] [tail|dd]
165 # Unpack those pesky makeself generated files ...
166 # They're shell scripts with the binary package tagged onto
167 # the end of the archive. Loki utilized the format as does
168 # many other game companies.
170 # If the file is not specified, then ${A} is used. If the
171 # offset is not specified then we will attempt to extract
172 # the proper offset from the script itself.
174 local src_input=${1:-${A}}
175 local src=$(find_unpackable_file "${src_input}")
179 [[ -z ${src} ]] && die "Could not locate source for '${src_input}'"
181 unpack_banner "${src}"
183 if [[ -z ${skip} ]] ; then
184 local ver=$(grep -m1 -a '#.*Makeself' "${src}" | awk '{print $NF}')
188 1.5.*|1.6.0-nv*) # tested 1.5.{3,4,5} ... guessing 1.5.x series is same
189 skip=$(grep -a ^skip= "${src}" | cut -d= -f2)
192 skip=$(grep -a ^$'\t'tail "${src}" | awk '{print $2}' | cut -b2-)
195 skip=$(grep -a ^offset= "${src}" | awk '{print $2}' | cut -b2-)
199 skip=$(grep -a ^offset= "${src}" | awk '{print $3}' | head -n 1)
203 skip=`grep -a ^offset= "${src}" | awk '{print $3}'`
206 2.1.4|2.1.5|2.1.6|2.2.0)
207 skip=$(grep -a offset=.*head.*wc "${src}" | awk '{print $3}' | head -n 1)
208 skip=$(head -n ${skip} "${src}" | wc -c)
212 eerror "I'm sorry, but I was unable to support the Makeself file."
213 eerror "The version I detected was '${ver}'."
214 eerror "Please file a bug about the file ${src##*/} at"
215 eerror "https://bugs.gentoo.org/ so that support can be added."
216 die "makeself version '${ver}' not supported"
219 debug-print "Detected Makeself version ${ver} ... using ${skip} as offset"
222 tail) exe="tail -n +${skip} '${src}'";;
223 dd) exe="dd ibs=${skip} skip=1 if='${src}'";;
224 *) die "makeself cant handle exe '${exe}'"
227 # lets grab the first few bytes of the file to figure out what kind of archive it is
228 local filetype tmpfile="${T}/${FUNCNAME}"
229 eval ${exe} 2>/dev/null | head -c 512 > "${tmpfile}"
230 filetype=$(file -b "${tmpfile}") || die
233 eval ${exe} | tar --no-same-owner -xf -
236 eval ${exe} | bzip2 -dc | tar --no-same-owner -xf -
239 eval ${exe} | tar --no-same-owner -xzf -
242 eval ${exe} | gunzip | tar --no-same-owner -xf -
245 eval ${exe} | unxz | tar --no-same-owner -xf -
248 eerror "Unknown filetype \"${filetype}\" ?"
252 assert "failure unpacking (${filetype}) makeself ${src##*/} ('${ver}' +${skip})"
255 # @FUNCTION: unpack_deb
256 # @USAGE: <one deb to unpack>
258 # Unpack a Debian .deb archive in style.
260 [[ $# -eq 1 ]] || die "Usage: ${FUNCNAME} <file>"
262 local deb=$(find_unpackable_file "$1")
264 unpack_banner "${deb}"
266 # on AIX ar doesn't work out as their ar used a different format
267 # from what GNU ar (and thus what .deb files) produce
268 if [[ -n ${EPREFIX} ]] ; then
271 [[ ${REPLY} = "!<arch>" ]] || die "${deb} does not seem to be a deb archive"
272 local f timestamp uid gid mode size magic
273 while read f timestamp uid gid mode size magic ; do
274 [[ -n ${f} && -n ${size} ]] || continue # ignore empty lines
275 if [[ ${f} = "data.tar"* ]] ; then
276 head -c "${size}" > "${f}"
278 head -c "${size}" > /dev/null # trash it
288 # Clean things up #458658. No one seems to actually care about
289 # these, so wait until someone requests to do something else ...
290 rm -f debian-binary {control,data}.tar*
293 # @FUNCTION: unpack_cpio
294 # @USAGE: <one cpio to unpack>
296 # Unpack a cpio archive, file "-" means stdin.
298 [[ $# -eq 1 ]] || die "Usage: ${FUNCNAME} <file>"
300 # needed as cpio always reads from stdin
301 local cpio_cmd=( cpio --make-directories --extract --preserve-modification-time )
302 if [[ $1 == "-" ]] ; then
303 unpack_banner "stdin"
306 local cpio=$(find_unpackable_file "$1")
307 unpack_banner "${cpio}"
308 "${cpio_cmd[@]}" <"${cpio}"
312 # @FUNCTION: unpack_zip
315 # Unpack zip archives.
316 # This function ignores all non-fatal errors (i.e. warnings).
317 # That is useful for zip archives with extra crap attached
318 # (e.g. self-extracting archives).
320 [[ $# -eq 1 ]] || die "Usage: ${FUNCNAME} <file>"
322 local zip=$(find_unpackable_file "$1")
323 unpack_banner "${zip}"
326 [[ $? -le 1 ]] || die "unpacking ${zip} failed (arch=unpack_zip)"
329 # @FUNCTION: _unpacker
330 # @USAGE: <one archive to unpack>
333 # Unpack the specified archive. We only operate on one archive here
334 # to keep down on the looping logic (that is handled by `unpacker`).
336 [[ $# -eq 1 ]] || die "Usage: ${FUNCNAME} <file>"
339 local m=$(echo "${a}" | tr '[:upper:]' '[:lower:]')
340 a=$(find_unpackable_file "${a}")
342 # first figure out the decompression method
345 local bzcmd=${PORTAGE_BZIP2_COMMAND:-$(type -P pbzip2 || type -P bzip2)}
346 local bzuncmd=${PORTAGE_BUNZIP2_COMMAND:-${bzcmd} -d}
347 : ${UNPACKER_BZ2:=${bzuncmd}}
348 comp="${UNPACKER_BZ2} -c"
355 : ${UNPACKER_LZIP:=$(type -P plzip || type -P pdlzip || type -P lzip)}
356 comp="${UNPACKER_LZIP} -dc" ;;
360 # then figure out if there are any archiving aspects
363 *.tgz|*.tbz|*.tbz2|*.txz|*.tar.*|*.tar)
364 arch="tar --no-same-owner -xof" ;;
366 arch="unpack_cpio" ;;
370 arch="unpack_makeself" ;;
372 # Not all shell scripts are makeself
373 if head -n 30 "${a}" | grep -qs '#.*Makeself' ; then
374 arch="unpack_makeself"
378 # Makeself archives can be annoyingly named
379 if head -c 100 "${a}" | grep -qs '#.*Makeself' ; then
380 arch="unpack_makeself"
387 # finally do the unpack
388 if [[ -z ${arch}${comp} ]] ; then
393 [[ ${arch} != unpack_* ]] && unpack_banner "${a}"
395 if [[ -z ${arch} ]] ; then
396 # Need to decompress the file into $PWD #408801
398 ${comp} "${a}" > "${_a##*/}"
399 elif [[ -z ${comp} ]] ; then
402 ${comp} "${a}" | ${arch} -
405 assert "unpacking ${a} failed (comp=${comp} arch=${arch})"
408 # @FUNCTION: unpacker
409 # @USAGE: [archives to unpack]
411 # This works in the same way that `unpack` does. If you don't specify
412 # any files, it will default to ${A}.
415 [[ $# -eq 0 ]] && set -- ${A}
416 for a ; do _unpacker "${a}" ; done
419 # @FUNCTION: unpacker_src_unpack
421 # Run `unpacker` to unpack all our stuff.
422 unpacker_src_unpack() {
426 # @FUNCTION: unpacker_src_uri_depends
427 # @USAGE: [archives that we will unpack]
428 # @RETURN: Dependencies needed to unpack all the archives
430 # Walk all the specified files (defaults to $SRC_URI) and figure out the
431 # dependencies that are needed to unpack things.
433 # Note: USE flags are not yet handled.
434 unpacker_src_uri_depends() {
437 [[ $# -eq 0 ]] && set -- ${SRC_URI}
444 d="app-arch/unrar" ;;
446 d="app-arch/p7zip" ;;
448 d="app-arch/xz-utils" ;;
450 d="app-arch/unzip" ;;
452 d="|| ( app-arch/plzip app-arch/pdlzip app-arch/lzip )" ;;
460 EXPORT_FUNCTIONS src_unpack