@@ -40,7 +40,7 @@ func loadMetadata(dir string, opts *Options) error {
40
40
opts .Env .Ignore = true
41
41
}
42
42
43
- contents , err := os .ReadFile (filepath . Join ( filepath . Clean ( dir ), "data" ) )
43
+ contents , err := os .ReadFile (opts . Script . Name )
44
44
if err != nil {
45
45
return err
46
46
}
@@ -60,12 +60,12 @@ type archiveMetadata struct {
60
60
Env map [string ]string `json:"env"`
61
61
}
62
62
63
+ const maxFileSize = 1024 * 1024 * 10 // 10M
64
+
63
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
@@ -103,6 +103,56 @@ func extractArchive(dir string, input io.Reader) error {
103
103
if err = file .Close (); err != nil {
104
104
return err
105
105
}
106
+
107
+ // if it is a link or symlink, we copy the content of the linked file to the target
108
+ // we assume the linked file was already processed and exists in the directory.
109
+ case tar .TypeLink , tar .TypeSymlink :
110
+ if shouldSkip (target ) {
111
+ continue
112
+ }
113
+
114
+ linkedFile := filepath .Join (dir , filepath .Clean (filepath .FromSlash (header .Linkname )))
115
+ if err := followLink (linkedFile , target ); err != nil {
116
+ return err
117
+ }
106
118
}
107
119
}
108
120
}
121
+
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
+ }
128
+
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
136
+
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
+ }
142
+
143
+ file , err := os .OpenFile (target , os .O_CREATE | os .O_WRONLY , info .Mode ()) //nolint:gosec
144
+ if err != nil {
145
+ return err
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
158
+ }
0 commit comments