psserver: swallow io.EOF when getting full content in FileRanger (#1238)
This commit is contained in:
parent
331f0cc845
commit
218e95728c
@ -123,8 +123,6 @@ func TestPiece(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRetrieve(t *testing.T) {
|
func TestRetrieve(t *testing.T) {
|
||||||
t.Skip("flaky")
|
|
||||||
|
|
||||||
ctx := testcontext.New(t)
|
ctx := testcontext.New(t)
|
||||||
defer ctx.Cleanup()
|
defer ctx.Cleanup()
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"storj.io/storj/pkg/utils"
|
"github.com/zeebo/errs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fileRanger struct {
|
type fileRanger struct {
|
||||||
@ -46,14 +46,35 @@ func (rr *fileRanger) Range(ctx context.Context, offset, length int64) (io.ReadC
|
|||||||
}
|
}
|
||||||
_, err = fh.Seek(offset, io.SeekStart)
|
_, err = fh.Seek(offset, io.SeekStart)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = utils.CombineErrors(err, fh.Close())
|
return nil, Error.Wrap(errs.Combine(err, fh.Close()))
|
||||||
return nil, Error.Wrap(err)
|
|
||||||
}
|
}
|
||||||
return struct {
|
|
||||||
io.Reader
|
return &FileReader{fh, length}, nil
|
||||||
io.Closer
|
}
|
||||||
}{
|
|
||||||
Reader: io.LimitReader(fh, length),
|
// FileReader implements limit reader with io.EOF only on last read.
|
||||||
Closer: fh,
|
type FileReader struct {
|
||||||
}, nil
|
file *os.File
|
||||||
|
remaining int64
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read reads from the underlying file.
|
||||||
|
func (reader *FileReader) Read(data []byte) (n int, err error) {
|
||||||
|
if reader.remaining <= 0 {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
if int64(len(data)) > reader.remaining {
|
||||||
|
data = data[0:reader.remaining]
|
||||||
|
}
|
||||||
|
n, err = reader.file.Read(data)
|
||||||
|
reader.remaining -= int64(n)
|
||||||
|
if err == io.EOF && reader.remaining == 0 {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes the underlying file.
|
||||||
|
func (reader *FileReader) Close() error {
|
||||||
|
return reader.file.Close()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user