Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go-fuzz-build fix: not-package dirs #311

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions go-fuzz-build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,37 @@ func (c *Context) clonePackage(p *packages.Package) {
c.copyFile(f, dst)
}

// p.GoFiles is always non-empty.
dir := filepath.Dir(p.GoFiles[0])
guzenok marked this conversation as resolved.
Show resolved Hide resolved
// Сopy subdirs which aren't packages.
subdirs, err := ioutil.ReadDir(dir)
if err != nil {
c.failf("failed to scan dir '%v': %v", dir, err)
}
for _, d := range subdirs {
guzenok marked this conversation as resolved.
Show resolved Hide resolved
if d.IsDir() {
src := filepath.Join(dir, d.Name())
dst := filepath.Join(newDir, d.Name())
c.copyDir(src, dst, isPackage)
}
}

// TODO: do we need to look for and copy go.mod?
}

// isPackage reports whether dir contains Go source files.
func isPackage(files []os.FileInfo) bool {
for _, f := range files {
if f.IsDir() {
continue
}
if strings.HasSuffix(f.Name(), ".go") {
return true
}
}
return false
}

// packageNamed extracts the package listed in path.
func (c *Context) packageNamed(path string) (pkgs *packages.Package) {
all := c.packagesNamed(path)
Expand Down Expand Up @@ -736,17 +764,22 @@ func (c *Context) instrumentPackages(blocks *[]CoverBlock, sonar *[]CoverBlock)
packages.Visit(c.pkgs, nil, visit)
}

func (c *Context) copyDir(dir, newDir string) {
func (c *Context) copyDir(dir, newDir string, filters ...func([]os.FileInfo) bool) {
files, err := ioutil.ReadDir(dir)
if err != nil {
c.failf("failed to scan dir '%v': %v", dir, err)
}
for _, filter := range filters {
if rejected := filter(files); rejected {
return
}
}
c.mkdirAll(newDir)
for _, f := range files {
src := filepath.Join(dir, f.Name())
dst := filepath.Join(newDir, f.Name())
if f.IsDir() {
guzenok marked this conversation as resolved.
Show resolved Hide resolved
c.copyDir(src, dst)
c.copyDir(src, dst, filters...)
} else {
c.copyFile(src, dst)
}
Expand Down