From 920572964becaf638ff93dba94343d4d67e204be Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 17 Mar 2017 09:29:58 +0100 Subject: [PATCH] rethink zip extraction --- sketch_loader.go | 2 +- utils/utils.go | 27 ++++++++++----------------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/sketch_loader.go b/sketch_loader.go index f2b92355..ca6858f9 100644 --- a/sketch_loader.go +++ b/sketch_loader.go @@ -85,7 +85,7 @@ func (s *SketchLoader) Run(ctx *types.Context) error { logger := ctx.GetLogger() - if !utils.SliceContains(allSketchFilePaths, sketchLocation) { + if !utils.SliceContains(allSketchFilePaths, sketchLocation) && !ctx.SketchZipped { return i18n.ErrorfWithLogger(logger, constants.MSG_CANT_FIND_SKETCH_IN_PATH, sketchLocation, filepath.Dir(sketchLocation)) } diff --git a/utils/utils.go b/utils/utils.go index 13960d4e..67f575b9 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -499,19 +499,15 @@ func ExtractZip(filePath string, location string) (string, error) { var dirList []string - for _, f := range r.File { - dirList = append(dirList, f.Name) - } - - basedir := findBaseDir(dirList) - for _, f := range r.File { fullname := filepath.Join(location, strings.Replace(f.Name, "", "", -1)) if f.FileInfo().IsDir() { + dirList = append(dirList, fullname) os.MkdirAll(fullname, 0755) } else { _, err := os.Stat(filepath.Dir(fullname)) if err != nil { + dirList = append(dirList, filepath.Dir(fullname)) os.MkdirAll(filepath.Dir(fullname), 0755) } perms := f.FileInfo().Mode().Perm() @@ -537,26 +533,23 @@ func ExtractZip(filePath string, location string) (string, error) { } } } + basedir := filepath.Base(findBaseDir(dirList)) return filepath.Join(location, basedir), nil } func findBaseDir(dirList []string) string { baseDir := "" + minLen := 256 // https://github.com/backdrop-ops/contrib/issues/55#issuecomment-73814500 dontdiff := []string{"pax_global_header"} - for index := range dirList { - if SliceContains(dontdiff, dirList[index]) { + for _, dir := range dirList { + if SliceContains(dontdiff, dir) { continue } - candidateBaseDir := dirList[index] - for i := index; i < len(dirList); i++ { - if !strings.Contains(dirList[i], candidateBaseDir) { - return baseDir - } - } - // avoid setting the candidate if it is the last file - if dirList[len(dirList)-1] != candidateBaseDir { - baseDir = candidateBaseDir + //get the shortest string + if len(dir) < minLen { + baseDir = dir + minLen = len(dir) } } return baseDir