Skip to content

Commit a5c8dda

Browse files
committed
Return extracted file list from Uncompress function
This will help to get absolute path of the files which we want to extract using a filefilter so it would be easy to copy that file to proper destination.
1 parent 29e8898 commit a5c8dda

File tree

5 files changed

+31
-27
lines changed

5 files changed

+31
-27
lines changed

pkg/crc/machine/bundle/metadata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (bundle *CrcBundleInfo) resolvePath(filename string) string {
9595
}
9696

9797
func Extract(sourcepath string) (*CrcBundleInfo, error) {
98-
err := extract.Uncompress(sourcepath, constants.MachineCacheDir)
98+
_, err := extract.Uncompress(sourcepath, constants.MachineCacheDir)
9999
if err != nil {
100100
return nil, err
101101
}

pkg/crc/oc/oc_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (oc *OcCached) cacheOc() error {
6767
}
6868

6969
// Extract the tarball and put it the cache directory.
70-
err = extract.UncompressWithFilter(assetTmpFile, tmpDir, matchOcBinaryName)
70+
_, err = extract.UncompressWithFilter(assetTmpFile, tmpDir, matchOcBinaryName)
7171
if err != nil {
7272
return errors.Wrapf(err, "Cannot uncompress '%s'", assetTmpFile)
7373
}

pkg/crc/podman/podman_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (podman *PodmanCached) cachePodman() error {
6767
}
6868

6969
// Extract the tarball and put it the cache directory.
70-
err = extract.Uncompress(assetTmpFile, tmpDir)
70+
_, err = extract.Uncompress(assetTmpFile, tmpDir)
7171
if err != nil {
7272
return errors.Wrapf(err, "Cannot uncompress '%s'", assetTmpFile)
7373
}

pkg/crc/preflight/preflight_check_tray_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func downloadOrExtractTrayApp() error {
210210
if err != nil && !goos.IsExist(err) {
211211
return errors.Wrap(err, "Cannot create the target directory.")
212212
}
213-
err = extract.Uncompress(archivePath, outputPath)
213+
_, err = extract.Uncompress(archivePath, outputPath)
214214
if err != nil {
215215
return errors.Wrapf(err, "Cannot uncompress '%s'", archivePath)
216216
}

pkg/extract/extract.go

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ import (
1414
"github.com/xi2/xz"
1515
)
1616

17-
func UncompressWithFilter(tarball, targetDir string, fileFilter func(string) bool) error {
17+
func UncompressWithFilter(tarball, targetDir string, fileFilter func(string) bool) ([]string, error) {
1818
return uncompress(tarball, targetDir, fileFilter)
1919
}
2020

21-
func Uncompress(tarball, targetDir string) error {
21+
func Uncompress(tarball, targetDir string) ([]string, error) {
2222
return uncompress(tarball, targetDir, nil)
2323
}
2424

25-
func uncompress(tarball, targetDir string, fileFilter func(string) bool) error {
25+
func uncompress(tarball, targetDir string, fileFilter func(string) bool) ([]string, error) {
2626
logging.Debugf("Uncompressing %s to %s", tarball, targetDir)
2727

2828
if strings.HasSuffix(tarball, ".zip") {
@@ -32,19 +32,19 @@ func uncompress(tarball, targetDir string, fileFilter func(string) bool) error {
3232
var filereader io.Reader
3333
file, err := os.Open(filepath.Clean(tarball))
3434
if err != nil {
35-
return err
35+
return nil, err
3636
}
3737
defer file.Close()
3838

3939
if strings.HasSuffix(tarball, ".tar.xz") || strings.HasSuffix(tarball, ".crcbundle") {
4040
filereader, err = xz.NewReader(file, 0)
4141
if err != nil {
42-
return err
42+
return nil, err
4343
}
4444
} else if strings.HasSuffix(tarball, ".tar.gz") {
4545
reader, err := gzip.NewReader(file)
4646
if err != nil {
47-
return err
47+
return nil, err
4848
}
4949
defer reader.Close()
5050
filereader = io.Reader(reader)
@@ -57,19 +57,20 @@ func uncompress(tarball, targetDir string, fileFilter func(string) bool) error {
5757
return untar(filereader, targetDir, fileFilter)
5858
}
5959

60-
func untar(reader io.Reader, targetDir string, fileFilter func(string) bool) error {
60+
func untar(reader io.Reader, targetDir string, fileFilter func(string) bool) ([]string, error) {
61+
var extractedFiles []string
6162
tarReader := tar.NewReader(reader)
6263

6364
for {
6465
header, err := tarReader.Next()
6566
switch {
6667
// if no more files are found return
6768
case err == io.EOF:
68-
return nil
69+
return extractedFiles, nil
6970

7071
// return any other error
7172
case err != nil:
72-
return err
73+
return extractedFiles, err
7374

7475
// if the header is nil, just skip it (not sure how this happens)
7576
case header == nil:
@@ -90,50 +91,52 @@ func untar(reader io.Reader, targetDir string, fileFilter func(string) bool) err
9091
case tar.TypeDir:
9192
if _, err := os.Stat(path); err != nil {
9293
if err := os.MkdirAll(path, header.FileInfo().Mode()); err != nil {
93-
return err
94+
return nil, err
9495
}
9596
}
9697

9798
// if it's a file create it
9899
case tar.TypeReg, tar.TypeGNUSparse:
99100
// tar.Next() will externally only iterate files, so we might have to create intermediate directories here
100101
if err = os.MkdirAll(filepath.Dir(path), 0750); err != nil {
101-
return err
102+
return nil, err
102103
}
103104
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, header.FileInfo().Mode())
104105
if err != nil {
105-
return err
106+
return nil, err
106107
}
107108
defer file.Close()
108109

109110
// copy over contents
110111
if _, err := io.Copy(file, tarReader); err != nil {
111-
return err
112+
return nil, err
112113
}
114+
extractedFiles = append(extractedFiles, path)
113115
}
114116
}
115117
}
116118

117-
func Unzip(archive, target string) error {
119+
func Unzip(archive, target string) ([]string, error) {
118120
return unzip(archive, target, nil)
119121
}
120122

121-
func unzip(archive, target string, fileFilter func(string) bool) error {
123+
func unzip(archive, target string, fileFilter func(string) bool) ([]string, error) {
124+
var extractedFiles []string
122125
reader, err := zip.OpenReader(archive)
123126
if err != nil {
124-
return err
127+
return nil, err
125128
}
126129

127130
if err := os.MkdirAll(target, 0750); err != nil {
128-
return err
131+
return nil, err
129132
}
130133

131134
for _, file := range reader.File {
132135
path := filepath.Join(target, file.Name) // #nosec G305
133136

134137
// Check for ZipSlip. More Info: https://snyk.io/research/zip-slip-vulnerability
135138
if !strings.HasPrefix(path, filepath.Clean(target)+string(os.PathSeparator)) {
136-
return fmt.Errorf("%s: illegal file path", path)
139+
return nil, fmt.Errorf("%s: illegal file path", path)
137140
}
138141

139142
if fileFilter != nil && !fileFilter(path) {
@@ -143,27 +146,28 @@ func unzip(archive, target string, fileFilter func(string) bool) error {
143146
if file.FileInfo().IsDir() {
144147
err = os.MkdirAll(path, file.Mode())
145148
if err != nil {
146-
return err
149+
return nil, err
147150
}
148151
continue
149152
}
150153

151154
fileReader, err := file.Open()
152155
if err != nil {
153-
return err
156+
return nil, err
154157
}
155158
defer fileReader.Close()
156159

157160
targetFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
158161
if err != nil {
159-
return err
162+
return nil, err
160163
}
161164
defer targetFile.Close()
162165

163166
if _, err := io.Copy(targetFile, fileReader); err != nil {
164-
return err
167+
return nil, err
165168
}
169+
extractedFiles = append(extractedFiles, path)
166170
}
167171

168-
return nil
172+
return extractedFiles, nil
169173
}

0 commit comments

Comments
 (0)