From 509f79078c796170cd71f8f147266c9054399b9a 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] 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 | 48 ++++++++++++++++--------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/common/hooks/do-extract/00-distfiles.sh b/common/hooks/do-extract/00-distfiles.sh index ca2011d5c45..c173fe2af1e 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 innerdir + local f j curfile found extractdir innerdir num_dirs local TAR_CMD if [ -z "$distfiles" -a -z "$checksum" ]; then @@ -20,10 +20,6 @@ hook() { fi done - if [ -n "$create_wrksrc" ]; then - mkdir -p "${wrksrc}" || msg_error "$pkgver: failed to create wrksrc.\n" - fi - # Disable trap on ERR; the code is smart enough to report errors and abort. trap - ERR @@ -31,6 +27,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 +72,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 +121,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 +152,31 @@ hook() { ;; esac done + + # find "$extractdir" -mindepth 1 -maxdepth 1 -printf '1\n' | wc -l + # However, it requires GNU's find + num_dirs=0 + for f in "$extractdir"/* "$extractdir"/.*; do + if [ -e "$f" ] || [ -L "$f" ]; then + case "$f" in + */. | */..) ;; + *) + innerdir="$f" + num_dirs=$(( num_dirs + 1 )) + ;; + esac + fi + done + rm -rf "$wrksrc" + if [ "$num_dirs" = 1 ] && [ -d "$innerdir" ] && [ -z "$create_wrksrc" ]; then + # rename the subdirectory (top-level of distfiles) to $wrksrc + mv "$innerdir" "$wrksrc" && + rmdir "$extractdir" + elif [ "$num_dirs" -gt 1 ] || [ -n "$create_wrksrc" ]; then + # rename the tmpdir to wrksrc + mv "$extractdir" "$wrksrc" + else + mkdir -p "$wrksrc" + fi || + msg_error "$pkgver: failed to move sources to $wrksrc\n" }