#!/bin/sh
#
# $Revision: 1.3 $
#
# rpmextract
# Extract files from an rpm to a specified directory.
# - by default the directory is $TMPDIR/<rpm> (or /tmp/<rpm>) but this
# can be overridden.
#
# Copyright (C) 2005 Simon J. Mudd (
[email protected])
#
# Newer versions of this script may be made available at:
#
http://ftp.wl0.org/rpmdiff/
#
# Usage: rpmextract [-l] [-d dir] <rpm>
#
# options
# -l list files only (don't extract them)
# -d output files to directory dir
#
# Exit Status
# Should be 0 if everything is ok
#
# License:
# 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 may 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.
#
# An on-line copy of the GNU General Public License can be found
#
http://www.fsf.org/copyleft/gpl.html.
[ -n "$DEBUG" ] && set -x
myname=$(basename $0)
myversion=$(echo '$Id: rpmextract,v 1.3 2005/01/09 08:33:19 sjmudd Exp $' | awk '{ print $3 }')
# fatal error and stop
msg_fatal () {
echo "FATAL: $*" >&2
exit 2
}
# standard message
msg_info () {
echo "$*"
}
## list the contents of the rpm
list_rpm_files () {
local local_rpm="$1"
msg_info "Listing files in ${local_rpm}"
rpm2cpio ${local_rpm} | cpio -it --quiet || msg_fatal "problem listing files in ${local_rpm}"
}
# extract rpm to path, ensure destination directory does not exist.
extract_rpm () {
local local_rpm="$1"
local local_dir="$2"
[ "${local_rpm}" = "${local_dir}" ] || {
[ -d "${local_dir}" ] && msg_fatal "directory ${local_dir} already exists"
msg_info "Extracting files from $(basename ${local_dir}) to ${local_dir}"
( mkdir ${local_dir} && cd ${local_dir} && rpm2cpio ${local_rpm} | cpio -im --quiet) ||\
msg_fatal "problem extracting ${local_rpm}"
}
}
list=
to_dir=
while getopts :ld: flag
do
case $flag in
l) list=1;;
d) to_dir="$OPTARG";;
*) msg_fatal "invalid option"
esac
done
shift $(($OPTIND - 1))
[ $# = 1 ] || msg_fatal "Usage: $myname [-l] [-d directory] <rpm>"
from_rpm="$1"
tmpdir=${TMPDIR:-/tmp}
# check the from
[ -n "${from_rpm}" ] || msg_fatal "empty rpm name"
[ -f "${from_rpm}" ] || msg_fatal "${from_rpm} is not a file (or does not exist)"
if [ -z "$list" ]; then
[ -z "${to_dir}" ] && to_dir=${tmpdir}/$(basename ${from_rpm})
[ -e "${to_dir}" ] && msg_fatal "${to_dir} already exists"
fi
# look for cpio
(
cpiodir=
IFS=:
for dir in $PATH
do
if [ -x $dir/cpio ]; then
cpiodir=$dir
break
fi
done
[ -n "$cpiodir" ]
) || msg_fatal "can not find rpm2cpio needed to extract/list rpm ${from_rpm}"
if [ -n "$list" ]; then
list_rpm_files "${from_rpm}"
else
extract_rpm "${from_rpm}" "${to_dir}"
fi