Skip to content

Commit

Permalink
Use filepath.WalkDir instead of filepath.Walk
Browse files Browse the repository at this point in the history
... to optimize away some lstat(2) calls.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
  • Loading branch information
mtrmac committed Apr 13, 2022
1 parent 23a4605 commit d9d3cec
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
5 changes: 3 additions & 2 deletions cmd/skopeo/sync.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions integration/copy_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"io/fs"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions integration/sync_test.go
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit d9d3cec

Please sign in to comment.