Skip to content

Commit 9205729

Browse files
facchinmcmaglie
authored andcommitted
rethink zip extraction
1 parent e3a3096 commit 9205729

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

Diff for: 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

Diff for: utils/utils.go

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

500500
var dirList []string
501501

502-
for _, f := range r.File {
503-
dirList = append(dirList, f.Name)
504-
}
505-
506-
basedir := findBaseDir(dirList)
507-
508502
for _, f := range r.File {
509503
fullname := filepath.Join(location, strings.Replace(f.Name, "", "", -1))
510504
if f.FileInfo().IsDir() {
505+
dirList = append(dirList, fullname)
511506
os.MkdirAll(fullname, 0755)
512507
} else {
513508
_, err := os.Stat(filepath.Dir(fullname))
514509
if err != nil {
510+
dirList = append(dirList, filepath.Dir(fullname))
515511
os.MkdirAll(filepath.Dir(fullname), 0755)
516512
}
517513
perms := f.FileInfo().Mode().Perm()
@@ -537,26 +533,23 @@ func ExtractZip(filePath string, location string) (string, error) {
537533
}
538534
}
539535
}
536+
basedir := filepath.Base(findBaseDir(dirList))
540537
return filepath.Join(location, basedir), nil
541538
}
542539

543540
func findBaseDir(dirList []string) string {
544541
baseDir := ""
542+
minLen := 256
545543
// https://github.com/backdrop-ops/contrib/issues/55#issuecomment-73814500
546544
dontdiff := []string{"pax_global_header"}
547-
for index := range dirList {
548-
if SliceContains(dontdiff, dirList[index]) {
545+
for _, dir := range dirList {
546+
if SliceContains(dontdiff, dir) {
549547
continue
550548
}
551-
candidateBaseDir := dirList[index]
552-
for i := index; i < len(dirList); i++ {
553-
if !strings.Contains(dirList[i], candidateBaseDir) {
554-
return baseDir
555-
}
556-
}
557-
// avoid setting the candidate if it is the last file
558-
if dirList[len(dirList)-1] != candidateBaseDir {
559-
baseDir = candidateBaseDir
549+
//get the shortest string
550+
if len(dir) < minLen {
551+
baseDir = dir
552+
minLen = len(dir)
560553
}
561554
}
562555
return baseDir

0 commit comments

Comments
 (0)