瀏覽代碼

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>
Sebastiaan van Stijn 2 年之前
父節點
當前提交
ec000ce555
共有 5 個文件被更改,包括 7 次插入11 次删除
  1. 1 1
      pkg/archive/archive.go
  2. 1 1
      pkg/archive/changes_other.go
  3. 2 5
      pkg/archive/copy_unix_test.go
  4. 2 3
      pkg/archive/diff.go
  5. 1 1
      pkg/archive/utils_test.go

+ 1 - 1
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

+ 1 - 1
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
 		}

+ 2 - 5
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)
 }
 

+ 2 - 3
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
 				})

+ 1 - 1
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