Skip to content

Commit

Permalink
rethink zip extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
facchinm committed Mar 17, 2017
1 parent 74e4bd8 commit a762bb6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/arduino.cc/builder/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 src/arduino.cc/builder/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,19 +477,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 @@ -515,26 +511,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

0 comments on commit a762bb6

Please sign in to comment.