Skip to content

Commit

Permalink
use filepath.WalkDir to avoid stat-ing files unnecessarily
Browse files Browse the repository at this point in the history
  • Loading branch information
airhorns committed May 11, 2024
1 parent b5ba504 commit 3b51048
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions internal/files/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,22 @@ func HardlinkDir(olddir, newdir string) error {
return fmt.Errorf("cannot create new root dir %v: %w", olddir, err)
}

return filepath.Walk(olddir, func(oldpath string, info fs.FileInfo, err error) error {
return filepath.WalkDir(olddir, func(oldpath string, d os.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("failed to walk dir: %v, %w", info, err)
return fmt.Errorf("failed to walk dir: %v, %w", oldpath, err)
}
if oldpath == olddir {
return nil
}

newpath := filepath.Join(newdir, strings.TrimPrefix(oldpath, olddir))

if info.IsDir() {
err := os.Mkdir(newpath, info.Mode())
if d.IsDir() {
info, err := d.Info()
if err != nil {
return fmt.Errorf("cannot access directory info %v: %w", oldpath, err)
}
err = os.Mkdir(newpath, info.Mode())
if err != nil {
return fmt.Errorf("cannot create dir %v: %w", newpath, err)
}
Expand Down

0 comments on commit 3b51048

Please sign in to comment.