@@ -176,17 +176,20 @@ func unpackRecursive(ctx context.Context, opts *UnpackOptions, visitedRefs []str
176176
177177 case mediatype .DatasetBaseType :
178178 // Since some datasets may be remote, we need to search the Kitfile for the next non-remote dataset
179- var entry artifact.DataSet
179+ var entry * artifact.DataSet
180180 for idx := datasetIdx ; idx < len (config .DataSets ); idx ++ {
181181 dataset := config .DataSets [idx ]
182182 if dataset .RemotePath != "" {
183183 continue
184184 }
185- entry = dataset
185+ entry = & dataset
186186 datasetIdx = idx + 1
187187 break
188188 }
189- if ! shouldUnpackLayer (entry , opts .FilterConfs ) {
189+ if entry == nil {
190+ continue
191+ }
192+ if ! shouldUnpackLayer (* entry , opts .FilterConfs ) {
190193 continue
191194 }
192195 layerInfo , layerPath = entry .LayerInfo , entry .Path
@@ -249,15 +252,30 @@ func unpackRecursive(ctx context.Context, opts *UnpackOptions, visitedRefs []str
249252 return err
250253 }
251254 for path , s3Ref := range remoteFiles {
255+ _ , relPath , err := filesystem .VerifySubpath (opts .UnpackDir , path )
256+ if err != nil {
257+ return fmt .Errorf ("error verifying path %s for remote reference: %w" , path , err )
258+ }
259+
252260 output .Debugf ("Downloading remote dataset: Bucket: %s, Key: %s" , s3Ref .Bucket , s3Ref .Key )
253- if _ , err := os .Stat (path ); ! errors .Is (err , os .ErrNotExist ) && ! opts .Overwrite {
254- return fmt .Errorf ("failed to unpack remote dataset: path '%s' already exists" , path )
261+ if fi , exists := filesystem .PathExists (relPath ); exists {
262+ if opts .IgnoreExisting {
263+ output .Debugf ("File %s already exists; skipping" , path )
264+ continue
265+ }
266+ if ! opts .Overwrite {
267+ return fmt .Errorf ("failed to unpack remote dataset: path '%s' already exists" , path )
268+ }
269+ if ! fi .Mode ().IsRegular () {
270+ return fmt .Errorf ("failed to unpack remote dataset: path '%s' already exists and is not a regular file" , path )
271+ }
255272 }
256- pathDir := filepath .Dir (path )
273+
274+ pathDir := filepath .Dir (relPath )
257275 if err := os .MkdirAll (pathDir , 0755 ); err != nil {
258276 return fmt .Errorf ("failed to create directory %s: %w" , pathDir , err )
259277 }
260- if err := s3api .DownloadObject (ctx , client , & s3Ref , path ); err != nil {
278+ if err := s3api .DownloadObject (ctx , client , & s3Ref , relPath ); err != nil {
261279 return fmt .Errorf ("failed to download remote dataset for path %s: %w" , path , err )
262280 }
263281 output .Infof ("Downloaded remote S3 dataset for path %s" , path )
0 commit comments