Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved findFile function and codeql action #122

Merged
merged 4 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
fail-fast: false
matrix:
language: [ 'go' ]
os: [ubuntu-latest, windows-latest, macos-latest]
os: [ubuntu-latest, windows-latest]
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
VALIDATE_ALL_CODEBASE: true
VALIDATE_GO: false
VALIDATE_MARKDOWN: false
VALIDATE_JSCPD: false
Valentin-Kaiser marked this conversation as resolved.
Show resolved Hide resolved
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

golangci-lint:
Expand Down
23 changes: 23 additions & 0 deletions dbase/io_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"io"
"os"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -636,3 +637,25 @@ func (g GenericIO) getRelatedHandle(file *File) (io.ReadWriteSeeker, error) {
}
return handle, nil
}

// Walk the dir and find the file case insensitive
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
})

if err != nil {
return "", err
}

return foundFile, nil
}
20 changes: 2 additions & 18 deletions dbase/io_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (u UnixIO) OpenTable(config *Config) (*File, error) {
debugf("Opening table: %s - Read-only: %v - Exclusive: %v - Untested: %v - Trim spaces: %v - Write lock: %v - ValidateCodepage: %v - InterpretCodepage: %v", config.Filename, config.ReadOnly, config.Exclusive, config.Untested, config.TrimSpaces, config.WriteLock, config.ValidateCodePage, config.InterpretCodePage)
fileExtension := FileExtension(strings.ToUpper(filepath.Ext(config.Filename)))
fileName := filepath.Clean(config.Filename)
fileName, err := _findFile(fileName)
fileName, err := findFile(fileName)
if err != nil {
return nil, WrapError(err)
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func (u UnixIO) openMemo(file *File, filename string, mode int, container bool)
if container {
ext = DCT
}
relatedFile, err := _findFile(strings.TrimSuffix(filename, path.Ext(filename)) + string(ext))
relatedFile, err := findFile(strings.TrimSuffix(filename, path.Ext(filename)) + string(ext))
if err != nil {
return WrapError(err)
}
Expand Down Expand Up @@ -680,22 +680,6 @@ func (u UnixIO) Deleted(file *File) (bool, error) {
return buf[0] == byte(Deleted), nil
}

func _findFile(name string) (string, error) {
debugf("Searching for file: %s", name)
// Read all files in the directory
files, err := os.ReadDir(filepath.Dir(name))
if err != nil {
return "", NewError("failed to read directory").Details(err)
}
for _, file := range files {
if strings.EqualFold(file.Name(), filepath.Base(name)) {
debugf("Found file: %s", file.Name())
return filepath.Join(filepath.Dir(name), file.Name()), nil
}
}
return name, nil
}

func (u UnixIO) getHandle(file *File) (*os.File, error) {
handle, ok := file.handle.(*os.File)
if !ok {
Expand Down
20 changes: 2 additions & 18 deletions dbase/io_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (w WindowsIO) OpenTable(config *Config) (*File, error) {
}
debugf("Opening table: %s - Read-only: %v - Exclusive: %v - Untested: %v - Trim spaces: %v - Write lock: %v - ValidateCodepage: %v - InterpretCodepage: %v", config.Filename, config.ReadOnly, config.Exclusive, config.Untested, config.TrimSpaces, config.WriteLock, config.ValidateCodePage, config.InterpretCodePage)
var err error
config.Filename, err = _findFile(filepath.Clean(config.Filename))
config.Filename, err = findFile(filepath.Clean(config.Filename))
if err != nil {
return nil, WrapError(err)
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func (w WindowsIO) initTable(config *Config, file *File) error {
func (w WindowsIO) initRelated(config *Config, file *File) error {
if MemoFlag.Defined(file.header.TableFlags) {
ext := FPT
if FileExtension(filepath.Ext(config.Filename)) == DBC {
if strings.ToUpper(filepath.Ext(config.Filename)) == string(DBC) {
ext = DCT
}
relatedFile := strings.TrimSuffix(config.Filename, path.Ext(config.Filename)) + string(ext)
Expand Down Expand Up @@ -781,22 +781,6 @@ func (w WindowsIO) Deleted(file *File) (bool, error) {
return Marker(buf[0]) == Deleted, nil
}

func _findFile(name string) (string, error) {
debugf("Searching for file: %s", name)
// Read all files in the directory
files, err := os.ReadDir(filepath.Dir(name))
if err != nil {
return "", NewErrorf("reading directory failed").Details(err)
}
for _, file := range files {
if strings.EqualFold(file.Name(), filepath.Base(name)) {
debugf("Found file: %s", file.Name())
return filepath.Join(filepath.Dir(name), file.Name()), nil
}
}
return name, nil
}

func (w WindowsIO) getHandle(file *File) (*windows.Handle, error) {
handle, ok := file.handle.(*windows.Handle)
if !ok {
Expand Down
Loading