Skip to content

Commit 7cb3ffe

Browse files
emcfarlanecolinmarc
authored andcommitted
Change FileReader.ReadAt to return io.EOF for short reads
Previously we would return io.ErrUnexpectedEOF, but this better matches the behavior of os.File.ReadAt. Fixes #115
1 parent 84dbd09 commit 7cb3ffe

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

file_reader.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,24 @@ func (f *FileReader) ReadAt(b []byte, off int64) (int, error) {
192192
return 0, io.ErrClosedPipe
193193
}
194194

195+
if off < 0 {
196+
return 0, &os.PathError{"readat", f.name, errors.New("negative offset")}
197+
}
198+
195199
_, err := f.Seek(off, 0)
196200
if err != nil {
197201
return 0, err
198202
}
199203

200-
return io.ReadFull(f, b)
204+
n, err := io.ReadFull(f, b)
205+
206+
// For some reason, os.File.ReadAt returns io.EOF in this case instead of
207+
// io.ErrUnexpectedEOF.
208+
if err == io.ErrUnexpectedEOF {
209+
err = io.EOF
210+
}
211+
212+
return n, err
201213
}
202214

203215
// Readdir reads the contents of the directory associated with file and returns

file_reader_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ func TestFileReadAt(t *testing.T) {
130130
assert.EqualValues(t, testStr2, string(buf))
131131
}
132132

133+
func TestFileReadAtEOF(t *testing.T) {
134+
client := getClient(t)
135+
136+
file, err := client.Open("/_test/foo.txt")
137+
require.NoError(t, err)
138+
139+
buf := make([]byte, 10)
140+
_, err = file.ReadAt(buf, 1)
141+
142+
assert.Equal(t, err, io.EOF)
143+
}
144+
133145
func TestFileSeek(t *testing.T) {
134146
client := getClient(t)
135147

0 commit comments

Comments
 (0)