Skip to content

Commit 8fc9d0d

Browse files
committed
merge #20
2 parents df3e8e3 + 85a405f commit 8fc9d0d

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

memfs/memory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (f *file) Seek(offset int64, whence int) (int64, error) {
203203
case io.SeekStart:
204204
f.position = offset
205205
case io.SeekEnd:
206-
f.position = int64(f.content.Len()) - offset
206+
f.position = int64(f.content.Len()) + offset
207207
}
208208

209209
return f.position, nil

memfs/storage.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ type content struct {
161161

162162
func (c *content) WriteAt(p []byte, off int64) (int, error) {
163163
prev := len(c.bytes)
164+
165+
diff := int(off) - prev
166+
if diff > 0 {
167+
c.bytes = append(c.bytes, make([]byte, diff)...)
168+
}
169+
164170
c.bytes = append(c.bytes[:off], p...)
165171
if len(c.bytes) < prev {
166172
c.bytes = c.bytes[:prev]

test/fs_suite.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,26 @@ func (s *FilesystemSuite) testFileSeek(c *C, offset int64, whence int) {
318318
c.Assert(f.Close(), IsNil)
319319
}
320320

321+
func (s *FilesystemSuite) TestSeekToEndAndWrite(c *C) {
322+
defaultMode := os.FileMode(0666)
323+
324+
f, err := s.FS.OpenFile("foo1", os.O_CREATE|os.O_TRUNC|os.O_RDWR, defaultMode)
325+
c.Assert(err, IsNil)
326+
c.Assert(f.Filename(), Equals, "foo1")
327+
328+
_, err = f.Seek(10, io.SeekEnd)
329+
c.Assert(err, IsNil)
330+
331+
n, err := f.Write([]byte(`TEST`))
332+
c.Assert(err, IsNil)
333+
c.Assert(n, Equals, 4)
334+
335+
_, err = f.Seek(0, io.SeekStart)
336+
c.Assert(err, IsNil)
337+
338+
s.testReadClose(c, f, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00TEST")
339+
}
340+
321341
func (s *FilesystemSuite) TestFileSeekClosed(c *C) {
322342
err := WriteFile(s.FS, "foo", []byte("foo"), 0644)
323343
c.Assert(err, IsNil)

0 commit comments

Comments
 (0)