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 61af1ad commit 870bd53
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
5 changes: 3 additions & 2 deletions oci/layout/oci_dest_test.go
Expand Up @@ -3,6 +3,7 @@ package layout
import (
"bytes"
"context"
"io/fs"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -142,7 +143,7 @@ func putTestConfig(t *testing.T, ociRef ociReference, tmpDir string) {
assert.NoError(t, err)

paths := []string{}
err = filepath.Walk(tmpDir, func(path string, info os.FileInfo, err error) error {
err = filepath.WalkDir(tmpDir, func(path string, _ fs.DirEntry, err error) error {
paths = append(paths, path)
return nil
})
Expand All @@ -165,7 +166,7 @@ func putTestManifest(t *testing.T, ociRef ociReference, tmpDir string) {
assert.NoError(t, err)

paths := []string{}
err = filepath.Walk(tmpDir, func(path string, info os.FileInfo, err error) error {
err = filepath.WalkDir(tmpDir, func(path string, _ fs.DirEntry, err error) error {
paths = append(paths, path)
return nil
})
Expand Down
9 changes: 5 additions & 4 deletions pkg/sysregistriesv2/system_registries_v2.go
Expand Up @@ -2,6 +2,7 @@ package sysregistriesv2

import (
"fmt"
"io/fs"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -643,17 +644,17 @@ func dropInConfigs(wrapper configWrapper) ([]string, error) {
dirPaths = append(dirPaths, wrapper.userConfigDirPath)
}
for _, dirPath := range dirPaths {
err := filepath.Walk(dirPath,
err := filepath.WalkDir(dirPath,
// WalkFunc to read additional configs
func(path string, info os.FileInfo, err error) error {
func(path string, d fs.DirEntry, err error) error {
switch {
case err != nil:
// return error (could be a permission problem)
return err
case info == nil:
case d == nil:
// this should only happen when err != nil but let's be sure
return nil
case info.IsDir():
case d.IsDir():
if path != dirPath {
// make sure to not recurse into sub-directories
return filepath.SkipDir
Expand Down

0 comments on commit 870bd53

Please sign in to comment.