1
0
mirror of https://github.com/golang/go synced 2024-11-26 22:11:25 -07:00

all: add String for fs.{FileInfo,DirEntry} implementations

The new String methods use the new FormatFileInfo and
FormatDirEntry functions.

Fixes #54451

Change-Id: I414cdfc212ec3c316fb2734756d2117842a23631
Reviewed-on: https://go-review.googlesource.com/c/go/+/491175
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
This commit is contained in:
Ian Lance Taylor 2023-05-01 17:38:08 -07:00 committed by Gopher Robot
parent 09a24a91b5
commit 1596aeec8e
18 changed files with 83 additions and 0 deletions

View File

@ -607,6 +607,10 @@ func (fi headerFileInfo) Mode() (mode fs.FileMode) {
return mode return mode
} }
func (fi headerFileInfo) String() string {
return fs.FormatFileInfo(fi)
}
// 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

View File

@ -780,6 +780,10 @@ func (f *fileListEntry) ModTime() time.Time {
func (f *fileListEntry) Info() (fs.FileInfo, error) { return f, nil } func (f *fileListEntry) Info() (fs.FileInfo, error) { return f, nil }
func (f *fileListEntry) String() string {
return fs.FormatDirEntry(f)
}
// toValidName coerces name to be a valid name for fs.FS.Open. // toValidName coerces name to be a valid name for fs.FS.Open.
func toValidName(name string) string { func toValidName(name string) string {
name = strings.ReplaceAll(name, `\`, `/`) name = strings.ReplaceAll(name, `\`, `/`)

View File

@ -190,6 +190,10 @@ func (fi headerFileInfo) Sys() any { return fi.fh }
func (fi headerFileInfo) Info() (fs.FileInfo, error) { return fi, nil } func (fi headerFileInfo) Info() (fs.FileInfo, error) { return fi, nil }
func (fi headerFileInfo) String() string {
return fs.FormatFileInfo(fi)
}
// FileInfoHeader creates a partially-populated FileHeader from an // FileInfoHeader creates a partially-populated FileHeader from an
// fs.FileInfo. // fs.FileInfo.
// Because fs.FileInfo's Name method returns only the base name of // Because fs.FileInfo's Name method returns only the base name of

View File

@ -48,6 +48,10 @@ func (i fileInfo) IsDir() bool { return false }
func (i fileInfo) Size() int64 { return i.f.Size } func (i fileInfo) Size() int64 { return i.f.Size }
func (i fileInfo) Sys() any { return nil } func (i fileInfo) Sys() any { return nil }
func (i fileInfo) String() string {
return fs.FormatFileInfo(i)
}
// NewArchive returns a new Archive containing all the files in the directory dir. // NewArchive returns a new Archive containing all the files in the directory dir.
// The archive can be amended afterward using methods like Add and Filter. // The archive can be amended afterward using methods like Add and Filter.
func NewArchive(dir string) (*Archive, error) { func NewArchive(dir string) (*Archive, error) {

View File

@ -583,6 +583,10 @@ func (f fakeFile) ModTime() time.Time { return f.real.ModTime() }
func (f fakeFile) IsDir() bool { return f.real.IsDir() } func (f fakeFile) IsDir() bool { return f.real.IsDir() }
func (f fakeFile) Sys() any { return f.real.Sys() } func (f fakeFile) Sys() any { return f.real.Sys() }
func (f fakeFile) String() string {
return fs.FormatFileInfo(f)
}
// missingFile provides an fs.FileInfo for an overlaid file where the // missingFile provides an fs.FileInfo for an overlaid file where the
// destination file in the overlay doesn't exist. It returns zero values // destination file in the overlay doesn't exist. It returns zero values
// for the fileInfo methods other than Name, set to the file's name, and Mode // for the fileInfo methods other than Name, set to the file's name, and Mode
@ -596,6 +600,10 @@ func (f missingFile) ModTime() time.Time { return time.Unix(0, 0) }
func (f missingFile) IsDir() bool { return false } func (f missingFile) IsDir() bool { return false }
func (f missingFile) Sys() any { return nil } func (f missingFile) Sys() any { return nil }
func (f missingFile) String() string {
return fs.FormatFileInfo(f)
}
// fakeDir provides an fs.FileInfo implementation for directories that are // fakeDir provides an fs.FileInfo implementation for directories that are
// implicitly created by overlaid files. Each directory in the // implicitly created by overlaid files. Each directory in the
// path of an overlaid file is considered to exist in the overlay filesystem. // path of an overlaid file is considered to exist in the overlay filesystem.
@ -608,6 +616,10 @@ func (f fakeDir) ModTime() time.Time { return time.Unix(0, 0) }
func (f fakeDir) IsDir() bool { return true } func (f fakeDir) IsDir() bool { return true }
func (f fakeDir) Sys() any { return nil } func (f fakeDir) Sys() any { return nil }
func (f fakeDir) String() string {
return fs.FormatFileInfo(f)
}
// Glob is like filepath.Glob but uses the overlay file system. // Glob is like filepath.Glob but uses the overlay file system.
func Glob(pattern string) (matches []string, err error) { func Glob(pattern string) (matches []string, err error) {
Trace("Glob", pattern) Trace("Glob", pattern)

View File

@ -1155,6 +1155,10 @@ func (fi dataFileInfo) ModTime() time.Time { return time.Time{} }
func (fi dataFileInfo) IsDir() bool { return false } func (fi dataFileInfo) IsDir() bool { return false }
func (fi dataFileInfo) Sys() any { return nil } func (fi dataFileInfo) Sys() any { return nil }
func (fi dataFileInfo) String() string {
return fs.FormatFileInfo(fi)
}
// hasPathPrefix reports whether the path s begins with the // hasPathPrefix reports whether the path s begins with the
// elements in prefix. // elements in prefix.
func hasPathPrefix(s, prefix string) bool { func hasPathPrefix(s, prefix string) bool {

View File

@ -179,3 +179,7 @@ func (d *statDirEntry) Name() string { return d.info.Name() }
func (d *statDirEntry) IsDir() bool { return d.info.IsDir() } func (d *statDirEntry) IsDir() bool { return d.info.IsDir() }
func (d *statDirEntry) Type() fs.FileMode { return d.info.Mode().Type() } func (d *statDirEntry) Type() fs.FileMode { return d.info.Mode().Type() }
func (d *statDirEntry) Info() (fs.FileInfo, error) { return d.info, nil } func (d *statDirEntry) Info() (fs.FileInfo, error) { return d.info, nil }
func (d *statDirEntry) String() string {
return fs.FormatDirEntry(d)
}

View File

@ -497,6 +497,10 @@ func (f *FakeFile) Sys() any {
return nil return nil
} }
func (f *FakeFile) String() string {
return fs.FormatFileInfo(f)
}
// Special helpers. // Special helpers.
func (f *FakeFile) Entry() *archive.Entry { func (f *FakeFile) Entry() *archive.Entry {

View File

@ -243,6 +243,10 @@ func (f *file) Mode() fs.FileMode {
return 0444 return 0444
} }
func (f *file) String() string {
return fs.FormatFileInfo(f)
}
// dotFile is a file for the root directory, // dotFile is a file for the root directory,
// which is omitted from the files list in a FS. // which is omitted from the files list in a FS.
var dotFile = &file{name: "./"} var dotFile = &file{name: "./"}

View File

@ -67,6 +67,10 @@ func (di dirInfo) Name() string {
return di.fileInfo.Name() return di.fileInfo.Name()
} }
func (di dirInfo) String() string {
return FormatDirEntry(di)
}
// FileInfoToDirEntry returns a DirEntry that returns information from info. // FileInfoToDirEntry returns a DirEntry that returns information from info.
// If info is nil, FileInfoToDirEntry returns nil. // If info is nil, FileInfoToDirEntry returns nil.
func FileInfoToDirEntry(info FileInfo) DirEntry { func FileInfoToDirEntry(info FileInfo) DirEntry {

View File

@ -135,3 +135,7 @@ func (d *statDirEntry) Name() string { return d.info.Name() }
func (d *statDirEntry) IsDir() bool { return d.info.IsDir() } func (d *statDirEntry) IsDir() bool { return d.info.IsDir() }
func (d *statDirEntry) Type() FileMode { return d.info.Mode().Type() } func (d *statDirEntry) Type() FileMode { return d.info.Mode().Type() }
func (d *statDirEntry) Info() (FileInfo, error) { return d.info, nil } func (d *statDirEntry) Info() (FileInfo, error) { return d.info, nil }
func (d *statDirEntry) String() string {
return FormatDirEntry(d)
}

View File

@ -768,6 +768,10 @@ func (f *fakeFileInfo) Mode() fs.FileMode {
return 0644 return 0644
} }
func (f *fakeFileInfo) String() string {
return fs.FormatFileInfo(f)
}
type fakeFile struct { type fakeFile struct {
io.ReadSeeker io.ReadSeeker
fi *fakeFileInfo fi *fakeFileInfo

View File

@ -6,6 +6,7 @@ package os
import ( import (
"io" "io"
"io/fs"
"syscall" "syscall"
) )
@ -79,3 +80,7 @@ func (de dirEntry) Name() string { return de.fs.Name() }
func (de dirEntry) IsDir() bool { return de.fs.IsDir() } func (de dirEntry) IsDir() bool { return de.fs.IsDir() }
func (de dirEntry) Type() FileMode { return de.fs.Mode().Type() } func (de dirEntry) Type() FileMode { return de.fs.Mode().Type() }
func (de dirEntry) Info() (FileInfo, error) { return de.fs, nil } func (de dirEntry) Info() (FileInfo, error) { return de.fs, nil }
func (de dirEntry) String() string {
return fs.FormatDirEntry(de)
}

View File

@ -7,6 +7,7 @@ package os
import ( import (
"internal/syscall/windows" "internal/syscall/windows"
"io" "io"
"io/fs"
"runtime" "runtime"
"sync" "sync"
"syscall" "syscall"
@ -140,3 +141,7 @@ func (de dirEntry) Name() string { return de.fs.Name() }
func (de dirEntry) IsDir() bool { return de.fs.IsDir() } func (de dirEntry) IsDir() bool { return de.fs.IsDir() }
func (de dirEntry) Type() FileMode { return de.fs.Mode().Type() } func (de dirEntry) Type() FileMode { return de.fs.Mode().Type() }
func (de dirEntry) Info() (FileInfo, error) { return de.fs, nil } func (de dirEntry) Info() (FileInfo, error) { return de.fs, nil }
func (de dirEntry) String() string {
return fs.FormatDirEntry(de)
}

View File

@ -9,6 +9,7 @@ package os
import ( import (
"internal/poll" "internal/poll"
"internal/syscall/unix" "internal/syscall/unix"
"io/fs"
"runtime" "runtime"
"syscall" "syscall"
) )
@ -432,6 +433,10 @@ func (d *unixDirent) Info() (FileInfo, error) {
return lstat(d.parent + "/" + d.name) return lstat(d.parent + "/" + d.name)
} }
func (d *unixDirent) String() string {
return fs.FormatDirEntry(d)
}
func newUnixDirent(parent, name string, typ FileMode) (DirEntry, error) { func newUnixDirent(parent, name string, typ FileMode) (DirEntry, error) {
ude := &unixDirent{ ude := &unixDirent{
parent: parent, parent: parent,

View File

@ -553,6 +553,10 @@ func (d *statDirEntry) IsDir() bool { return d.info.IsDir() }
func (d *statDirEntry) Type() fs.FileMode { return d.info.Mode().Type() } func (d *statDirEntry) Type() fs.FileMode { return d.info.Mode().Type() }
func (d *statDirEntry) Info() (fs.FileInfo, error) { return d.info, nil } func (d *statDirEntry) Info() (fs.FileInfo, error) { return d.info, nil }
func (d *statDirEntry) String() string {
return fs.FormatDirEntry(d)
}
// Walk walks the file tree rooted at root, calling fn for each file or // Walk walks the file tree rooted at root, calling fn for each file or
// directory in the tree, including root. // directory in the tree, including root.
// //

View File

@ -571,6 +571,10 @@ func (d *statDirEntry) IsDir() bool { return d.info.IsDir() }
func (d *statDirEntry) Type() fs.FileMode { return d.info.Mode().Type() } func (d *statDirEntry) Type() fs.FileMode { return d.info.Mode().Type() }
func (d *statDirEntry) Info() (fs.FileInfo, error) { return d.info, nil } func (d *statDirEntry) Info() (fs.FileInfo, error) { return d.info, nil }
func (d *statDirEntry) String() string {
return fs.FormatDirEntry(d)
}
func TestWalkDir(t *testing.T) { func TestWalkDir(t *testing.T) {
testWalk(t, filepath.WalkDir, 2) testWalk(t, filepath.WalkDir, 2)
} }

View File

@ -159,6 +159,10 @@ func (i *mapFileInfo) IsDir() bool { return i.f.Mode&fs.ModeDir !
func (i *mapFileInfo) Sys() any { return i.f.Sys } func (i *mapFileInfo) Sys() any { return i.f.Sys }
func (i *mapFileInfo) Info() (fs.FileInfo, error) { return i, nil } func (i *mapFileInfo) Info() (fs.FileInfo, error) { return i, nil }
func (i *mapFileInfo) String() string {
return fs.FormatFileInfo(i)
}
// An openMapFile is a regular (non-directory) fs.File open for reading. // An openMapFile is a regular (non-directory) fs.File open for reading.
type openMapFile struct { type openMapFile struct {
path string path string