Skip to content

Commit

Permalink
Merge pull request #26 from ajnavarro/fix/seek-file-position
Browse files Browse the repository at this point in the history
memfs: bug fixing
  • Loading branch information
mcuadros committed Apr 19, 2017
2 parents b383e4e + 4b48e46 commit 99d8398
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
7 changes: 4 additions & 3 deletions memfs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ type file struct {

func (f *file) Read(b []byte) (int, error) {
n, err := f.ReadAt(b, f.position)
if err != nil {
return 0, err
f.position += int64(n)

if err == io.EOF && n != 0 {
err = nil
}

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

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

return n, err
}
Expand Down
11 changes: 8 additions & 3 deletions memfs/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (c *content) WriteAt(p []byte, off int64) (int, error) {
return len(p), nil
}

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

n := copy(b, c.bytes[off:off+l])
return n, nil
btr := c.bytes[off : off+l]
if len(btr) < len(b) {
err = io.EOF
}
n = copy(b, btr)

return
}
48 changes: 48 additions & 0 deletions test/fs_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,54 @@ func (s *FilesystemSuite) TestReadAtOnReadOnly(c *C) {
c.Assert(f.Close(), IsNil)
}

func (s *FilesystemSuite) TestReadAtEOF(c *C) {
err := WriteFile(s.FS, "foo", []byte("TEST"), 0644)
c.Assert(err, IsNil)

f, err := s.FS.Open("foo")
c.Assert(err, IsNil)

rf, ok := f.(io.ReaderAt)
c.Assert(ok, Equals, true)

b := make([]byte, 5)
n, err := rf.ReadAt(b, 0)
c.Assert(err, Equals, io.EOF)
c.Assert(n, Equals, 4)
c.Assert(string(b), Equals, "TEST\x00")

err = f.Close()
c.Assert(err, IsNil)
}

func (s *FilesystemSuite) TestReadAtOffset(c *C) {
err := WriteFile(s.FS, "foo", []byte("TEST"), 0644)
c.Assert(err, IsNil)

f, err := s.FS.Open("foo")
c.Assert(err, IsNil)

rf, ok := f.(io.ReaderAt)
c.Assert(ok, Equals, true)

o, err := f.Seek(0, io.SeekCurrent)
c.Assert(err, IsNil)
c.Assert(o, Equals, int64(0))

b := make([]byte, 4)
n, err := rf.ReadAt(b, 0)
c.Assert(err, IsNil)
c.Assert(n, Equals, 4)
c.Assert(string(b), Equals, "TEST")

o, err = f.Seek(0, io.SeekCurrent)
c.Assert(err, IsNil)
c.Assert(o, Equals, int64(0))

err = f.Close()
c.Assert(err, IsNil)
}

func (s *FilesystemSuite) TestReadWriteLargeFile(c *C) {
f, err := s.FS.Create("foo")
c.Assert(err, IsNil)
Expand Down

0 comments on commit 99d8398

Please sign in to comment.