Make the walk processor act the same as Go's walker

This commit is contained in:
Dane Everitt 2020-07-16 22:01:50 -07:00
parent d262c12b43
commit a72d6f3768
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53

View File

@ -58,25 +58,29 @@ func (w *PooledFileWalker) process(path string) error {
sp := filepath.Join(p, f.Name()) sp := filepath.Join(p, f.Name())
i, err := os.Stat(sp) i, err := os.Stat(sp)
if err = w.callback(sp, i, err); err != nil { // Call the user-provided callback for this file or directory. If an error is returned that is
if err == filepath.SkipDir { // not a SkipDir call, abort the entire process and bubble that error up.
return nil if err = w.callback(sp, i, err); err != nil && err != filepath.SkipDir {
}
return err return err
} }
if i.IsDir() { // If this is a directory, and we didn't get a SkipDir error, continue through by pushing another
// job to the pool to handle it. If we requested a skip, don't do anything just continue on to the
// next item.
if i.IsDir() && err != filepath.SkipDir {
w.push(sp) w.push(sp)
} else if !i.IsDir() && err == filepath.SkipDir {
// Per the spec for the callback, if we get a SkipDir error but it is returned for an item
// that is _not_ a directory, abort the remaining operations on the directory.
return nil
} }
} }
return nil return nil
} }
// Push a new path into the worker pool. // Push a new path into the worker pool and increment the waitgroup so that we do not return too
// // early and cause panic's as internal directories attempt to submit to the pool.
// @todo probably helps to handle errors.
func (w *PooledFileWalker) push(path string) { func (w *PooledFileWalker) push(path string) {
w.wg.Add(1) w.wg.Add(1)
w.pool.Submit(func() { w.pool.Submit(func() {