[pacman-dev] [PATCH v3 1/2] 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> --- v3: split commit, this first part implements a function useful for checking if we have a known good compression type scripts/libmakepkg/util/compress.sh.in | 53 +++++++++++++++++++------- 1 file 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 } -- 2.28.0
Currently the list of supported formats for an archive, is maintained in two places. And repo-add does not actually get updated. :( In the process, remove some of the logical duplication when calling bsdtar/compress_as. Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- v3: use get_compression_command to do an early check if we can compress, thus preserving the old behavior of failing on invalid compression types. scripts/repo-add.sh.in | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/scripts/repo-add.sh.in b/scripts/repo-add.sh.in index 160fd93a..7182d1b8 100644 --- a/scripts/repo-add.sh.in +++ b/scripts/repo-add.sh.in @@ -44,6 +44,7 @@ USE_COLOR='y' PREVENT_DOWNGRADE=0 # Import libmakepkg +source "$LIBRARY"/util/compress.sh source "$LIBRARY"/util/message.sh # ensure we have a sane umask set @@ -189,21 +190,13 @@ verify_signature() { } verify_repo_extension() { - local repofile=$1 - - case $repofile in - *.db.tar.gz) TAR_OPT="-z" ;; - *.db.tar.bz2) TAR_OPT="-j" ;; - *.db.tar.xz) TAR_OPT="-J" ;; - *.db.tar.zst) TAR_OPT="--zstd" ;; - *.db.tar.Z) TAR_OPT="-Z" ;; - *.db.tar) TAR_OPT="" ;; - *) error "$(gettext "'%s' does not have a valid database archive extension.")" \ - "$repofile" - exit 1 ;; - esac + local junk=() + if [[ $1 = *.db.tar* ]] && get_compression_command "$1" junk; then + return 0 + fi - printf '%s' "$TAR_OPT" + error "$(gettext "'%s' does not have a valid database archive extension.")" "$1" + exit 1 } # write an entry to the pacman database @@ -522,7 +515,6 @@ rotate_db() { } create_db() { - TAR_OPT=$(verify_repo_extension "$REPO_DB_FILE") # $LOCKFILE is already guaranteed to be absolute so this is safe dirname=${LOCKFILE%/*} @@ -532,13 +524,13 @@ create_db() { tempname=$dirname/.tmp.$filename pushd "$tmpdir/$repo" >/dev/null - if ( shopt -s nullglob; files=(*); (( ${#files[*]} )) ); then - bsdtar -c ${TAR_OPT} -f "$tempname" * - else + local files=(*) + if [[ ${files[*]} = '*' ]]; then # we have no packages remaining? zip up some emptyness warning "$(gettext "No packages remain, creating empty database.")" - bsdtar -c ${TAR_OPT} -f "$tempname" -T /dev/null + files=(-T /dev/null) fi + bsdtar -cf - "${files[@]}" | compress_as "$filename" > "$tempname" popd >/dev/null create_signature "$tempname" @@ -656,7 +648,7 @@ else LOCKFILE=$PWD/$REPO_DB_FILE.lck fi -verify_repo_extension "$REPO_DB_FILE" >/dev/null +verify_repo_extension "$REPO_DB_FILE" REPO_DB_PREFIX=${REPO_DB_FILE##*/} REPO_DB_PREFIX=${REPO_DB_PREFIX%.db.*} -- 2.28.0
On 6/8/20 12:02 am, Eli Schwartz wrote:
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> ---
v3: split commit, this first part implements a function useful for checking if we have a known good compression type
I'm happy with this and the following patch. A
participants (2)
-
Allan McRae
-
Eli Schwartz