Skip to content

Commit

Permalink
Change FileReader.ReadAt to return io.EOF for short reads
Browse files Browse the repository at this point in the history
Previously we would return io.ErrUnexpectedEOF, but this better matches the
behavior of os.File.ReadAt.

Fixes #115
  • Loading branch information
emcfarlane authored and colinmarc committed May 11, 2018
1 parent 84dbd09 commit 7cb3ffe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 13 additions & 1 deletion file_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,24 @@ func (f *FileReader) ReadAt(b []byte, off int64) (int, error) {
return 0, io.ErrClosedPipe
}

if off < 0 {
return 0, &os.PathError{"readat", f.name, errors.New("negative offset")}
}

_, err := f.Seek(off, 0)
if err != nil {
return 0, err
}

return io.ReadFull(f, b)
n, err := io.ReadFull(f, b)

// For some reason, os.File.ReadAt returns io.EOF in this case instead of
// io.ErrUnexpectedEOF.
if err == io.ErrUnexpectedEOF {
err = io.EOF
}

return n, err
}

// Readdir reads the contents of the directory associated with file and returns
Expand Down
12 changes: 12 additions & 0 deletions file_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ func TestFileReadAt(t *testing.T) {
assert.EqualValues(t, testStr2, string(buf))
}

func TestFileReadAtEOF(t *testing.T) {
client := getClient(t)

file, err := client.Open("/_test/foo.txt")
require.NoError(t, err)

buf := make([]byte, 10)
_, err = file.ReadAt(buf, 1)

assert.Equal(t, err, io.EOF)
}

func TestFileSeek(t *testing.T) {
client := getClient(t)

Expand Down

0 comments on commit 7cb3ffe

Please sign in to comment.