| 1 | # Copyright 1999-2012 Gentoo Foundation |
1 | # Copyright 1999-2012 Gentoo Foundation |
| 2 | # Distributed under the terms of the GNU General Public License v2 |
2 | # Distributed under the terms of the GNU General Public License v2 |
| 3 | # $Header: /var/cvsroot/gentoo-x86/eclass/unpacker.eclass,v 1.1 2012/02/05 04:48:42 vapier Exp $ |
3 | # $Header: /var/cvsroot/gentoo-x86/eclass/unpacker.eclass,v 1.9 2012/05/11 07:46:44 vapier Exp $ |
| 4 | |
4 | |
| 5 | # @ECLASS: unpacker.eclass |
5 | # @ECLASS: unpacker.eclass |
| 6 | # @MAINTAINER: |
6 | # @MAINTAINER: |
| 7 | # base-system@gentoo.org |
7 | # base-system@gentoo.org |
| 8 | # @BLURB: helpers for extraneous file formats and consistent behavior across EAPIs |
8 | # @BLURB: helpers for extraneous file formats and consistent behavior across EAPIs |
| … | |
… | |
| 92 | |
92 | |
| 93 | local metaskip=$(tail -c ${sizeoff_t} "${src}" | hexdump -e \"%i\") |
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\") |
94 | local tailskip=$(tail -c $((${sizeoff_t}*2)) "${src}" | head -c ${sizeoff_t} | hexdump -e \"%i\") |
| 95 | |
95 | |
| 96 | # grab metadata for debug reasons |
96 | # grab metadata for debug reasons |
| 97 | local metafile=$(emktemp) |
97 | local metafile="${T}/${FUNCNAME}.meta" |
| 98 | tail -c +$((${metaskip}+1)) "${src}" > "${metafile}" |
98 | tail -c +$((${metaskip}+1)) "${src}" > "${metafile}" |
| 99 | |
99 | |
| 100 | # rip out the final file name from the metadata |
100 | # rip out the final file name from the metadata |
| 101 | local datafile=$(tail -c +$((${metaskip}+1)) "${src}" | strings | head -n 1) |
101 | local datafile=$(tail -c +$((${metaskip}+1)) "${src}" | strings | head -n 1) |
| 102 | datafile=$(basename "${datafile}") |
102 | datafile=$(basename "${datafile}") |
| 103 | |
103 | |
| 104 | # now lets uncompress/untar the file if need be |
104 | # now lets uncompress/untar the file if need be |
| 105 | local tmpfile=$(emktemp) |
105 | local tmpfile="${T}/${FUNCNAME}" |
| 106 | tail -c +$((${tailskip}+1)) ${src} 2>/dev/null | head -c 512 > ${tmpfile} |
106 | tail -c +$((${tailskip}+1)) ${src} 2>/dev/null | head -c 512 > "${tmpfile}" |
| 107 | |
107 | |
| 108 | local iscompressed=$(file -b "${tmpfile}") |
108 | local iscompressed=$(file -b "${tmpfile}") |
| 109 | if [[ ${iscompressed:0:8} == "compress" ]] ; then |
109 | if [[ ${iscompressed:0:8} == "compress" ]] ; then |
| 110 | iscompressed=1 |
110 | iscompressed=1 |
| 111 | mv ${tmpfile}{,.Z} |
111 | mv "${tmpfile}"{,.Z} |
| 112 | gunzip ${tmpfile} |
112 | gunzip "${tmpfile}" |
| 113 | else |
113 | else |
| 114 | iscompressed=0 |
114 | iscompressed=0 |
| 115 | fi |
115 | fi |
| 116 | local istar=$(file -b "${tmpfile}") |
116 | local istar=$(file -b "${tmpfile}") |
| 117 | if [[ ${istar:0:9} == "POSIX tar" ]] ; then |
117 | if [[ ${istar:0:9} == "POSIX tar" ]] ; then |
| … | |
… | |
| 125 | # | dd ibs=${tailskip} skip=1 \ |
125 | # | dd ibs=${tailskip} skip=1 \ |
| 126 | # | gzip -dc \ |
126 | # | gzip -dc \ |
| 127 | # > ${datafile} |
127 | # > ${datafile} |
| 128 | if [ ${iscompressed} -eq 1 ] ; then |
128 | if [ ${iscompressed} -eq 1 ] ; then |
| 129 | if [ ${istar} -eq 1 ] ; then |
129 | if [ ${istar} -eq 1 ] ; then |
| 130 | tail -c +$((${tailskip}+1)) ${src} 2>/dev/null \ |
130 | tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \ |
| 131 | | head -c $((${metaskip}-${tailskip})) \ |
131 | | head -c $((${metaskip}-${tailskip})) \ |
| 132 | | tar -xzf - |
132 | | tar -xzf - |
| 133 | else |
133 | else |
| 134 | tail -c +$((${tailskip}+1)) ${src} 2>/dev/null \ |
134 | tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \ |
| 135 | | head -c $((${metaskip}-${tailskip})) \ |
135 | | head -c $((${metaskip}-${tailskip})) \ |
| 136 | | gzip -dc \ |
136 | | gzip -dc \ |
| 137 | > ${datafile} |
137 | > ${datafile} |
| 138 | fi |
138 | fi |
| 139 | else |
139 | else |
| 140 | if [ ${istar} -eq 1 ] ; then |
140 | if [ ${istar} -eq 1 ] ; then |
| 141 | tail -c +$((${tailskip}+1)) ${src} 2>/dev/null \ |
141 | tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \ |
| 142 | | head -c $((${metaskip}-${tailskip})) \ |
142 | | head -c $((${metaskip}-${tailskip})) \ |
| 143 | | tar --no-same-owner -xf - |
143 | | tar --no-same-owner -xf - |
| 144 | else |
144 | else |
| 145 | tail -c +$((${tailskip}+1)) ${src} 2>/dev/null \ |
145 | tail -c +$((${tailskip}+1)) "${src}" 2>/dev/null \ |
| 146 | | head -c $((${metaskip}-${tailskip})) \ |
146 | | head -c $((${metaskip}-${tailskip})) \ |
| 147 | > ${datafile} |
147 | > ${datafile} |
| 148 | fi |
148 | fi |
| 149 | fi |
149 | fi |
| 150 | true |
150 | true |
| … | |
… | |
| 194 | ;; |
194 | ;; |
| 195 | 2.1.3) |
195 | 2.1.3) |
| 196 | skip=`grep -a ^offset= "${src}" | awk '{print $3}'` |
196 | skip=`grep -a ^offset= "${src}" | awk '{print $3}'` |
| 197 | (( skip++ )) |
197 | (( skip++ )) |
| 198 | ;; |
198 | ;; |
| 199 | 2.1.4|2.1.5) |
199 | 2.1.4|2.1.5|2.1.6) |
| 200 | skip=$(grep -a offset=.*head.*wc "${src}" | awk '{print $3}' | head -n 1) |
200 | skip=$(grep -a offset=.*head.*wc "${src}" | awk '{print $3}' | head -n 1) |
| 201 | skip=$(head -n ${skip} "${src}" | wc -c) |
201 | skip=$(head -n ${skip} "${src}" | wc -c) |
| 202 | exe="dd" |
202 | exe="dd" |
| 203 | ;; |
203 | ;; |
| 204 | *) |
204 | *) |
| … | |
… | |
| 216 | dd) exe="dd ibs=${skip} skip=1 if='${src}'";; |
216 | dd) exe="dd ibs=${skip} skip=1 if='${src}'";; |
| 217 | *) die "makeself cant handle exe '${exe}'" |
217 | *) die "makeself cant handle exe '${exe}'" |
| 218 | esac |
218 | esac |
| 219 | |
219 | |
| 220 | # lets grab the first few bytes of the file to figure out what kind of archive it is |
220 | # lets grab the first few bytes of the file to figure out what kind of archive it is |
| 221 | local filetype tmpfile=$(emktemp) |
221 | local filetype tmpfile="${T}/${FUNCNAME}" |
| 222 | eval ${exe} 2>/dev/null | head -c 512 > "${tmpfile}" |
222 | eval ${exe} 2>/dev/null | head -c 512 > "${tmpfile}" |
| 223 | filetype=$(file -b "${tmpfile}") || die |
223 | filetype=$(file -b "${tmpfile}") || die |
| 224 | case ${filetype} in |
224 | case ${filetype} in |
| 225 | *tar\ archive*) |
225 | *tar\ archive*) |
| 226 | eval ${exe} | tar --no-same-owner -xf - |
226 | eval ${exe} | tar --no-same-owner -xf - |
| … | |
… | |
| 251 | |
251 | |
| 252 | local deb=$(find_unpackable_file "$1") |
252 | local deb=$(find_unpackable_file "$1") |
| 253 | |
253 | |
| 254 | unpack_banner "${deb}" |
254 | unpack_banner "${deb}" |
| 255 | |
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 |
| 256 | ar x "${deb}" |
273 | ar x "${deb}" |
|
|
274 | fi |
|
|
275 | |
| 257 | unpack ./data.tar* |
276 | unpacker ./data.tar* |
| 258 | } |
277 | } |
| 259 | |
278 | |
| 260 | # @FUNCTION: _unpacker |
279 | # @FUNCTION: _unpacker |
| 261 | # @USAGE: <one archive to unpack> |
280 | # @USAGE: <one archive to unpack> |
| 262 | # @INTERNAL |
281 | # @INTERNAL |
| … | |
… | |
| 271 | a=$(find_unpackable_file "${a}") |
290 | a=$(find_unpackable_file "${a}") |
| 272 | |
291 | |
| 273 | # first figure out the decompression method |
292 | # first figure out the decompression method |
| 274 | case ${m} in |
293 | case ${m} in |
| 275 | *.bz2|*.tbz|*.tbz2) |
294 | *.bz2|*.tbz|*.tbz2) |
| 276 | local bzcmd=${PORTAGE_BZIP2_COMMAND:-$(type -P pbzip2 || bzip2)} |
295 | local bzcmd=${PORTAGE_BZIP2_COMMAND:-$(type -P pbzip2 || type -P bzip2)} |
| 277 | local bzuncmd=${PORTAGE_BUNZIP2_COMMAND:-${PORTAGE_BZIP2_COMMAND} -d} |
296 | local bzuncmd=${PORTAGE_BUNZIP2_COMMAND:-${bzcmd} -d} |
| 278 | : ${UNPACKER_BZ2:=${bzuncmd}} |
297 | : ${UNPACKER_BZ2:=${bzuncmd}} |
| 279 | comp="${UNPACKER_BZ2} -c" |
298 | comp="${UNPACKER_BZ2} -c" |
| 280 | ;; |
299 | ;; |
| 281 | *.z|*.gz|*.tgz) |
300 | *.z|*.gz|*.tgz) |
| 282 | comp="gzip -dc" ;; |
301 | comp="gzip -dc" ;; |
| … | |
… | |
| 284 | comp="xz -dc" ;; |
303 | comp="xz -dc" ;; |
| 285 | *) comp="" ;; |
304 | *) comp="" ;; |
| 286 | esac |
305 | esac |
| 287 | |
306 | |
| 288 | # then figure out if there are any archiving aspects |
307 | # then figure out if there are any archiving aspects |
|
|
308 | arch="" |
| 289 | case ${m} in |
309 | case ${m} in |
| 290 | *.tgz|*.tbz|*.tbz2|*.txz|*.tar.*|*.tar) |
310 | *.tgz|*.tbz|*.tbz2|*.txz|*.tar.*|*.tar) |
| 291 | arch="tar --no-same-owner -xof" ;; |
311 | arch="tar --no-same-owner -xof" ;; |
| 292 | *.deb) |
312 | *.deb) |
| 293 | arch="unpack_deb" ;; |
313 | arch="unpack_deb" ;; |
| 294 | *.run) |
314 | *.run) |
| 295 | arch="unpack_makeself" ;; |
315 | arch="unpack_makeself" ;; |
| 296 | *) arch="" ;; |
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 | ;; |
| 297 | esac |
328 | esac |
| 298 | |
329 | |
| 299 | # finally do the unpack |
330 | # finally do the unpack |
| 300 | if [[ -z ${arch}${comp} ]] ; then |
331 | if [[ -z ${arch}${comp} ]] ; then |
| 301 | unpack "${a}" |
332 | unpack "$1" |
| 302 | return $? |
333 | return $? |
| 303 | fi |
334 | fi |
| 304 | |
335 | |
| 305 | [[ ${arch} != unpack_* ]] && unpack_banner "${a}" |
336 | [[ ${arch} != unpack_* ]] && unpack_banner "${a}" |
| 306 | |
337 | |
| 307 | if [[ -z ${arch} ]] ; then |
338 | if [[ -z ${arch} ]] ; then |
|
|
339 | # Need to decompress the file into $PWD #408801 |
|
|
340 | local _a=${a%.*} |
| 308 | ${comp} "${a}" > "${a%.*}" |
341 | ${comp} "${a}" > "${_a##*/}" |
| 309 | elif [[ -z ${comp} ]] ; then |
342 | elif [[ -z ${comp} ]] ; then |
| 310 | ${arch} "${a}" |
343 | ${arch} "${a}" |
| 311 | else |
344 | else |
| 312 | ${comp} "${a}" | ${arch} - |
345 | ${comp} "${a}" | ${arch} - |
| 313 | fi |
346 | fi |