Skip to content

Commit

Permalink
Option to use number for user and/or group names (#385)
Browse files Browse the repository at this point in the history
* 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 <mholt@users.noreply.github.com>
  • Loading branch information
breezerider and mholt committed Sep 14, 2023
1 parent 24fa33e commit 1de2118
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion tar.go
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down

0 comments on commit 1de2118

Please sign in to comment.