Fixing directory walk error check on windows (#1951)

* Fixing directory walk error check on windows

* moving check to helper package

* replacing godirwalk with standard library
This commit is contained in:
Mzack9999 2022-05-08 08:52:21 +02:00 committed by GitHub
parent 85215b5a67
commit a534b9f06c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 40 deletions

View File

@ -235,6 +235,7 @@ on extensive configurability, massive extensibility and ease of use.`)
} }
cleanupOldResumeFiles() cleanupOldResumeFiles()
} }
func cleanupOldResumeFiles() { func cleanupOldResumeFiles() {
root, err := config.GetConfigDir() root, err := config.GetConfigDir()
if err != nil { if err != nil {
@ -246,6 +247,7 @@ func cleanupOldResumeFiles() {
} }
_ = fileutil.DeleteFilesOlderThan(root, filter) _ = fileutil.DeleteFilesOlderThan(root, filter)
} }
func createGroup(flagSet *goflags.FlagSet, groupName, description string, flags ...*goflags.FlagData) { func createGroup(flagSet *goflags.FlagSet, groupName, description string, flags ...*goflags.FlagData) {
flagSet.SetGroup(groupName, description) flagSet.SetGroup(groupName, description)
for _, currentFlag := range flags { for _, currentFlag := range flags {

View File

@ -19,7 +19,7 @@ require (
github.com/json-iterator/go v1.1.12 github.com/json-iterator/go v1.1.12
github.com/julienschmidt/httprouter v1.3.0 github.com/julienschmidt/httprouter v1.3.0
github.com/karlseguin/ccache v2.0.3+incompatible github.com/karlseguin/ccache v2.0.3+incompatible
github.com/karrick/godirwalk v1.17.0 github.com/karrick/godirwalk v1.17.0 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/miekg/dns v1.1.48 github.com/miekg/dns v1.1.48
github.com/olekukonko/tablewriter v0.0.5 github.com/olekukonko/tablewriter v0.0.5

View File

@ -1,11 +1,11 @@
package runner package runner
import ( import (
"io/fs"
"os" "os"
"path/filepath"
"strings" "strings"
"github.com/karrick/godirwalk"
"github.com/projectdiscovery/gologger" "github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v2/pkg/parsers" "github.com/projectdiscovery/nuclei/v2/pkg/parsers"
"github.com/projectdiscovery/nuclei/v2/pkg/templates" "github.com/projectdiscovery/nuclei/v2/pkg/templates"
@ -40,9 +40,13 @@ func (r *Runner) listAvailableTemplates() {
r.templatesConfig.TemplateVersion, r.templatesConfig.TemplateVersion,
r.templatesConfig.TemplatesDirectory, r.templatesConfig.TemplatesDirectory,
) )
err := directoryWalker( err := filepath.WalkDir(
r.templatesConfig.TemplatesDirectory, r.templatesConfig.TemplatesDirectory,
func(path string, d *godirwalk.Dirent) error { func(path string, d fs.DirEntry, err error) error {
// continue on errors
if err != nil {
return nil
}
if d.IsDir() && path != r.templatesConfig.TemplatesDirectory { if d.IsDir() && path != r.templatesConfig.TemplatesDirectory {
gologger.Print().Msgf("\n%s:\n\n", r.colorizer.Bold(r.colorizer.BgBrightBlue(d.Name())).String()) gologger.Print().Msgf("\n%s:\n\n", r.colorizer.Bold(r.colorizer.BgBrightBlue(d.Name())).String())
} else if strings.HasSuffix(path, ".yaml") { } else if strings.HasSuffix(path, ".yaml") {
@ -56,13 +60,3 @@ func (r *Runner) listAvailableTemplates() {
gologger.Error().Msgf("Could not find templates in directory '%s': %s\n", r.templatesConfig.TemplatesDirectory, err) gologger.Error().Msgf("Could not find templates in directory '%s': %s\n", r.templatesConfig.TemplatesDirectory, err)
} }
} }
func directoryWalker(fsPath string, callback func(fsPath string, d *godirwalk.Dirent) error) error {
return godirwalk.Walk(fsPath, &godirwalk.Options{
Callback: callback,
ErrorCallback: func(fsPath string, err error) godirwalk.ErrorAction {
return godirwalk.SkipNode
},
Unsorted: true,
})
}

View File

@ -1,11 +1,11 @@
package catalog package catalog
import ( import (
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/karrick/godirwalk"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/projectdiscovery/gologger" "github.com/projectdiscovery/gologger"
@ -136,12 +136,13 @@ func (c *Catalog) findFileMatches(absPath string, processed map[string]struct{})
// findDirectoryMatches finds matches for templates from a directory // findDirectoryMatches finds matches for templates from a directory
func (c *Catalog) findDirectoryMatches(absPath string, processed map[string]struct{}) ([]string, error) { func (c *Catalog) findDirectoryMatches(absPath string, processed map[string]struct{}) ([]string, error) {
var results []string var results []string
err := godirwalk.Walk(absPath, &godirwalk.Options{ err := filepath.WalkDir(
Unsorted: true, absPath,
ErrorCallback: func(fsPath string, err error) godirwalk.ErrorAction { func(path string, d fs.DirEntry, err error) error {
return godirwalk.SkipNode // continue on errors
}, if err != nil {
Callback: func(path string, d *godirwalk.Dirent) error { return nil
}
if !d.IsDir() && strings.HasSuffix(path, ".yaml") { if !d.IsDir() && strings.HasSuffix(path, ".yaml") {
if _, ok := processed[path]; !ok { if _, ok := processed[path]; !ok {
results = append(results, path) results = append(results, path)
@ -150,6 +151,6 @@ func (c *Catalog) findDirectoryMatches(absPath string, processed map[string]stru
} }
return nil return nil
}, },
}) )
return results, err return results, err
} }

View File

@ -1,6 +1,8 @@
package compare package compare
import "strings" import (
"strings"
)
// StringSlice compares two string slices for equality // StringSlice compares two string slices for equality
func StringSlice(a, b []string) bool { func StringSlice(a, b []string) bool {

View File

@ -2,11 +2,11 @@ package file
import ( import (
"io" "io"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/karrick/godirwalk"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/projectdiscovery/fileutil" "github.com/projectdiscovery/fileutil"
"github.com/projectdiscovery/folderutil" "github.com/projectdiscovery/folderutil"
@ -86,12 +86,13 @@ func (request *Request) findFileMatches(absPath string, processed map[string]str
// findDirectoryMatches finds matches for templates from a directory // findDirectoryMatches finds matches for templates from a directory
func (request *Request) findDirectoryMatches(absPath string, processed map[string]struct{}, callback func(string)) error { func (request *Request) findDirectoryMatches(absPath string, processed map[string]struct{}, callback func(string)) error {
err := godirwalk.Walk(absPath, &godirwalk.Options{ err := filepath.WalkDir(
Unsorted: true, absPath,
ErrorCallback: func(fsPath string, err error) godirwalk.ErrorAction { func(path string, d fs.DirEntry, err error) error {
return godirwalk.SkipNode // continue on errors
}, if err != nil {
Callback: func(path string, d *godirwalk.Dirent) error { return nil
}
if d.IsDir() { if d.IsDir() {
return nil return nil
} }
@ -104,7 +105,7 @@ func (request *Request) findDirectoryMatches(absPath string, processed map[strin
} }
return nil return nil
}, },
}) )
return err return err
} }

View File

@ -1,11 +1,11 @@
package offlinehttp package offlinehttp
import ( import (
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/karrick/godirwalk"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -80,12 +80,13 @@ func (request *Request) findFileMatches(absPath string, processed map[string]str
// findDirectoryMatches finds matches for templates from a directory // findDirectoryMatches finds matches for templates from a directory
func (request *Request) findDirectoryMatches(absPath string, processed map[string]struct{}, callback func(string)) error { func (request *Request) findDirectoryMatches(absPath string, processed map[string]struct{}, callback func(string)) error {
err := godirwalk.Walk(absPath, &godirwalk.Options{ err := filepath.WalkDir(
Unsorted: true, absPath,
ErrorCallback: func(fsPath string, err error) godirwalk.ErrorAction { func(p string, d fs.DirEntry, err error) error {
return godirwalk.SkipNode // continue on errors
}, if err != nil {
Callback: func(p string, d *godirwalk.Dirent) error { return nil
}
if d.IsDir() { if d.IsDir() {
return nil return nil
} }
@ -98,6 +99,6 @@ func (request *Request) findDirectoryMatches(absPath string, processed map[strin
} }
return nil return nil
}, },
}) )
return err return err
} }