Add recursive directory search with depth limits
This commit is contained in:
50
internal/engine/directory_walk.go
Normal file
50
internal/engine/directory_walk.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
)
|
||||
|
||||
var errImportDirectoryEntryLimit = errors.New("import directory-entry visit limit reached")
|
||||
|
||||
// visitBoundedImportDirectory shares entry accounting across directory and glob searches.
|
||||
func visitBoundedImportDirectory(access *filesystemAccess, directory string, limit int, accounting *importAccounting, visit func(fs.DirEntry) error) error {
|
||||
if accounting == nil {
|
||||
accounting = &importAccounting{}
|
||||
}
|
||||
var visitError error
|
||||
err := access.readDirectory(directory, func(entry fs.DirEntry) bool {
|
||||
accounting.entries++
|
||||
if accounting.entries > limit {
|
||||
visitError = errImportDirectoryEntryLimit
|
||||
return false
|
||||
}
|
||||
if err := visit(entry); err != nil {
|
||||
visitError = err
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
if visitError != nil {
|
||||
return visitError
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func importDirectoryEntryIsDirectory(entry fs.DirEntry) (bool, error) {
|
||||
mode := entry.Type()
|
||||
if mode&fs.ModeSymlink != 0 {
|
||||
return false, nil
|
||||
}
|
||||
if mode.IsDir() {
|
||||
return true, nil
|
||||
}
|
||||
if mode != 0 {
|
||||
return false, nil
|
||||
}
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return info.IsDir(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user