Skip to content

Commit

Permalink
join Context.copyNotPkgDir() and copyDir()
Browse files Browse the repository at this point in the history
  • Loading branch information
guzenok committed Jan 3, 2021
1 parent 0209c7d commit 48e365b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
29 changes: 7 additions & 22 deletions go-fuzz-build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ func (c *Context) clonePackage(p *packages.Package) {
if d.IsDir() {
src := filepath.Join(dir, d.Name())
dst := filepath.Join(newDir, d.Name())
c.copyNotPkgDir(src, dst)
c.copyDir(src, dst, isNotPackage)
}
}

Expand Down Expand Up @@ -750,38 +750,23 @@ 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)
}
c.mkdirAll(newDir)
for _, f := range files {
src := filepath.Join(dir, f.Name())
dst := filepath.Join(newDir, f.Name())
if f.IsDir() {
c.copyDir(src, dst)
} else {
c.copyFile(src, dst)
for _, filtered := range filters {
if filtered(files) {
// Don't copy filtered dir.
return
}
}
}

func (c *Context) copyNotPkgDir(dir, newDir string) {
files, err := ioutil.ReadDir(dir)
if err != nil {
c.failf("failed to scan dir '%v': %v", dir, err)
}
if isPackage(files) {
// Don't copy go-package dir.
return
}
c.mkdirAll(newDir)
for _, f := range files {
src := filepath.Join(dir, f.Name())
dst := filepath.Join(newDir, f.Name())
if f.IsDir() {
c.copyNotPkgDir(src, dst)
c.copyDir(src, dst, filters...)
} else {
c.copyFile(src, dst)
}
Expand Down
15 changes: 10 additions & 5 deletions go-fuzz-build/pkgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ func packageDir(p *packages.Package) string {
return dir
}

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

0 comments on commit 48e365b

Please sign in to comment.