Skip to content

Commit

Permalink
fix: broken embedio on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jabdr committed Aug 24, 2024
1 parent 0c0eed6 commit d9d7c90
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
15 changes: 15 additions & 0 deletions internal/io/embedio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io

import (
"io/fs"
"path/filepath"
"strings"
)

// FSReadFile wraps fs.ReadFile supporting embedio on windows
func FSReadFile(fsys fs.FS, name string) ([]byte, error) {
if filepath.Separator != '/' {
name = strings.ReplaceAll(name, string(filepath.Separator), "/")

Check warning on line 12 in internal/io/embedio.go

View check run for this annotation

Codecov / codecov/patch

internal/io/embedio.go#L12

Added line #L12 was not covered by tests
}
return fs.ReadFile(fsys, name)
}
14 changes: 8 additions & 6 deletions internal/operators/from_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
"errors"
"io/fs"
"os"
"path"
"path/filepath"

"github.com/corazawaf/coraza/v3/internal/io"
)

var errEmptyDirs = errors.New("empty dirs")

func loadFromFile(filepath string, dirs []string, root fs.FS) ([]byte, error) {
if path.IsAbs(filepath) {
return fs.ReadFile(root, filepath)
func loadFromFile(filename string, dirs []string, root fs.FS) ([]byte, error) {
if filepath.IsAbs(filename) {
return io.FSReadFile(root, filename)
}

if len(dirs) == 0 {
Expand All @@ -30,8 +32,8 @@ func loadFromFile(filepath string, dirs []string, root fs.FS) ([]byte, error) {
)

for _, p := range dirs {
absFilepath := path.Join(p, filepath)
content, err = fs.ReadFile(root, absFilepath)
absFilepath := filepath.Join(p, filename)
content, err = io.FSReadFile(root, absFilepath)
if err != nil {
if os.IsNotExist(err) {
continue
Expand Down
2 changes: 1 addition & 1 deletion internal/seclang/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (p *Parser) FromFile(profilePath string) error {
p.currentFile = profilePath
lastDir := p.currentDir
p.currentDir = filepath.Dir(profilePath)
file, err := fs.ReadFile(p.root, profilePath)
file, err := io.FSReadFile(p.root, profilePath)
if err != nil {
// we don't use defer for this as tinygo does not seem to like it
p.currentDir = originalDir
Expand Down

0 comments on commit d9d7c90

Please sign in to comment.