@@ -60,12 +60,12 @@ type archiveMetadata struct {
60
60
Env map [string ]string `json:"env"`
61
61
}
62
62
63
- //nolint:forbidigo,gocognit
63
+ const maxFileSize = 1024 * 1024 * 10 // 10M
64
+
65
+ //nolint:forbidigo
64
66
func extractArchive (dir string , input io.Reader ) error {
65
67
reader := tar .NewReader (input )
66
68
67
- const maxFileSize = 1024 * 1024 * 10 // 10M
68
-
69
69
for {
70
70
header , err := reader .Next ()
71
71
@@ -87,7 +87,7 @@ func extractArchive(dir string, input io.Reader) error {
87
87
}
88
88
89
89
case tar .TypeReg :
90
- if ext := filepath . Ext (target ); ext == ".csv" || ( ext == ".json" && filepath . Base ( target ) != "metadata.json" ) {
90
+ if shouldSkip (target ) {
91
91
continue
92
92
}
93
93
@@ -107,37 +107,52 @@ func extractArchive(dir string, input io.Reader) error {
107
107
// if it is a link or symlink, we copy the content of the linked file to the target
108
108
// we assume the linked file was already processed and exists in the directory.
109
109
case tar .TypeLink , tar .TypeSymlink :
110
- if ext := filepath . Ext (target ); ext == ".csv" || ( ext == ".json" && filepath . Base ( target ) != "metadata.json" ) {
110
+ if shouldSkip (target ) {
111
111
continue
112
112
}
113
113
114
114
linkedFile := filepath .Join (dir , filepath .Clean (filepath .FromSlash (header .Linkname )))
115
- source , err := os .Open (filepath .Clean (linkedFile ))
116
- if err != nil {
115
+ if err := followLink (linkedFile , target ); err != nil {
117
116
return err
118
117
}
119
- defer source .Close () //nolint:errcheck
118
+ }
119
+ }
120
+ }
120
121
121
- // we need to get the lined file info to create the target file with the same permissions
122
- info , err := source .Stat ()
123
- if err != nil {
124
- return err
125
- }
122
+ // indicates if the file should be skipped during extraction
123
+ // we skip csv files and .json except metadata.json
124
+ func shouldSkip (target string ) bool {
125
+ ext := filepath .Ext (target )
126
+ return ext == ".csv" || (ext == ".json" && filepath .Base (target ) != "metadata.json" )
127
+ }
126
128
127
- file , err := os .OpenFile (target , os .O_CREATE | os .O_WRONLY , info .Mode ())
128
- if err != nil {
129
- return err
130
- }
129
+ //nolint:forbidigo
130
+ func followLink (linkedFile string , target string ) error {
131
+ source , err := os .Open (filepath .Clean (linkedFile ))
132
+ if err != nil {
133
+ return err
134
+ }
135
+ defer source .Close () //nolint:errcheck
131
136
132
- _ , err = io .Copy (file , source )
133
- if err != nil {
134
- return err
135
- }
137
+ // we need to get the lined file info to create the target file with the same permissions
138
+ info , err := source .Stat ()
139
+ if err != nil {
140
+ return err
141
+ }
136
142
137
- err = file .Close ()
138
- if err != nil {
139
- return err
140
- }
141
- }
143
+ file , err := os .OpenFile (target , os .O_CREATE | os .O_WRONLY , info .Mode ()) //nolint:gosec
144
+ if err != nil {
145
+ return err
142
146
}
147
+
148
+ _ , err = io .Copy (file , source )
149
+ if err != nil {
150
+ return err
151
+ }
152
+
153
+ err = file .Close ()
154
+ if err != nil {
155
+ return err
156
+ }
157
+ return nil
143
158
}
0 commit comments