summaryrefslogtreecommitdiffstats
path: root/unpack-debuginfo.sh
blob: dd5d0f86b8d64b7de27298362806328c5c846a21 (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
#!/bin/bash
# unpack-debuginfo.sh - a simple script to unpack an RPM in a manner
# usable by debuginfofs.
# Copyright 2009 Red Hat, Inc.
#
# This program 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.
#
# This program 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# 
# Author: Will Woods <wwoods@redhat.com>

source config.sh

if [ -z "$libdir" ]; then
    echo "Couldn't find config.sh - exiting."
    exit 1
fi

if [ $# -lt 2 ]; then
    echo "Usage: $0 reponame rpmfile [rpmfile...]"
    echo "unpacks the given rpmfiles under $libdir/reponame"
    exit 1
fi

function rpm_nevra {
    if [ -z "$1" ]; then return; fi
    n_evra="$(rpm -qp $1 --qf '%{N} %{E}:%{V}-%{R}.%{ARCH}' 2>/dev/null)"
    set -- $n_evra
    n=$1
    evra=$2
    if [ "${evra:0:1}" == ":" ]; then
        evra="0$evra"
    elif [ "${evra:0:6}" == "(none)" ]; then
        evra="0${evra:6}"
    fi
    echo ${n}-${evra}
}

function rpm_envra {
    if [ -z "$1" ]; then return; fi
    envra="$(rpm -qp $1 --qf '%{E}:%{N}-%{V}-%{R}.%{ARCH}' 2>/dev/null)"
    if [ "${envra:0:1}" == ":" ]; then
        envra="0$envra"
    elif [ "${envra:0:6}" == "(none)" ]; then
        envra="0${envra:6}"
    fi
    echo $envra
}

function unpack_rpm {
    [ "$debug" ] && echo "unpack_rpm($1 $2)" >&2
    rpmfile="$1"
    targetdir="$2"
    rpm2cpio $1 | ( cd $targetdir; cpio --quiet -iumd )
    # Fix dir perms so that anyone can read the data
    find $targetdir -type d -exec chmod a+rx {} +
}

reponame="$1"
shift

# TODO perm check

# FIXME only do this if we're sure that reponame is OK?
mkdir -p "$libdir/$reponame"

if [ ! -d "$libdir/$reponame" ]; then
    echo "$libdir/$reponame does not exist - please create it first."
    exit 1
fi

while [ $# -gt 0 ]; do
    nevra="$(rpm_nevra $1)"
    first_letter="${nevra:0:1}"
    targetdir="$libdir/$reponame/$first_letter/$nevra"
    if [ -d $targetdir ]; then
        [ "$verbose" ] && echo "$nevra already unpacked; skipping..."
    else
        mkdir -p $targetdir
        unpack_rpm $1 $targetdir
    fi
    shift
done