From ca58ac17d234f3376da13ade7bcd91738d5e295e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90o=C3=A0n=20Tr=E1=BA=A7n=20C=C3=B4ng=20Danh?= Date: Sat, 18 Sep 2021 21:02:51 +0700 Subject: [PATCH 1/2] hooks: do-extract: simplify gem extraction --- common/hooks/do-extract/00-distfiles.sh | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/common/hooks/do-extract/00-distfiles.sh b/common/hooks/do-extract/00-distfiles.sh index 922f7029491a..ca2011d5c45a 100644 --- a/common/hooks/do-extract/00-distfiles.sh +++ b/common/hooks/do-extract/00-distfiles.sh @@ -3,7 +3,7 @@ hook() { local srcdir="$XBPS_SRCDISTDIR/$pkgname-$version" - local f j curfile found extractdir + local f j curfile found extractdir innerdir local TAR_CMD if [ -z "$distfiles" -a -z "$checksum" ]; then @@ -150,16 +150,10 @@ hook() { fi ;; gem) - case "$TAR_CMD" in - *bsdtar) - $TAR_CMD -xOf $srcdir/$curfile data.tar.gz | \ - $TAR_CMD -xz -C "$extractdir" -s ",^,${wrksrc##*/}/," -f - - ;; - *) - $TAR_CMD -xOf $srcdir/$curfile data.tar.gz | \ - $TAR_CMD -xz -C "$extractdir" --transform="s,^,${wrksrc##*/}/," - ;; - esac + 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 From 10d12f95ce0483594df18a75d04e7f6adede96b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90o=C3=A0n=20Tr=E1=BA=A7n=20C=C3=B4ng=20Danh?= Date: Sat, 18 Sep 2021 21:35:34 +0700 Subject: [PATCH 2/2] hooks: do-extract: extract to temp dir then rename Extracting to temporary directory then renaming to real $wrksrc, will make the do-extract steps works atomicity. Either $wrksrc is there and complete, or it's not there. Accidentally, this change has a side effect, we can no longer care about the name of top-level components of a tarball, since we will rename the top level directory in question to $wrksrc. IOW, we don't need to set $wrksrc any longer. The side effect of above side effect: we can starting to build multiple packages that have same top-level's name without clean from now on. In another hand, we only rename the inner directory if the extracted file hierarchy has single top-level directory, we will use the renamed-temporary directory as the $wrksrc, $create_wrksrc variable is no longer relevant, and do-clean will always work probably instead of leaving some trash behind like before. --- common/hooks/do-extract/00-distfiles.sh | 32 ++++++++++++++----------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/common/hooks/do-extract/00-distfiles.sh b/common/hooks/do-extract/00-distfiles.sh index ca2011d5c45a..f14fa5823885 100644 --- a/common/hooks/do-extract/00-distfiles.sh +++ b/common/hooks/do-extract/00-distfiles.sh @@ -20,9 +20,7 @@ hook() { fi done - if [ -n "$create_wrksrc" ]; then - mkdir -p "${wrksrc}" || msg_error "$pkgver: failed to create wrksrc.\n" - fi + rm -rf "$wrksrc" # Disable trap on ERR; the code is smart enough to report errors and abort. trap - ERR @@ -31,6 +29,9 @@ hook() { [ -z "$TAR_CMD" ] && TAR_CMD="$(command -v tar)" [ -z "$TAR_CMD" ] && msg_error "xbps-src: no suitable tar cmd (bsdtar, tar)\n" + extractdir=$(mktemp -d "$XBPS_BUILDDIR/.extractdir-XXXXXXX") || + msg_error "Cannot create temporary dir for do-extract\n" + msg_normal "$pkgver: extracting distfile(s), please wait...\n" for f in ${distfiles}; do @@ -73,12 +74,6 @@ hook() { *) msg_error "$pkgver: unknown distfile suffix for $curfile.\n";; esac - if [ -n "$create_wrksrc" ]; then - extractdir="$wrksrc" - else - extractdir="$XBPS_BUILDDIR" - fi - case ${cursufx} in tar|txz|tbz|tlz|tgz|crate) $TAR_CMD -x --no-same-permissions --no-same-owner -f $srcdir/$curfile -C "$extractdir" @@ -128,11 +123,7 @@ hook() { fi ;; txt) - if [ "$create_wrksrc" ]; then - cp -f $srcdir/$curfile "$extractdir" - else - msg_error "$pkgname: ${curfile##*.} files can only be extracted when create_wrksrc is set\n" - fi + cp -f $srcdir/$curfile "$extractdir" ;; 7z) if command -v 7z &>/dev/null; then @@ -163,4 +154,17 @@ hook() { ;; esac done + + # perhap below command is less magic? + # find "$extractdir" -mindepth 1 -maxdepth 1 -printf '1\n' | wc -l + shopt -s nullglob + set -- "$extractdir"/* "$extractdir"/.* + shopt -u nullglob + if [ $# = 3 ]; then + mv "$1" "$wrksrc" && + rmdir "$extractdir" + else + mv "$extractdir" "$wrksrc" + fi || + msg_error "$pkgver: failed to move sources to $wrksrc\n" }