-
Notifications
You must be signed in to change notification settings - Fork 31
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
feat: add ReadFileWithSizeLimit
and use it on x/wasm
#696
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
30d8384
Add `ReadFileWithSizeLimit` and use it on `x/wasm`
tnasu a2d5bdd
Update CHANGELOG.md
tnasu e9b7170
Merge branch 'main' into main-fix-XE1-01
tnasu 200df68
Refactor internal/os into x/wasm
tnasu 741d0a8
Revert "Refactor internal/os into x/wasm"
tnasu 7b4bb6e
Use `ReadFileWithSizeLimit` on `x/distribution`
tnasu 80de34c
Modify to one liner
tnasu 19d28f9
Merge branch 'main' into main-fix-XE1-01
tnasu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package os | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
// ReadFileWithSizeLimit expanded os.ReadFile for checking the file size before reading it | ||
func ReadFileWithSizeLimit(name string, sizeLimit int64) ([]byte, error) { | ||
f, err := os.Open(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { | ||
err := f.Close() | ||
if err != nil { | ||
fmt.Printf("Cannot close the file: %s\n", name) | ||
} | ||
}() | ||
|
||
var size int | ||
if info, err := f.Stat(); err == nil { | ||
size64 := info.Size() | ||
// Check the file size | ||
if size64 > sizeLimit { | ||
return nil, fmt.Errorf("the file is too large: %s, size limit over > %d", name, sizeLimit) | ||
} | ||
if int64(int(size64)) == size64 { | ||
size = int(size64) | ||
} | ||
} | ||
size++ // one byte for final read at EOF | ||
|
||
// If a file claims a small size, read at least 512 bytes. | ||
// In particular, files in Linux's /proc claim size 0 but | ||
// then do not work right if read in small pieces, | ||
// so an initial read of 1 byte would not work correctly. | ||
if size < 512 { | ||
size = 512 | ||
} | ||
|
||
data := make([]byte, 0, size) | ||
for { | ||
if len(data) >= cap(data) { | ||
d := append(data[:cap(data)], 0) | ||
data = d[:len(data)] | ||
} | ||
n, err := f.Read(data[len(data):cap(data)]) | ||
data = data[:len(data)+n] | ||
if err != nil { | ||
if err == io.EOF { | ||
err = nil | ||
} | ||
return data, err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package os | ||
|
||
import ( | ||
"os" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestReadFileWithSizeLimit(t *testing.T) { | ||
filename := "file.go" | ||
file, err := os.ReadFile(filename) | ||
require.NoError(t, err) | ||
|
||
type args struct { | ||
name string | ||
sizeLimit int64 | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []byte | ||
wantErr bool | ||
}{ | ||
{"cannot open error", args{"", 0}, nil, true}, | ||
{"size limit over error", args{filename, 0}, nil, true}, | ||
{"simple reading file success", args{filename, 100000}, file, false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := ReadFileWithSizeLimit(tt.args.name, tt.args.sizeLimit) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("ReadFileWithSizeLimit() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("ReadFileWithSizeLimit() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about changing one line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated it. Thanks