Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rethink zip extraction
  • Loading branch information
facchinm authored and cmaglie committed Jan 11, 2018
commit 920572964becaf638ff93dba94343d4d67e204be
2 changes: 1 addition & 1 deletion sketch_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
27 changes: 10 additions & 17 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down