aboutsummaryrefslogtreecommitdiff
blob: d32300c70231bfbc46557d4e62ed7a3269e80fb5 (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
196
197
198
199
200
201
202
#!/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##*/}
warn() { echo "${argv0}: warning: $*" 1>&2; }
err()  { echo "${argv0}: warning: $*" 1>&2; exit 1; }

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
		err "${msg}"
	else
		exit 0
	fi
fi

# allow for local development
if [ "${0#./}" != "${0}" -a -x "./rpmoffset" ] ; then
	rpmoffset="./rpmoffset"
else
	rpmoffset="rpmoffset"
fi

compress="cat"
case ${argv0#rpm} in
	unpack)                    suffix="";;
	2tar)                      suffix=".tar";;
	2tarbz2)  compress="bzip2" suffix=".tar.bz2";;
	2tbz2)    compress="bzip2" suffix=".tbz2";;
	2tarlzma) compress="lzma"  suffix=".tar.lzma";;
	2tgz)     compress="gzip"  suffix=".tgz";;
	2tarxz)   compress="xz"    suffix=".tar.xz";;
	2txz)     compress="xz"    suffix=".txz";;
	2targz|*) 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
			err "${WORKDIR}: unable to create a temp directory"
		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
		rm -rf "${WORKDIR}" &
		err "${file}: ${DEST}: unable to create working directory"
	fi

	# extract the CPIO from the RPM and unpack it
	(
		decompressor=""
		# Disable rpm2cpio as it might be broken #249769,
		# or it might be too old #292057
		#if command -v rpm2cpio >/dev/null 2>&1 ; then
		if false ; then
			decompressor="rpm2cpio"
			rpm2cpio "${file}"
		else
			# do it by hand :/
			set -- $(${rpmoffset} -v < "${file}")
			decompressor=$1
			offset=$2
			[ -z "${offset}" ] && err "unable to locate cpio offset (broken/unknown compression?)"
			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
		false
	elif [ -n "${suffix}" ] ; then
		# repack the files into the appropriate tar file
		(
			cd "${WORKDIR}"
			tar cf - "./${base}"
		) | ${compress} | (
			if ${stdout} ; then
				cat
			else
				cat > "${outfile}${suffix}"
			fi
		)
	else
		# just unpacking, so move the files
		cp -pPR "${DEST}/" ./
	fi
	tret=$?
	if [ ${tret} -eq 0 ] ; then
		msg=OK
	else
		ret=${tret}
		msg=FAIL
	fi
	${verbose} && echo ${msg}

	rm -rf "${DEST}"
done

# No need for this as the trap will take care of it
#rm -rf "${WORKDIR}"

exit ${ret}