Skip to content

Commit

Permalink
Merge pull request #39 from sfc-gh-thardie/master
Browse files Browse the repository at this point in the history
Adding support for WriteAt
  • Loading branch information
pjbgf authored Dec 11, 2023
2 parents d23351b + 9fe7b83 commit 9f60ea9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ type File interface {
// Name returns the name of the file as presented to Open.
Name() string
io.Writer
// TODO: Add io.WriterAt for v6
// io.WriterAt
io.Reader
io.ReaderAt
io.Seeker
Expand Down
8 changes: 6 additions & 2 deletions memfs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ func (f *file) Seek(offset int64, whence int) (int64, error) {
}

func (f *file) Write(p []byte) (int, error) {
return f.WriteAt(p, f.position)
}

func (f *file) WriteAt(p []byte, off int64) (int, error) {
if f.isClosed {
return 0, os.ErrClosed
}
Expand All @@ -280,8 +284,8 @@ func (f *file) Write(p []byte) (int, error) {
return 0, errors.New("write not supported")
}

n, err := f.content.WriteAt(p, f.position)
f.position += int64(n)
n, err := f.content.WriteAt(p, off)
f.position = off + int64(n)

return n, err
}
Expand Down
4 changes: 4 additions & 0 deletions test/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func (*FileMock) ReadAt(b []byte, off int64) (int, error) {
return 0, nil
}

func (*FileMock) WriteAt(b []byte, off int64) (int, error) {
return 0, nil
}

func (*FileMock) Seek(offset int64, whence int) (int64, error) {
return 0, nil
}
Expand Down

0 comments on commit 9f60ea9

Please sign in to comment.