switch to a breadth first traversal

This commit is contained in:
Kovid Goyal
2025-06-24 22:01:49 +05:30
parent 7c6fa2db57
commit ccfd1d406e

View File

@@ -66,7 +66,7 @@ type FileSystemScanner struct {
root_dir string root_dir string
mutex sync.Mutex mutex sync.Mutex
results []ResultItem results []ResultItem
dir_reader func(path string, level int) ([]fs.DirEntry, error) dir_reader func(path string) ([]fs.DirEntry, error)
err error err error
} }
@@ -74,9 +74,7 @@ func NewFileSystemScanner(root_dir string, notify chan int) (fss *FileSystemScan
ans := &FileSystemScanner{root_dir: root_dir, listeners: []chan int{notify}, results: make([]ResultItem, 0, 1024)} ans := &FileSystemScanner{root_dir: root_dir, listeners: []chan int{notify}, results: make([]ResultItem, 0, 1024)}
ans.in_progress.Store(true) ans.in_progress.Store(true)
ans.keep_going.Store(true) ans.keep_going.Store(true)
ans.dir_reader = func(path string, level int) ([]fs.DirEntry, error) { ans.dir_reader = os.ReadDir
return os.ReadDir(path)
}
return ans return ans
} }
@@ -147,22 +145,25 @@ func (fss *FileSystemScanner) worker() {
close(l) close(l)
} }
}() }()
var scan_dir func(string, int)
seen_dirs := make(map[string]bool) seen_dirs := make(map[string]bool)
scan_dir = func(dir string, level int) { dir := fss.root_dir
if !fss.keep_going.Load() || seen_dirs[dir] { pos := 0
return // do a breadth first traversal of the filesystem
for dir != "" {
if !fss.keep_going.Load() {
break
} }
if !seen_dirs[dir] {
seen_dirs[dir] = true seen_dirs[dir] = true
entries, err := fss.dir_reader(dir, level) entries, err := fss.dir_reader(dir)
if err != nil { if err != nil {
if level == 0 { if dir == fss.root_dir {
fss.keep_going.Store(false) fss.keep_going.Store(false)
fss.mutex.Lock() fss.mutex.Lock()
fss.err = err fss.err = err
fss.mutex.Unlock() fss.mutex.Unlock()
} }
return break
} }
ns := fss.results ns := fss.results
new_sz := len(ns) + len(entries) new_sz := len(ns) + len(entries)
@@ -203,14 +204,16 @@ func (fss *FileSystemScanner) worker() {
default: default:
} }
} }
for _, x := range new_items { }
if x.ftype.IsDir() { dir = ""
scan_dir(x.abspath, level+1) for pos < len(fss.results) && dir == "" {
if fss.results[pos].ftype.IsDir() {
dir = fss.results[pos].abspath
}
pos++
} }
} }
} }
scan_dir(fss.root_dir, 0)
}
type FileSystemScorer struct { type FileSystemScorer struct {
scanner Scanner scanner Scanner
@@ -271,7 +274,7 @@ func (fss *FileSystemScorer) worker(on_results chan int, worker_wait *sync.WaitG
} }
} else { } else {
if fss.keep_going.Load() { if fss.keep_going.Load() {
fss.on_results(nil, true) fss.on_results(fss.scanner.Error(), true)
} }
} }
}() }()