Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 05576c9

Browse files
committedOct 23, 2019
additions:
- act on error in walk function (OS can detect symbolic links loops (lnk -> lnk)) - add a deepness detector for non trivial loops (dir/lnk -> ../..)
1 parent 98bc2e3 commit 05576c9

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed
 

‎arduino/builder/sketch.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"path/filepath"
2323
"regexp"
2424
"strings"
25+
"fmt"
2526

2627
"github.com/arduino/arduino-cli/arduino/globals"
2728
"github.com/arduino/arduino-cli/arduino/sketch"
@@ -59,7 +60,7 @@ func SketchSaveItemCpp(item *sketch.Item, destPath string) error {
5960
}
6061

6162
// SimpleLocalWalk locally replaces filepath.Walk and/but goes through symlinks
62-
func SimpleLocalWalk(root string, walkFn func(path string, info os.FileInfo, err error) error) error {
63+
func SimpleLocalWalkRecursive(root string, maxDepth int, walkFn func(path string, info os.FileInfo, err error) error) error {
6364

6465
info, err := os.Stat(root)
6566

@@ -73,10 +74,14 @@ func SimpleLocalWalk(root string, walkFn func(path string, info os.FileInfo, err
7374
}
7475

7576
if info.IsDir() {
77+
if maxDepth <= 0 {
78+
return walkFn(root, info, errors.New("Filesystem bottom is too deep (directory recursion or filesystem really deep): " + root))
79+
}
80+
maxDepth--
7681
files, err := ioutil.ReadDir(root)
7782
if err == nil {
7883
for _, file := range files {
79-
err = SimpleLocalWalk(root+string(os.PathSeparator)+file.Name(), walkFn)
84+
err = SimpleLocalWalkRecursive(root+string(os.PathSeparator)+file.Name(), maxDepth, walkFn)
8085
if err == filepath.SkipDir {
8186
return nil
8287
}
@@ -87,6 +92,11 @@ func SimpleLocalWalk(root string, walkFn func(path string, info os.FileInfo, err
8792
return nil
8893
}
8994

95+
func SimpleLocalWalk(root string, walkFn func(path string, info os.FileInfo, err error) error) error {
96+
// see discussion in https://github.com/arduino/arduino-cli/pull/421
97+
return SimpleLocalWalkRecursive(root, 40, walkFn)
98+
}
99+
90100
// SketchLoad collects all the files composing a sketch.
91101
// The parameter `sketchPath` holds a path pointing to a single sketch file or a sketch folder,
92102
// the path must be absolute.
@@ -124,6 +134,12 @@ func SketchLoad(sketchPath, buildPath string) (*sketch.Sketch, error) {
124134
// collect all the sketch files
125135
var files []string
126136
err = SimpleLocalWalk(sketchFolder, func(path string, info os.FileInfo, err error) error {
137+
138+
if err != nil {
139+
fmt.Printf("\nerror: %+v\n\n", err)
140+
return filepath.SkipDir;
141+
}
142+
127143
// ignore hidden files and skip hidden directories
128144
if strings.HasPrefix(info.Name(), ".") {
129145
if info.IsDir() {

0 commit comments

Comments
 (0)
Please sign in to comment.