From cedb039693d41f31766d5944796677ad9ac373e1 Mon Sep 17 00:00:00 2001 From: nikpivkin Date: Tue, 7 May 2024 11:45:53 +0600 Subject: [PATCH] refactor: lazy writing symlinks --- pkg/iac/scanners/helm/parser/parser_tar.go | 28 ++++++++++++---------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/pkg/iac/scanners/helm/parser/parser_tar.go b/pkg/iac/scanners/helm/parser/parser_tar.go index 4c859e092174..f8d692eaa977 100644 --- a/pkg/iac/scanners/helm/parser/parser_tar.go +++ b/pkg/iac/scanners/helm/parser/parser_tar.go @@ -116,13 +116,7 @@ func copySymlink(fsys *memoryfs.FS, src, dst string) error { return nil } - f, err := fsys.Open(src) - if err != nil { - return fmt.Errorf("open symlink error: %w", err) - } - defer f.Close() - - if err := copyFile(fsys, f, dst); err != nil { + if err := copyFileLazy(fsys, src, dst); err != nil { return fmt.Errorf("copy file error: %w", err) } @@ -158,12 +152,7 @@ func copyDir(fsys *memoryfs.FS, src, dst string) error { dst := path.Join(dst, filePath[len(src):]) - f, err := fsys.Open(filePath) - if err != nil { - return err - } - - if err := copyFile(fsys, f, dst); err != nil { + if err := copyFileLazy(fsys, filePath, dst); err != nil { return fmt.Errorf("copy file error: %w", err) } return nil @@ -171,3 +160,16 @@ func copyDir(fsys *memoryfs.FS, src, dst string) error { return fs.WalkDir(fsys, src, walkFn) } + +func copyFileLazy(fsys *memoryfs.FS, src, dst string) error { + if err := fsys.MkdirAll(path.Dir(dst), fs.ModePerm); err != nil && !errors.Is(err, fs.ErrExist) { + return fmt.Errorf("mkdir error: %w", err) + } + return fsys.WriteLazyFile(dst, func() (io.Reader, error) { + f, err := fsys.Open(src) + if err != nil { + return nil, err + } + return f, nil + }, fs.ModePerm) +}