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

fix: broken embedio on windows #1135

Open
wants to merge 3 commits into
base: gh-windows
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing our license header

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, having a whole package for a single function looks like an overkill

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The io package already exists. The other files did not seem appropriate in this package, so I choose a new. If you wish a different approach, then please give a more detailed description of what you have in mind.


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

// FSReadFile wraps fs.ReadFile supporting embedio on windows
func FSReadFile(fsys fs.FS, name string) ([]byte, error) {
fzipi marked this conversation as resolved.
Show resolved Hide resolved
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
fzipi marked this conversation as resolved.
Show resolved Hide resolved
}
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
Loading