@@ -45,10 +45,18 @@ const (
4545 conflictFormat = "%s (__cozy__: %s)"
4646)
4747
48+ // maxWalkRecursive is the maximum amount of recursion allowed for the
49+ // recursive walk process.
50+ const maxWalkRecursive = 512
51+
4852// ErrSkipDir is used in WalkFn as an error to skip the current
4953// directory. It is not returned by any function of the package.
5054var ErrSkipDir = errors .New ("skip directories" )
5155
56+ // ErrWalkOverflow is used in the walk process when the maximum amount of
57+ // recursivity allowed is reached when browsing the index tree.
58+ var ErrWalkOverflow = errors .New ("vfs: walk overflow" )
59+
5260// Fs is an interface providing a set of high-level methods to interact with
5361// the file-system binaries and metadata.
5462type Fs interface {
@@ -517,10 +525,13 @@ func Walk(fs VFS, root string, walkFn WalkFn) error {
517525 if err != nil {
518526 return walkFn (root , dir , file , err )
519527 }
520- return walk (fs , root , dir , file , walkFn )
528+ return walk (fs , root , dir , file , walkFn , 0 )
521529}
522530
523- func walk (fs VFS , name string , dir * DirDoc , file * FileDoc , walkFn WalkFn ) error {
531+ func walk (fs VFS , name string , dir * DirDoc , file * FileDoc , walkFn WalkFn , count int ) error {
532+ if count >= maxWalkRecursive {
533+ return ErrWalkOverflow
534+ }
524535 err := walkFn (name , dir , file , nil )
525536 if err != nil {
526537 if dir != nil && err == ErrSkipDir {
@@ -546,7 +557,7 @@ func walk(fs VFS, name string, dir *DirDoc, file *FileDoc, walkFn WalkFn) error
546557 } else {
547558 fullpath = path .Join (name , d .DocName )
548559 }
549- if err = walk (fs , fullpath , d , f , walkFn ); err != nil {
560+ if err = walk (fs , fullpath , d , f , walkFn , count + 1 ); err != nil {
550561 return err
551562 }
552563 }
0 commit comments