From 1de21189c6c94972341654d537394d2985973b18 Mon Sep 17 00:00:00 2001 From: breezerider Date: Thu, 14 Sep 2023 23:52:48 +0200 Subject: [PATCH] Option to use number for user and/or group names (#385) * Option to use number for user and/or group names * add NumericUid and NumericGid to Tar struct * writeFileToArchive: set Uname and Gname to empty string if NumericUid and NumericGid are true, respectively. * Join options to use numeric user and group id * join NumericUid and NumericGid in Tar struct * name the new option NumericUidGid * Apply suggestions from code review --------- Co-authored-by: Matt Holt --- tar.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tar.go b/tar.go index ce719695..c07efb4f 100644 --- a/tar.go +++ b/tar.go @@ -17,6 +17,9 @@ func init() { } type Tar struct { + // If true, preserve only numeric user and group id + NumericUIDGID bool + // If true, errors encountered during reading or writing // a file within an archive will be logged and the // operation will continue on remaining files. @@ -71,7 +74,7 @@ func (t Tar) ArchiveAsync(ctx context.Context, output io.Writer, jobs <-chan Arc return nil } -func (Tar) writeFileToArchive(ctx context.Context, tw *tar.Writer, file File) error { +func (t Tar) writeFileToArchive(ctx context.Context, tw *tar.Writer, file File) error { if err := ctx.Err(); err != nil { return err // honor context cancellation } @@ -81,6 +84,10 @@ func (Tar) writeFileToArchive(ctx context.Context, tw *tar.Writer, file File) er return fmt.Errorf("file %s: creating header: %w", file.NameInArchive, err) } hdr.Name = file.NameInArchive // complete path, since FileInfoHeader() only has base name + if t.NumericUIDGID { + hdr.Uname = "" + hdr.Gname = "" + } if err := tw.WriteHeader(hdr); err != nil { return fmt.Errorf("file %s: writing header: %w", file.NameInArchive, err)