Skip to content

Commit

Permalink
Merge pull request #154 from Valentin-Kaiser/152-remove-filepathwalk-…
Browse files Browse the repository at this point in the history
…while-opening-file

case-insensitive file search has been reworked and no longer uses filepath.Walk
  • Loading branch information
Valentin-Kaiser authored Feb 18, 2025
2 parents 40542cf + 5df7f28 commit 1ba8b95
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions dbase/io_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dbase
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -638,24 +639,21 @@ func (g GenericIO) getRelatedHandle(file *File) (io.ReadWriteSeeker, error) {
return handle, nil
}

// Walk the dir and find the file case insensitive
// findFile searches for the file case-insensitive in the same directory
func findFile(f string) (string, error) {
var foundFile string
err := filepath.Walk(filepath.Dir(f), func(path string, _ os.FileInfo, err error) error {
if err != nil {
return err
}

if strings.EqualFold(filepath.Base(path), filepath.Base(f)) {
foundFile = path
}

return nil
})
dir := filepath.Dir(f)
filename := filepath.Base(f)

files, err := os.ReadDir(dir)
if err != nil {
return "", err
}

return foundFile, nil
for _, file := range files {
if strings.EqualFold(file.Name(), filename) {
return filepath.Join(dir, file.Name()), nil
}
}

return "", fmt.Errorf("file not found: %s", filename)
}

0 comments on commit 1ba8b95

Please sign in to comment.