aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/ebuild/utils/unpack
blob: 5f154cb2562f6125ccc896469190993f9f4570a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env bash
# vim: set sw=4 sts=4 et :

# Copyright (c) 2006, 2007, 2008, 2011 Ciaran McCreesh
#
# Based in part upon ebuild.sh from Portage, which is Copyright 1995-2005
# Gentoo Foundation and distributed under the terms of the GNU General
# Public License v2.
#
# This file is part of the Paludis package manager. Paludis is free software;
# you can redistribute it and/or modify it under the terms of the GNU General
# Public License as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307  USA

die()
{
    echo "unpack: error: $@" 1>&2
    if [[ -z ${PALUDIS_FAILURE_IS_NONFATAL} ]]; then
        echo "$(basename ${0} ): making ebuild PID ${EBUILD_KILL_PID} exit with error" 1>&2
        kill -s SIGUSR1 "${EBUILD_KILL_PID}"
    fi
    exit 123
}

assert()
{
    local _pipestatus="${PIPESTATUS[*]}"
    [[ -z "${_pipestatus//[ 0]/}" ]] || die "$@: $_pipestatus"
}

unpack_tar() {
    echo tar xf "${1}" --no-same-owner "${TAR_OPTIONS[@]}" 1>&2
    tar xf "${1}" --no-same-owner "${TAR_OPTIONS[@]}"
}

unpack_tar.gz() {
    echo tar zxf "${1}" --no-same-owner "${TAR_OPTIONS[@]}" 1>&2
    tar zxf "${1}" --no-same-owner "${TAR_OPTIONS[@]}"
}

unpack_tar.bz2() {
    echo tar jxf "${1}" --no-same-owner "${TAR_OPTIONS[@]}" ${UNPACKONLY:+"${UNPACKONLY}"} 1>&2
    tar jxf "${1}" --no-same-owner "${TAR_OPTIONS[@]}" ${UNPACKONLY:+"${UNPACKONLY}"}
}

unpack_zip() {
    echo unzip -qo "${1}" 1>&2
    unzip -qo "${1}"
}

unpack_gz() {
    echo gzip -dc "${1}" \> "${UNPACKTODIR}/$(basename "${1%.*}" )" 1>&2
    gzip -dc "${1}" > "${UNPACKTODIR}/$(basename "${1%.*}" )"
}

unpack_bz2() {
    echo bzip2 -dc "${1}" \> "${UNPACKTODIR}/$(basename "${1%.*}" )" 1>&2
    bzip2 -dc "${1}" > "${UNPACKTODIR}/$(basename "${1%.*}" )" || die "Couldn't unpack ${1}"
}

unpack_rar() {
    echo unrar x -idq "${1}" 1>&2
    unrar x -idq "${1}"
}

unpack_lha() {
    echo lha xfq "${1}" 1>&2
    lha xfq "${1}"
}

unpack_a() {
    echo ar x "${1}" 1>&2
    ar x "${1}" 1>&2
}

unpack_tar.lzma() {
    echo lzma -dc "${1}" \| strip_tar_corruption \| tar xf - --no-same-owner "${TAR_OPTIONS[@]}"
    lzma -dc "${1}" | strip_tar_corruption | tar xf - --no-same-owner "${TAR_OPTIONS[@]}"
    assert "Couldn't unpack ${1}"
}

unpack_lzma() {
    echo lzma -dc "${1}" \> "${UNPACKTODIR}/$(basename "${1%.*}" )" 1>&2
    lzma -dc "${1}" > "${UNPACKTODIR}/$(basename "${1%.*}" )"
}

unpack_7z() {
    echo 7z x -y "${1}"
    7z x -y "${1}"
}

unpack_tar.xz() {
    echo xz -dc "${1}" \| strip_tar_corruption \| tar xf - --no-same-owner "${TAR_OPTIONS[@]}"
    xz -dc "${1}" | strip_tar_corruption | tar xf - --no-same-owner "${TAR_OPTIONS[@]}"
    assert "Couldn't unpack ${1}"
}

unpack_xz() {
    echo xz -dc "${1}" \> "${UNPACKTODIR}/$(basename "${1%.*}" )" 1>&2
    xz -dc "${1}" > "${UNPACKTODIR}/$(basename "${1%.*}" )"
}

unpack_one()
{
    local file=${1} compare=${1}
    [[ -n ${PALUDIS_UNPACK_CASE_INSENSITIVE} ]] &&
        compare=$(tr '[:upper:]' '[:lower:]' <<<"${compare}")

    [[ -z ${file} ]] && die "Bad argument for unpack_one"
    [[ -e ${file} ]] || die "${file} doesn't exist"

    if [[ -n "${UNPACKONLY}" ]] ; then
        case ${compare} in
            *.tar.bz2|*.tbz2|*.tbz)
            ;;

            *)
            die "Can't use --only with ${file}"
            ;;
        esac
    fi

    local group split suffix
    for group in ${PALUDIS_UNPACK_SUFFIXES}; do
        split=( $(IFS=,; echo ${group} ) )
        for suffix in ${split[@]}; do
            [[ -n ${PALUDIS_UNPACK_CASE_INSENSITIVE} ]] &&
                suffix=$(tr '[:upper:]' '[:lower:]' <<<"${suffix}")
            if [[ ${compare} == *.${suffix} ]]; then
                unpack_${split[0]} "${file}" || die "Couldn't unpack ${file}"
                return
            fi
        done
    done

    if [[ -n "${PALUDIS_UNPACK_UNRECOGNISED_IS_FATAL}" ]] ; then
        die "Don't know how to unpack ${file}"
    else
        echo "Skipping unpack for ${file}"
    fi
}

export UNPACKFROMDIR="${!PALUDIS_UNPACK_FROM_VAR}"
export UNPACKTODIR="${PWD}"
unset UNPACKONLY
TAR_OPTIONS=( )

while [[ "${1#--}" != "${1}" ]] ; do
    if [[ "${1}" == "--only" ]] ; then
        shift
        export UNPACKONLY="${1}"
        shift
    elif [[ "${1}" == "--if-compressed" ]] ; then
        export PALUDIS_UNPACK_UNRECOGNISED_IS_FATAL=
        shift
    elif [[ "${1}" == --tar-option=* ]] ; then
        TAR_OPTIONS[${#TAR_OPTIONS[@]}]="${1#*=}"
        shift
    else
        die "Unrecognised option: ${1}"
    fi
done

[[ -z "$@" ]] && die "No arguments given to unpack"

for x in "$@" ; do
    x=$(echo $x )
    echo ">>> Unpacking ${x} to ${UNPACKTODIR}"
    if [[ ${x} != */* ]]; then
        x=${UNPACKFROMDIR}/${x}
    elif [[ ${x:0:2} == ./* ]]; then
        :
    elif [[ -n ${PALUDIS_UNPACK_ANY_PATH} ]]; then
        :
    else
        die "Invalid path given to unpack: ${x}"
    fi
    mkdir -p ${UNPACKTODIR}
    cd ${UNPACKTODIR}
    unpack_one "${x}"
done

if [[ -n "${PALUDIS_UNPACK_FIX_PERMISSIONS}" ]] ; then
    find ${UNPACKTODIR} -mindepth 1 -maxdepth 1 ! -type l -print0 | xargs -0 -r chmod -fR a+rX,u+w,go-w
fi