Skip to content

Commit 124d651

Browse files
use statUnix
Fixes #50102 Change-Id: I8ec67a56f2ab61d78ae5890e5a80cd2e8acd9a38
1 parent 292ab66 commit 124d651

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

src/archive/tar/common.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ func (fi headerFileInfo) String() string {
612612
}
613613

614614
// sysStat, if non-nil, populates h from system-dependent fields of fi.
615-
var sysStat func(fi fs.FileInfo, h *Header) error
615+
var sysStat func(fi fs.FileInfo, h *Header, useFileInfoNames bool) error
616616

617617
const (
618618
// Mode constants from the USTAR spec:
@@ -715,7 +715,9 @@ func FileInfoHeader(fi fs.FileInfo, link string) (*Header, error) {
715715
}
716716
}
717717
}
718+
var useFileInfoNames = false
718719
if iface, ok := fi.(FileInfoNames); ok {
720+
useFileInfoNames = true
719721
var err error
720722
h.Gname, err = iface.Gname()
721723
if err != nil {
@@ -733,10 +735,9 @@ func FileInfoHeader(fi fs.FileInfo, link string) (*Header, error) {
733735
if err != nil {
734736
return nil, err
735737
}
736-
return h, nil
737738
}
738739
if sysStat != nil {
739-
return h, sysStat(fi, h)
740+
return h, sysStat(fi, h, useFileInfoNames)
740741
}
741742
return h, nil
742743
}

src/archive/tar/stat_unix.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,30 @@ func init() {
2323
// The downside is that renaming uname or gname by the OS never takes effect.
2424
var userMap, groupMap sync.Map // map[int]string
2525

26-
func statUnix(fi fs.FileInfo, h *Header) error {
27-
sys, ok := fi.Sys().(*syscall.Stat_t)
28-
if !ok {
29-
return nil
30-
}
31-
h.Uid = int(sys.Uid)
32-
h.Gid = int(sys.Gid)
26+
func statUnix(fi fs.FileInfo, h *Header, useFileInfoNames bool) error {
27+
if !useFileInfoNames {
28+
sys, ok := fi.Sys().(*syscall.Stat_t)
29+
if !ok {
30+
return nil
31+
}
32+
h.Uid = int(sys.Uid)
33+
h.Gid = int(sys.Gid)
3334

34-
// Best effort at populating Uname and Gname.
35-
// The os/user functions may fail for any number of reasons
36-
// (not implemented on that platform, cgo not enabled, etc).
37-
if u, ok := userMap.Load(h.Uid); ok {
38-
h.Uname = u.(string)
39-
} else if u, err := user.LookupId(strconv.Itoa(h.Uid)); err == nil {
40-
h.Uname = u.Username
41-
userMap.Store(h.Uid, h.Uname)
42-
}
43-
if g, ok := groupMap.Load(h.Gid); ok {
44-
h.Gname = g.(string)
45-
} else if g, err := user.LookupGroupId(strconv.Itoa(h.Gid)); err == nil {
46-
h.Gname = g.Name
47-
groupMap.Store(h.Gid, h.Gname)
35+
// Best effort at populating Uname and Gname.
36+
// The os/user functions may fail for any number of reasons
37+
// (not implemented on that platform, cgo not enabled, etc).
38+
if u, ok := userMap.Load(h.Uid); ok {
39+
h.Uname = u.(string)
40+
} else if u, err := user.LookupId(strconv.Itoa(h.Uid)); err == nil {
41+
h.Uname = u.Username
42+
userMap.Store(h.Uid, h.Uname)
43+
}
44+
if g, ok := groupMap.Load(h.Gid); ok {
45+
h.Gname = g.(string)
46+
} else if g, err := user.LookupGroupId(strconv.Itoa(h.Gid)); err == nil {
47+
h.Gname = g.Name
48+
groupMap.Store(h.Gid, h.Gname)
49+
}
4850
}
4951

5052
h.AccessTime = statAtime(sys)

0 commit comments

Comments
 (0)