Revert "archive/tar: add FileInfoNames interface"

This reverts CL 514235. Also reverts CL 518056 which is a followup
fix.

Reason for revert: Proposal #50102 defined an interface that is
too specific to UNIX-y systems and also didn't make much sense.
The proposal is un-accepted, and we'll revisit in Go 1.23.

Fixes (via backport) #65245.
Updates #50102.

Change-Id: I41ba0ee286c1d893e6564a337e5d76418d19435d
Reviewed-on: https://go-review.googlesource.com/c/go/+/558295
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
Cherry Mui 2024-01-24 15:00:47 -05:00
parent 749ebaa446
commit 5000b51680
4 changed files with 0 additions and 119 deletions

View File

@ -1,13 +1,4 @@
pkg archive/tar, method (*Writer) AddFS(fs.FS) error #58000 pkg archive/tar, method (*Writer) AddFS(fs.FS) error #58000
pkg archive/tar, type FileInfoNames interface { Gname, IsDir, ModTime, Mode, Name, Size, Sys, Uname } #50102
pkg archive/tar, type FileInfoNames interface, Gname(int) (string, error) #50102
pkg archive/tar, type FileInfoNames interface, IsDir() bool #50102
pkg archive/tar, type FileInfoNames interface, ModTime() time.Time #50102
pkg archive/tar, type FileInfoNames interface, Mode() fs.FileMode #50102
pkg archive/tar, type FileInfoNames interface, Name() string #50102
pkg archive/tar, type FileInfoNames interface, Size() int64 #50102
pkg archive/tar, type FileInfoNames interface, Sys() interface{} #50102
pkg archive/tar, type FileInfoNames interface, Uname(int) (string, error) #50102
pkg archive/zip, method (*Writer) AddFS(fs.FS) error #54898 pkg archive/zip, method (*Writer) AddFS(fs.FS) error #54898
pkg cmp, func Or[$0 comparable](...$0) $0 #60204 pkg cmp, func Or[$0 comparable](...$0) $0 #60204
pkg crypto/x509, func OIDFromInts([]uint64) (OID, error) #60665 pkg crypto/x509, func OIDFromInts([]uint64) (OID, error) #60665

View File

@ -614,8 +614,6 @@ func (fi headerFileInfo) String() string {
// sysStat, if non-nil, populates h from system-dependent fields of fi. // sysStat, if non-nil, populates h from system-dependent fields of fi.
var sysStat func(fi fs.FileInfo, h *Header) error var sysStat func(fi fs.FileInfo, h *Header) error
var loadUidAndGid func(fi fs.FileInfo, uid, gid *int)
const ( const (
// Mode constants from the USTAR spec: // Mode constants from the USTAR spec:
// See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_06 // See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_06
@ -641,10 +639,6 @@ const (
// Since fs.FileInfo's Name method only returns the base name of // Since fs.FileInfo's Name method only returns the base name of
// the file it describes, it may be necessary to modify Header.Name // the file it describes, it may be necessary to modify Header.Name
// to provide the full path name of the file. // to provide the full path name of the file.
//
// If fi implements [FileInfoNames]
// the Gname and Uname of the header are
// provided by the methods of the interface.
func FileInfoHeader(fi fs.FileInfo, link string) (*Header, error) { func FileInfoHeader(fi fs.FileInfo, link string) (*Header, error) {
if fi == nil { if fi == nil {
return nil, errors.New("archive/tar: FileInfo is nil") return nil, errors.New("archive/tar: FileInfo is nil")
@ -717,38 +711,12 @@ func FileInfoHeader(fi fs.FileInfo, link string) (*Header, error) {
} }
} }
} }
if iface, ok := fi.(FileInfoNames); ok {
var err error
if loadUidAndGid != nil {
loadUidAndGid(fi, &h.Uid, &h.Gid)
}
h.Gname, err = iface.Gname(h.Gid)
if err != nil {
return nil, err
}
h.Uname, err = iface.Uname(h.Uid)
if err != nil {
return nil, err
}
return h, nil
}
if sysStat != nil { if sysStat != nil {
return h, sysStat(fi, h) return h, sysStat(fi, h)
} }
return h, nil return h, nil
} }
// FileInfoNames extends [FileInfo] to translate UID/GID to names.
// Passing an instance of this to [FileInfoHeader] permits the caller
// to control UID/GID resolution.
type FileInfoNames interface {
fs.FileInfo
// Uname should translate a UID into a user name.
Uname(uid int) (string, error)
// Gname should translate a GID into a group name.
Gname(gid int) (string, error)
}
// isHeaderOnlyType checks if the given type flag is of the type that has no // isHeaderOnlyType checks if the given type flag is of the type that has no
// data section even if a size is specified. // data section even if a size is specified.
func isHeaderOnlyType(flag byte) bool { func isHeaderOnlyType(flag byte) bool {

View File

@ -17,7 +17,6 @@ import (
func init() { func init() {
sysStat = statUnix sysStat = statUnix
loadUidAndGid = loadUidAndGidFunc
} }
// userMap and groupMap caches UID and GID lookups for performance reasons. // userMap and groupMap caches UID and GID lookups for performance reasons.
@ -100,12 +99,3 @@ func statUnix(fi fs.FileInfo, h *Header) error {
} }
return nil return nil
} }
func loadUidAndGidFunc(fi fs.FileInfo, uid, gid *int) {
sys, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return
}
*uid = int(sys.Uid)
*gid = int(sys.Gid)
}

View File

@ -848,71 +848,3 @@ func Benchmark(b *testing.B) {
}) })
} }
const (
testUid = 10
testGid = 20
)
type fileInfoNames struct{}
func (f *fileInfoNames) Name() string {
return "tmp"
}
func (f *fileInfoNames) Size() int64 {
return 0
}
func (f *fileInfoNames) Mode() fs.FileMode {
return 0777
}
func (f *fileInfoNames) ModTime() time.Time {
return time.Time{}
}
func (f *fileInfoNames) IsDir() bool {
return false
}
func (f *fileInfoNames) Sys() any {
return nil
}
func (f *fileInfoNames) Uname(uid int) (string, error) {
if uid == testUid {
return "Uname", nil
}
return "", nil
}
func (f *fileInfoNames) Gname(gid int) (string, error) {
if gid == testGid {
return "Gname", nil
}
return "", nil
}
func TestFileInfoHeaderUseFileInfoNames(t *testing.T) {
origLoadUidAndGid := loadUidAndGid
defer func() {
loadUidAndGid = origLoadUidAndGid
}()
loadUidAndGid = func(fi fs.FileInfo, uid, gid *int) {
*uid = testUid
*gid = testGid
}
info := &fileInfoNames{}
header, err := FileInfoHeader(info, "")
if err != nil {
t.Fatal(err)
}
if header.Uname != "Uname" {
t.Fatalf("header.Uname: got %v, want %v", header.Uname, "Uname")
}
if header.Gname != "Gname" {
t.Fatalf("header.Gname: got %v, want %v", header.Gname, "Gname")
}
}