mirror of
https://github.com/golang/go
synced 2024-11-18 18:04:46 -07:00
io/fs, path/filepath, cmd/gofmt: replace the custom statDirEntry with fs.FileInfoToDirEntry()
Use fs.FileInfoToDirEntry() to reduce duplicate code.
This commit is contained in:
parent
eaae2d45c7
commit
bc61665d33
@ -126,7 +126,7 @@ func genFilenames(t *testing.T, filenames chan<- string) {
|
|||||||
if *files != "" {
|
if *files != "" {
|
||||||
for _, filename := range strings.Split(*files, ",") {
|
for _, filename := range strings.Split(*files, ",") {
|
||||||
fi, err := os.Stat(filename)
|
fi, err := os.Stat(filename)
|
||||||
handleFile(filename, &statDirEntry{fi}, err)
|
handleFile(filename, fs.FileInfoToDirEntry(fi), err)
|
||||||
}
|
}
|
||||||
return // ignore files under -root
|
return // ignore files under -root
|
||||||
}
|
}
|
||||||
@ -170,16 +170,3 @@ func TestAll(t *testing.T) {
|
|||||||
fmt.Printf("processed %d files\n", nfiles)
|
fmt.Printf("processed %d files\n", nfiles)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type statDirEntry struct {
|
|
||||||
info fs.FileInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *statDirEntry) Name() string { return d.info.Name() }
|
|
||||||
func (d *statDirEntry) IsDir() bool { return d.info.IsDir() }
|
|
||||||
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) String() string {
|
|
||||||
return fs.FormatDirEntry(d)
|
|
||||||
}
|
|
||||||
|
@ -119,23 +119,10 @@ func WalkDir(fsys FS, root string, fn WalkDirFunc) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
err = fn(root, nil, err)
|
err = fn(root, nil, err)
|
||||||
} else {
|
} else {
|
||||||
err = walkDir(fsys, root, &statDirEntry{info}, fn)
|
err = walkDir(fsys, root, FileInfoToDirEntry(info), fn)
|
||||||
}
|
}
|
||||||
if err == SkipDir || err == SkipAll {
|
if err == SkipDir || err == SkipAll {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type statDirEntry struct {
|
|
||||||
info FileInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *statDirEntry) Name() string { return d.info.Name() }
|
|
||||||
func (d *statDirEntry) IsDir() bool { return d.info.IsDir() }
|
|
||||||
func (d *statDirEntry) Type() FileMode { return d.info.Mode().Type() }
|
|
||||||
func (d *statDirEntry) Info() (FileInfo, error) { return d.info, nil }
|
|
||||||
|
|
||||||
func (d *statDirEntry) String() string {
|
|
||||||
return FormatDirEntry(d)
|
|
||||||
}
|
|
||||||
|
@ -545,7 +545,7 @@ func WalkDir(root string, fn fs.WalkDirFunc) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
err = fn(root, nil, err)
|
err = fn(root, nil, err)
|
||||||
} else {
|
} else {
|
||||||
err = walkDir(root, &statDirEntry{info}, fn)
|
err = walkDir(root, fs.FileInfoToDirEntry(info), fn)
|
||||||
}
|
}
|
||||||
if err == SkipDir || err == SkipAll {
|
if err == SkipDir || err == SkipAll {
|
||||||
return nil
|
return nil
|
||||||
@ -553,19 +553,6 @@ func WalkDir(root string, fn fs.WalkDirFunc) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type statDirEntry struct {
|
|
||||||
info fs.FileInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *statDirEntry) Name() string { return d.info.Name() }
|
|
||||||
func (d *statDirEntry) IsDir() bool { return d.info.IsDir() }
|
|
||||||
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) 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.
|
||||||
//
|
//
|
||||||
|
@ -560,25 +560,12 @@ func tempDirCanonical(t *testing.T) string {
|
|||||||
func TestWalk(t *testing.T) {
|
func TestWalk(t *testing.T) {
|
||||||
walk := func(root string, fn fs.WalkDirFunc) error {
|
walk := func(root string, fn fs.WalkDirFunc) error {
|
||||||
return filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
|
return filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
|
||||||
return fn(path, &statDirEntry{info}, err)
|
return fn(path, fs.FileInfoToDirEntry(info), err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
testWalk(t, walk, 1)
|
testWalk(t, walk, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
type statDirEntry struct {
|
|
||||||
info fs.FileInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *statDirEntry) Name() string { return d.info.Name() }
|
|
||||||
func (d *statDirEntry) IsDir() bool { return d.info.IsDir() }
|
|
||||||
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) 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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user