Hello World

File build-pkg-rpm of Package build

#
# RPM specific functions.
#
################################################################
#
# Copyright (c) 1995-2014 SUSE Linux Products GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or 3 as
# published by the Free Software Foundation.
#
# 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 (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################


pkg_initdb_rpm() {
    echo "initializing rpm db..."
    mkdir -p $BUILD_ROOT/var/lib/rpm
    # rpm v5 does not have initdb
    if ! test -e $BUILD_ROOT/usr/lib/rpm/cpuinfo.yaml ; then
	if test -x $BUILD_ROOT/usr/bin/rpmdb ; then
	    chroot $BUILD_ROOT /usr/bin/rpmdb --initdb || cleanup_and_exit 1
	else
	    chroot $BUILD_ROOT rpm --initdb || cleanup_and_exit 1
	fi
    fi
    # hack: add nofsync to db config to speed up install
    mkdir -p $BUILD_ROOT/root
    DBI_OTHER=`chroot $BUILD_ROOT rpm --eval '%{?__dbi_other}'`
    echo "%__dbi_other $DBI_OTHER nofsync" > $BUILD_ROOT/.rpmmacros
    echo "%__dbi_other $DBI_OTHER nofsync" > $BUILD_ROOT/root/.rpmmacros
}

pkg_prepare_rpm() {
    rpm_set_checkopts
    rpm_init_cumulate
}

pkg_erase_rpm() {
    chroot $BUILD_ROOT rpm --nodeps -e $PKG 2>&1 | {
	local retry
	while read line; do
	    case "$line" in
		r*failed:\ No\ such\ file\ or\ directory) ;;
		error:\ failed\ to\ stat\ *:\ No\ such\ file\ or\ directory) ;;
		error:\ *scriptlet\ failed*)
		    echo "$line"
		    retry=1
		;;
		*) echo "$line" ;;
	    esac
	done
	if test -n "$retry" ; then
	    echo "re-try deleting $PKG using --noscripts"
	    chroot $BUILD_ROOT rpm --nodeps --noscripts -e $PKG || true
	fi
    }
}

rpm_set_checkopts() {
    RPMCHECKOPTS=
    # on Fedora 10 rpmbuild is in a separate package so we need something else to
    # detect rpm4
    test -x $BUILD_ROOT/usr/bin/rpmquery && RPMCHECKOPTS="--nodigest --nosignature"
}

rpm_init_cumulate() {
    cumulate=-1
    CUMULATED_LIST=()
    CUMULATED_PIDS=()
    CUMULATED_HMD5=()
    DO_CUMULATE=
    local cumulaterpms=$(queryconfig --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" --archpath "$BUILD_ARCH" buildflags cumulaterpms)
    if test -n "$cumulaterpms" ; then
	test "$cumulaterpms" != 0 && DO_CUMULATE=true
    else
	# compatibility: auto-set cumulate for newer suse distros
	typeset -ri suse_version=$(queryconfig --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" --archpath "$BUILD_ARCH" eval '%{?suse_version}')
	if ((suse_version > 1220)) ; then 
	    DO_CUMULATE=true
	fi
    fi
}

pkg_verify_installed_rpm() {
    chroot $BUILD_ROOT rpm --verify $PKG 2>&1 | tee $TMPFILE
    if grep ^missing $TMPFILE > /dev/null ; then
	return 1
    fi
    return 0
}

pkg_cumulate_rpm() {
    test "$DO_CUMULATE" = true || return 1
    # work around for cross-build installs, we must not overwrite the running rpm
    if test "$PKG" = rpm ; then
	for i in $BUILD_ROOT/.init_b_cache/preinstalls/rpm-x86-* ; do
	    test -e "$i" && return 1
	done
    fi
    let cumulate++
    CUMULATED_LIST[$cumulate]=".init_b_cache/$PKG.rpm"
    CUMULATED_PIDS[$cumulate]="$PKGID"
    CUMULATED_HMD5[$cumulate]="$PKG_HDRMD5"
    return 0
}

pkg_install_rpm() {
    export ADDITIONAL_PARAMS=
    if test "$USE_FORCE" = true ; then
	export ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS --force"
    fi
    # work around for cross-build installs, we must not overwrite the running rpm
    if test "$PKG" = rpm ; then
	for i in $BUILD_ROOT/.init_b_cache/preinstalls/rpm-x86-* ; do
	    test -e "$i" && ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS --justdb"
	done
    fi
    ( cd $BUILD_ROOT && chroot $BUILD_ROOT rpm --ignorearch --nodeps -U --oldpackage --ignoresize $RPMCHECKOPTS \
		$ADDITIONAL_PARAMS .init_b_cache/$PKG.rpm 2>&1 || \
	  touch $BUILD_ROOT/exit ) | \
	      grep -v "^warning:.*saved as.*rpmorig$"
}

pkg_sysrootinstall_rpm() {
    if test "x$1" = "x--prepare" ; then
	assert_dir_path "$BUILD_SYSROOT"
	chroot $BUILD_ROOT mkdir -p "$BUILD_SYSROOT"
	chroot $BUILD_ROOT rpm  --root "$BUILD_SYSROOT" --initdb
	local installed=$(chroot $BUILD_ROOT rpm  --root "$BUILD_SYSROOT" -qa)
	test -n "$installed" && chroot $BUILD_ROOT rpm  --root "$BUILD_SYSROOT" --nodeps --noscripts -e --allmatches $installed
	return
    fi
    if test "x$1" = "x--finalize" ; then
	return
    fi
    export ADDITIONAL_PARAMS=
    if test "$USE_FORCE" = true ; then
	export ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS --force"
    fi
    ( cd $BUILD_ROOT && chroot $BUILD_ROOT rpm --ignorearch --nodeps -U --oldpackage --ignoresize $RPMCHECKOPTS \
		$ADDITIONAL_PARAMS --noscripts --root "$BUILD_SYSROOT" .init_b_cache/$PKG.rpm 2>&1 || \
	  touch $BUILD_ROOT/exit ) | \
	      grep -v "^warning:.*saved as.*rpmorig$"
}

pkg_finalize_rpm() {
    if test -n "${CUMULATED_LIST[*]}" ; then
	echo "now installing cumulated packages"
	for ((num=0; num<=cumulate; num++)) ; do
	    echo ${CUMULATED_LIST[$num]}
	    PKG=${CUMULATED_LIST[$num]##*/}
	    test "$BUILD_ROOT/.init_b_cache/rpms/$PKG" -ef "$BUILD_ROOT/${CUMULATED_LIST[$num]}" && continue
	    cp --remove-destination $BUILD_ROOT/.init_b_cache/rpms/$PKG $BUILD_ROOT/${CUMULATED_LIST[$num]} || cleanup_and_exit 1
	done > $BUILD_ROOT/.init_b_cache/manifest
	( cd $BUILD_ROOT && chroot $BUILD_ROOT rpm --ignorearch --nodeps -Uh --oldpackage --ignoresize --verbose $RPMCHECKOPTS \
		$ADDITIONAL_PARAMS .init_b_cache/manifest 2>&1 || touch $BUILD_ROOT/exit )
	for ((num=0; num<=cumulate; num++)) ; do
	    rm -f $BUILD_ROOT/${CUMULATED_LIST[$num]}
	done
	rm -f $BUILD_ROOT/.init_b_cache/manifest
	check_exit
	for ((num=0; num<=cumulate; num++)) ; do
	    PKG=${CUMULATED_LIST[$num]##*/}
	    echo "${CUMULATED_PIDS[$num]}" > $BUILD_ROOT/installed-pkg/${PKG%.rpm}
	    test -n "${CUMULATED_HMD5[$num]}" || continue
	    echo "${CUMULATED_HMD5[$num]} ${CUMULATED_PIDS[$num]}" > $BUILD_ROOT/.preinstall_image/${PKG%.rpm}
	done
    fi
}

RPM_PAYLOADDECOMPRESS_PROBED=

probe_rpm_payloaddecompress() {
    local l
    RPM_PAYLOADDECOMPRESS_PROBED=true
    RPM_PAYLOADDECOMPRESS_LZMA="lzma -d"
    RPM_PAYLOADDECOMPRESS_XZ="xz -d"
    RPM_PAYLOADDECOMPRESS_ZSTD="zstd -d"
    while read l ; do
	case "$l" in
	    *rpmlib\(PayloadIsLzma\)*) RPM_PAYLOADDECOMPRESS_LZMA=cat ;;
	    *rpmlib\(PayloadIsXz\)*)   RPM_PAYLOADDECOMPRESS_XZ=cat ;;
	    *rpmlib\(PayloadIsZstd\)*) RPM_PAYLOADDECOMPRESS_ZSTD=cat ;;
	esac
    done < <(rpm --showrc)
}

check_rpm_decompressor() {
    if ! "$1" </dev/null >/dev/null 2>&1 ; then
	test -f "$BUILD_DIR/${1}dec.sh" || cleanup_and_exit 3 "no $1 decoder available in host system"
	PAYLOADDECOMPRESS="bash $BUILD_DIR/${1}dec.sh"
    fi
}

pkg_preinstall_rpm() {
    test -d "$BUILD_INIT_CACHE/scripts" || mkdir -p "$BUILD_INIT_CACHE/scripts"

    test -z "$RPM_PAYLOADDECOMPRESS_PROBED" && probe_rpm_payloaddecompress
    PAYLOADDECOMPRESS=cat
    # if our rpm can decompress everything, we don't need to probe what compression
    # we actually have
    if test "$RPM_PAYLOADDECOMPRESS_LZMA" != "cat" -o "$RPM_PAYLOADDECOMPRESS_XZ" != "cat" -o "$RPM_PAYLOADDECOMPRESS_ZSTD" != "cat"; then
        case $(rpm -qp --nodigest --nosignature --qf "%{PAYLOADCOMPRESSOR}\n" "$BUILD_INIT_CACHE/rpms/$PKG.rpm") in
            lzma) PAYLOADDECOMPRESS="$RPM_PAYLOADDECOMPRESS_LZMA" ;;
            xz)   PAYLOADDECOMPRESS="$RPM_PAYLOADDECOMPRESS_XZ" ;;
            zstd) PAYLOADDECOMPRESS="$RPM_PAYLOADDECOMPRESS_ZSTD" ;;
        esac
    fi
    if test "$PAYLOADDECOMPRESS" = "cat" ; then
        rpm2cpio "$BUILD_INIT_CACHE/rpms/$PKG.rpm" | $CPIO || cleanup_and_exit 1
    else
	test "$PAYLOADDECOMPRESS" = "lzma -d" && check_rpm_decompressor lzma
	test "$PAYLOADDECOMPRESS" = "xz -d"   && check_rpm_decompressor xz
	test "$PAYLOADDECOMPRESS" = "zstd -d" && check_rpm_decompressor zstd
        rpm2cpio "$BUILD_INIT_CACHE/rpms/$PKG.rpm" | $PAYLOADDECOMPRESS | $CPIO || cleanup_and_exit 1
    fi
    test -L "$BUILD_INIT_CACHE/scripts" && cleanup_and_exit 1
    rm -rf "$BUILD_INIT_CACHE/scripts/$PKG.pre" "$BUILD_INIT_CACHE/scripts/$PKG.preprog" "$BUILD_INIT_CACHE/scripts/$PKG.post" "$BUILD_INIT_CACHE/scripts/$PKG.postprog"
    if test -e "$BUILD_INIT_CACHE/scripts/$PKG.run" ; then
	rpm -qp --nodigest --nosignature --qf "%{PREIN}" "$BUILD_INIT_CACHE/rpms/$PKG.rpm" > "$BUILD_INIT_CACHE/scripts/$PKG.pre"
	rpm -qp --nodigest --nosignature --qf "%{PREINPROG}" "$BUILD_INIT_CACHE/rpms/$PKG.rpm" > "$BUILD_INIT_CACHE/scripts/$PKG.preprog"
	rpm -qp --nodigest --nosignature --qf "%{POSTIN}" "$BUILD_INIT_CACHE/rpms/$PKG.rpm" > "$BUILD_INIT_CACHE/scripts/$PKG.post"
	rpm -qp --nodigest --nosignature --qf "%{POSTINPROG}" "$BUILD_INIT_CACHE/rpms/$PKG.rpm" > "$BUILD_INIT_CACHE/scripts/$PKG.postprog"
	rm -f "$BUILD_INIT_CACHE/scripts/.none"
	echo -n '(none)' > "$BUILD_INIT_CACHE/scripts/.none"
	cmp -s "$BUILD_INIT_CACHE/scripts/$PKG.pre" "$BUILD_INIT_CACHE/scripts/.none" && rm -f "$BUILD_INIT_CACHE/scripts/$PKG.pre"
	cmp -s "$BUILD_INIT_CACHE/scripts/$PKG.post" "$BUILD_INIT_CACHE/scripts/.none" && rm -f "$BUILD_INIT_CACHE/scripts/$PKG.post"
	rm -f "$BUILD_INIT_CACHE/scripts/.none"
    fi
    # hack for rpm erasures
    if test -d "$BUILD_ROOT/installed-pkg" -a -z "$PREPARE_VM" ; then
	# call for rpm-4.x and not rpm-devel
        test -z "${PKG##rpm-[0-9]*}" && chroot . rpm --rebuilddb
        # also exec for exchanged rpm !  naming is rpm-x86-<target>-<ver>
        test -z "${PKG##rpm-x86-*[0-9]*}" && chroot . rpm --rebuilddb
    fi
}

pkg_runscripts_rpm() {
    if test -e "$BUILD_ROOT/.init_b_cache/scripts/$PKG.pre" ; then
	echo "running $PKG preinstall script"
	local prog
	read prog < "$BUILD_ROOT/.init_b_cache/scripts/$PKG.preprog"
	(cd $BUILD_ROOT && chroot $BUILD_ROOT "${prog:-sh}" ".init_b_cache/scripts/$PKG.pre" 0)
	rm -f "$BUILD_ROOT/.init_b_cache/scripts/$PKG.pre" "$BUILD_ROOT/.init_b_cache/scripts/$PKG.preprog"
    fi
    if test -e "$BUILD_ROOT/.init_b_cache/scripts/$PKG.post" ; then
	echo "running $PKG postinstall script"
	local prog
	read prog < "$BUILD_ROOT/.init_b_cache/scripts/$PKG.postprog"
	(cd $BUILD_ROOT && chroot $BUILD_ROOT "${prog:-sh}" ".init_b_cache/scripts/$PKG.post" 1)
	rm -f "$BUILD_ROOT/.init_b_cache/scripts/$PKG.post" "$BUILD_ROOT/.init_b_cache/scripts/$PKG.postprog"
    fi
}