Files
scratchpkg/pkgadd
2019-11-21 15:07:25 +08:00

334 lines
9.1 KiB
Bash
Executable File

#!/bin/bash
#
# scratchpkg
#
# Copyright (c) 2018 by Emmett1 (emmett1.2miligrams@gmail.com)
#
# 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 3 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 should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM
export LC_ALL=C
interrupted() {
echo
ret 1
}
msg() {
echo -e "==> $1"
}
msg2() {
echo -e " -> $1"
}
msgerr() {
echo -e "==> ERROR: $1"
}
msgwarn() {
echo -e "==> WARNING: $1"
}
help() {
cat << EOF
Usage:
$(basename $0) [ <options> <package.spkg.txz> ]
Options:
-u, --upgrade upgrade package
-r, --reinstall reinstall package
-c, --ignore-conflict ignore conflict when installing package
-v, --verbose print files installed
-h, --help show this help message
--no-preinstall skip preinstall script before build/install package
--no-postinstall skip postinstall script after install package
--no-preupgrade skip preupgrade script before upgrade package
--no-postupgrade skip postupgrade script after upgrade package
--no-backup skip backup when upgrading package
--root=<path> install to custom root directory
EOF
}
extract_opt() {
for opt in $@; do
case $opt in
--*) OPTS+=($opt) ;;
-*) for (( i=1; i<${#opt}; i++ )); do OPTS+=(-${opt:$i:1}); done ;;
*) OPTS+=($opt) ;;
esac
done
echo ${OPTS[@]}
}
parse_opts() {
if [ -z "$1" ]; then
SHOWHELP=yes
else
while [ "$1" ]; do
case $1 in
-u | --upgrade) UPGRADE_PKG=yes ;;
-r | --reinstall) REINSTALL_PKG=yes ;;
-c | --ignore-conflict) IGNORE_CONFLICT=yes ;;
-v | --verbose) VERBOSE_INSTALL="-v" ;;
-h | --help) SHOWHELP=yes ;;
--no-preinstall) NO_PREINSTALL=yes ;;
--no-postinstall) NO_POSTINSTALL=yes ;;
--no-preupgrade) NO_PREUPGRADE=yes ;;
--no-postupgrade) NO_POSTUPGRADE=yes ;;
--no-backup) NO_BACKUP=yes ;;
--root=*) ROOT_DIR="${1#*=}" ;;
*.spkg.tar.*) PKGNAME="$(realpath $1)" ;;
*) msg "Invalid option! ($1)"; exit 1 ;;
esac
shift
done
fi
}
ret() {
# remove lock and all tmp files on exit
rm -f $LOCK_FILE $TMP_PKGADD $TMP_PKGINSTALL
exit $1
}
isinstalled() {
if [ -s $INDEX_DIR/$1/.pkginfo ] && grep -q $1 $INDEX_DIR/$1/.pkginfo; then
return 0
else
return 1
fi
}
parse_opts $(extract_opt $@)
INDEX_DIR="$ROOT_DIR/var/lib/scratchpkg/index"
LOCK_FILE="$ROOT_DIR/var/lib/scratchpkg/spkg.lock"
# disable pre/post install/upgrade script when install to custom root location
if [ "$ROOT_DIR" != "" ]; then
NO_PREINSTALL=yes
NO_POSTINSTALL=yes
NO_PREUPGRADE=yes
NO_POSTUPGRADE=yes
fi
# show help page
if [ "$SHOWHELP" ] || [ -z "$PKGNAME" ]; then
help
ret 0
fi
if [ ! -d $INDEX_DIR ]; then
msgerr "Package's database directory not exist! ($INDEX_DIR)"
ret 1
fi
# check for root access
if [ "$UID" != "0" ]; then
msgerr "Installing package need root access!"
ret 1
fi
# check for lock file
if [ -f $LOCK_FILE ]; then
msgerr "Cant install/remove package simultaneously."
msgerr "remove '$LOCK_FILE' if no install/remove package process running."
exit 1
else
touch $LOCK_FILE
if [ "$?" != 0 ]; then
msgerr "Cant create lock file in '$LOCK_FILE'"
exit 1
fi
fi
BASEPKGNAME=$(basename $PKGNAME)
# check existence of package file
if [ ! -f "$PKGNAME" ]; then
msgerr "Package '$1' not exist!"
ret 1
fi
noextname=${BASEPKGNAME%*.spkg.tar.*}
release=${noextname##*-}
noextname=${noextname%-*}
version=${noextname##*-}
name=${noextname%-*}
# get package information if installed
if isinstalled $name; then
iversion=$(grep ^version $INDEX_DIR/$name/.pkginfo | cut -d " " -f3-)
irelease=$(grep ^release $INDEX_DIR/$name/.pkginfo | cut -d " " -f3-)
ALREADYINSTALLED=yes
fi
if [ "$ALREADYINSTALLED" = "yes" ] && [ ! "$UPGRADE_PKG" ] && [ ! "$REINSTALL_PKG" ]; then
echo "Package '$name' already installed. ($iversion-$irelease)"
ret 0
fi
# cant reinstall if not installed and cant upgrade if up-to-date
if [ "$UPGRADE_PKG" ] || [ "$REINSTALL_PKG" ]; then
if [ "$ALREADYINSTALLED" != "yes" ]; then
msgerr "Package '$name' not installed."
ret 1
fi
if [ "$UPGRADE_PKG" ]; then
if [ "$version-$release" = "$iversion-$irelease" ]; then
echo "Package '$name' is up-to-date. ($iversion-$irelease)"
ret 0
fi
fi
fi
echo -ne "Loading $BASEPKGNAME...\033[0K\r"
TMP_PKGADD="$(mktemp)" || exit 1
TMP_PKGINSTALL="$(mktemp)" || exit 1
# check integrity of package and save list file/dirs to install in the meantime
tar -tf $PKGNAME > $TMP_PKGADD 2>/dev/null
if [ $? != 0 ]; then
msgerr "Package '$1' is corrupted!"
ret 1
fi
echo -ne "\033[0K"
# set operation whether install/upgrade/reinstall
# install is default
opr=install
[ "$UPGRADE_PKG" ] && opr=upgrade
[ "$REINSTALL_PKG" ] && opr=reinstall
echo "$opr: $name-$version-$release..."
# check for file conflict
if [ ! "$IGNORE_CONFLICT" ]; then
while read -r line; do
if [ "$line" = "${line%.*}.spkgnew" ]; then
line=${line%.*}
fi
if [ -e "$ROOT_DIR/$line" ] || [ -L "$ROOT_DIR/$line" ]; then
if [ "$UPGRADE_PKG" ] || [ "$REINSTALL_PKG" ]; then
if [ ! "$(grep -Fx "$line" "$INDEX_DIR/$name/.files")" ]; then
echo "$line $owner"
conflictedfile=yes
fi
else
echo "$line"
conflictedfile=yes
fi
fi
done < <(grep -Ev ^.pkg* $TMP_PKGADD | grep -v '/$')
if [ "$conflictedfile" = "yes" ]; then
msgerr "File conflict found!"
ret 1
fi
fi
# pre-install and pre-upgrade script
if grep -qx .pkginstall $TMP_PKGADD; then
if [ ! "$NO_PREINSTALL" ] && [ ! "$UPGRADE_PKG" ]; then
pushd "$ROOT_DIR"/ >/dev/null
sh <(tar -xf "$PKGNAME" .pkginstall -O) pre-install "$version"
popd >/dev/null
fi
if [ "$UPGRADE_PKG" ] && [ ! "$NO_PREUPGRADE" ]; then
pushd "$ROOT_DIR"/ >/dev/null
sh <(tar -xf "$PKGNAME" .pkginstall -O) pre-upgrade "$version" "$iversion"
popd >/dev/null
fi
fi
# exclude .pkg* files when extract into system
for i in $(grep ^.pkg* $TMP_PKGADD); do
excludefile="$excludefile --exclude=$i"
done
rm -f $TMP_PKGINSTALL
# extract package into ROOT_DIR
tar --keep-directory-symlink -p -x -v -f $PKGNAME -C "$ROOT_DIR"/ $excludefile | while read -r line; do
if [ "$line" = "${line%.*}.spkgnew" ]; then
line=${line%.*}
if [ "$UPGRADE_PKG" ] || [ "$REINSTALL_PKG" ]; then
if [ ! -e "$ROOT_DIR/$line" ] || [ "$NO_BACKUP" = yes ]; then
mv "$ROOT_DIR/$line".spkgnew "$ROOT_DIR/$line"
fi
else
mv "$ROOT_DIR/$line".spkgnew "$ROOT_DIR/$line"
fi
fi
[ "$VERBOSE_INSTALL" ] && echo "extracted '$line'"
echo "$line" >> $TMP_PKGINSTALL
done
# remove old files from old package that not exist in new package
if [ "$UPGRADE_PKG" ] || [ "$REINSTALL_PKG" ]; then
rmlist_file=$(mktemp)
rmlist_dir=$(mktemp)
reserve_dir=$(mktemp)
rmlist_all=$(mktemp)
grep '/$' $INDEX_DIR/*/.files | grep -v $INDEX_DIR/$name/.files | awk -F : '{print $2}' | sort | uniq > $reserve_dir # get list reserved dirs
grep -Fxv -f $TMP_PKGINSTALL $INDEX_DIR/$name/.files > $rmlist_all # get list files and dirs to remove
grep -v '/$' $rmlist_all | tac > $rmlist_file # get files only to remove
grep -Fxv -f $reserve_dir $rmlist_all | grep '/$' | tac > $rmlist_dir # get dirs only (safe) to remove
pushd "$ROOT_DIR"/ >/dev/null
[ -s $rmlist_file ] && xargs -a $rmlist_file -d'\n' rm $VERBOSE_INSTALL
[ -s $rmlist_dir ] && xargs -a $rmlist_dir -d'\n' rmdir $VERBOSE_INSTALL
popd >/dev/null
rm -f $rmlist_file $rmlist_dir $reserve_dir $rmlist_all
fi
# register package into database
rm -fr $INDEX_DIR/$name
mkdir $INDEX_DIR/$name
echo "name = $name" > $INDEX_DIR/$name/.pkginfo
echo "version = $version" >> $INDEX_DIR/$name/.pkginfo
echo "release = $release" >> $INDEX_DIR/$name/.pkginfo
install -m644 $TMP_PKGINSTALL $INDEX_DIR/$name/.files
for ii in $(grep ^.pkg* $TMP_PKGADD); do
pkgfiles="$pkgfiles $ii"
done
if [ "$pkgfiles" ]; then
tar -x -f $PKGNAME -C $INDEX_DIR/$name $pkgfiles >/dev/null 2>&1
fi
if [ -f $INDEX_DIR/$name/.pkginstall ]; then
if [ ! "$NO_POSTINSTALL" ] && [ ! "$UPGRADE_PKG" ]; then
pushd "$ROOT_DIR"/ >/dev/null
sh $INDEX_DIR/$name/.pkginstall post-install "$version"
popd >/dev/null
fi
if [ "$UPGRADE_PKG" ] && [ ! "$NO_POSTUPGRADE" ]; then
pushd "$ROOT_DIR"/ >/dev/null
sh $INDEX_DIR/$name/.pkginstall post-upgrade "$version" "$iversion"
popd >/dev/null
fi
fi
# running ldconfig
if [ -x "$ROOT_DIR"/sbin/ldconfig ]; then
"$ROOT_DIR"/sbin/ldconfig -r "$ROOT_DIR"/
fi
ret 0