3
# dscextract.sh - Extract a single file from a Debian source package
4
# Copyright (C) 2011 Christoph Berg <myon@debian.org>
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28
while getopts "f" opt ; do
35
shift $(($OPTIND - 1))
37
[ $# = 2 ] || die "Usage: $(basename $0) <dsc> <file>"
39
test -e "$DSC" || die "$DSC not found"
42
DSCDIR=$(dirname "$DSC")
43
WORKDIR=$(mktemp -d --tmpdir dscextract.XXXXXX)
44
trap "rm -rf $WORKDIR" 0 2 3 15
46
if DIFFGZ=$(egrep '^ [0-9a-f]{32,64} [0-9]+ [^ ]+\.diff\.(gz|bz2)$' "$DSC") ; then
47
DIFFGZ=$(echo "$DIFFGZ" | cut -d ' ' -f 4 | head -n 1)
48
test -e "$DSCDIR/$DIFFGZ" || die "$DSCDIR/$DIFFGZ: not found"
49
filterdiff -p1 -i "$FILE" -z "$DSCDIR/$DIFFGZ" > "$WORKDIR/patch"
50
if test -s "$WORKDIR/patch" ; then
51
# case 1: file found in .diff.gz
52
if ! grep -q '^@@ -0,0 ' "$WORKDIR/patch" ; then
53
# case 1a: patch requires original file
54
ORIGTGZ=$(egrep '^ [0-9a-f]{32,64} [0-9]+ [^ ]+\.orig\.tar\.(gz|bz2)$' "$DSC") || die "no orig.tar.gz found in $DSC"
55
ORIGTGZ=$(echo "$ORIGTGZ" | cut -d ' ' -f 4 | head -n 1)
60
test -e "$DSCDIR/$ORIGTGZ" || die "$DSCDIR/$ORIGTGZ not found"
61
tar --extract --to-stdout --gzip --file "$DSCDIR/$ORIGTGZ" --wildcards "*/$FILE" > "$WORKDIR/output" 2>/dev/null || :
62
test -s "$WORKDIR/output" || die "$FILE not found in $DSCDIR/$ORIGTGZ, but required by patch"
64
patch --silent "$WORKDIR/output" < "$WORKDIR/patch"
65
test -s "$WORKDIR/output" || die "patch $FILE did not produce any output"
68
elif [ "$FAST" ] ; then
69
# in fast mode, don't bother looking into .orig.tar.gz
74
if DEBIANTARGZ=$(egrep '^ [0-9a-f]{32,64} [0-9]+ [^ ]+\.debian\.tar\.(gz|bz2)$' "$DSC") ; then
77
DEBIANTARGZ=$(echo "$DEBIANTARGZ" | cut -d ' ' -f 4 | head -n 1)
78
test -e "$DSCDIR/$DEBIANTARGZ" || die "$DSCDIR/$DEBIANTARGZ not found"
83
tar --extract --to-stdout $ZIP --file "$DSCDIR/$DEBIANTARGZ" "$FILE" > "$WORKDIR/output" 2>/dev/null || :
84
test -s "$WORKDIR/output" || exit 1
85
# case 2a: file found in .debian.tar.gz
88
# for 3.0 format, no need to look in other places here
91
ORIGTGZ=$(egrep '^ [0-9a-f]{32,64} [0-9]+ [^ ]+\.orig\.tar\.(gz|bz2)$' "$DSC") || die "no orig.tar.gz found in $DSC"
92
ORIGTGZ=$(echo "$ORIGTGZ" | cut -d ' ' -f 4 | head -n 1)
93
test -e "$DSCDIR/$ORIGTGZ" || die "$DSCDIR/$ORIGTGZ not found"
98
tar --extract --to-stdout $ZIP --file "$DSCDIR/$ORIGTGZ" --wildcards --no-wildcards-match-slash "*/$FILE" > "$WORKDIR/output" 2>/dev/null || :
99
test -s "$WORKDIR/output" || exit 1
100
# case 2b: file found in .orig.tar.gz
101
# TODO: apply patches from debian.tar.gz
102
cat "$WORKDIR/output"
108
if TARGZ=$(egrep '^ [0-9a-f]{32,64} [0-9]+ [^ ]+\.tar\.(gz|bz2)$' "$DSC") ; then
109
TARGZ=$(echo "$TARGZ" | cut -d ' ' -f 4 | head -n 1)
110
test -e "$DSCDIR/$TARGZ" || die "$DSCDIR/$TARGZ not found"
113
*.bz2) ZIP=--bzip2 ;;
115
tar --extract --to-stdout $ZIP --file "$DSCDIR/$TARGZ" --wildcards --no-wildcards-match-slash "*/$FILE" > "$WORKDIR/output" 2>/dev/null || :
116
test -s "$WORKDIR/output" || exit 1
117
# case 3: file found in .tar.gz or .orig.tar.gz
118
cat "$WORKDIR/output"