[PATCH] tar: don't write [ug]name to tarball when --numeric-owner is set

Sertonix sertonix at posteo.net
Fri Aug 29 12:59:20 UTC 2025


Allows a more reproducible tarball creation

function                                             old     new   delta
tar_main                                            1273    1291     +18
writeTarHeader                                       871     873      +2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 20/0)               Total: 20 bytes

Signed-off-by: Sertonix <sertonix at posteo.net>
---
 archival/tar.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/archival/tar.c b/archival/tar.c
index 4ac870d9d..ce864d6aa 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -155,7 +155,7 @@ typedef struct HardLinkInfo {
 typedef struct TarBallInfo {
 	int tarFd;                      /* Open-for-write file descriptor
 	                                 * for the tarball */
-	int verboseFlag;                /* Whether to print extra stuff or not */
+	char flags;                     /* For TBINFO_*, see below */
 # if ENABLE_FEATURE_TAR_FROM
 	const llist_t *excludeList;     /* List of files to not include */
 # endif
@@ -168,6 +168,11 @@ typedef struct TarBallInfo {
 	                                 * to include the tarball into itself */
 } TarBallInfo;
 
+enum {
+	TBINFO_VERBOSE       = 1 << 0,  /* Whether to print extra stuff or not */
+	TBINFO_NUMERIC_OWNER = 1 << 1,  /* Don't write uname/gname to tarball */
+};
+
 /* A nice enum with all the possible tar file content types */
 enum {
 	REGTYPE = '0',		/* regular file */
@@ -316,9 +321,11 @@ static int writeTarHeader(struct TarBallInfo *tbInfo,
 	/* users report that files with negative st_mtime cause trouble, so: */
 	PUT_OCTAL(header.mtime, statbuf->st_mtime >= 0 ? statbuf->st_mtime : 0);
 
-	/* Enter the user and group names */
-	safe_strncpy(header.uname, get_cached_username(statbuf->st_uid), sizeof(header.uname));
-	safe_strncpy(header.gname, get_cached_groupname(statbuf->st_gid), sizeof(header.gname));
+	if (!(tbInfo->flags & TBINFO_NUMERIC_OWNER)) {
+		/* Enter the user and group names */
+		safe_strncpy(header.uname, get_cached_username(statbuf->st_uid), sizeof(header.uname));
+		safe_strncpy(header.gname, get_cached_groupname(statbuf->st_gid), sizeof(header.gname));
+	}
 
 	if (tbInfo->hlInfo) {
 		/* This is a hard link */
@@ -418,7 +425,7 @@ static int writeTarHeader(struct TarBallInfo *tbInfo,
 	chksum_and_xwrite_tar_header(tbInfo->tarFd, &header);
 
 	/* Now do the verbose thing (or not) */
-	if (tbInfo->verboseFlag) {
+	if (tbInfo->flags & TBINFO_VERBOSE) {
 		FILE *vbFd = stdout;
 
 		/* If archive goes to stdout, verbose goes to stderr */
@@ -1209,7 +1216,8 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
 # endif
 		tbInfo = xzalloc(sizeof(*tbInfo));
 		tbInfo->tarFd = tar_handle->src_fd;
-		tbInfo->verboseFlag = verboseFlag;
+		tbInfo->flags = (verboseFlag ? TBINFO_VERBOSE : 0) |
+			(opt & OPT_NUMERIC_OWNER ? TBINFO_NUMERIC_OWNER : 0);
 # if ENABLE_FEATURE_TAR_FROM
 		tbInfo->excludeList = tar_handle->reject;
 # endif
-- 
2.51.0



More information about the busybox mailing list