Skip to content

Commit

Permalink
Merge pull request #122 from aaronlehmann/fi-panic
Browse files Browse the repository at this point in the history
Fix panic when fi is nil in walk callback
  • Loading branch information
tonistiigi committed Mar 15, 2022
2 parents b19f7f9 + c08f231 commit 9ed6126
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err
return nil
}

var dir visitedDir
var (
dir visitedDir
isDir bool
)
if fi != nil {
isDir = fi.IsDir()
}

if includeMatcher != nil || excludeMatcher != nil {
for len(parentDirs) != 0 {
Expand All @@ -134,7 +140,7 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err
parentDirs = parentDirs[:len(parentDirs)-1]
}

if fi.IsDir() {
if isDir {
dir = visitedDir{
fi: fi,
path: path,
Expand All @@ -156,12 +162,12 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err
return errors.Wrap(err, "failed to match includepatterns")
}

if fi.IsDir() {
if isDir {
dir.includeMatchInfo = matchInfo
}

if !m {
if fi.IsDir() && onlyPrefixIncludes {
if isDir && onlyPrefixIncludes {
// Optimization: we can skip walking this dir if no include
// patterns could match anything inside it.
dirSlash := path + string(filepath.Separator)
Expand Down Expand Up @@ -191,12 +197,12 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err
return errors.Wrap(err, "failed to match excludepatterns")
}

if fi.IsDir() {
if isDir {
dir.excludeMatchInfo = matchInfo
}

if m {
if fi.IsDir() && onlyPrefixExcludeExceptions {
if isDir && onlyPrefixExcludeExceptions {
// Optimization: we can skip walking this dir if no
// exceptions to exclude patterns could match anything
// inside it.
Expand Down Expand Up @@ -230,7 +236,7 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err

if includeMatcher != nil || excludeMatcher != nil {
defer func() {
if fi.IsDir() {
if isDir {
parentDirs = append(parentDirs, dir)
}
}()
Expand Down

0 comments on commit 9ed6126

Please sign in to comment.