-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix/LLN1107
- Loading branch information
Showing
5 changed files
with
110 additions
and
6 deletions.
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