Skip to content

Commit

Permalink
avoid calling os.Lstat on every visited file
Browse files Browse the repository at this point in the history
  • Loading branch information
kavishgr committed May 1, 2024
1 parent 690239f commit 86c6365
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"runtime"
"io/fs"
)

func cleanup(tempdir string) error {
Expand All @@ -25,32 +26,37 @@ func cleanup(tempdir string) error {
}
}

err := filepath.Walk(tempdir, func(binpath string, info os.FileInfo, err error) error {
err := filepath.WalkDir(tempdir, func(binpath string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

// Check if the file is a regular file
if !info.Mode().IsRegular() {
// fmt.Println("Not regular: ", binpath)
if binpath == tempdir {
return nil
}

// If a directory - skip
if d.IsDir() {
// fmt.Println("Not regular: ", binpath) // comment
return nil
}

// Open the file
f, err := os.Open(binpath)
defer f.Close()
if err != nil {
return nil
return err
}

// Verify if the open file is either ELF or Mach-O
// Verify if the open file is either an ELF or Mach-O
if err := verifyFile(f); err == nil {
err = os.Chmod(binpath, 0755)
// fmt.Println("Binary: ", binpath) //comment
if err != nil {
return err
}
} else {
// fmt.Println("Removing: ", binpath)
// fmt.Println("Removing: ", binpath) // comment
os.Remove(binpath)
}
return nil
Expand Down

0 comments on commit 86c6365

Please sign in to comment.