Skip to content

Commit

Permalink
Use same walker for slow and fast modes
Browse files Browse the repository at this point in the history
  • Loading branch information
lebauce committed Nov 27, 2023
1 parent edad5f6 commit e21f498
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,5 @@ replace oras.land/oras-go => oras.land/oras-go v1.2.4-0.20230801060855-932dd06d3
// testcontainers-go has a bug with versions v0.25.0 and v0.26.0
// ref: https://github.com/testcontainers/testcontainers-go/issues/1782
replace github.com/testcontainers/testcontainers-go => github.com/testcontainers/testcontainers-go v0.23.0

replace github.com/saracen/walker => github.com/lebauce/walker v0.0.0-20230418153152-7f29bb2dc950
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,8 @@ github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
github.com/lebauce/walker v0.0.0-20230418153152-7f29bb2dc950 h1:luGiVRLSZy6Ga+hfwfdeXZq1jAQsT0r0WF0TBdgCPXk=
github.com/lebauce/walker v0.0.0-20230418153152-7f29bb2dc950/go.mod h1:FU+7qU8DeQQgSZDmmThMJi93kPkLFgy0oVAcLxurjIk=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/liamg/iamgo v0.0.9 h1:tADGm3xVotyRJmuKKaH4+zsBn7LOcvgdpuF3WsSKW3c=
Expand Down Expand Up @@ -1520,8 +1522,6 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb
github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4=
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/saracen/walker v0.1.3 h1:YtcKKmpRPy6XJTHJ75J2QYXXZYWnZNQxPCVqZSHVV/g=
github.com/saracen/walker v0.1.3/go.mod h1:FU+7qU8DeQQgSZDmmThMJi93kPkLFgy0oVAcLxurjIk=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo=
Expand Down
28 changes: 12 additions & 16 deletions pkg/fanal/walker/fs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package walker

import (
"io/fs"
"os"
"path/filepath"

Expand Down Expand Up @@ -82,33 +81,30 @@ func (w FS) Walk(root string, fn WalkFunc) error {

type fastWalkFunc func(pathname string, fi os.FileInfo) error

func (w FS) walkFast(root string, walkFn fastWalkFunc) error {
func (w FS) walk(root string, walkFn fastWalkFunc, walkOpts ...swalker.Option) error {
// error function called for every error encountered
errorCallbackOption := swalker.WithErrorCallback(w.errCallback)

if err := swalker.Walk(root, walkFn, append(walkOpts, errorCallbackOption)...); err != nil {
return xerrors.Errorf("walk error: %w", err)
}
return nil
}

func (w FS) walkFast(root string, walkFn fastWalkFunc, walkOpts ...swalker.Option) error {
// Multiple goroutines stat the filesystem concurrently. The provided
// walkFn must be safe for concurrent use.
log.Logger.Debugf("Walk the file tree rooted at '%s' in parallel", root)
if err := swalker.Walk(root, walkFn, errorCallbackOption); err != nil {
if err := w.walk(root, walkFn, walkOpts...); err != nil {
return xerrors.Errorf("walk error: %w", err)
}
return nil
}

func (w FS) walkSlow(root string, walkFn fastWalkFunc) error {
func (w FS) walkSlow(root string, walkFn fastWalkFunc, walkOpts ...swalker.Option) error {
log.Logger.Debugf("Walk the file tree rooted at '%s' in series", root)
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return w.errCallback(path, err)
}
info, err := d.Info()
if err != nil {
return xerrors.Errorf("file info error: %w", err)
}
return walkFn(path, info)
})
if err != nil {
return xerrors.Errorf("walk dir error: %w", err)
if err := w.walk(root, walkFn, append(walkOpts, swalker.WithLimit(1))...); err != nil {
return xerrors.Errorf("walk error: %w", err)
}
return nil
}
Expand Down

0 comments on commit e21f498

Please sign in to comment.