Skip to content

Commit

Permalink
Use os.(*File).ReadDir to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
apocelipes committed Sep 27, 2024
1 parent 41ea82f commit 01e977b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
16 changes: 9 additions & 7 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ package gocodewalker
import (
"bytes"
"errors"
"github.com/boyter/gocodewalker/go-gitignore"
"golang.org/x/sync/errgroup"
"io/fs"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"

"github.com/boyter/gocodewalker/go-gitignore"
"golang.org/x/sync/errgroup"
)

const (
Expand Down Expand Up @@ -239,7 +241,7 @@ func (f *FileWalker) walkDirectoryRecursive(iteration int,
}
defer d.Close()

foundFiles, err := d.Readdir(-1)
foundFiles, err := d.ReadDir(-1)
if err != nil {
// nothing we can do with this so return nil and process as best we can
if f.errorsHandler(err) {
Expand All @@ -248,8 +250,8 @@ func (f *FileWalker) walkDirectoryRecursive(iteration int,
return err
}

files := []os.FileInfo{}
dirs := []os.FileInfo{}
files := []fs.DirEntry{}
dirs := []fs.DirEntry{}

// We want to break apart the files and directories from the
// return as we loop over them differently and this avoids some
Expand Down Expand Up @@ -409,7 +411,7 @@ func (f *FileWalker) walkDirectoryRecursive(iteration int,

// Ignore hidden files
if !f.IncludeHidden {
s, err := IsHidden(file, directory)
s, err := IsHiddenDirEntry(file, directory)
if err != nil {
if !f.errorsHandler(err) {
return err
Expand Down Expand Up @@ -555,7 +557,7 @@ func (f *FileWalker) walkDirectoryRecursive(iteration int,

// Ignore hidden directories
if !f.IncludeHidden {
s, err := IsHidden(dir, directory)
s, err := IsHiddenDirEntry(dir, directory)
if err != nil {
if !f.errorsHandler(err) {
return err
Expand Down
6 changes: 6 additions & 0 deletions hidden.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
package gocodewalker

import (
"io/fs"
"os"
)

// IsHidden Returns true if file is hidden
func IsHidden(file os.FileInfo, directory string) (bool, error) {
return IsHiddenDirEntry(fs.FileInfoToDirEntry(file), directory)
}

// IsHiddenDirEntry is similar to [IsHidden], excepts it accepts [fs.DirEntry] as its argument
func IsHiddenDirEntry(file fs.DirEntry, directory string) (bool, error) {
return file.Name()[0:1] == ".", nil
}
6 changes: 6 additions & 0 deletions hidden_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
package gocodewalker

import (
"io/fs"
"os"
"path"
"syscall"
)

// IsHidden Returns true if file is hidden
func IsHidden(file os.FileInfo, directory string) (bool, error) {
return IsHiddenDirEntry(fs.FileInfoToDirEntry(file), directory)
}

// IsHiddenDirEntry is similar to [IsHidden], excepts it accepts [fs.DirEntry] as its argument
func IsHiddenDirEntry(file fs.DirEntry, directory string) (bool, error) {
fullpath := path.Join(directory, file.Name())
pointer, err := syscall.UTF16PtrFromString(fullpath)
if err != nil {
Expand Down

0 comments on commit 01e977b

Please sign in to comment.