kiss: simplify pkg_etc

This commit is contained in:
Dylan Araps 2020-09-14 15:02:04 +03:00
parent cb3d826bf0
commit 2356a2bc78
No known key found for this signature in database
GPG Key ID: 46D62DD9F1DE636E

125
kiss
View File

@ -954,10 +954,13 @@ pkg_install_files() {
# Copy files and create directories (preserving permissions), # Copy files and create directories (preserving permissions),
# skipping anything located in /etc/. # skipping anything located in /etc/.
# case $line in
# The 'test' will run with '-e' for no-overwrite and '-z' /etc/?*[!/])
# for overwrite. # Handle file in /etc/.
case $line in /etc/*) ;; [ -d "$KISS_ROOT/$line" ] || test "$1" "$KISS_ROOT/$line" ||
pkg_etc_file "$2" "${line#/}"
;;
*/) */)
# Skip directories if they already exist in the file system. # Skip directories if they already exist in the file system.
# (Think /usr/bin, /usr/lib, etc). # (Think /usr/bin, /usr/lib, etc).
@ -977,6 +980,8 @@ pkg_install_files() {
} }
esac esac
done ||: done ||:
pkg_etc_cnt=0
} }
pkg_remove_files() { pkg_remove_files() {
@ -1011,59 +1016,49 @@ pkg_remove_files() {
done ||: done ||:
} }
pkg_etc() ( pkg_etc_file() {
[ -d "$tar_dir/$pkg/etc" ] || return 0 pkg_etc_cnt=$((pkg_etc_cnt + 1))
cd "$tar_dir/$pkg" log "$pkg" "Doing 3-way handshake for $2"
# Create all directories beforehand. sum_new=$(sh256 "$1/$2") 2>/dev/null ||:
find etc -type d | while read -r dir; do sum_sys=$(cd "$KISS_ROOT/"; sh256 "$2") 2>/dev/null ||:
mkdir -p "$KISS_ROOT/$dir" sum_old=$(awk "NR == $pkg_etc_cnt" "$mak_dir/c") 2>/dev/null ||:
done echo $pkg_etc_cnt
# Handle files in /etc/ based on a 3-way checksum check. printf '%s\n' "Previous: ${sum_old:-null}"
find etc -type f | sort | while read -r file; do printf '%s\n' "System: ${sum_sys:-null}"
i=$((i + 1)) printf '%s\n' "New: ${sum_new:-null}"
{ sum_new=$(sh256 "$file") # Use a case statement to easily compare three strings at
sum_sys=$(cd "$KISS_ROOT/"; sh256 "$file") # the same time. Pretty nifty.
sum_old=$(awk "NR == $i" "$mak_dir/c"); } 2>/dev/null ||: case ${sum_old:-null}${sum_sys:-null}${sum_new} in
# old = Y, sys = X, new = Y
"${sum_new}${sum_sys}${sum_old}")
log "Skipping $2"
return 0
;;
log "$pkg" "Doing 3-way handshake for $file" # old = X, sys = X, new = X
printf '%s\n' "Previous: ${sum_old:-null}" # old = X, sys = Y, new = Y
printf '%s\n' "System: ${sum_sys:-null}" # old = X, sys = X, new = Y
printf '%s\n' "New: ${sum_new:-null}" "${sum_old}${sum_old}${sum_old}"|\
"${sum_old:-null}${sum_sys}${sum_sys}"|\
"${sum_sys}${sum_old}"*)
log "Installing $2"
new=
;;
# Use a case statement to easily compare three strings at # All other cases.
# the same time. Pretty nifty. *)
case ${sum_old:-null}${sum_sys:-null}${sum_new} in war "$pkg" "saving /$2 as /$2.new"
# old = Y, sys = X, new = Y new=.new
"${sum_new}${sum_sys}${sum_old}") ;;
log "Skipping $file" esac
continue
;;
# old = X, sys = X, new = X cp -fPp "$1/$2" "$KISS_ROOT/${2}${new}"
# old = X, sys = Y, new = Y chown root:root "$KISS_ROOT/${2}${new}" 2>/dev/null
# old = X, sys = X, new = Y }
"${sum_old}${sum_old}${sum_old}"|\
"${sum_old:-null}${sum_sys}${sum_sys}"|\
"${sum_sys}${sum_old}"*)
log "Installing $file"
new=
;;
# All other cases.
*)
war "$pkg" "saving /$file as /$file.new"
new=.new
;;
esac
cp -fPp "$file" "$KISS_ROOT/${file}${new}"
chown root:root "$KISS_ROOT/${file}${new}" 2>/dev/null
done ||:
)
pkg_remove() { pkg_remove() {
# Remove a package and all of its files. The '/etc' directory is handled # Remove a package and all of its files. The '/etc' directory is handled
@ -1174,8 +1169,6 @@ pkg_install() {
run_hook pre-install "$pkg" "$tar_dir/$pkg" run_hook pre-install "$pkg" "$tar_dir/$pkg"
pkg_conflicts "$pkg" pkg_conflicts "$pkg"
log "$pkg" "Installing package"
# Block being able to abort the script with Ctrl+C during installation. # Block being able to abort the script with Ctrl+C during installation.
# Removes all risk of the user aborting a package installation leaving # Removes all risk of the user aborting a package installation leaving
# an incomplete package installed. # an incomplete package installed.
@ -1186,17 +1179,9 @@ pkg_install() {
cp -f "$sys_db/$pkg/manifest" "$mak_dir/m" 2>/dev/null ||: cp -f "$sys_db/$pkg/manifest" "$mak_dir/m" 2>/dev/null ||:
cp -f "$sys_db/$pkg/etcsums" "$mak_dir/c" 2>/dev/null ||: cp -f "$sys_db/$pkg/etcsums" "$mak_dir/c" 2>/dev/null ||:
# Install the package's files by iterating over its manifest. log "$pkg" "Installing package"
pkg_install_files -z "$tar_dir/$pkg" pkg_install_files -z "$tar_dir/$pkg"
# Handle /etc/ files in a special way (via a 3-way checksum) to determine
# how these files should be installed. Do we overwrite the existing file?
# Do we install it as $file.new to avoid deleting user configuration? etc.
#
# This is more or less similar to Arch Linux's Pacman with the user manually
# handling the .new files when and if they appear.
pkg_etc
# This is the aforementioned step removing any files from the old version of # This is the aforementioned step removing any files from the old version of
# the package if the installation is an update. Each file type has to be # the package if the installation is an update. Each file type has to be
# specially handled to ensure no system breakage occurs. # specially handled to ensure no system breakage occurs.
@ -1204,15 +1189,13 @@ pkg_install() {
# Files in /etc/ are skipped entirely as they'll be handled via a 3-way # Files in /etc/ are skipped entirely as they'll be handled via a 3-way
# checksum system due to the nature of their existence. # checksum system due to the nature of their existence.
grep -vFxf "$sys_db/$pkg/manifest" "$mak_dir/m" 2>/dev/null | grep -vFxf "$sys_db/$pkg/manifest" "$mak_dir/m" 2>/dev/null |
pkg_remove_files pkg_remove_files
# Install the package's files a second time to fix any mess caused by the # Install the package's files a second time to fix any mess caused by the
# above removal of the previous version of the package. # above removal of the previous version of the package.
log "$pkg" "Verifying installation" log "$pkg" "Verifying installation"
pkg_install_files -e "$tar_dir/$pkg" pkg_install_files -e "$tar_dir/$pkg"
# Reset 'trap' to its original value. Installation is done so we no longer
# need to block 'Ctrl+C'.
trap pkg_clean EXIT INT trap pkg_clean EXIT INT
if [ -x "$sys_db/$pkg/post-install" ]; then if [ -x "$sys_db/$pkg/post-install" ]; then
@ -1383,15 +1366,19 @@ args() {
case $action in case $action in
b|build|c|checksum|d|download|i|install|r|remove) b|build|c|checksum|d|download|i|install|r|remove)
[ "$1" ] || { [ "$1" ] || {
# Intentional.
# shellcheck disable=2031
export KISS_PATH=${PWD%/*}:$KISS_PATH export KISS_PATH=${PWD%/*}:$KISS_PATH
set -- "${PWD##*/}" set -- "${PWD##*/}"
} }
case "$*" in for arg do
*'*'*|*'!'*|*'['*|*']'*|*' '*|*' '*) case $arg in
die "Arguments contain invalid characters: '!*[ \t]'" *'*'*|*'!'*|*'['*|*']'*|*' '*|*' '*)
;; die "Argument contains: '!*[ \t]' ($arg)"
esac ;;
esac
done
;; ;;
esac esac