Use filepath.WalkDir instead of filepath.Walk

... to optimize away some lstat(2) calls.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
Miloslav Trmač 2022-04-13 20:06:12 +02:00
parent 23a4605742
commit d9d3ceca45
3 changed files with 10 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
@ -258,11 +259,11 @@ func imagesToCopyFromRepo(sys *types.SystemContext, repoRef reference.Named) ([]
// and any error encountered.
func imagesToCopyFromDir(dirPath string) ([]types.ImageReference, error) {
var sourceReferences []types.ImageReference
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
err := filepath.WalkDir(dirPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !info.IsDir() && info.Name() == "manifest.json" {
if !d.IsDir() && d.Name() == "manifest.json" {
dirname := filepath.Dir(path)
ref, err := directory.Transport.ParseReference(dirname)
if err != nil {

View File

@ -6,6 +6,7 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"io/fs"
"log"
"net/http"
"net/http/httptest"
@ -831,15 +832,15 @@ func (s *CopySuite) TestCopyCompression(c *check.C) {
func findRegularFiles(c *check.C, root string) []string {
result := []string{}
err := filepath.Walk(root, filepath.WalkFunc(func(path string, info os.FileInfo, err error) error {
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if info.Mode().IsRegular() {
if d.Type().IsRegular() {
result = append(result, path)
}
return nil
}))
})
c.Assert(err, check.IsNil)
return result
}

View File

@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
@ -102,11 +103,11 @@ func (s *SyncSuite) TearDownSuite(c *check.C) {
func assertNumberOfManifestsInSubdirs(c *check.C, dir string, expectedCount int) {
nManifests := 0
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !info.IsDir() && info.Name() == "manifest.json" {
if !d.IsDir() && d.Name() == "manifest.json" {
nManifests++
return filepath.SkipDir
}