@@ -14,15 +14,15 @@ import (
14
14
"github.com/xi2/xz"
15
15
)
16
16
17
- func UncompressWithFilter (tarball , targetDir string , fileFilter func (string ) bool ) error {
17
+ func UncompressWithFilter (tarball , targetDir string , fileFilter func (string ) bool ) ([] string , error ) {
18
18
return uncompress (tarball , targetDir , fileFilter )
19
19
}
20
20
21
- func Uncompress (tarball , targetDir string ) error {
21
+ func Uncompress (tarball , targetDir string ) ([] string , error ) {
22
22
return uncompress (tarball , targetDir , nil )
23
23
}
24
24
25
- func uncompress (tarball , targetDir string , fileFilter func (string ) bool ) error {
25
+ func uncompress (tarball , targetDir string , fileFilter func (string ) bool ) ([] string , error ) {
26
26
logging .Debugf ("Uncompressing %s to %s" , tarball , targetDir )
27
27
28
28
if strings .HasSuffix (tarball , ".zip" ) {
@@ -32,19 +32,19 @@ func uncompress(tarball, targetDir string, fileFilter func(string) bool) error {
32
32
var filereader io.Reader
33
33
file , err := os .Open (filepath .Clean (tarball ))
34
34
if err != nil {
35
- return err
35
+ return nil , err
36
36
}
37
37
defer file .Close ()
38
38
39
39
if strings .HasSuffix (tarball , ".tar.xz" ) || strings .HasSuffix (tarball , ".crcbundle" ) {
40
40
filereader , err = xz .NewReader (file , 0 )
41
41
if err != nil {
42
- return err
42
+ return nil , err
43
43
}
44
44
} else if strings .HasSuffix (tarball , ".tar.gz" ) {
45
45
reader , err := gzip .NewReader (file )
46
46
if err != nil {
47
- return err
47
+ return nil , err
48
48
}
49
49
defer reader .Close ()
50
50
filereader = io .Reader (reader )
@@ -57,19 +57,20 @@ func uncompress(tarball, targetDir string, fileFilter func(string) bool) error {
57
57
return untar (filereader , targetDir , fileFilter )
58
58
}
59
59
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
61
62
tarReader := tar .NewReader (reader )
62
63
63
64
for {
64
65
header , err := tarReader .Next ()
65
66
switch {
66
67
// if no more files are found return
67
68
case err == io .EOF :
68
- return nil
69
+ return extractedFiles , nil
69
70
70
71
// return any other error
71
72
case err != nil :
72
- return err
73
+ return extractedFiles , err
73
74
74
75
// if the header is nil, just skip it (not sure how this happens)
75
76
case header == nil :
@@ -90,50 +91,52 @@ func untar(reader io.Reader, targetDir string, fileFilter func(string) bool) err
90
91
case tar .TypeDir :
91
92
if _ , err := os .Stat (path ); err != nil {
92
93
if err := os .MkdirAll (path , header .FileInfo ().Mode ()); err != nil {
93
- return err
94
+ return nil , err
94
95
}
95
96
}
96
97
97
98
// if it's a file create it
98
99
case tar .TypeReg , tar .TypeGNUSparse :
99
100
// tar.Next() will externally only iterate files, so we might have to create intermediate directories here
100
101
if err = os .MkdirAll (filepath .Dir (path ), 0750 ); err != nil {
101
- return err
102
+ return nil , err
102
103
}
103
104
file , err := os .OpenFile (path , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , header .FileInfo ().Mode ())
104
105
if err != nil {
105
- return err
106
+ return nil , err
106
107
}
107
108
defer file .Close ()
108
109
109
110
// copy over contents
110
111
if _ , err := io .Copy (file , tarReader ); err != nil {
111
- return err
112
+ return nil , err
112
113
}
114
+ extractedFiles = append (extractedFiles , path )
113
115
}
114
116
}
115
117
}
116
118
117
- func Unzip (archive , target string ) error {
119
+ func Unzip (archive , target string ) ([] string , error ) {
118
120
return unzip (archive , target , nil )
119
121
}
120
122
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
122
125
reader , err := zip .OpenReader (archive )
123
126
if err != nil {
124
- return err
127
+ return nil , err
125
128
}
126
129
127
130
if err := os .MkdirAll (target , 0750 ); err != nil {
128
- return err
131
+ return nil , err
129
132
}
130
133
131
134
for _ , file := range reader .File {
132
135
path := filepath .Join (target , file .Name ) // #nosec G305
133
136
134
137
// Check for ZipSlip. More Info: https://snyk.io/research/zip-slip-vulnerability
135
138
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 )
137
140
}
138
141
139
142
if fileFilter != nil && ! fileFilter (path ) {
@@ -143,27 +146,28 @@ func unzip(archive, target string, fileFilter func(string) bool) error {
143
146
if file .FileInfo ().IsDir () {
144
147
err = os .MkdirAll (path , file .Mode ())
145
148
if err != nil {
146
- return err
149
+ return nil , err
147
150
}
148
151
continue
149
152
}
150
153
151
154
fileReader , err := file .Open ()
152
155
if err != nil {
153
- return err
156
+ return nil , err
154
157
}
155
158
defer fileReader .Close ()
156
159
157
160
targetFile , err := os .OpenFile (path , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , file .Mode ())
158
161
if err != nil {
159
- return err
162
+ return nil , err
160
163
}
161
164
defer targetFile .Close ()
162
165
163
166
if _ , err := io .Copy (targetFile , fileReader ); err != nil {
164
- return err
167
+ return nil , err
165
168
}
169
+ extractedFiles = append (extractedFiles , path )
166
170
}
167
171
168
- return nil
172
+ return extractedFiles , nil
169
173
}
0 commit comments