From 06695133c9c17cf5677c0133d9e5bbeb3d7e9880 Mon Sep 17 00:00:00 2001
From: Sertonix <sertonix@posteo.net>
Date: Mon, 22 Apr 2024 14:54:23 +0200
Subject: [PATCH] abuild: preserve attributes without setfattr/getfattr

The setfattr/getfattr commands were not guaranteed to be available and
abuild wouldn't properly error when they are not. That may cause some
hidden issues.

Changing the code to not need setfattr/getfattr like it is done in
default_dbg ([3ad93d9a838]) and pacman mkpkg ([pacman@88d054093c1]).

The code works by first writing the stripped output into a temporary file
and then replace the contents of the already existing file without changing
any file metadata.

[3ad93d9a838]: https://gitlab.alpinelinux.org/alpine/abuild/-/commit/3ad93d9a83817195ba79962e6e2d4aaab0ec2add
[pacman@88d054093c1]: https://gitlab.archlinux.org/pacman/pacman/-/commit/88d054093c1c99a697d95b26bd9aad5bc4d8e170
---
 abuild.in         | 14 ++++++++------
 tests/abuild_test | 11 -----------
 2 files changed, 8 insertions(+), 17 deletions(-)

diff --git a/abuild.in b/abuild.in
index 2ef68b8..b4a6d41 100644
--- a/abuild.in
+++ b/abuild.in
@@ -28,7 +28,6 @@ fi
 : ${APK:="apk"}
 : ${ADDUSER:="abuild-adduser"}
 : ${ADDGROUP:="abuild-addgroup"}
-: ${SETFATTR:="setfattr"}
 
 apk_opt_wait="--wait 30"
 doc_threshold=$((2 * 1024 * 1024)) # 2 MiB
@@ -2600,6 +2599,8 @@ stripbin() {
 	esac
 
 	msg "Stripping binaries"
+
+	mkdir "$pkgbasedir/.strip-tmp"
 	scanelf --recursive --nobanner --osabi --etype "ET_DYN,ET_EXEC" . \
 		| while read type osabi filename; do
 
@@ -2607,12 +2608,13 @@ stripbin() {
 		[ -e "$filename" ] || continue
 
 		[ "$osabi" != "STANDALONE" ] || continue
-		local XATTR=$(getfattr --match="" --dump "${filename}")
-		"${stripcmd}" "${filename}"
-		if [ -n "$XATTR" ]; then
-			echo "$XATTR" | "$SETFATTR" --restore=-
-		fi
+		local tmp="$pkgbasedir/.strip-tmp/${filename##*/}"
+		"${stripcmd}" -o "$tmp" "${filename}"
+		# only replace content to preserve attributes
+		cat "$tmp" > "$filename"
+		rm "$tmp"
 	done
+	rmdir "$pkgbasedir/.strip-tmp"
 }
 
 # simply list target apks
diff --git a/tests/abuild_test b/tests/abuild_test
index 91d0374..cc6aa86 100755
--- a/tests/abuild_test
+++ b/tests/abuild_test
@@ -10,7 +10,6 @@ init_tests \
 	abuild_invalid_filename \
 	abuild_usr_lib64 \
 	abuild_dbg_subpackage \
-	abuild_SETFATTR_in_dbg \
 	abuild_reproducible \
 	abuild_checksum_generation \
 	abuild_checksum_duplicates \
@@ -238,16 +237,6 @@ abuild_dbg_subpackage_body() {
 	fi
 }
 
-abuild_SETFATTR_in_dbg_body() {
-	init_keys
-	cp -ra "$testrepo"/dbgpkg .
-	cd dbgpkg
-	SETFATTR=true atf_check -s exit:0 \
-		-o match:"hello world" \
-		-e match:"Build complete" \
-		abuild
-}
-
 abuild_reproducible_body() {
 	init_keys
 	cp -ra "$testrepo" .