Skip to content

Commit

Permalink
merge #20
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Apr 15, 2017
2 parents df3e8e3 + 85a405f commit 8fc9d0d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion memfs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (f *file) Seek(offset int64, whence int) (int64, error) {
case io.SeekStart:
f.position = offset
case io.SeekEnd:
f.position = int64(f.content.Len()) - offset
f.position = int64(f.content.Len()) + offset
}

return f.position, nil
Expand Down
6 changes: 6 additions & 0 deletions memfs/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ type content struct {

func (c *content) WriteAt(p []byte, off int64) (int, error) {
prev := len(c.bytes)

diff := int(off) - prev
if diff > 0 {
c.bytes = append(c.bytes, make([]byte, diff)...)
}

c.bytes = append(c.bytes[:off], p...)
if len(c.bytes) < prev {
c.bytes = c.bytes[:prev]
Expand Down
20 changes: 20 additions & 0 deletions test/fs_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,26 @@ func (s *FilesystemSuite) testFileSeek(c *C, offset int64, whence int) {
c.Assert(f.Close(), IsNil)
}

func (s *FilesystemSuite) TestSeekToEndAndWrite(c *C) {
defaultMode := os.FileMode(0666)

f, err := s.FS.OpenFile("foo1", os.O_CREATE|os.O_TRUNC|os.O_RDWR, defaultMode)
c.Assert(err, IsNil)
c.Assert(f.Filename(), Equals, "foo1")

_, err = f.Seek(10, io.SeekEnd)
c.Assert(err, IsNil)

n, err := f.Write([]byte(`TEST`))
c.Assert(err, IsNil)
c.Assert(n, Equals, 4)

_, err = f.Seek(0, io.SeekStart)
c.Assert(err, IsNil)

s.testReadClose(c, f, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00TEST")
}

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

0 comments on commit 8fc9d0d

Please sign in to comment.