Files
scratchpkg/pkgadd
2019-05-23 12:58:20 +08:00

326 lines
8.6 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=yes ;;
-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 ;;
--no-color) NOCOLOR=yes ;;
--root=*) ROOT="${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 $TMP_PKGINSTALL_BKP $TMP_PKGADD_RMLIST
exit $1
}
isinstalled() {
if [ -s $INDEX_DIR/$1/.pkginfo ] && [[ $(grep $1 $INDEX_DIR/$1/.pkginfo) ]]; then
return 0
else
return 1
fi
}
parse_opts $(extract_opt $@)
ROOT_DIR=${ROOT:-}
INDEX_DIR="$ROOT_DIR/var/lib/scratchpkg/index"
LOCK_FILE="$ROOT_DIR/var/lib/scratchpkg/spkg.lock"
# disable pre/post install 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 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
if [ -n "$PKGNAME" ]; then
BASEPKGNAME=$(basename $PKGNAME)
fi
# check existence of package file
if [ ! -f "$PKGNAME" ]; then
msgerr "Package '$1' not exist!"
ret 1
fi
# check for root access
if [ "$UID" != "0" ]; then
msgerr "Installing package need root access!"
ret 1
fi
noextname=${BASEPKGNAME%*.spkg.tar.*}
release=${noextname##*-}
noextname=${noextname%-*}
version=${noextname##*-}
name=${noextname%-*}
# get package information if installed
if isinstalled $name; then
iname=$(cat $INDEX_DIR/$name/.pkginfo | grep ^name | cut -d " " -f3-)
iversion=$(cat $INDEX_DIR/$name/.pkginfo | grep ^version | cut -d " " -f3-)
irelease=$(cat $INDEX_DIR/$name/.pkginfo | grep ^release | cut -d " " -f3-)
ALREADYINSTALLED=yes
fi
if [ "$ALREADYINSTALLED" = "yes" ] && [ ! "$UPGRADE_PKG" ] && [ ! "$REINSTALL_PKG" ]; then
echo "Package '$name' already installed. ($iversion-$irelease)"
ret 0
fi
TMP_PKGADD="$(mktemp)" || exit 1
TMP_PKGINSTALL="$(mktemp)" || exit 1
TMP_PKGINSTALL_BKP="$(mktemp)" || exit 1
TMP_PKGADD_RMLIST="$(mktemp)" || exit 1
echo -ne "Loading $BASEPKGNAME...\033[0K\r"
# check integrity of package
tar -tf $PKGNAME > $TMP_PKGADD 2>/dev/null
if [ $? != 0 ]; then
msgerr "Package '$1' is corrupted!"
ret 1
fi
echo -ne "\033[0K"
# 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
### INSTALL PACKAGE INTO SYSTEM ###
opr=install
[ "$UPGRADE_PKG" ] && opr=upgrade
[ "$REINSTALL_PKG" ] && opr=reinstall
echo "$opr: $name-$version-$release..."
#ignore 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"
conflictedfile=yes
fi
else
echo "$line"
conflictedfile=yes
fi
fi
done < <(cat $TMP_PKGADD | grep -Ev ^.pkg* | grep -v '/$')
if [ "$conflictedfile" = "yes" ]; then
msgerr "File conflict found!"
ret 1
fi
fi
if [ $(grep -x .pkginstall $TMP_PKGADD) ]; then
if [ ! "$NO_PREINSTALL" ] && [ ! "$UPGRADE_PKG" ]; then
(cd "$ROOT_DIR"/ && sh <(tar -xf "$PKGNAME" .pkginstall -O) pre-install "$version")
fi
if [ "$UPGRADE_PKG" ] && [ ! "$NO_PREUPGRADE" ]; then
(cd "$ROOT_DIR"/ && sh <(tar -xf "$PKGNAME" .pkginstall -O) pre-upgrade "$version" "$iversion")
fi
fi
for i in $(grep ^.pkg* $TMP_PKGADD); do
excludefile="$excludefile --exclude=$i"
done
rm -f $TMP_PKGINSTALL $TMP_PKGINSTALL_BKP
tar --keep-directory-symlink -p -x -v -f $PKGNAME -C "$ROOT_DIR"/ $excludefile | while read line; do
if [ "$line" = "${line%.*}.spkgnew" ]; then
echo "$line" >> $TMP_PKGINSTALL_BKP
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" = yes ] && echo ">>> $line"
echo "$line" >> $TMP_PKGINSTALL
done
# remove old files from old package that not exist in new package
if [ "$UPGRADE_PKG" ] || [ "$REINSTALL_PKG" ]; then
grep -Fxv -f $TMP_PKGINSTALL $INDEX_DIR/$name/.files > $TMP_PKGADD_RMLIST
grep -v '/$' $TMP_PKGADD_RMLIST | while read line; do
rm "$ROOT_DIR/$line" &>/dev/null
done
grep '/$' $TMP_PKGADD_RMLIST | tac | while read line; do
if [ ! "$(grep -R --exclude-dir="$name" -w "$line" "$INDEX_DIR"/*/.files)" ]; then
rmdir "$ROOT_DIR/$line" &>/dev/null
fi
done
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
[ -f $TMP_PKGINSTALL_BKP ] && install -m644 $TMP_PKGINSTALL_BKP $INDEX_DIR/$name/.bkpfiles
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
(cd "$ROOT_DIR"/ && sh $INDEX_DIR/$name/.pkginstall post-install "$version")
fi
if [ "$UPGRADE_PKG" ] && [ ! "$NO_POSTUPGRADE" ]; then
(cd "$ROOT_DIR"/ && sh $INDEX_DIR/$name/.pkginstall post-upgrade "$version" "$iversion")
fi
fi
# running ldconfig
if [ -x "$ROOT_DIR"/sbin/ldconfig ]; then
$ROOT_DIR/sbin/ldconfig -r "$ROOT_DIR"/
fi
ret 0