kiss: split sha256 into two functions

- Installation/removal needs the filtering, other parts of the
  package manager do not (so they can now use the bare function).

- Changed method of output transformation to something simpler.
This commit is contained in:
Dylan Araps 2021-07-20 20:27:12 +03:00
parent 60fbd39d73
commit cf9e663c76
No known key found for this signature in database
GPG Key ID: 13295DAC2CF13B5C
1 changed files with 24 additions and 26 deletions

50
kiss
View File

@ -194,6 +194,17 @@ decompress() {
} }
sh256() { sh256() {
# Higher level sh256 function which filters out non-existent
# files (and also directories).
for f do shift
[ -d "$f" ] || [ ! -e "$f" ] || set -- "$@" "$f"
done
# Only call _sh256 if we have files to operate on.
for f do _sh256 "$@"; return 0; done
}
_sh256() {
# There's no standard utility to generate sha256 checksums. # There's no standard utility to generate sha256 checksums.
# This is a simple wrapper around sha256sum, sha256, shasum, # This is a simple wrapper around sha256sum, sha256, shasum,
# openssl, digest, ... which will use whatever is available. # openssl, digest, ... which will use whatever is available.
@ -201,13 +212,7 @@ sh256() {
# All utilities must match 'sha256sum' output. # All utilities must match 'sha256sum' output.
# #
# Example: '<checksum> <file>' # Example: '<checksum> <file>'
unset hash
# Filter out directories and anything which does not exist.
for f do shift
[ -d "$f" ] || [ ! -e "$f" ] || set -- "$@" "$f"
done
! equ "$#" 0 || return 0
# Set the arguments based on found sha256 utility. # Set the arguments based on found sha256 utility.
case ${cmd_sha##*/} in case ${cmd_sha##*/} in
@ -217,26 +222,19 @@ sh256() {
digest) set -- -a sha256 "$@" ;; digest) set -- -a sha256 "$@" ;;
esac esac
# This is now one call to the checksums command rather than IFS=$newline
# one per file. We also display errors now rather than not
# (due to old runtime detection method).
hash=$("$cmd_sha" "$@") || die "Failed to generate checksums"
# Intentional, globbing disabled. # Generate checksums for all input files. This is a single
# shellcheck disable=2046,2086 # call to the utility rather than one per file.
set -- $hash _hash=$("$cmd_sha" "$@") || die "Failed to generate checksums"
# As the output is '<hash> <file>' and the above list is # Strip the filename from each element.
# split on whitespace; we need to pop every 2nd element. # '<checksum> ?<file>' -> '<checksum>'
for sum do case ${_i:-0} in for sum in $_hash; do
0) _i=1; set -- "$@" "$sum" ;; hash=$hash${hash:+"$newline"}${sum%% *}
1) _i=0 done
esac; shift; done
printf '%s\n' "$@" printf '%s\n' "$hash"
# Convert the list items to a newline separated string.
IFS=$newline hash=$*
unset IFS unset IFS
} }
@ -808,7 +806,7 @@ pkg_etcsums() {
set -- "$pkg_dir/$repo_name/$etc" "$@" set -- "$pkg_dir/$repo_name/$etc" "$@"
esac done < manifest esac done < manifest
sh256 "$@" > etcsums _sh256 "$@" > etcsums
} }
pkg_tar() { pkg_tar() {
@ -1021,7 +1019,7 @@ pkg_checksums() {
esac esac
done < "$repo_dir/sources" done < "$repo_dir/sources"
sh256 "$@" _sh256 "$@"
} }
pkg_verify() { pkg_verify() {