Skip to content

Commit

Permalink
fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nikpivkin committed May 3, 2024
1 parent c4cdbca commit 9841b99
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
52 changes: 29 additions & 23 deletions pkg/iac/scanners/helm/parser/parser_tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,16 @@ func (p *Parser) addTarToFS(archivePath string) (fs.FS, error) {
continue
}

symlinks[targetPath] = path.Join(filepath.Dir(targetPath), header.Linkname)
symlinks[targetPath] = path.Join(filepath.Dir(targetPath), header.Linkname) // nolint:gosec // virtual file system is used
default:
return nil, fmt.Errorf("header type %q is not supported", header.Typeflag)
}
}

for target, link := range symlinks {
fi, err := tarFS.Stat(link)
if err != nil {
p.debug.Log("stat error: %s", err)
continue
}
if fi.IsDir() {
if err := copyDir(tarFS, link, target); err != nil {
return nil, fmt.Errorf("copy dir error: %w", err)
}
continue
}

f, err := tarFS.Open(link)
if err != nil {
return nil, fmt.Errorf("open symlink error: %w", err)
if err := copySymlink(tarFS, link, target); err != nil {
return nil, fmt.Errorf("copy symlink error: %w", err)
}

if err := copyFile(tarFS, f, target); err != nil {
f.Close()
return nil, fmt.Errorf("copy file error: %w", err)
}
f.Close()
}

if err := tarFS.Remove(archivePath); err != nil {
Expand All @@ -119,6 +100,31 @@ func (p *Parser) addTarToFS(archivePath string) (fs.FS, error) {
return tarFS, nil
}

func copySymlink(fsys *memoryfs.FS, src, dst string) error {
fi, err := fsys.Stat(src)
if err != nil {
return nil
}
if fi.IsDir() {
if err := copyDir(fsys, src, dst); err != nil {
return fmt.Errorf("copy dir error: %w", err)
}
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 {
return fmt.Errorf("copy file error: %w", err)
}

return nil
}

func copyFile(fsys *memoryfs.FS, src io.Reader, dst string) error {
if err := fsys.MkdirAll(filepath.Dir(dst), fs.ModePerm); err != nil && !errors.Is(err, fs.ErrExist) {
return fmt.Errorf("mkdir error: %w", err)
Expand All @@ -136,7 +142,7 @@ func copyFile(fsys *memoryfs.FS, src io.Reader, dst string) error {
return nil
}

func copyDir(fsys *memoryfs.FS, src string, dst string) error {
func copyDir(fsys *memoryfs.FS, src, dst string) error {
walkFn := func(filePath string, entry fs.DirEntry, err error) error {
if err != nil {
return err
Expand Down
12 changes: 6 additions & 6 deletions pkg/iac/scanners/helm/test/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ func Test_tar_is_chart(t *testing.T) {

t.Logf("Running test: %s", test.testName)
testPath := filepath.Join("testdata", test.archiveFile)
file, err := os.Open(testPath)
defer func() { _ = file.Close() }()
require.NoError(t, err)

assert.Equal(t, test.isHelmChart, detection.IsHelmChartArchive(test.archiveFile, file))
func() {
file, err := os.Open(testPath)
require.NoError(t, err)
defer file.Close()

_ = file.Close()
assert.Equal(t, test.isHelmChart, detection.IsHelmChartArchive(test.archiveFile, file))
}()
}
}

Expand Down

0 comments on commit 9841b99

Please sign in to comment.