From ec000ce555a789b1461afc7fe88b62233599bb88 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 9 Oct 2022 17:12:04 +0200 Subject: [PATCH] pkg/archive: use filepath.WalkDir instead of filepath.Walk WalkDir is more performant as it doesn't perform an os.Lstat on every visited file or directory. Signed-off-by: Sebastiaan van Stijn --- pkg/archive/archive.go | 2 +- pkg/archive/changes_other.go | 2 +- pkg/archive/copy_unix_test.go | 7 ++----- pkg/archive/diff.go | 5 ++--- pkg/archive/utils_test.go | 2 +- 5 files changed, 7 insertions(+), 11 deletions(-) diff --git a/pkg/archive/archive.go b/pkg/archive/archive.go index 99ba34328e..3bc8cf1c21 100644 --- a/pkg/archive/archive.go +++ b/pkg/archive/archive.go @@ -904,7 +904,7 @@ func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error) ) walkRoot := getWalkRoot(srcPath, include) - filepath.Walk(walkRoot, func(filePath string, f os.FileInfo, err error) error { + filepath.WalkDir(walkRoot, func(filePath string, f os.DirEntry, err error) error { if err != nil { logrus.Errorf("Tar: Can't stat file %s to tar: %s", srcPath, err) return nil diff --git a/pkg/archive/changes_other.go b/pkg/archive/changes_other.go index 0e4399a43b..833798bd11 100644 --- a/pkg/archive/changes_other.go +++ b/pkg/archive/changes_other.go @@ -41,7 +41,7 @@ func collectFileInfoForChanges(oldDir, newDir string) (*FileInfo, *FileInfo, err func collectFileInfo(sourceDir string) (*FileInfo, error) { root := newRootFileInfo() - err := filepath.Walk(sourceDir, func(path string, f os.FileInfo, err error) error { + err := filepath.WalkDir(sourceDir, func(path string, _ os.DirEntry, err error) error { if err != nil { return err } diff --git a/pkg/archive/copy_unix_test.go b/pkg/archive/copy_unix_test.go index f8fca00ce6..99e195bebf 100644 --- a/pkg/archive/copy_unix_test.go +++ b/pkg/archive/copy_unix_test.go @@ -101,7 +101,8 @@ func dirContentsEqual(t *testing.T, newDir, oldDir string) (err error) { } func logDirContents(t *testing.T, dirPath string) { - logWalkedPaths := filepath.WalkFunc(func(path string, info os.FileInfo, err error) error { + t.Logf("logging directory contents: %q", dirPath) + err := filepath.WalkDir(dirPath, func(path string, info os.DirEntry, err error) error { if err != nil { t.Errorf("stat error for path %q: %s", path, err) return nil @@ -115,10 +116,6 @@ func logDirContents(t *testing.T, dirPath string) { return nil }) - - t.Logf("logging directory contents: %q", dirPath) - - err := filepath.Walk(dirPath, logWalkedPaths) assert.NilError(t, err) } diff --git a/pkg/archive/diff.go b/pkg/archive/diff.go index 8eeccb608b..4fce40b4c6 100644 --- a/pkg/archive/diff.go +++ b/pkg/archive/diff.go @@ -121,7 +121,7 @@ func UnpackLayer(dest string, layer io.Reader, options *TarOptions) (size int64, if err != nil { return 0, err } - err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + err = filepath.WalkDir(dir, func(path string, info os.DirEntry, err error) error { if err != nil { if os.IsNotExist(err) { err = nil // parent was deleted @@ -132,8 +132,7 @@ func UnpackLayer(dest string, layer io.Reader, options *TarOptions) (size int64, return nil } if _, exists := unpackedPaths[path]; !exists { - err := os.RemoveAll(path) - return err + return os.RemoveAll(path) } return nil }) diff --git a/pkg/archive/utils_test.go b/pkg/archive/utils_test.go index a00ae680c0..7693b862ff 100644 --- a/pkg/archive/utils_test.go +++ b/pkg/archive/utils_test.go @@ -139,7 +139,7 @@ func testBreakout(untarFn string, tmpdir string, headers []*tar.Header) error { // Since victim/hello was generated with time.Now(), it is safe to assume // that any file whose content matches exactly victim/hello, managed somehow // to access victim/hello. - return filepath.Walk(dest, func(path string, info os.FileInfo, err error) error { + return filepath.WalkDir(dest, func(path string, info os.DirEntry, err error) error { if info.IsDir() { if err != nil { // skip directory if error