-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathparser_tar.go
176 lines (144 loc) · 4.2 KB
/
parser_tar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package parser
import (
"archive/tar"
"compress/gzip"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"github.com/liamg/memoryfs"
"github.com/aquasecurity/trivy/pkg/iac/detection"
"github.com/aquasecurity/trivy/pkg/log"
)
var errSkipFS = errors.New("skip parse FS")
func (p *Parser) addTarToFS(archivePath string) (fs.FS, error) {
tarFS := memoryfs.CloneFS(p.workingFS)
file, err := tarFS.Open(archivePath)
if err != nil {
return nil, fmt.Errorf("failed to open tar: %w", err)
}
defer file.Close()
var tr *tar.Reader
if detection.IsZip(archivePath) {
zipped, err := gzip.NewReader(file)
if err != nil {
return nil, fmt.Errorf("failed to create gzip reader: %w", err)
}
defer zipped.Close()
tr = tar.NewReader(zipped)
} else {
tr = tar.NewReader(file)
}
checkExistedChart := true
symlinks := make(map[string]string)
for {
header, err := tr.Next()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return nil, fmt.Errorf("failed to get next entry: %w", err)
}
name := filepath.ToSlash(header.Name)
if checkExistedChart {
// Do not add archive files to FS if the chart already exists
// This can happen when the source chart is located next to an archived chart (with the `helm package` command)
// The first level folder in the archive is equal to the Chart name
if _, err := tarFS.Stat(path.Dir(archivePath) + "/" + path.Dir(name)); err == nil {
return nil, errSkipFS
}
checkExistedChart = false
}
// get the individual path and extract to the current directory
targetPath := path.Join(path.Dir(archivePath), path.Clean(name))
link := filepath.ToSlash(header.Linkname)
switch header.Typeflag {
case tar.TypeDir:
if err := tarFS.MkdirAll(targetPath, os.FileMode(header.Mode)); err != nil && !errors.Is(err, fs.ErrExist) {
return nil, err
}
case tar.TypeReg:
p.logger.Debug("Unpacking tar entry", log.FilePath(targetPath))
if err := copyFile(tarFS, tr, targetPath); err != nil {
return nil, err
}
case tar.TypeSymlink:
if path.IsAbs(link) {
p.logger.Debug("Symlink is absolute, skipping", log.String("link", link))
continue
}
symlinks[targetPath] = path.Join(path.Dir(targetPath), link) // 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 {
if err := copySymlink(tarFS, link, target); err != nil {
return nil, fmt.Errorf("copy symlink error: %w", err)
}
}
if err := tarFS.Remove(archivePath); err != nil {
return nil, fmt.Errorf("remove tar from FS error: %w", err)
}
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
}
if err := copyFileLazy(fsys, src, 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(path.Dir(dst), fs.ModePerm); err != nil && !errors.Is(err, fs.ErrExist) {
return fmt.Errorf("mkdir error: %w", err)
}
b, err := io.ReadAll(src)
if err != nil {
return fmt.Errorf("read error: %w", err)
}
if err := fsys.WriteFile(dst, b, fs.ModePerm); err != nil {
return fmt.Errorf("write file error: %w", err)
}
return nil
}
func copyDir(fsys *memoryfs.FS, src, dst string) error {
walkFn := func(filePath string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() {
return nil
}
dst := path.Join(dst, filePath[len(src):])
if err := copyFileLazy(fsys, filePath, dst); err != nil {
return fmt.Errorf("copy file error: %w", err)
}
return nil
}
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)
}