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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-10-09 17:12:04 +02:00
parent f595434abc
commit ec000ce555
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
5 changed files with 7 additions and 11 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
})

View File

@ -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