From 89761938c7a2758b527e349775b1aafa51a29fff Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Fri, 4 Apr 2025 14:54:22 +0200 Subject: [PATCH] pkg/hubtest: use os.CopyFS() (#3539) --- pkg/hubtest/utils.go | 43 ++++--------------------------------------- 1 file changed, 4 insertions(+), 39 deletions(-) diff --git a/pkg/hubtest/utils.go b/pkg/hubtest/utils.go index b42a73461..6bfba1380 100644 --- a/pkg/hubtest/utils.go +++ b/pkg/hubtest/utils.go @@ -71,47 +71,12 @@ func checkPathNotContained(path string, subpath string) error { return nil } +// CopyDir copies the content of a directory to another directory. +// It delegates the operation to os.CopyFS with an additional check to prevent infinite loops. func CopyDir(src string, dest string) error { - err := checkPathNotContained(src, dest) - if err != nil { + if err := checkPathNotContained(src, dest); err != nil { return err } - f, err := os.Open(src) - if err != nil { - return err - } - - file, err := f.Stat() - if err != nil { - return err - } - - if !file.IsDir() { - return errors.New("Source " + file.Name() + " is not a directory!") - } - - err = os.MkdirAll(dest, 0o755) - if err != nil { - return err - } - - files, err := os.ReadDir(src) - if err != nil { - return err - } - - for _, f := range files { - if f.IsDir() { - if err = CopyDir(filepath.Join(src, f.Name()), filepath.Join(dest, f.Name())); err != nil { - return err - } - } else { - if err = Copy(filepath.Join(src, f.Name()), filepath.Join(dest, f.Name())); err != nil { - return err - } - } - } - - return nil + return os.CopyFS(dest, os.DirFS(src)) }