do-extract: move main logic into helper function

This commit is contained in:
Đoàn Trần Công Danh 2023-07-11 23:02:44 +07:00 committed by Đoàn Trần Công Danh
parent 38c01ef74e
commit 2deec738e1
2 changed files with 165 additions and 122 deletions

View File

@ -0,0 +1,164 @@
vextract() {
local sc=--strip-components=1
local dst=
while [ "$#" -ne 1 ]; do
case "$1" in
-C)
if [ -z "$2" ]; then
msg_error "$pkgver: vextract -C <directory>.\n"
fi
dst="$2"
mkdir -p "$dst"
shift 2
;;
--no-strip-components)
sc=
shift
;;
--strip-components=*)
sc="$1"
shift
;;
--)
shift; break ;;
*)
break ;;
esac
done
local TAR_CMD sfx
local archive="$1"
local ret=0
TAR_CMD="$(command -v bsdtar)"
[ -z "$TAR_CMD" ] && TAR_CMD="$(command -v tar)"
[ -z "$TAR_CMD" ] && msg_error "xbps-src: no suitable tar cmd (bsdtar, tar)\n"
case "$archive" in
*.tar.lzma) sfx="txz";;
*.tar.lz) sfx="tlz";;
*.tlz) sfx="tlz";;
*.tar.xz) sfx="txz";;
*.txz) sfx="txz";;
*.tar.bz2) sfx="tbz";;
*.tbz) sfx="tbz";;
*.tar.gz) sfx="tgz";;
*.tgz) sfx="tgz";;
*.tar.zst) sfx="tzst";;
*.tzst) sfx="tzst";;
*.gz) sfx="gz";;
*.xz) sfx="xz";;
*.bz2) sfx="bz2";;
*.zst) sfx="zst";;
*.tar) sfx="tar";;
*.zip) sfx="zip";;
*.rpm) sfx="rpm";;
*.deb) sfx="deb";;
*.patch) sfx="txt";;
*.diff) sfx="txt";;
*.txt) sfx="txt";;
*.sh) sfx="txt";;
*.7z) sfx="7z";;
*.gem) sfx="gem";;
*.crate) sfx="crate";;
*) msg_error "$pkgver: unknown distfile suffix for $archive.\n";;
esac
case ${sfx} in
tar|txz|tbz|tlz|tgz|tzst|crate)
$TAR_CMD ${sc:+"$sc"} ${dst:+-C "$dst"} -x \
--no-same-permissions --no-same-owner \
-f $archive
;;
gz|bz2|xz|zst)
cp -f $archive "$dst"
(
if [ "$dst" ]; then cd "$dst"; fi
case ${sfx} in
gz)
gunzip -f $archive
;;
bz2)
bunzip2 -f $archive
;;
xz)
unxz -f $archive
;;
zst)
unzstd $archive
;;
esac
)
;;
zip)
if command -v unzip &>/dev/null; then
unzip -o -q $archive ${dst:+-d "$dst"}
elif command -v bsdtar &>/dev/null; then
bsdtar ${sc:+"$sc"} ${dst:+-C "$dst"} -xf $archive
else
msg_error "$pkgver: cannot find unzip or bsdtar bin for extraction.\n"
fi
;;
rpm)
if ! command -v bsdtar &>/dev/null; then
msg_error "$pkgver: cannot find bsdtar for extraction.\n"
fi
bsdtar ${sc:+"$sc"} ${dst:+-C "$dst"} -x \
--no-same-permissions --no-same-owner -f $archive
;;
deb)
if command -v bsdtar &>/dev/null; then
bsdtar -x -O -f "$archive" "data.tar.*" |
bsdtar ${sc:+"$sc"} ${dst:+-C "$dst"} -x \
--no-same-permissions --no-same-owner -f -
else
msg_error "$pkgver: cannot find bsdtar for extraction.\n"
fi
;;
txt)
cp -f $archive "$dst"
;;
7z)
if command -v 7z &>/dev/null; then
7z x $archive -o"$dst"
elif command -v bsdtar &>/dev/null; then
bsdtar ${sc:+"$sc"} ${dst:+-C "$dst"} -xf $archive
else
msg_error "$pkgver: cannot find 7z or bsdtar bin for extraction.\n"
fi
;;
gem)
$TAR_CMD -xOf $archive data.tar.gz |
$TAR_CMD ${sc:+"$sc"} ${dst:+-C "$dst"} -xz -f -
;;
*)
msg_error "$pkgver: cannot guess $archive extract suffix. ($sfx)\n"
;;
esac
if [ "$?" -ne 0 ]; then
msg_error "$pkgver: extracting $archive.\n"
fi
}
vsrcextract() {
local sc=--strip-components=1
local dst=
while [ "$#" -ge 1 ]; do
case "$1" in
-C)
if [ -z "$2" ]; then
msg_error "$pkgver: vsrcextract -C <directory>.\n"
fi
dst="$2"
shift 2
;;
--no-strip-components|--strip-components=*)
sc="$1"
shift
;;
*)
break ;;
esac
done
vextract "$sc" ${dst:+-C "$dst"} \
"${XBPS_SRCDISTDIR}/${pkgname}-${version}/$1"
}

View File

@ -45,128 +45,7 @@ hook() {
unset found
continue
fi
case $curfile in
*.tar.lzma) cursufx="txz";;
*.tar.lz) cursufx="tlz";;
*.tlz) cursufx="tlz";;
*.tar.xz) cursufx="txz";;
*.txz) cursufx="txz";;
*.tar.bz2) cursufx="tbz";;
*.tbz) cursufx="tbz";;
*.tar.gz) cursufx="tgz";;
*.tgz) cursufx="tgz";;
*.tar.zst) cursufx="tzst";;
*.tzst) cursufx="tzst";;
*.gz) cursufx="gz";;
*.xz) cursufx="xz";;
*.bz2) cursufx="bz2";;
*.zst) cursufx="zst";;
*.tar) cursufx="tar";;
*.zip) cursufx="zip";;
*.rpm) cursufx="rpm";;
*.deb) cursufx="deb";;
*.patch) cursufx="txt";;
*.diff) cursufx="txt";;
*.txt) cursufx="txt";;
*.sh) cursufx="txt";;
*.7z) cursufx="7z";;
*.gem) cursufx="gem";;
*.crate) cursufx="crate";;
*) msg_error "$pkgver: unknown distfile suffix for $curfile.\n";;
esac
case ${cursufx} in
tar|txz|tbz|tlz|tgz|tzst|crate)
$TAR_CMD -x --no-same-permissions --no-same-owner -f $srcdir/$curfile -C "$extractdir"
if [ $? -ne 0 ]; then
msg_error "$pkgver: extracting $curfile into $XBPS_BUILDDIR.\n"
fi
;;
gz|bz2|xz|zst)
cp -f $srcdir/$curfile "$extractdir"
cd "$extractdir"
case ${cursufx} in
gz)
gunzip -f $curfile
;;
bz2)
bunzip2 -f $curfile
;;
xz)
unxz -f $curfile
;;
zst)
unzstd $curfile
;;
esac
;;
zip)
if command -v unzip &>/dev/null; then
unzip -o -q $srcdir/$curfile -d "$extractdir"
if [ $? -ne 0 ]; then
msg_error "$pkgver: extracting $curfile into $XBPS_BUILDDIR.\n"
fi
elif command -v bsdtar &>/dev/null; then
bsdtar -xf $srcdir/$curfile -C "$extractdir"
if [ $? -ne 0 ]; then
msg_error "$pkgver: extracting $curfile into $XBPS_BUILDDIR.\n"
fi
else
msg_error "$pkgver: cannot find unzip or bsdtar bin for extraction.\n"
fi
;;
rpm)
if ! command -v bsdtar &>/dev/null; then
msg_error "$pkgver: cannot find bsdtar for extraction.\n"
fi
bsdtar -x --no-same-permissions --no-same-owner -f $srcdir/$curfile -C "$extractdir"
if [ $? -ne 0 ]; then
msg_error "$pkgver: extracting $curfile into $XBPS_BUILDDIR.\n"
fi
;;
deb)
if command -v bsdtar &>/dev/null; then
bsdtar -x -O -f "$srcdir/$curfile" "data.tar.*" |
bsdtar -C "$extractdir" -x --no-same-permissions --no-same-owner
if [ $? -ne 0 ]; then
msg_error "$pkgver: extracting $curfile into $XBPS_BUILDDIR.\n"
fi
else
msg_error "$pkgver: cannot find bsdtar for extraction.\n"
fi
;;
txt)
cp -f $srcdir/$curfile "$extractdir"
;;
7z)
if command -v 7z &>/dev/null; then
7z x $srcdir/$curfile -o"$extractdir"
if [ $? -ne 0 ]; then
msg_error "$pkgver: extracting $curfile into $XBPS_BUILDDIR.\n"
fi
elif command -v bsdtar &>/dev/null; then
bsdtar -xf $srcdir/$curfile -C "$extractdir"
if [ $? -ne 0 ]; then
msg_error "$pkgver: extracting $curfile into $XBPS_BUILDDIR.\n"
fi
else
msg_error "$pkgver: cannot find 7z or bsdtar bin for extraction.\n"
fi
;;
gem)
innerdir="$extractdir/${wrksrc##*/}"
mkdir -p "$innerdir"
$TAR_CMD -xOf $srcdir/$curfile data.tar.gz |
$TAR_CMD -xz -C "$innerdir" -f -
if [ $? -ne 0 ]; then
msg_error "$pkgver: extracting $curfile into $XBPS_BUILDDIR.\n"
fi
;;
*)
msg_error "$pkgver: cannot guess $curfile extract suffix. ($cursufx)\n"
;;
esac
vsrcextract --no-strip-components -C "$extractdir" "$curfile"
done
cd "$extractdir"