Skip to content

Commit 99d8398

Browse files
authored
Merge pull request #26 from ajnavarro/fix/seek-file-position
memfs: bug fixing
2 parents b383e4e + 4b48e46 commit 99d8398

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

memfs/memory.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,10 @@ type file struct {
170170

171171
func (f *file) Read(b []byte) (int, error) {
172172
n, err := f.ReadAt(b, f.position)
173-
if err != nil {
174-
return 0, err
173+
f.position += int64(n)
174+
175+
if err == io.EOF && n != 0 {
176+
err = nil
175177
}
176178

177179
return n, err
@@ -187,7 +189,6 @@ func (f *file) ReadAt(b []byte, off int64) (int, error) {
187189
}
188190

189191
n, err := f.content.ReadAt(b, off)
190-
f.position += int64(n)
191192

192193
return n, err
193194
}

memfs/storage.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (c *content) WriteAt(p []byte, off int64) (int, error) {
185185
return len(p), nil
186186
}
187187

188-
func (c *content) ReadAt(b []byte, off int64) (int, error) {
188+
func (c *content) ReadAt(b []byte, off int64) (n int, err error) {
189189
size := int64(len(c.bytes))
190190
if off >= size {
191191
return 0, io.EOF
@@ -196,6 +196,11 @@ func (c *content) ReadAt(b []byte, off int64) (int, error) {
196196
l = size - off
197197
}
198198

199-
n := copy(b, c.bytes[off:off+l])
200-
return n, nil
199+
btr := c.bytes[off : off+l]
200+
if len(btr) < len(b) {
201+
err = io.EOF
202+
}
203+
n = copy(b, btr)
204+
205+
return
201206
}

test/fs_suite.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,54 @@ func (s *FilesystemSuite) TestReadAtOnReadOnly(c *C) {
715715
c.Assert(f.Close(), IsNil)
716716
}
717717

718+
func (s *FilesystemSuite) TestReadAtEOF(c *C) {
719+
err := WriteFile(s.FS, "foo", []byte("TEST"), 0644)
720+
c.Assert(err, IsNil)
721+
722+
f, err := s.FS.Open("foo")
723+
c.Assert(err, IsNil)
724+
725+
rf, ok := f.(io.ReaderAt)
726+
c.Assert(ok, Equals, true)
727+
728+
b := make([]byte, 5)
729+
n, err := rf.ReadAt(b, 0)
730+
c.Assert(err, Equals, io.EOF)
731+
c.Assert(n, Equals, 4)
732+
c.Assert(string(b), Equals, "TEST\x00")
733+
734+
err = f.Close()
735+
c.Assert(err, IsNil)
736+
}
737+
738+
func (s *FilesystemSuite) TestReadAtOffset(c *C) {
739+
err := WriteFile(s.FS, "foo", []byte("TEST"), 0644)
740+
c.Assert(err, IsNil)
741+
742+
f, err := s.FS.Open("foo")
743+
c.Assert(err, IsNil)
744+
745+
rf, ok := f.(io.ReaderAt)
746+
c.Assert(ok, Equals, true)
747+
748+
o, err := f.Seek(0, io.SeekCurrent)
749+
c.Assert(err, IsNil)
750+
c.Assert(o, Equals, int64(0))
751+
752+
b := make([]byte, 4)
753+
n, err := rf.ReadAt(b, 0)
754+
c.Assert(err, IsNil)
755+
c.Assert(n, Equals, 4)
756+
c.Assert(string(b), Equals, "TEST")
757+
758+
o, err = f.Seek(0, io.SeekCurrent)
759+
c.Assert(err, IsNil)
760+
c.Assert(o, Equals, int64(0))
761+
762+
err = f.Close()
763+
c.Assert(err, IsNil)
764+
}
765+
718766
func (s *FilesystemSuite) TestReadWriteLargeFile(c *C) {
719767
f, err := s.FS.Create("foo")
720768
c.Assert(err, IsNil)

0 commit comments

Comments
 (0)