aboutsummaryrefslogtreecommitdiff
blob: b09b4dcd3764bf1a23ef29d6219351d681284a20 (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
#!/bin/sh
# Copyright 1997, 1998 Patrick Volkerding, Moorhead, MN USA
# Copyright 2002 Slackware Linux, Inc., Concord, CA USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

argv0=${0##*/}

usage=false
stdout=false
verbose=false
strip=false
files=false
msg=""
for opt; do
	case ${opt} in
		-h|--help)       usage=true;;
		-O|--stdout)     stdout=true;;
		-v|--verbose)    verbose=true;;
		-S|--strip-path) strip=true;;
		--)              break;;
		-*)              usage=true msg="unknown option '${opt}'";;
		*)               files=true;;
	esac
done

if ! ${files} || ${usage} ; then
	cat <<-EOF
	${argv0}: Converts RPM archives to tar archives

	Usage: ${argv0} [options] <rpms>

	Options:
	  -h, --help       This help screen (imagine that)
	  -O, --stdout     Write tarball to stdout
	  -S, --strip-path Strip package name from tarball
	  -v, --verbose    Verbose output
	EOF

	if [ -n "${msg}" ] ; then
		echo "Error: ${msg}" 1>&2
		exit 1
	else
		exit 0
	fi
fi

compress="cat"
case ${argv0} in
	rpm2tar)                      suffix=".tar";;
	rpm2tarbz2)  compress="bzip2" suffix=".tar.bz2";;
	rpm2tbz2)    compress="bzip2" suffix=".tbz2";;
	rpm2tarlzma) compress="lzma"  suffix=".tar.lzma";;
	rpm2tgz)     compress="gzip"  suffix=".tgz";;
	rpm2targz|*) compress="gzip"  suffix=".tar.gz";;
esac
case ${argv0} in
	rpm2tar*) strip=true;;
esac

# try to get a temp dir using progressively older/crappier methods
WORKDIR=""
trap 'rm -rf "${WORKDIR}"' 0

WORKDIR=$(mktemp -d --tmpdir ${argv0}.XXXXXX 2>/dev/null)
if [ -z "${WORKDIR}" ] ; then
	WORKDIR=$(mktemp -d -t ${argv0}.XXXXXX 2>/dev/null)
	if [ -z "${WORKDIR}" ] ; then
		[ -z "${TMPDIR}" ] && TMPDIR="/tmp"
		WORKDIR=$(mcookie 2>/dev/null)
		if [ -n "${WORKDIR}" ] ; then
			WORKDIR="${TMPDIR}/${WORKDIR}"
		else
			WORKDIR="${TMPDIR}/$$"
		fi

		worked=false
		if rm -rf "${WORKDIR}" ; then
			if mkdir -m 700 -p "${WORKDIR}" ; then
				worked=true
			elif mkdir -p "${WORKDIR}" ; then
				if chmod 700 "${WORKDIR}" ; then
					worked=true
				fi
			fi
		fi
		if ! ${worked} ; then
			echo "${argv0}: ${WORKDIR}: unable to create a temp directory"
			exit 1
		fi
	fi
fi

ret=0
dashdash=false
for file; do
	if ! ${dashdash} ; then
		case ${file} in
			-v|--verbose) continue;;
			-O|--stdout) continue;;
			-S|--strip-path) continue;;
			--) dashdash=true; continue;;
		esac
	fi

	${verbose} && printf "Processing file: ${file} ... "

	outfile=${file##*/}
	outfile=${outfile%.rpm}
	${strip} && base="" || base=${outfile%.src}
	DEST="${WORKDIR}/${base}"
	rm -rf "${DEST}"
	if ! mkdir "${DEST}" ; then
		echo "${argv0}: ${file}: ${DEST}: unable to create working directory"
		rm -rf "${WORKDIR}"
		exit 1
	fi

	# extract the CPIO from the RPM and unpack it
	(
		decompressor=""
		if command -v rpm2cpio 2>/dev/null 1>&2 ; then
			decompressor="rpm2cpio"
			rpm2cpio "${file}"
		else
			# do it by hand :/
			offset=$(rpmoffset < "${file}")
			magic=`(dd ibs=${offset} skip=1 if="${file}" count=1 | dd bs=10 count=1 | od -b) 2>/dev/null`
			case ${magic} in
				"0000000 102 132 150 "*)   decompressor="bzip2";;
				"0000000 037 213 010 "*|*) decompressor="gzip";;
			esac
			dd ibs=${offset} skip=1 if="${file}" 2>/dev/null | ${decompressor} -dc
		fi
		[ $? -ne 0 ] && echo "${argv0}: ${file}: failed to extract cpio via ${decompressor} (not actually an RPM?)" 1>&2
	) | (
		cd "${DEST}"
		# filter stupid blocks info from cpio
		cpio -i -m -d 2>/dev/null
	)

	if [ $? -ne 0 ] ; then
		ret=1

		${verbose} && echo "FAIL"
	else
		# repack the files into the appropriate tar file
		(
			cd "${WORKDIR}"
			tar cf - "./${base}"
		) | ${compress} | (
			if ${stdout} ; then
				cat
			else
				cat > "${outfile}${suffix}"
			fi
		)

		${verbose} && echo "OK"
	fi

	rm -rf "${DEST}"
done

rm -rf "${WORKDIR}"

exit ${ret}