git: e571b0f8f8c1 - main - etcupdate: Fix -N support for build command

From: Jessica Clarke <jrtc27_at_FreeBSD.org>
Date: Mon, 03 Oct 2022 16:17:00 UTC
The branch main has been updated by jrtc27:

URL: https://cgit.FreeBSD.org/src/commit/?id=e571b0f8f8c1730330fb656d54f081beb993c962

commit e571b0f8f8c1730330fb656d54f081beb993c962
Author:     Jessica Clarke <jrtc27@FreeBSD.org>
AuthorDate: 2022-10-03 16:16:19 +0000
Commit:     Jessica Clarke <jrtc27@FreeBSD.org>
CommitDate: 2022-10-03 16:16:19 +0000

    etcupdate: Fix -N support for build command
    
    Whilst febca0e64361 added -N for both build and extract, it only fully
    worked for extract; build would perform the actual tree build with
    -DNO_ROOT and construct the intended METALOG, but the subsequent tarball
    creation did not take this into account and just tarred up the the
    directory as-is rather than using the METALOG. This resulted in the
    permissions and ownership not being correct, as well as there being a
    stray METALOG file in the tarball's root.
    
    Reported by:    avg
    Reviewed by:    avg, jhb, imp
    Tested by:      avg
    Fixes:          febca0e64361 ("etcupdate: Add a -N flag to perform a NO_ROOT build")
    Differential Revision:  https://reviews.freebsd.org/D36845
---
 usr.sbin/etcupdate/etcupdate.sh | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/usr.sbin/etcupdate/etcupdate.sh b/usr.sbin/etcupdate/etcupdate.sh
index 0190e5868f05..445b8e105c61 100755
--- a/usr.sbin/etcupdate/etcupdate.sh
+++ b/usr.sbin/etcupdate/etcupdate.sh
@@ -1321,7 +1321,7 @@ handle_added_file()
 # Build a new tree and save it in a tarball.
 build_cmd()
 {
-	local dir
+	local dir tartree
 
 	if [ $# -ne 1 ]; then
 		echo "Missing required tarball."
@@ -1342,7 +1342,12 @@ build_cmd()
 		remove_tree $dir
 		exit 1
 	fi
-	if ! tar cfj $1 -C $dir . >&3 2>&1; then
+	if [ -n "$noroot" ]; then
+		tartree=@METALOG
+	else
+		tartree=.
+	fi
+	if ! tar cfj $1 -C $dir $tartree >&3 2>&1; then
 		echo "Failed to create tarball."
 		remove_tree $dir
 		exit 1