Skip to content

Commit a762bb6

Browse files
committed
rethink zip extraction
1 parent 74e4bd8 commit a762bb6

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/arduino.cc/builder/sketch_loader.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (s *SketchLoader) Run(ctx *types.Context) error {
8585

8686
logger := ctx.GetLogger()
8787

88-
if !utils.SliceContains(allSketchFilePaths, sketchLocation) {
88+
if !utils.SliceContains(allSketchFilePaths, sketchLocation) && !ctx.SketchZipped {
8989
return i18n.ErrorfWithLogger(logger, constants.MSG_CANT_FIND_SKETCH_IN_PATH, sketchLocation, filepath.Dir(sketchLocation))
9090
}
9191

src/arduino.cc/builder/utils/utils.go

+10-17
Original file line numberDiff line numberDiff line change
@@ -477,19 +477,15 @@ func ExtractZip(filePath string, location string) (string, error) {
477477

478478
var dirList []string
479479

480-
for _, f := range r.File {
481-
dirList = append(dirList, f.Name)
482-
}
483-
484-
basedir := findBaseDir(dirList)
485-
486480
for _, f := range r.File {
487481
fullname := filepath.Join(location, strings.Replace(f.Name, "", "", -1))
488482
if f.FileInfo().IsDir() {
483+
dirList = append(dirList, fullname)
489484
os.MkdirAll(fullname, 0755)
490485
} else {
491486
_, err := os.Stat(filepath.Dir(fullname))
492487
if err != nil {
488+
dirList = append(dirList, filepath.Dir(fullname))
493489
os.MkdirAll(filepath.Dir(fullname), 0755)
494490
}
495491
perms := f.FileInfo().Mode().Perm()
@@ -515,26 +511,23 @@ func ExtractZip(filePath string, location string) (string, error) {
515511
}
516512
}
517513
}
514+
basedir := filepath.Base(findBaseDir(dirList))
518515
return filepath.Join(location, basedir), nil
519516
}
520517

521518
func findBaseDir(dirList []string) string {
522519
baseDir := ""
520+
minLen := 256
523521
// https://github.com/backdrop-ops/contrib/issues/55#issuecomment-73814500
524522
dontdiff := []string{"pax_global_header"}
525-
for index := range dirList {
526-
if SliceContains(dontdiff, dirList[index]) {
523+
for _, dir := range dirList {
524+
if SliceContains(dontdiff, dir) {
527525
continue
528526
}
529-
candidateBaseDir := dirList[index]
530-
for i := index; i < len(dirList); i++ {
531-
if !strings.Contains(dirList[i], candidateBaseDir) {
532-
return baseDir
533-
}
534-
}
535-
// avoid setting the candidate if it is the last file
536-
if dirList[len(dirList)-1] != candidateBaseDir {
537-
baseDir = candidateBaseDir
527+
//get the shortest string
528+
if len(dir) < minLen {
529+
baseDir = dir
530+
minLen = len(dir)
538531
}
539532
}
540533
return baseDir

0 commit comments

Comments
 (0)