pkgmeek: cleanup the work directory more consistently

This commit is contained in:
John McQuah 2022-06-03 07:36:52 -04:00
parent 0e45a89aa9
commit c4c7f71c27

View File

@ -8,17 +8,16 @@
main() {
######################## main routine ################################
local o_ignored work _local_ here url u f pkg_utd
local errDL=0; local errUZ=0;
local o_ignored pkg_dir src_dir work _local_ here url u f pkg_utd; local errDL=0; local errUZ=0;
parse_options "$@"
[ "$PKGMK_RECURSIVE" = "no" ] || recursive "$@"
#
# Exit early if cleaning was requested
#
if [ "$PKGMK_CLEAN" = "yes" ]; then
[ "$PKGMK_MTIME_ONLY" = "yes" ] && o_ignored="-utd"
[ "$PKGMK_UPDATE_FOOTPRINT" = "yes" ] && o_ignored+=" -uf"
[ "$PKGMK_CHECK_SIG" = "yes" ] && o_ignored+=" -cs"
[ "$PKGMK_MTIME_ONLY" = "yes" ] && o_ignored="'-utd' "
[ "$PKGMK_UPDATE_FOOTPRINT" = "yes" ] && o_ignored+="'-uf' "
[ "$PKGMK_CHECK_SIG" = "yes" ] && o_ignored+="'-cs' "
[ "$PKGMK_REFRESH_SIG" = "yes" ] && refresh_signature
[ "$o_ignored" = "" ] || { warning "option -c nullifies these requested options:";
info "$o_ignored"; }
@ -170,7 +169,9 @@ if [ "$pkg_utd" = 0 ] || [ "$PKGMK_FORCE" = "yes" ]; then
# The actual build step!
(SRC=$(pwd)/src; PKG=$(pwd)/pkg; cd src; set -x; build) &&
info "Build succeeded. Moving on to compression." ||
{ error "Unsuccessful build!"; exit "$E_BUILD"; }
{ error "Unsuccessful build!"; cleanup_work;
[ "$PKGMK_KEEP_WORK" = "yes" ] || { cd "$PKGMK_ROOT"; rm -rf "$work"; };
exit "$E_BUILD"; }
[ -f "$PKGMK_ROOT/.nostrip" ] && ns_filter="| grep -v -f $PKGMK_ROOT/.nostrip"
find pkg -type f $ns_filter | while read -r f; do
@ -188,16 +189,17 @@ if [ "$pkg_utd" = 0 ] || [ "$PKGMK_FORCE" = "yes" ]; then
[ $UID = 0 ] || fake_uid="--uid 0 --gid 0"
if (cd pkg; bsdtar --format=gnutar $fake_uid -cf "$pkg_dir$package" *); then
info "Package creation successful."
else { error "Unable to create the compressed package $package."; exit $E_DIRPERM; }
else
{ error "Unable to create the compressed package $package."; cleanup_work;
exit "$E_BUILD"; }
fi
# Ensure that $work/ contains the package or a symlink to it, then check the footprint
[ "$pkg_dir" = "$(pwd)/" ] || ln -sf "$pkg_dir$package" .
[ "$PKGMK_IGNORE_FOOTPRINT" = "yes" ] || check_manifest footprint || exit $E_MANIFEST
# Clean up, part 1: remove soft links and the {src,pkg} trees
find . -maxdepth 1 -mindepth 1 -type l -delete
[ "$PKGMK_KEEP_WORK" = "yes" ] || rm -rf src pkg
# Clean up, part 1: remove soft links and the work directory
find . -maxdepth 1 -mindepth 1 -type l -delete; cleanup_work
fi # Continue from here if the extract and build were skipped
# Install if requested
@ -210,9 +212,6 @@ fi # Continue from here if the extract and build were skipped
&& info "$(basename $PKGMK_INSTALL_COMMAND) $package succeeded."; } || \
{ error "Unable to install $package using pkgadd."; exit "$E_INSTALL"; }
# Clean up, part 2
[ "$PKGMK_KEEP_WORK" = "yes" ] || [ "$pkg_dir" = "$(pwd)/" ] || rm -f "$package"
# Done!
}
@ -285,19 +284,16 @@ parse_options() {
-i|--install) PKGMK_INSTALL_COMMAND="/usr/bin/pkgadd" ;;
-u|--upgrade) PKGMK_INSTALL_COMMAND="/usr/bin/pkgadd -u" ;;
-pk|--public-key)
[ "$2" ] || { echo "$(basename "$PKGMK_COMMAND"): option $1 requires an argument";
[ "$2" ] && PKGMK_PUBLICKEY="$2" || { echo "$(basename "$PKGMK_COMMAND"): option $1 requires an argument";
exit 1; }
PKGMK_PUBLICKEY="$2"
shift ;;
-sk|--secret-key)
[ "$2" ] || { echo "$(basename "$PKGMK_COMMAND"): option $1 requires an argument";
[ "$2" ] && PKGMK_PRIVATEKEY="$2" || { echo "$(basename "$PKGMK_COMMAND"): option $1 requires an argument";
exit 1; }
PKGMK_PRIVATEKEY="$2"
shift ;;
-cf|--config-file)
[ "$2" ] || { echo "$(basename "$PKGMK_COMMAND"): option $1 requires an argument";
[ "$2" ] && PKGMK_CONF="$2"|| { echo "$(basename "$PKGMK_COMMAND"): option $1 requires an argument";
exit 1; }
PKGMK_CONF="$2"
shift ;;
-v|--version)
echo "$(basename "$PKGMK_COMMAND") (pkgutils) $PKGMK_VERSION"; exit 0 ;;
@ -342,6 +338,7 @@ print_help() {
validate_pkgfile() { # called from within PKGMK_ROOT
local errcode kv
[ -f Pkgfile ] || { error "no Pkgfile found. $PKGMK_ROOT is not a valid ports directory."; return "$E_PKGFILE"; }
check_signature "pre-Pkgfile" | parse_signify_output
errcode=$?
[ "$errcode" = 0 ] || info "Use '-is' if you have independent confirmation of the port's integrity."
@ -362,7 +359,7 @@ check_reqvars () {
[ $((checksum & 4)) = 4 ] && nullvars+=" 'release'"
[ $((checksum & 8)) = 8 ] && nullvars+=" 'build()'"
[ "$checksum" = 0 ] || { error "Pkgfile does not specify these required variables:"; \
info "$nullvars"; exit "$E_PKGFILE"; }
info "$nullvars"; return "$E_PKGFILE"; }
}
check_pkg_mtime() { # can be called even if some sources are missing
@ -399,21 +396,18 @@ cat_manifest() {
check_manifest() {
local FILTER TRUTH CN CM
local FILE="$1.tmp"; local retval=0; local severity=error;
[ -f "$package" ] || [ "$1" = "md5sum" ] || { error "$package not found. Cannot check $1.";
local retval=0; local severity=error;
[ -f "$pkgdir$package" ] || [ "$1" = "md5sum" ] || { error "$package not found. Cannot check $1.";
return "$E_MANIFEST"; }
[ "$1" = "md5sum" ] && FILTER="| sort -k 3" || FILTER=""
[ "$1" = "md5sum" ] && FILTER="-k 3" || FILTER=""
TRUTH="$PKGMK_ROOT/.$1"
cat_manifest $1 $FILTER > "$FILE"
if [ -f "$TRUTH" ]; then
cat "$TRUTH" $FILTER > "${FILE%tmp}orig"
diff -w -t -U 0 <(sort "${FILE%tmp}orig") <(sort "${FILE}") | \
sed '/^@@/d; /^+++/d; /^---/d; s/^+/NEW /g; s/^-/MISSING /g' \
> "${FILE%tmp}diff"
if [ -s "${FILE%tmp}diff" ]; then
CN=$(grep -c ^NEW "${FILE%tmp}diff")
CM=$(grep -c ^MISSING "${FILE%tmp}diff")
diff -w -t -U 0 <(cat "$TRUTH" | sort $FILTER) <(cat_manifest $1 | sort $FILTER) | \
sed '/^@@/d; /^+++/d; /^---/d; s/^+/NEW /g; s/^-/MISSING /g' > "$1.diff"
if [ -s "$1.diff" ]; then
CN=$(grep -c ^NEW "$1.diff")
CM=$(grep -c ^MISSING "$1.diff")
if [ "$1" = "footprint" ]; then
[ "$PKGMK_IGNORE_NEW" = "no" ] && retval+=$CN || severity=warning
[ "$PKGMK_IGNORE_MISSING" = "no" ] && retval+=$CM
@ -422,12 +416,12 @@ check_manifest() {
retval=$E_MANIFEST
fi
$severity "$1 mismatch found:"
cat "${FILE%tmp}diff" >&2
cat "$1.diff" >&2
fi
rm "$1.diff"
else
warning ".$1 not found, creating new."; mv "$FILE" "$TRUTH"
warning ".$1 not found, creating new."; cat_manifest $1 > "$TRUTH"
fi
rm -f "$FILE" "${FILE%tmp}orig" "${FILE%tmp}diff"
return $retval
}
@ -524,6 +518,10 @@ refresh_signature() {
fi
}
cleanup_work() {
[ "$PKGMK_KEEP_WORK" = "yes" ] || { cd "$PKGMK_ROOT"; rm -rf "$work"; }
}
recursive() {
local ARGS DIR FILE
[ "$PKGMK_CLEAN" = "no" ] || { find "$PKGMK_ROOT" -name Pkgfile -printf "%h\n" \
@ -553,8 +551,8 @@ error() {
######################## end of subroutines ###########################
## Now ensure that they cannot be overwritten when sourcing Pkgfile ##
readonly -f main info warning error print_help parse_options recursive \
validate_pkgfile check_reqvars check_pkg_mtime cat_manifest check_manifest \
cat_signature check_signature parse_signify_output refresh_signature
readonly -f main info warning error print_help parse_options validate_pkgfile \
check_reqvars check_pkg_mtime cat_manifest check_manifest cat_signature \
check_signature parse_signify_output refresh_signature cleanup_work recursive
main "$@"