Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/scripts/libmakepkg
diff options
context:
space:
mode:
authorEli Schwartz <eschwartz@archlinux.org>2020-08-05 10:02:09 -0400
committerAllan McRae <allan@archlinux.org>2020-08-10 09:57:32 +1000
commit74aacf44958e1343b910b3fbdcf753393857f070 (patch)
tree32a5422f01af0ee240993b4041e52b709f66f052 /scripts/libmakepkg
parent2a352dc059f696eeda1e18116e468994c1a38430 (diff)
libmakepkg: extend compress.sh to also permit checking validity
get_compression_command() can now be used to do upfront checks for whether a given extension is known to do something successfully. This is useful when writing tools in which an unknown compression type is a fatal error. Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> Signed-off-by: Allan McRae <allan@archlinux.org>
Diffstat (limited to 'scripts/libmakepkg')
-rw-r--r--scripts/libmakepkg/util/compress.sh.in53
1 files changed, 40 insertions, 13 deletions
diff --git a/scripts/libmakepkg/util/compress.sh.in b/scripts/libmakepkg/util/compress.sh.in
index 16420beb..d35a01fa 100644
--- a/scripts/libmakepkg/util/compress.sh.in
+++ b/scripts/libmakepkg/util/compress.sh.in
@@ -24,6 +24,7 @@ LIBMAKEPKG_UTIL_COMPRESS_SH=1
LIBRARY=${LIBRARY:-'@libmakepkgdir@'}
source "$LIBRARY/util/message.sh"
+source "$LIBRARY/util/pkgbuild.sh"
# Wrapper around many stream compression formats, for use in the middle of a
@@ -31,20 +32,46 @@ source "$LIBRARY/util/message.sh"
compress_as() {
# $1: final archive filename extension for compression type detection
- local ext=".tar${1##*.tar}"
+ local cmd ext=${1#${1%.tar*}}
+
+ if ! get_compression_command "$ext" cmd; then
+ warning "$(gettext "'%s' is not a valid archive extension.")" "${ext:-${1##*/}}"
+ cat
+ else
+ "${cmd[@]}"
+ fi
+}
+
+# Retrieve the compression command for an archive extension, or cat for .tar,
+# and save it to an existing array name. If the extension cannot be found,
+# clear the array and return failure.
+get_compression_command() {
+ local extarray ext=$1 outputvar=$2
+ local resolvecmd=() fallback=()
case "$ext" in
- *.tar.gz) ${COMPRESSGZ[@]:-gzip -c -f -n} ;;
- *.tar.bz2) ${COMPRESSBZ2[@]:-bzip2 -c -f} ;;
- *.tar.xz) ${COMPRESSXZ[@]:-xz -c -z -} ;;
- *.tar.zst) ${COMPRESSZST[@]:-zstd -c -z -q -} ;;
- *.tar.lrz) ${COMPRESSLRZ[@]:-lrzip -q} ;;
- *.tar.lzo) ${COMPRESSLZO[@]:-lzop -q} ;;
- *.tar.Z) ${COMPRESSZ[@]:-compress -c -f} ;;
- *.tar.lz4) ${COMPRESSLZ4[@]:-lz4 -q} ;;
- *.tar.lz) ${COMPRESSLZ[@]:-lzip -c -f} ;;
- *.tar) cat ;;
- *) warning "$(gettext "'%s' is not a valid archive extension.")" \
- "$ext"; cat ;;
+ *.tar.gz) fallback=(gzip -c -f -n) ;;
+ *.tar.bz2) fallback=(bzip2 -c -f) ;;
+ *.tar.xz) fallback=(xz -c -z -) ;;
+ *.tar.zst) fallback=(zstd -c -z -q -) ;;
+ *.tar.lrz) fallback=(lrzip -q) ;;
+ *.tar.lzo) fallback=(lzop -q) ;;
+ *.tar.Z) fallback=(compress -c -f) ;;
+ *.tar.lz4) fallback=(lz4 -q) ;;
+ *.tar.lz) fallback=(lzip -c -f) ;;
+ *.tar) fallback=(cat) ;;
+ # do not respect unknown COMPRESS* env vars
+ *) array_build "$outputvar" resolvecmd; return 1 ;;
esac
+
+ ext=${ext#*.tar.}
+ if [[ -n $ext ]]; then
+ extarray="COMPRESS${ext^^}[@]"
+ resolvecmd=("${!extarray}")
+ fi
+ if (( ${#resolvecmd[@]} == 0 )); then
+ resolvecmd=("${fallback[@]}")
+ fi
+
+ array_build "$outputvar" resolvecmd
}